]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dumb/MostPopularIter.php
Reformat code
[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     {
16         $this->_pages = array();
17         $pages = &$this->_pages;
18
19         while ($page = $all_pages->next()) {
20             if (!isset($page['pagedata']))
21                 $page['pagedata'] = $backend->get_pagedata($page['pagename']);
22             $pages[] = $page;
23         }
24
25         if ($limit < 0) { //sort pages in reverse order - ie least popular first.
26             usort($pages, 'WikiDB_backend_dumb_MostPopularIter_sortf_rev');
27             $limit = -$limit;
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     {
41         return array_shift($this->_pages);
42     }
43
44     function free()
45     {
46         unset($this->_pages);
47     }
48 }
49
50 function WikiDB_backend_dumb_MostPopularIter_sortf($a, $b)
51 {
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 {
62     $ahits = $bhits = 0;
63     if (isset($a['pagedata']['hits']))
64         $ahits = (int)$a['pagedata']['hits'];
65     if (isset($b['pagedata']['hits']))
66         $bhits = (int)$b['pagedata']['hits'];
67     return $ahits - $bhits;
68 }
69
70 // Local Variables:
71 // mode: php
72 // tab-width: 8
73 // c-basic-offset: 4
74 // c-hanging-comment-ender-p: nil
75 // indent-tabs-mode: nil
76 // End: