4 * Copyright 1999,2000,2001,2002,2005 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 require_once('lib/PageList.php');
26 * With 1.3.11 the "pages" argument was renamed to "numpages".
27 * action=upgrade should deal with pages containing RandomPage modified earlier than 2005-01-24
29 class WikiPlugin_RandomPage
33 return _("RandomPage");
36 function getDescription () {
37 return _("Displays a list of randomly chosen pages or redirects to a random page.");
40 function getDefaultArguments() {
43 PageList::supportedArgs(),
44 array('numpages' => 20, // was pages
45 'pages' => false, // deprecated
47 'hidename' => false, // only for numpages=1
48 'exclude' => $this->default_exclude(),
52 function run($dbi, $argstr, &$request, $basepage) {
53 $args = $this->getArgs($argstr, $request);
57 if (is_integer($pages)) {
60 // fix new pages handling in arg preprozessor.
61 } elseif (is_array($pages)) {
62 $numpages = (int)$pages[0];
63 if ($numpages > 0 and !$dbi->isWikiPage($numpages)) $pages = false;
67 $allpages = $dbi->getAllPages(false, $sortby, $limit, $exclude);
68 $pagearray = $allpages->asArray();
69 better_srand(); // Start with a good seed.
71 if (($numpages == 1) && $pagearray) {
72 $page = $pagearray[array_rand($pagearray)];
73 $pagename = $page->getName();
75 $request->redirect(WikiURL($pagename, false, 'absurl')); // noreturn
77 return WikiLink($pagename, false, _("RandomPage"));
79 return WikiLink($pagename);
82 $numpages = min( max(1, (int) $numpages), 20, count($pagearray));
83 $pagelist = new PageList($info, $exclude, $args);
84 $shuffle = array_rand($pagearray, $numpages);
85 if (is_array($shuffle)) {
86 foreach ($shuffle as $i)
87 if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
88 } else { // if $numpages = 1
89 if (isset($pagearray[$shuffle]))
90 $pagelist->addPage($pagearray[$shuffle]);
95 function default_exclude() {
96 // Some useful default pages to exclude.
97 $default_exclude = 'RandomPage,HomePage,AllPages,RecentChanges,RecentEdits,FullRecentChanges';
98 foreach (explode(",", $default_exclude) as $e) {
99 $exclude[] = gettext($e);
101 return implode(",", $exclude);
109 // c-hanging-comment-ender-p: nil
110 // indent-tabs-mode: nil