random page plugin

random_page plugin by Jean Marc Massou
A Random Page Action Plugins, MediaWiki Random Page like.

Last updated on 2009-01-03. Provides Action.
No compatibility info given!

Tagged with mediawiki, page, random.

My goal is to have the same Function as MediaWiki 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 do this job to learn DokuWiki…(genius of DokuWiki functions, not mine…;-)), So all the credits gone to DokuWiki base code… 1) This is experimental.

The link to put in your page

This is a action plugin that implement its own action “?do=randompage”. It is not very impressive, may be fixed one day to something better.

This plugins has two files : action.php and random.png, its icon :

So after install just put a link in your template like this.

<?PHP
echo '<a href="'. $conf['baseurl'] . $conf['basedir'] .'/doku.php?do=randompage" >';
?><img src="<?php echo DOKU_BASE?>lib/plugins/random_page/random.png" alt="Random"> A Random Page</a>

Download

Todo

  1. Use Do instead of Doit !2)
  2. Clean the code.
  3. Test…
  4. use page.idx…
  5. Verify ACL
  6. 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(&$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's has been done :
- I have make a “do while” loop through ten iterations and for each, I check ACL, first time ACL is OK I stop the loop.
If 10 ACL test are wrong we give it the current ID…
If you have a lot of private Page, ACL check could make 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 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"):

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

1) Andreas Gohr and Angelo Bertolli help me to correct the good way my bad coding attitude :!
2) First version use Doit, now we use just Do
 
plugin/random_page.txt · Last modified: 2009/06/17 22:58 by Laynee
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsDarcsXRefTranslate