]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RandomPage.php
fix for pages=1
[SourceForge/phpwiki.git] / lib / plugin / RandomPage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RandomPage.php,v 1.11 2004-10-04 23:42:42 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 require_once('lib/PageList.php');
24
25 class WikiPlugin_RandomPage
26 extends WikiPlugin
27 {
28     function getName () {
29         return _("RandomPage");
30     }
31
32     function getDescription () {
33         return _("Displays a list of randomly chosen pages or redirects to a random page.");
34     }
35
36     function getVersion() {
37         return preg_replace("/[Revision: $]/", '',
38                             "\$Revision: 1.11 $");
39     }
40
41     function getDefaultArguments() {
42         return array('pages'        => 1,
43                      'redirect'     => false,
44                      'hidename'     => false, // only for pages=1
45                      'exclude'      => $this->default_exclude(),
46                      'info'         => '');
47     }
48
49     function run($dbi, $argstr, &$request, $basepage) {
50         extract($this->getArgs($argstr, $request));
51
52         $allpages = $dbi->getAllPages();
53
54         $exclude = $exclude ? explode(",", $exclude) : array();
55         foreach ($exclude as $e) {
56             $_exclude []= trim($e);
57         }
58
59         while ($page = $allpages->next()) {
60             if (!in_array($page->getName(), $_exclude))
61                 $pagearray[] = $page;
62         }
63
64         better_srand(); // Start with a good seed.
65
66         if ($pages == 1 && $pagearray) {
67             $page = $pagearray[array_rand($pagearray)];
68             if ($redirect)
69                 $request->redirect(WikiURL($page, false, 'absurl')); // noreturn
70             if ($hidename)
71                 return WikiLink($page, false, _("RandomPage"));
72             else
73                 return WikiLink($page);
74         }
75
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]);
85         }
86         return $pagelist;
87     }
88
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));
94         }
95         return implode(", ", $_exclude);
96     }
97 };
98
99
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, ...)
103 //
104 // Revision 1.9  2003/01/18 22:01:43  carstenklapp
105 // Code cleanup:
106 // Reformatting & tabs to spaces;
107 // Added copyleft, getVersion, getDescription, rcs_id.
108 //
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).
113
114 // Local Variables:
115 // mode: php
116 // tab-width: 8
117 // c-basic-offset: 4
118 // c-hanging-comment-ender-p: nil
119 // indent-tabs-mode: nil
120 // End:
121 ?>