File: RandomPage.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (141 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php // -*-php-*-
rcs_id('$Id: RandomPage.php,v 1.13 2005/01/25 08:09:26 rurban Exp $');
/**
 Copyright 1999,2000,2001,2002,2005 $ThePhpWikiProgrammingTeam

 This file is part of PhpWiki.

 PhpWiki is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 PhpWiki is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with PhpWiki; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once('lib/PageList.php');

/**
 * With 1.3.11 the "pages" argument was renamed to "numpages".
 * action=upgrade should deal with pages containing RandomPage modified earlier than 2005-01-24
 */
class WikiPlugin_RandomPage
extends WikiPlugin
{
    function getName () {
        return _("RandomPage");
    }

    function getDescription () {
        return _("Displays a list of randomly chosen pages or redirects to a random page.");
    }

    function getVersion() {
        return preg_replace("/[Revision: $]/", '',
                            "\$Revision: 1.13 $");
    }

    function getDefaultArguments() {
        return array_merge
            (
             PageList::supportedArgs(),
             array('numpages'     => 20,     // was pages
                   'pages'        => false, // deprecated
                   'redirect'     => false,
                   'hidename'     => false, // only for numpages=1
                   'exclude'      => $this->default_exclude(),
                   'info'         => ''));
    }

    function run($dbi, $argstr, &$request, $basepage) {
        $args = $this->getArgs($argstr, $request);
        extract($args);

        // fix deprecated arg
        if (is_integer($pages)) {
            $numpages = $pages;
            $pages = false;
        // fix new pages handling in arg preprozessor.    
        } elseif (is_array($pages)) {
            $numpages = (int)$pages[0];
            if ($numpages > 0 and !$dbi->isWikiPage($numpages)) $pages = false;
            else $numpages = 1;
        }

        $allpages = $dbi->getAllPages(false, $sortby, $limit, $exclude);
        $pagearray = $allpages->asArray();
        better_srand(); // Start with a good seed.

        if (($numpages == 1) && $pagearray) {
            $page = $pagearray[array_rand($pagearray)];
            $pagename = $page->getName();
            if ($redirect)
                $request->redirect(WikiURL($pagename, false, 'absurl')); // noreturn
            if ($hidename)
                return WikiLink($pagename, false, _("RandomPage"));
            else
                return WikiLink($pagename);
        }

        $numpages = min( max(1, (int) $numpages), 20, count($pagearray));
        $pagelist = new PageList($info, $exclude, $args);
        $shuffle = array_rand($pagearray, $numpages);
        if (is_array($shuffle)) {
            foreach ($shuffle as $i)
                if (isset($pagearray[$i])) $pagelist->addPage($pagearray[$i]);
        } else { // if $numpages = 1
             if (isset($pagearray[$shuffle]))
	         $pagelist->addPage($pagearray[$shuffle]);
        }
        return $pagelist;
    }

    function default_exclude() {
        // Some useful default pages to exclude.
        $default_exclude = 'RandomPage,HomePage,AllPages,RecentChanges,RecentEdits,FullRecentChanges';
        foreach (explode(",", $default_exclude) as $e) {
            $exclude[] = gettext($e);
        }
        return implode(",", $exclude);
    }
};


// $Log: RandomPage.php,v $
// Revision 1.13  2005/01/25 08:09:26  rurban
// deprecate pages, replaced by numpages; use complicated transition code; improved pagelist args support
//
// Revision 1.12  2004/11/25 08:30:15  rurban
// dont mess around with spaces
//
// Revision 1.11  2004/10/04 23:42:42  rurban
// fix for pages=1
//
// Revision 1.10  2004/02/17 12:11:36  rurban
// 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, ...)
//
// Revision 1.9  2003/01/18 22:01:43  carstenklapp
// Code cleanup:
// Reformatting & tabs to spaces;
// Added copyleft, getVersion, getDescription, rcs_id.
//
// Revision 1.8  2003/01/04 02:25:41  carstenklapp
// Added copyleft and plugin description & version, tweaked default
// exclude list code to allow spaces (a cosmetic workaround for
// PluginManager plugin).

// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>