[pmwiki-users] automatic Trail with random back & next

Hans design5 at softflow.co.uk
Mon Jul 21 15:54:08 CDT 2008

Here is an extended version:

## {(rndpage label trailpagename group=GroupName exclude=PageA,PageB)}
$MarkupExpr['rndpage'] = 'MakeRandomNext($pagename, $args[0], $args[1], $argp)';
function MakeRandomNext($pagename, $label, $trailname='', $args) {
        global $RandomPageNamesUsed;
        $trail = array();
        //if no trailname make trail from all group pages
        if ($trailname=='') {
                if (isset($args['group'])) $group = $args['group'];
                else $group = PageVar($pagename, '$Group');
                $gplist = ListPages("/^$group\\..+/");
                $exclude = array(
                if (isset($args['exclude'])) {
                        $excl = explode(',',$args['exclude']);
                        foreach($excl as $i => $p)
                                $excl[$i] = MakePageName($group.'.HomePage',$p);
                        $exclude = array_merge($exclude, $excl);
                foreach($gplist as $i => $p)
                        if (in_array($p,$exclude) || in_array($p,(array)$RandomPageNamesUsed)) 
                foreach($gplist as $i => $p)    
                        $trail[$i]['pagename'] = $p;
        else  $trail = ReadTrail($pagename, $trailname);
        //pick random pagename from trail
        $max = count($trail)-1;
        $i = rand(0,$max);
        $pn = $trail[$i]['pagename'];
        $RandomPageNamesUsed[] = $pn;
        if ($label=='title') $label = '+';
        if ($label=='') $label = 'next';        
        if ($label=='name') $label = PageVar($pn, '$Name'); 
        return "[[$pn|$label]]";

This function will exclude some "system" pages by default. Other
pages can be added to b excluded using parameter
exclude=PageName1,PageName2,Pageame3 etc CSV list of pagenames.

The function also keeps track of pages already used, so you won't
see doubles.

As a consequence it got rather elaborate...

Please test!


More information about the pmwiki-users mailing list