]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MostPopular.php
Test 'limit' argument is numeric to avoid SQL injection
[SourceForge/phpwiki.git] / lib / plugin / MostPopular.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
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_MostPopular
27 extends WikiPlugin
28 {
29     function getName () {
30         return _("MostPopular");
31     }
32
33     function getDescription () {
34         return _("List the most popular pages.");
35     }
36
37     function getVersion() {
38         return preg_replace("/[Revision: $]/", '',
39                             "\$Revision$");
40     }
41
42     function getDefaultArguments() {
43         return array_merge
44             (
45              PageList::supportedArgs(),
46              array('pagename' => '[pagename]', // hackish
47                    //'exclude'  => '',
48                    'limit'    => 20, // limit <0 returns least popular pages
49                    'noheader' => 0,
50                    'sortby'   => '-hits',
51                    'info'     => false,
52                    //'paging'   => 'auto'
53                    ));
54     }
55     
56     // info arg allows multiple columns
57     // info=mtime,hits,summary,version,author,locked,minor
58     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
59     // sortby: only pagename or hits. mtime not!
60
61     function run($dbi, $argstr, &$request, $basepage) {
62         $args = $this->getArgs($argstr, $request);
63
64         if (!empty($args['limit']) && !is_numeric($args['limit'])) {
65             return $this->error(_("Illegal 'limit' argument: must be numeric"));
66         }
67
68         extract($args);
69         if (strstr($sortby,'mtime')) {
70             trigger_error(_("sortby=mtime not supported with MostPopular"),
71                           E_USER_WARNING);
72             $sortby = '';
73         }
74         $columns = $info ? explode(",", $info) : array();
75         array_unshift($columns, 'hits');
76         
77         if (! $request->getArg('count')) {
78             //$args['count'] = $dbi->numPages(false,$exclude);
79             $allpages = $dbi->mostPopular(0, $sortby);
80             $args['count'] = $allpages->count();
81         } else {
82             $args['count'] = $request->getArg('count');
83         }
84         //$dbi->touch();
85         $pages = $dbi->mostPopular($limit, $sortby);
86         $pagelist = new PageList($columns, $exclude, $args);
87         while ($page = $pages->next()) {
88             $hits = $page->get('hits');
89             // don't show pages with no hits if most popular pages wanted
90             if ($hits == 0 && $limit > 0) {
91                 break;
92             }
93             $pagelist->addPage($page);
94         }
95         $pages->free();
96
97         if (! $noheader) {
98             if ($limit > 0) {
99                 $pagelist->setCaption(fmt("The %d most popular pages of this wiki:", $limit));
100             } else if ($limit < 0) {
101                 $pagelist->setCaption(fmt("The %d least popular pages of this wiki:", -$limit));
102             } else {
103                 $pagelist->setCaption(_("Visited pages on this wiki, ordered by popularity:"));
104             }
105         }
106
107         return $pagelist;
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 ?>