====== random page plugin ====== ---- plugin ---- description: A Random Page Action plugin, like MediaWiki's Random Page function. author : Jean Marc Massou email : massou at gmail dot com type : action lastupdate : 2009-01-03 compatible : depends : conflicts : similar : tags : random page, random, mediawiki ---- {{http://jeanmarcmassou.free.fr/random.png}}{{http://jeanmarcmassou.free.fr/random.png}}{{http://jeanmarcmassou.free.fr/random.png}}{{http://jeanmarcmassou.free.fr/random.png}}{{http://jeanmarcmassou.free.fr/random.png}}{{http://jeanmarcmassou.free.fr/random.png}} 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: {{http://jeanmarcmassou.free.fr/random.png}} So after installing, just put a link in your template like this: '; ?>lib/plugins/random_page/random.png" alt="Random"> A Random Page ===== Download ===== [[http://massou.tk/v2/lib/exe/fetch.php?id=blogw%3Aa_random_page_v2_for_2009&cache=cache&media=blogw:random_page.zip|V2 (For test and Validation]] [[http://jeanmarcmassou.free.fr/dokuwiki/lib/exe/fetch.php?id=blogw:random_page_a_mediawiki_tool_like_for_dokuwiki&cache=cache&media=blogw:random_page.zip|V1 (use DOIT instead of Do DEPRECATED) : Zipped File]] ===== Todo ===== - Use Do instead of Doit !((First version use Doit, now we use just Do)) - 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: */ // 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(&$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. --- //[[andi@splitbrain.org|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"): ---- === 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 "
section (taken from the examples provided by the ODT and export to PDF plugins):
">
---- === 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. --- //[[http://heroesonly.com/|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?