Table of Contents
randompage Plugin
Compatible with DokuWiki
- 2023-04-04 "Jack Jackrum" unknown
- 2022-07-31 "Igor" yes
- 2020-07-29 "Hogfather" unknown
- 2018-04-22 "Greebo" unknown
Extension name contains underscore, will not generate popularity points.
Similar to randompage2
My goal is to have the same function as MediaWiki's Random Page.
I use the search functions to have an array of files and output a random page of this array.
This is my first plugin… and really a shame I think for the implementation of the action.
I did this to learn DokuWiki… (the genius of DokuWiki functions, not mine…), So all the credit goes to the DokuWiki code base… (Andreas Gohr and Angelo Bertolli helped me to correct my bad coding attitude :!))
This is experimental.
The link to put in your page
This is a action plugin that implements its own action “?do=randompage”. It is not very impressive, and it may be fixed one day to something better.
This plugins has two files: action.php and random.png, its icon:
So after installing, just put a link in your template like this:
<?PHP echo '<a href=/cgi-bin/anon-www.cgi/https://www.dokuwiki.org/"'. $conf['baseurl'] . $conf['basedir'] .'/doku.php?do=randompage" >'; ?><img src=/cgi-bin/anon-www.cgi/https://www.dokuwiki.org/"<?php echo DOKU_BASE?>lib/plugins/random_page/random.png" alt="Random"> A Random Page</a>
Download
Todo
Use Do instead of Doit!1)Clean the code.Test…use page.idx…Verify ACL…- Exclude Page in a Param. not to do….
The Code
Ok, so the code of action.php:
<?php /** * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Jean Marc Massou <massou@gmail.com> */ // must be run within DokuWiki if(!defined('DOKU_INC')) die(); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'action.php'); class action_plugin_random_page extends DokuWiki_Action_Plugin { /** * return some info */ function getInfo(){ return array( 'author' => 'Jean Marc Massou', 'email' => 'massou@gmail.com', 'date' => '2008-03-13', 'name' => 'Random Page', 'desc' => 'Pick a random Page and display it like MediaWiki', 'url' => 'http://jeanmarcmassou.free.fr/dokuwiki/', ); } /* * Register its handlers with the DokuWiki's event controller */ function register(Doku_Event_Handler $controller) { $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'init', 'header'); } function init(&$event, $args) { // Catch the good request if ($_REQUEST['do'] == 'randompage') { // On efface les headers par défaut if ($args == 'header') { $this->action_randompage($event, $args); } } } function action_randompage(&$event, $args) { global $conf; global $ID; $data = array(); $dir = $conf['savedir']; $data = file ($dir.'/index/page.idx'); //We loops through ten random page... $i = 1; while ($i <= 10 & $i <> "ok"): //echo $i; $i++; $id = $data[array_rand($data, 1)]; $testACL = auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); if ($testACL > 1){ $i="ok"; //echo $id; } endwhile; if ($testACL < 1){ $id = $ID; } header("Location: ".wl($id,'',true)); //echo wl($page,'',true); exit(); } //Function from Php manual to get a random number in a Array function array_rand($array, $lim=1) { mt_srand((double) microtime() * 1000000); for($a=0; $a<=$lim; $a++){ $num[] = mt_srand(0, count($array)-1); } return @$num; } }
Remarks
I really like the idea but instead of using the search() function you should use the list of available pages which gets created by the full text search indexer. It is stored in data/index/page.idx
. This should be faster than rebuilding the whole list of available pages every time. — Andreas Gohr 2007-11-22 16:33
Ok, I have been fired and I will have some times in the future to correct this…jm_zz
That has been done:
I have made a “do while” loop through ten iterations and for each, I check ACL, first time ACL is okay I stop the loop.
If 10 ACL test are wrong we give it the current ID…
If you have a lot of private pages, the ACL check could fail the random function… because we do 10 random control… so perhaps it could be better to increase this parameter to 100 for example… but the function will be time and processor consuming…
for example:
//We loops through ten random page... $i = 1; while ($i <= 10 & $i <> "ok"): //We loops through one hundred random page... $i = 1; while ($i <= 100 & $i <> "ok"):
I had to add “global $USERINFO;
” at the beginning of the action_randompage
function. Without this, the ACL check always returned 0 to non admin users so they never get a random page… After this correction, the random_page plugin worked great for everybody. Thanks for it! (Etienne M.)
Alternate template code
I didn't like the plain link for calling this via a template, I'd prefer to have a button to match the other stock buttons, so I added the following to my “main.php” in the “<div class=“bar” id=“bar_top”> section (taken from the examples provided by the ODT and export to PDF plugins):
<form class="button" method="get" action=/cgi-bin/anon-www.cgi/https://www.dokuwiki.org/"<?php wl($ID)?>"> <div class="no"> <input type="submit" value="Random Page" class="button" /> <input type="hidden" name="do" value="randompage" /> <input type="hidden" name="id" value="<?php echo $ID?>" /> </div> </form>
Cleaning the code (done)
Thanks for the plugin. When going to a random page, there is a line feed character at the end of the URL. In your code, you have:
header("Location: ".wl($id,'',true)); echo wl($page,'',true); exit();
To fix this, I added trim():
header("Location: ".wl(trim($id),'',true)); // echo wl($page,'',true); exit();
Do instead of doit (Done)
Also, I wonder why you used the action “doit” instead of “do” which is used for other things. If it uses “do” at least it's consistent and can have other benefits for URL parsing, .htaccess control, URL building, stats view filtering, etc. — Angelo Bertolli 2008-11-29
Deleted pages
This plugin is not usable for me as it returns deleted pages. I've tried on 3 DokuWiki installs and the bug is always there. Does anyone has found a solution ? — Laynee 2009/06/17 22:52
Just recreate the page index ( re run ../bin/indexer.php) — ueli 2009-okt-29 22:18
Limit to a namespace
I want to create a link to a random page in a particular namespace. Is it possible to limit the random page to just one namespace?
Possible: yes - implemented: no.
You'd have to filter array built from the Index file for the namespace you want. Add PHP's array_filter with a callback to check if the current line matches the wanted namespace after the “$data” array is filled in line 62. — Christian Marg 2011/02/24 08:18
Anyone would do this? I have pretty different contents in namespaces…like medicine and games.. I need to limit random pages.. — S.C. Yoo 2012/11/10 10:14
I did this myself. Refer https://github.com/dryoo/DokuWiki-randompage . You can?do=randompage
&?do=nsrandompage
Download here — S.C. Yoo 2014-10-21 02:51
Fix for Dokuwiki Angua
In Dokuwiki Angua you must delete the underscore in the Foldername (rename it to “randompage”) and edit line 13 at the action.php like this:
class action_plugin_randompage extends DokuWiki_Action_Plugin {
Other Fixes
I fixed two issues I saw. The plugin was returning control characters at the end of URLs, and (as mentioned above) was returning pages that had been deleted. (Still could use some testing!) – pete@rasterweb.net