]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RandomPage.php
verbose arg
[SourceForge/phpwiki.git] / lib / plugin / RandomPage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RandomPage.php,v 1.12 2004-11-25 08:30:15 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.12 $");
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         $_exclude = $exclude ? explode(",", $exclude) : array();
54         while ($page = $allpages->next()) {
55             if (!in_array($page->getName(), $_exclude))
56                 $pagearray[] = $page;
57         }
58
59         better_srand(); // Start with a good seed.
60
61         if ($pages == 1 && $pagearray) {
62             $page = $pagearray[array_rand($pagearray)];
63             if ($redirect)
64                 $request->redirect(WikiURL($page, false, 'absurl')); // noreturn
65             if ($hidename)
66                 return WikiLink($page, false, _("RandomPage"));
67             else
68                 return WikiLink($page);
69         }
70
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]);
80         }
81         return $pagelist;
82     }
83
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);
89         }
90         return implode(",", $_exclude);
91     }
92 };
93
94
95 // $Log: not supported by cvs2svn $
96 // Revision 1.11  2004/10/04 23:42:42  rurban
97 // fix for pages=1
98 //
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, ...)
101 //
102 // Revision 1.9  2003/01/18 22:01:43  carstenklapp
103 // Code cleanup:
104 // Reformatting & tabs to spaces;
105 // Added copyleft, getVersion, getDescription, rcs_id.
106 //
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).
111
112 // Local Variables:
113 // mode: php
114 // tab-width: 8
115 // c-basic-offset: 4
116 // c-hanging-comment-ender-p: nil
117 // indent-tabs-mode: nil
118 // End:
119 ?>