pr coding: titles

grahamperrin

Son of Beastie

Reaction score: 1,006
Messages: 3,418

<https://forums.freebsd.org/help/bb-codes/#pr>

To forum administrators: please, is it possible for the result of coding to show more than just a positive integer?

See for example <https://forums.freebsd.org/posts/541332>,

1636517326631.png



Instead, the title of the bug report will be ideal. Thanks.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 13,071
Messages: 39,685

It's just a simple translation rule that translates [pr]{number}[/pr] to <a href=https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=number>PR {number}</a>. Not sure if you can do more fancier stuff. I don't think there's a way to fetch the URL first, then use the title of that received document.
 

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

It depends on how much time you'll spend. It for sure is possible to fetch the title and the first comment via AJAX and display it in a popup for example.
 

astyle

Daemon

Reaction score: 748
Messages: 1,626

I do agree that PR numbers look rather meaningless at first glance. It does take reading the thread AND clicking on the PR link to figure out what the PR is even about. Some forum softwares do support the kind of feature that grahamperrin and eternal_noob are talking about. I thought that a workable workaround would be to use the [HIGHLIGHT][/HIGHLIGHT] tag for the PR's, but that only produced a link with full URL for the text, it did not work the way I expected. This is rather surprising, considering that we do have [MEDIA][/MEDIA] tags that work for YT... I'd vote to give priority for FreeBSD's Bugzilla to show up properly in forum posts, rather than YT.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 13,071
Messages: 39,685

Those media tags are handled differently, they aren't custom bbcodes (which the PR is) but they're builtin on the forum software.
 

Attachments

  • forum-media.png
    forum-media.png
    95.1 KB · Views: 281
  • forum-pr.png
    forum-pr.png
    91 KB · Views: 284

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

custom bbcodes (which the PR is)
The screenshot is interesting.

I believe "Replacement mode: PHP callback" would be the right thing. You could write a short snippet which fetches the title and the first few sentences of the first comment and display it in a popup.
It depends on which PHP extensions are enabled on the server but there are a few ways to to so: https://oscarliang.com/six-ways-retrieving-webpage-content-php/

The title has a unique id which can be easily grep'ed for:
2021-11-16-021146_1920x1080_scrot.png

The comments are in a table, have a unique id and can be parsed as easy as well:
2021-11-16-021550_1920x1080_scrot.png
 

ondra_knezour

Aspiring Daemon

Reaction score: 232
Messages: 807

I don't know nothing about forum and its' PHP callbacks, but Bugzilla already has REST API and getting bug info may be as easy as
Code:
$bug_data = json_decode(file_get_contents('https://bugs.freebsd.org/bugzilla/rest/bug/259851?include_fields=summary'));
echo $bug_data->bugs[0]->summary;
 

danger@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 425
Messages: 1,022

it would definitely be possible to implement it with php callbacks, however it requires some XenForo and php knowledge...if someone comes up with a working solution I can apply it.
 

danger@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 425
Messages: 1,022

curl is available as well as file_get_contents
 

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

I am currently playing with a PHP callback and i need to know one thing: Which of the parameters to the callback would contain the actual PR id?

2021-12-04-160503_1920x1080_scrot.png
 

danger@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 425
Messages: 1,022

I guess you will have a better luck asking this at the XF community forums.
 

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

Well you could add a dummy class and tell me.

Something like this
PHP:
class Replacement
{
    public static function getPR($tagChildren, $tagOption, $tag, array $options, \XFBbCode\Renderer\AbstractRenderer $renderer)
    {
        var_dump($tagChildren);
        var_dump($tagOption);
        var_dump($tag);
        var_dump($options);
        var_dump($renderer);
    }
}

Add this as a replacement callback in you test instance, use a [PR]-tag to trigger the replacement and paste the output.

I am not eager to create an account over at the XF community forums.

Edit: Here is a screenshot of my test:
2021-12-04-184740_1920x1080_scrot.png

A [PR]-tag would be replaced with a link showing the title of that PR and a hidden popup with further information which will be made visible if one hovers that link.
The class is 98% done, all i need is to know which parameter is used to pass the id to the callback and a little cleanup.
 

astyle

Daemon

Reaction score: 748
Messages: 1,626

eternal_noob : I'd suggest that you get a dump of EVERYTHING and then grep for the parameter you want. Once you figure out which parameter is the correct one, then refine your code.
 

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

I don't have XenForo installed, i can't use the class i wrote. :'‑(

I need someone with a working XenForo installation to tell me or someone willing to make an account over at the XF forums.
 
OP
grahamperrin

grahamperrin

Son of Beastie

Reaction score: 1,006
Messages: 3,418

… someone willing to make an account over at the XF forums.

You can create an account for yourself, however some areas are limited to customers; other areas allow posting but a version is expected (i.e. these areas are intended for server admins); and so on.
 

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

Yeah, but i limit my web accounts to the bare minimum and I don't want another one.

Since i am stuck here now, i gonna post the code i have, maybe someone else may find it useful and can finish it.
I am out.

Note 1: You only need the Replacement class, the other stuff (i.e. HTML code) is only for testing that class.
Note 2: You need to uncomment the $renderer parameter, i commented it because you can't use it without XenForo.
Note 3: You either need the file_get_contents or the cURL code, you can remove the one you don't want.
Note 4: In order to strip down the markup, you could add the CSS to a custom file once rather than adding it to the replacement string every time.

PHP:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

class Replacement
{
    public static function getPR($tagChildren, $tagOption, $tag, array $options/*, \XFBbCode\Renderer\AbstractRenderer $renderer*/)
    {
        $pr_id = $tag;//259851; //TODO: get from a parameter. But which one?
        $url   = "https://bugs.freebsd.org/bugzilla/";

        $bug_data = null;

//        if (false) // true if file_get_contents should be used, false if cURL should be used
//        {
            $context  = stream_context_create(array('http' => array('method'=>'GET', 'header'=>'Connection: close\r\n')));  
            $bug_data = json_decode(file_get_contents("${url}rest/bug/${pr_id}?include_fields=summary,status,component,resolution,creator", false, $context));
//        }
//        else
//        {
//            $timeout = 5;
//            $ch = curl_init();
//            curl_setopt($ch, CURLOPT_URL, "${url}rest/bug/${pr_id}?include_fields=summary,status,component,resolution,creator");
//            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//            $bug_data = json_decode(curl_exec($ch));
//            curl_close($ch);
//        }

        $html = $pr_id;

        if ($bug_data !== null)
        {
            $summary    = htmlentities($bug_data->bugs[0]->summary, ENT_QUOTES, "UTF-8");
            $status     = htmlentities($bug_data->bugs[0]->status, ENT_QUOTES, "UTF-8");
            $component  = htmlentities($bug_data->bugs[0]->component, ENT_QUOTES, "UTF-8");
            $resolution = htmlentities($bug_data->bugs[0]->resolution, ENT_QUOTES, "UTF-8");
            $creator    = htmlentities($bug_data->bugs[0]->creator, ENT_QUOTES, "UTF-8");

            // add link
            $html = "<a class=\"pr_popup\" target=\"_blank\" title=\"Problem report\" href=\"${url}show_bug.cgi?id=${pr_id}\">PR: ${summary}</a>";

            // add popup (initially hidden)
            $html .= "<div class=\"pr_popup\"><ul><li>Status: ${status}</li><li>Component: ${component}</li><li>Resolution: ${resolution}</li><li>Creator: ${creator}</li></ul></div>";

            // add CSS TODO: make pretty
            $html .= "<style>
                        div.pr_popup { display: none; border: 1px solid black; background-color: #ffe6e6; padding: 4px; position: absolute; }
                        div.pr_popup ul { list-style-type: none; margin: 0; padding:0; }
                        a.pr_popup:hover + div.pr_popup { display: block; }
                    </style>";
        }

        return $html;
    }
}

// These are dummies, XenForo will pass them to the replacement function
$tagChildren = null;
$tagOption   = null;
$tag         = null;
$options     = array();
$renderer    = null;
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>PR test</title>
    </head>
    <body>
        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
        <div><?php echo Replacement::getPR($tagChildren, $tagOption, 259851, $options/*, $renderer*/); ?></div>
        <div><?php echo Replacement::getPR($tagChildren, $tagOption, 259852, $options/*, $renderer*/); ?></div>
    </body>
</html>
 

covacat

Daemon

Reaction score: 515
Messages: 1,040

the problem with the above implementantion is that it takes 2-4 seconds for such a request (at least in europe) and if there are several PRs on one page it will take forever for that page to load
it will come better with something like this (could be make to look better with a little more effort, also can easily be cached so if there are more than one link to the same PR it will do the request only once)

JavaScript:
$("tt").each(function() {
var t = $(this).text();
var that = this;
console.log(t);
if(t.match(/^PR [0-9]+$/)) {
  $.get('https://bugs.freebsd.org/bugzilla/rest/bug/'
  + t.substring(3) + '?include_fields=summary,status,component,resolution,creator',function(a,b,c) {
  if(a.bugs[0].summary) {
   $(that).attr("title",a.bugs[0].component + " => " + a.bugs[0].summary + "\n"
   + "Resolution: " + a.bugs[0].resolution + "\n" + "Status: " + a.bugs[0].status);
  }

 })
 }
})
 
OP
grahamperrin

grahamperrin

Son of Beastie

Reaction score: 1,006
Messages: 3,418

… 2-4 seconds for such a request …

Can the request be made only if a reader points (without clicking)?

Visually, much the same way that Bugzilla itself presents the summary line of a linked bug, as a hover tip, only if there's pointing at the link.
 

eternal_noob

Daemon

Reaction score: 881
Messages: 1,157

Yeah, i noticed too that the REST API of Bugzilla is very slow. I thought that this is the fault of my Raspberry Pi but if you guys tested it with real hardware, there must be something to it.
Maybe this is worth a PR and can be fixed.
Can the request be made only if a reader points (without clicking)?
You need a request for the title of the PR if you want it to be used as link caption.
 

covacat

Daemon

Reaction score: 515
Messages: 1,040

doing it with js after the document loads will make the problem less visible
doing it with js only on hover will have do display a popup with 'loading...' so the user will know something should come up
for testing you can paste the js code in browser development console, then move the mouse pointer over a PR
 
Top