]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PopularNearby.php
re-ordered locale init (as in 1.3.9)
[SourceForge/phpwiki.git] / lib / plugin / PopularNearby.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PopularNearby.php,v 1.2 2004-04-29 23:25:12 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
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 /** Re-implement the classic phpwiki-1.2 feature of the 
24  *  popular nearby pages, specific to the from/to links:
25  *    5 best incoming links: xx, xx, xx, ...
26  *    5 best outgoing links: xx, xx, xx, ...
27  *    5 most popular nearby: xx, xx, xx, ...
28  */
29 /* Usage:
30
31 * <small><?plugin PopularNearby mode=incoming ?></small>
32 * <small><?plugin PopularNearby mode=outgoing ?></small>
33 * <small><?plugin PopularNearby mode=nearby ?></small>
34
35 */
36  
37
38 require_once('lib/PageList.php');
39
40 class WikiPlugin_PopularNearby
41 extends WikiPlugin
42 {
43     function getName () {
44         return _("PopularNearby");
45     }
46
47     function getDescription () {
48         return _("List the most popular pages nearby.");
49     }
50
51     function getVersion() {
52         return preg_replace("/[Revision: $]/", '',
53                             "\$Revision: 1.2 $");
54     }
55
56     function getDefaultArguments() {
57         return array('pagename' => '[pagename]',
58                      'mode'     => 'nearby', // or 'incoming' or 'outgoing'
59                      'exclude'  => '',
60                      'limit'    => 5,
61                      'noheader' => 0,
62                     );
63     }
64     
65     function run($dbi, $argstr, &$request, $basepage) {
66         $args = $this->getArgs($argstr, $request);
67         extract($args);
68         $header = '';
69         $page = $dbi->getPage($pagename);
70         switch ($mode) {
71         case 'incoming': // not the hits, but the number of links
72             if (! $noheader )
73                 $header = sprintf(_("%d best incoming links: "),$limit); 
74             $links = $this->sortedLinks($page->getLinks("reversed"),"reversed",$limit);
75             break;
76         case 'outgoing': // not the hits, but the number of links
77             if (! $noheader )
78                 $header = sprintf(_("%d best outgoing links: "),$limit); 
79             $links = $this->sortedLinks($page->getLinks(),false,$limit);
80             break;
81         case 'nearby':  // all linksfrom and linksto, sorted by hits 
82             if (! $noheader )
83                 $header = sprintf(_("%d most popular nearby: "),$limit); 
84             $inlinks = $page->getLinks();
85             $outlinks = $page->getLinks('reversed');
86             $links = $this->sortedLinks(array_merge($inlinks->asArray(),
87                                                     $outlinks->asArray()),
88                                         false,$limit);
89             break;
90         }
91         $html = HTML($header);
92         for ($i=0; $i<count($links); $i++) {
93             $html->pushContent($links[$i]['format'],$i<count($links)-1?', ':'');
94         }
95         return $html;
96     }
97
98     /**
99      * Get and sort the links:
100      *   mode=nearby:   $pages Array
101      *   mode=incoming: $pages iter and $direction=true
102      *   mode=outgoing: $pages iter and $direction=false
103      *
104      * @access private
105      *
106      * @param $pages array of WikiDB_Page's or a Page_iterator
107      * @param $direction boolean: true if incoming links
108      * 
109      * @return Array of sorted links
110      */
111     function sortedLinks($pages, $direction=false, $limit=5) {
112         $links = array();
113         if (is_array($pages)) {
114             foreach ($pages as $page) {
115                 // just the number of hits
116                 $hits = $page->get('hits');
117                 if (!$hits) continue;
118                 $links[] = array('hits' => $hits,
119                                  'pagename' => $page->_pagename,
120                                  'format' => HTML(WikiLink($page->_pagename),' (' . $hits . ')'));
121             }
122         } else {
123             while ($page = $pages->next()) {
124                 // different score algorithm: 
125                 //   the number of links to/from the page
126                 $l = $page->getLinks(!$direction);
127                 $score = $l->count();
128                 if (!$score) continue;
129                 $name = $page->_pagename;
130                 $links[] = array('hits' => $score,
131                                  'pagename' => $name,
132                                  'format' => HTML(WikiLink($name),' (' . $score . ')'));
133             }
134             $pages->free();
135         }
136         if (count($links) > $limit)
137             array_splice($links, $limit);
138         return $this->sortByHits($links);
139     }
140
141     function sortByHits($links) {
142         if (!$links) return array();
143         usort($links,array('WikiPlugin_PopularNearby','cmp_hits'));
144         reset($links);
145         return $links;
146     }
147
148     function cmp_hits($a, $b) {
149         return ($a['hits'] < $b['hits']) ? 1 : ($a['hits'] == $b['hits']) ? 0 : -1;
150     }
151
152
153 };
154
155 // $Log: not supported by cvs2svn $
156 // Revision 1.1  2004/04/29 18:32:38  rurban
157 // Re-implement the classic phpwiki-1.2 feature of the
158 //  * 5 best incoming links: xx, xx, xx, ...
159 //  * 5 best outgoing links: xx, xx, xx, ...
160 //  * 5 most popular nearby: xx, xx, xx, ...
161 //
162
163 // Local Variables:
164 // mode: php
165 // tab-width: 8
166 // c-basic-offset: 4
167 // c-hanging-comment-ender-p: nil
168 // indent-tabs-mode: nil
169 // End:
170 ?>