]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RandomPage.php
Added copyleft and plugin description & version, tweaked default
[SourceForge/phpwiki.git] / lib / plugin / RandomPage.php
1 <?php // -*-php-*-
2 rcs_id('$Id: RandomPage.php,v 1.8 2003-01-04 02:25:41 carstenklapp Exp $');
3
4 /**
5  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 require_once('lib/PageList.php');
25
26 class WikiPlugin_RandomPage
27 extends WikiPlugin
28 {
29     function getName () {
30         return _("RandomPage");
31     }
32
33     function getDescription () {
34         return _("Displays a list of randomly chosen pages or redirects to a random page.");
35     }
36
37     function getVersion() {
38         return preg_replace("/[Revision: $]/", '',
39                             "\$Revision: 1.8 $");
40     }
41
42     function getDefaultArguments() {
43         return array('pages'        => 1,
44                      'redirect'     => false,
45                      'hidename'     => false, // only for pages=1
46                      'exclude'      => $this->default_exclude(),
47                      'info'         => '');
48     }
49
50     function run($dbi, $argstr, $request) {
51         extract($this->getArgs($argstr, $request));
52
53         $allpages = $dbi->getAllPages();
54
55         $exclude = $exclude ? explode(",", $exclude) : array();
56         foreach ($exclude as $e) {
57             $_exclude []= trim($e);
58         }
59
60         while ($page = $allpages->next()) {
61             if (!in_array($page->getName(), $_exclude))
62                 $pagearray[] = $page;
63         }
64
65         better_srand(); // Start with a good seed.
66
67         if ($pages == 1 && $pagearray) {
68             $page = $pagearray[array_rand($pagearray)];
69             if ($redirect)
70                 $request->redirect(WikiURL($page, false, 'absurl')); // noreturn
71             if ($hidename)
72                 return WikiLink($page, false, _("RandomPage"));
73             else
74                 return WikiLink($page);
75         }
76
77         $pages = min( max(1, (int)$pages), 20, count($pagearray));
78         $pagelist = new PageList($info);
79         $shuffle = array_rand($pagearray, $pages);
80         foreach ($shuffle as $i)
81             $pagelist->addPage($pagearray[$i]);
82         return $pagelist;
83     }
84
85     function default_exclude() {
86         // Some useful default pages to exclude.
87         $default_exclude = 'RandomPage, HomePage, AllPages, RecentChanges, RecentEdits, FullRecentChanges';
88         foreach (explode(",", $default_exclude) as $e) {
89             $_exclude[] = gettext(trim($e));
90         }
91         return implode(", ", $_exclude);
92     }
93 };
94
95 /**
96  $Log: not supported by cvs2svn $
97  */
98
99 // Local Variables:
100 // mode: php
101 // tab-width: 8
102 // c-basic-offset: 4
103 // c-hanging-comment-ender-p: nil
104 // indent-tabs-mode: nil
105 // End:
106 ?>