]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dumb/MostPopularIter.php
extra_empty_lines
[SourceForge/phpwiki.git] / lib / WikiDB / backend / dumb / MostPopularIter.php
1 <?php // -*-php-*-
2 // $Id$
3
4 require_once('lib/WikiDB/backend.php');
5
6 /**
7  * An inefficient but general most_popular iterator.
8  *
9  * This iterator will work with any backend which implements the
10  * backend::get_all_pages() and backend::get_pagedata()
11  * methods.
12  */
13 class WikiDB_backend_dumb_MostPopularIter
14 extends WikiDB_backend_iterator
15 {
16     function WikiDB_backend_dumb_MostPopularIter($backend, &$all_pages, $limit) {
17         $this->_pages = array();
18         $pages = &$this->_pages;
19
20         while ($page = $all_pages->next()) {
21             if (!isset($page['pagedata']))
22                 $page['pagedata'] = $backend->get_pagedata($page['pagename']);
23             $pages[] = $page;
24         }
25
26         if($limit < 0){  //sort pages in reverse order - ie least popular first.
27             usort($pages, 'WikiDB_backend_dumb_MostPopularIter_sortf_rev');
28             $limit = -$limit;
29         }
30         else usort($pages, 'WikiDB_backend_dumb_MostPopularIter_sortf');
31
32         if ($limit < 0) {
33             $pages = array_reverse($pages);
34             $limit = -$limit;
35         }
36
37         if ($limit && $limit < count($pages))
38             array_splice($pages, $limit);
39     }
40
41     function next() {
42         return array_shift($this->_pages);
43     }
44
45     function free() {
46         unset($this->_pages);
47     }
48 }
49
50 function WikiDB_backend_dumb_MostPopularIter_sortf($a,$b) {
51     $ahits = $bhits = 0;
52     if (isset($a['pagedata']['hits']))
53         $ahits = (int)$a['pagedata']['hits'];
54     if (isset($b['pagedata']['hits']))
55         $bhits = (int)$b['pagedata']['hits'];
56     return $bhits - $ahits;
57 }
58
59 function WikiDB_backend_dumb_MostPopularIter_sortf_rev($a,$b) {
60     $ahits = $bhits = 0;
61     if (isset($a['pagedata']['hits']))
62         $ahits = (int)$a['pagedata']['hits'];
63     if (isset($b['pagedata']['hits']))
64         $bhits = (int)$b['pagedata']['hits'];
65     return $ahits - $bhits;
66 }
67
68 // Local Variables:
69 // mode: php
70 // tab-width: 8
71 // c-basic-offset: 4
72 // c-hanging-comment-ender-p: nil
73 // indent-tabs-mode: nil
74 // End:
75 ?>