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