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