2 rcs_id('$Id: PopularNearby.php,v 1.1 2004-04-29 18:32:38 rurban Exp $');
4 Copyright 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
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, ...
31 * <?plugin PopularNearby mode=incoming ?>
32 * <?plugin PopularNearby mode=outgoing ?>
33 * <?plugin PopularNearby mode=nearby ?>
38 require_once('lib/PageList.php');
40 class WikiPlugin_PopularNearby
44 return _("PopularNearby");
47 function getDescription () {
48 return _("List the most popular pages nearby.");
51 function getVersion() {
52 return preg_replace("/[Revision: $]/", '',
56 function getDefaultArguments() {
57 return array('pagename' => '[pagename]',
58 'mode' => 'nearby', // or 'incoming' or 'outgoing'
60 'limit' => 5, // limit <0 returns least popular pages
65 function run($dbi, $argstr, &$request, $basepage) {
66 $args = $this->getArgs($argstr, $request);
69 $page = $dbi->getPage($pagename);
71 case 'incoming': // not the hits, but the number of links
73 $header = sprintf(_("%d best incoming links: "),$limit);
74 $links = $this->sortedLinks($page->getLinks("reversed"),"reversed",$limit);
76 case 'outgoing': // not the hits, but the number of links
78 $header = sprintf(_("%d best outgoing links: "),$limit);
79 $links = $this->sortedLinks($page->getLinks(),false,$limit);
81 case 'nearby': // all linksfrom and linksto, sorted by hits
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()),
91 // if (count($links) > $limit) array_splice($links, $limit);
92 $html = HTML($header);
93 for ($i=0; $i<count($links); $i++) {
94 $html->pushContent($links[$i]['format'],$i<count($links)-1?', ':'');
100 * Get and sort the links:
101 * mode=nearby: $pages Array
102 * mode=incoming: $pages iter and $direction=true
103 * mode=outgoing: $pages iter and $direction=false
107 * @param $pages array of WikiDB_Page's or a Page_iterator
108 * @param $direction boolean: true if incoming links
110 * @return Array of sorted links
112 function sortedLinks($pages, $direction=false, $limit=5) {
114 if (is_array($pages)) {
115 foreach ($pages as $page) {
116 // just the number of hits
117 $hits = $page->get('hits');
118 if (!$hits) continue;
119 $links[] = array('hits' => $hits,
120 'pagename' => $page->_pagename,
121 'format' => HTML(WikiLink($page->_pagename),' (' . $hits . ')'));
124 while ($page = $pages->next()) {
125 // different score algorithm:
126 // the number of links to/from the page
127 $l = $page->getLinks(!$direction);
128 $score = $l->count();
129 if (!$score) continue;
130 $name = $page->_pagename;
131 $links[] = array('hits' => $score,
133 'format' => HTML(WikiLink($name),' (' . $score . ')'));
137 if (count($links) > $limit)
138 array_splice($links, $limit);
139 return $this->sortByHits($links);
142 function sortByHits($links) {
143 if (!$links) return array();
144 usort($links,array('WikiPlugin_PopularNearby','cmp_hits'));
149 function cmp_hits($a, $b) {
150 return ($a['hits'] < $b['hits']) ? 1 : ($a['hits'] == $b['hits']) ? 0 : -1;
156 // $Log: not supported by cvs2svn $
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil