2 rcs_id('$Id: RandomPage.php,v 1.11 2004-10-04 23:42:42 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.11 $");
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();
54 $exclude = $exclude ? explode(",", $exclude) : array();
55 foreach ($exclude as $e) {
56 $_exclude []= trim($e);
59 while ($page = $allpages->next()) {
60 if (!in_array($page->getName(), $_exclude))
64 better_srand(); // Start with a good seed.
66 if ($pages == 1 && $pagearray) {
67 $page = $pagearray[array_rand($pagearray)];
69 $request->redirect(WikiURL($page, false, 'absurl')); // noreturn
71 return WikiLink($page, false, _("RandomPage"));
73 return WikiLink($page);
76 $pages = min( max(1, (int)$pages), 20, count($pagearray));
77 $pagelist = new PageList($info);
78 $shuffle = array_rand($pagearray, $pages);
79 if (is_array($pages)) {
80 foreach ($shuffle as $i)
81 if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
82 } else { // if $pages = 1
83 if (isset($pagearray[$shuffle]))
84 $pagelist->addPage($pagearray[$shuffle]);
89 function default_exclude() {
90 // Some useful default pages to exclude.
91 $default_exclude = 'RandomPage, HomePage, AllPages, RecentChanges, RecentEdits, FullRecentChanges';
92 foreach (explode(",", $default_exclude) as $e) {
93 $_exclude[] = gettext(trim($e));
95 return implode(", ", $_exclude);
100 // $Log: not supported by cvs2svn $
101 // Revision 1.10 2004/02/17 12:11:36 rurban
102 // 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, ...)
104 // Revision 1.9 2003/01/18 22:01:43 carstenklapp
106 // Reformatting & tabs to spaces;
107 // Added copyleft, getVersion, getDescription, rcs_id.
109 // Revision 1.8 2003/01/04 02:25:41 carstenklapp
110 // Added copyleft and plugin description & version, tweaked default
111 // exclude list code to allow spaces (a cosmetic workaround for
112 // PluginManager plugin).
118 // c-hanging-comment-ender-p: nil
119 // indent-tabs-mode: nil