]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PopularTags.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / plugin / PopularTags.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
3 /*
4  * Copyright 2007 Reini Urban
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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /* Usage:
24  * template tags.tmpl:
25  *   <?plugin PopularTags ?>
26  */
27
28 require_once('lib/PageList.php');
29
30 class WikiPlugin_PopularTags
31 extends WikiPlugin
32 {
33     function getName () {
34         return _("PopularTags");
35     }
36
37     function getDescription () {
38         return _("List the most popular tags.");
39     }
40
41     function getDefaultArguments() {
42         return array('pagename' => '[pagename]',
43                      'limit'    => 10,
44                      'mincount' => 5,
45                      'noheader' => 0,
46                     );
47     }
48
49     function run($dbi, $argstr, &$request, $basepage) {
50             $args = $this->getArgs($argstr, $request);
51         extract($args);
52
53         $maincat = $dbi->getPage(_("CategoryCategory"));
54         $bi = $maincat->getBackLinks(false);
55         $bl = array();
56         while ($b = $bi->next()) {
57             $name = $b->getName();
58             if (preg_match("/^"._("Template")."/", $name)) continue;
59             $pages = $b->getBackLinks(false);
60             $bl[] = array('name' => $name,
61                           'count' => $pages->count());
62         }
63
64         usort($bl, 'cmp_by_count');
65         $html = HTML::ul(); $i = 0;
66         foreach ($bl as $b) {
67             $i++;
68             $name  = $b['name'];
69             $count = $b['count'];
70             if ($count < $mincount) break;
71             if ($i > $limit) break;
72             $wo = preg_replace("/^("._("Category")."|"
73                                ._("Topic").")/", "", $name);
74             $wo = HTML(HTML::span($wo),HTML::raw("&nbsp;"),HTML::small("(".$count.")"));
75             $link = WikiLink($name, 'auto', $wo);
76             $html->pushContent(HTML::li($link));
77         }
78         return $html;
79     }
80 }
81
82 // get list of categories sorted by number of backlinks
83 function cmp_by_count($a, $b) {
84      if ($a['count'] == $b['count']) return 0;
85      return $a['count'] < $b['count'] ? 1 : -1;
86 }
87
88 // Local Variables:
89 // mode: php
90 // tab-width: 8
91 // c-basic-offset: 4
92 // c-hanging-comment-ender-p: nil
93 // indent-tabs-mode: nil
94 // End:
95 ?>