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