2 rcs_id('$Id: RandomPage.php,v 1.12 2004-11-25 08:30:15 rurban Exp $');
4 Copyright 1999, 2000, 2001, 2002 $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');
25 class WikiPlugin_RandomPage
29 return _("RandomPage");
32 function getDescription () {
33 return _("Displays a list of randomly chosen pages or redirects to a random page.");
36 function getVersion() {
37 return preg_replace("/[Revision: $]/", '',
38 "\$Revision: 1.12 $");
41 function getDefaultArguments() {
42 return array('pages' => 1,
44 'hidename' => false, // only for pages=1
45 'exclude' => $this->default_exclude(),
49 function run($dbi, $argstr, &$request, $basepage) {
50 extract($this->getArgs($argstr, $request));
52 $allpages = $dbi->getAllPages();
53 $_exclude = $exclude ? explode(",", $exclude) : array();
54 while ($page = $allpages->next()) {
55 if (!in_array($page->getName(), $_exclude))
59 better_srand(); // Start with a good seed.
61 if ($pages == 1 && $pagearray) {
62 $page = $pagearray[array_rand($pagearray)];
64 $request->redirect(WikiURL($page, false, 'absurl')); // noreturn
66 return WikiLink($page, false, _("RandomPage"));
68 return WikiLink($page);
71 $pages = min( max(1, (int)$pages), 20, count($pagearray));
72 $pagelist = new PageList($info);
73 $shuffle = array_rand($pagearray, $pages);
74 if (is_array($pages)) {
75 foreach ($shuffle as $i)
76 if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
77 } else { // if $pages = 1
78 if (isset($pagearray[$shuffle]))
79 $pagelist->addPage($pagearray[$shuffle]);
84 function default_exclude() {
85 // Some useful default pages to exclude.
86 $default_exclude = 'RandomPage,HomePage,AllPages,RecentChanges,RecentEdits,FullRecentChanges';
87 foreach (explode(",", $default_exclude) as $e) {
88 $_exclude[] = gettext($e);
90 return implode(",", $_exclude);
95 // $Log: not supported by cvs2svn $
96 // Revision 1.11 2004/10/04 23:42:42 rurban
99 // Revision 1.10 2004/02/17 12:11:36 rurban
100 // 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, ...)
102 // Revision 1.9 2003/01/18 22:01:43 carstenklapp
104 // Reformatting & tabs to spaces;
105 // Added copyleft, getVersion, getDescription, rcs_id.
107 // Revision 1.8 2003/01/04 02:25:41 carstenklapp
108 // Added copyleft and plugin description & version, tweaked default
109 // exclude list code to allow spaces (a cosmetic workaround for
110 // PluginManager plugin).
116 // c-hanging-comment-ender-p: nil
117 // indent-tabs-mode: nil