]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MostPopular.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / MostPopular.php
1 <?php
2
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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 require_once 'lib/PageList.php';
25
26 class WikiPlugin_MostPopular
27     extends WikiPlugin
28 {
29     function getDescription()
30     {
31         return _("List the most popular pages.");
32     }
33
34     function getDefaultArguments()
35     {
36         return array_merge
37         (
38             PageList::supportedArgs(),
39             array('pagename' => '[pagename]', // hackish
40                 //'exclude'  => '',
41                 'limit' => 20, // limit <0 returns least popular pages
42                 'noheader' => 0,
43                 'sortby' => '-hits',
44                 'info' => false,
45                 //'paging'   => 'auto'
46             ));
47     }
48
49     // info arg allows multiple columns
50     // info=mtime,hits,summary,version,author,locked,minor
51     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
52     // sortby: only pagename or hits. mtime not!
53
54     /**
55      * @param WikiDB $dbi
56      * @param string $argstr
57      * @param WikiRequest $request
58      * @param string $basepage
59      * @return mixed
60      */
61     function run($dbi, $argstr, &$request, $basepage)
62     {
63         $args = $this->getArgs($argstr, $request);
64         extract($args);
65         if (strstr($sortby, 'mtime')) {
66             trigger_error(_("sortby=mtime not supported with MostPopular"),
67                 E_USER_WARNING);
68             $sortby = '';
69         }
70         $columns = $info ? explode(",", $info) : array();
71         array_unshift($columns, 'hits');
72
73         if (!$request->getArg('count')) {
74             //$args['count'] = $dbi->numPages(false,$exclude);
75             $allpages = $dbi->mostPopular(0, $sortby);
76             $args['count'] = $allpages->count();
77         } else {
78             $args['count'] = $request->getArg('count');
79         }
80         //$dbi->touch();
81         $pages = $dbi->mostPopular($limit, $sortby);
82         $pagelist = new PageList($columns, $exclude, $args);
83         while ($page = $pages->next()) {
84             $hits = $page->get('hits');
85             // don't show pages with no hits if most popular pages wanted
86             if ($hits == 0 && $limit > 0) {
87                 break;
88             }
89             $pagelist->addPage($page);
90         }
91         $pages->free();
92
93         if (!$noheader) {
94             if ($limit > 0) {
95                 $pagelist->setCaption(fmt("The %d most popular pages of this wiki:", $limit));
96             } elseif ($limit < 0) {
97                 $pagelist->setCaption(fmt("The %d least popular pages of this wiki:", -$limit));
98             } else {
99                 $pagelist->setCaption(_("Visited pages on this wiki, ordered by popularity:"));
100             }
101         }
102
103         return $pagelist;
104     }
105 }
106
107 // Local Variables:
108 // mode: php
109 // tab-width: 8
110 // c-basic-offset: 4
111 // c-hanging-comment-ender-p: nil
112 // indent-tabs-mode: nil
113 // End: