]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RandomPage.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / RandomPage.php
1 <?php // -*-php-*-
2 // $Id$
3 /**
4  * Copyright 1999,2000,2001,2002,2005 $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 require_once('lib/PageList.php');
24
25 /**
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
28  */
29 class WikiPlugin_RandomPage
30 extends WikiPlugin
31 {
32     function getName () {
33         return _("RandomPage");
34     }
35
36     function getDescription () {
37         return _("Displays a list of randomly chosen pages or redirects to a random page.");
38     }
39
40     function getDefaultArguments() {
41         return array_merge
42             (
43              PageList::supportedArgs(),
44              array('numpages'     => 20,     // was pages
45                    'pages'        => false, // deprecated
46                    'redirect'     => false,
47                    'hidename'     => false, // only for numpages=1
48                    'exclude'      => $this->default_exclude(),
49                    'info'         => ''));
50     }
51
52     function run($dbi, $argstr, &$request, $basepage) {
53
54         $args = $this->getArgs($argstr, $request);
55         extract($args);
56
57         // Redirect would break HTML dump
58         if ($request->getArg('action') != 'browse') {
59             return $this->disabled(_("Plugin not run: not in browse mode"));
60         }
61
62         // fix deprecated arg
63         if (is_integer($pages)) {
64             $numpages = $pages;
65             $pages = false;
66         // fix new pages handling in arg preprozessor.
67         } elseif (is_array($pages)) {
68             $numpages = (int)$pages[0];
69             if ($numpages > 0 and !$dbi->isWikiPage($numpages)) $pages = false;
70             else $numpages = 1;
71         }
72
73         $allpages = $dbi->getAllPages(false, $sortby, $limit, $exclude);
74         $pagearray = $allpages->asArray();
75         better_srand(); // Start with a good seed.
76
77         if (($numpages == 1) && $pagearray) {
78             $page = $pagearray[array_rand($pagearray)];
79             $pagename = $page->getName();
80             if ($redirect)
81                 $request->redirect(WikiURL($pagename, false, 'absurl')); // noreturn
82             if ($hidename)
83                 return WikiLink($pagename, false, _("RandomPage"));
84             else
85                 return WikiLink($pagename);
86         }
87
88         $numpages = min( max(1, (int) $numpages), 20, count($pagearray));
89         $pagelist = new PageList($info, $exclude, $args);
90         $shuffle = array_rand($pagearray, $numpages);
91         if (is_array($shuffle)) {
92             foreach ($shuffle as $i)
93                 if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
94         } else { // if $numpages = 1
95              if (isset($pagearray[$shuffle]))
96                  $pagelist->addPage($pagearray[$shuffle]);
97         }
98         return $pagelist;
99     }
100
101     function default_exclude() {
102         // Some useful default pages to exclude.
103         $default_exclude = 'RandomPage,HomePage,AllPages,RecentChanges,RecentEdits,FullRecentChanges';
104         foreach (explode(",", $default_exclude) as $e) {
105             $exclude[] = gettext($e);
106         }
107         return implode(",", $exclude);
108     }
109 };
110
111 // Local Variables:
112 // mode: php
113 // tab-width: 8
114 // c-basic-offset: 4
115 // c-hanging-comment-ender-p: nil
116 // indent-tabs-mode: nil
117 // End:
118 ?>