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 getVersion() {
41 return preg_replace("/[Revision: $]/", '',
42 "\$Revision: 1.13 $");
45 function getDefaultArguments() {
48 PageList::supportedArgs(),
49 array('numpages' => 20, // was pages
50 'pages' => false, // deprecated
52 'hidename' => false, // only for numpages=1
53 'exclude' => $this->default_exclude(),
57 function run($dbi, $argstr, &$request, $basepage) {
58 $args = $this->getArgs($argstr, $request);
62 if (is_integer($pages)) {
65 // fix new pages handling in arg preprozessor.
66 } elseif (is_array($pages)) {
67 $numpages = (int)$pages[0];
68 if ($numpages > 0 and !$dbi->isWikiPage($numpages)) $pages = false;
72 $allpages = $dbi->getAllPages(false, $sortby, $limit, $exclude);
73 $pagearray = $allpages->asArray();
74 better_srand(); // Start with a good seed.
76 if (($numpages == 1) && $pagearray) {
77 $page = $pagearray[array_rand($pagearray)];
78 $pagename = $page->getName();
80 $request->redirect(WikiURL($pagename, false, 'absurl')); // noreturn
82 return WikiLink($pagename, false, _("RandomPage"));
84 return WikiLink($pagename);
87 $numpages = min( max(1, (int) $numpages), 20, count($pagearray));
88 $pagelist = new PageList($info, $exclude, $args);
89 $shuffle = array_rand($pagearray, $numpages);
90 if (is_array($shuffle)) {
91 foreach ($shuffle as $i)
92 if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
93 } else { // if $numpages = 1
94 if (isset($pagearray[$shuffle]))
95 $pagelist->addPage($pagearray[$shuffle]);
100 function default_exclude() {
101 // Some useful default pages to exclude.
102 $default_exclude = 'RandomPage,HomePage,AllPages,RecentChanges,RecentEdits,FullRecentChanges';
103 foreach (explode(",", $default_exclude) as $e) {
104 $exclude[] = gettext($e);
106 return implode(",", $exclude);
111 // $Log: not supported by cvs2svn $
112 // Revision 1.12 2004/11/25 08:30:15 rurban
113 // dont mess around with spaces
115 // Revision 1.11 2004/10/04 23:42:42 rurban
118 // Revision 1.10 2004/02/17 12:11:36 rurban
119 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
121 // Revision 1.9 2003/01/18 22:01:43 carstenklapp
123 // Reformatting & tabs to spaces;
124 // Added copyleft, getVersion, getDescription, rcs_id.
126 // Revision 1.8 2003/01/04 02:25:41 carstenklapp
127 // Added copyleft and plugin description & version, tweaked default
128 // exclude list code to allow spaces (a cosmetic workaround for
129 // PluginManager plugin).
135 // c-hanging-comment-ender-p: nil
136 // indent-tabs-mode: nil