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