]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dumb/WantedPagesIter.php
Harmonize file footer
[SourceForge/phpwiki.git] / lib / WikiDB / backend / dumb / WantedPagesIter.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
3
4 //require_once('lib/WikiDB/backend.php');
5
6 /**
7  * This iterator will work with any WikiDB_backend
8  * which has a working get_links(,'links_from') method.
9  *
10  * This is mostly here for testing, 'cause it's slow,slow,slow.
11  */
12 class WikiDB_backend_dumb_WantedPagesIter
13 extends WikiDB_backend_iterator
14 {
15     function WikiDB_backend_dumb_WantedPagesIter(&$backend, &$all_pages, $exclude='', $sortby='', $limit='') {
16         $this->_allpages   = $all_pages;
17         $this->_allpages_array   = $all_pages->asArray();
18         $this->_backend = &$backend;
19         if (!is_array($exclude))
20             $this->exclude = $exclude ? PageList::explodePageList($exclude) : array();
21         else
22             $this->exclude = $exclude;
23         $this->sortby = $sortby; // ignored
24         if ($limit) { // extract from,count from limit
25             list($this->from, $this->limit) = $backend->limit($limit);
26         } else {
27             $this->limit = 0;
28             $this->from = 0;
29         }
30         $this->pos = 0;
31         $this->pagelinks = array();
32     }
33
34     function next() {
35         while ($page = $this->_allpages->next()) {
36             while ($this->pagelinks) { // deferred return
37                 return array_pop($this->pagelinks);
38             }
39             $this->pagelinks = array();
40             if ($this->limit and $this->pos > $this->limit) break;
41             $pagename = $page['pagename'];
42             $links = $this->_backend->get_links($pagename, false);
43             while ($link = $links->next()) {
44                 if ($this->limit and $this->pos > $this->limit) break;
45                 if ($this->exclude and in_array($link['pagename'], $this->exclude)) continue;
46                 // better membership for a pageiterator?
47                 if (! in_array($link['pagename'], $this->_allpages_array)) {
48                     if ($this->from and $this->pos < $this->from) continue;
49                     // collect all links per page and return them deferred
50                     $link['wantedfrom'] = $pagename;
51                     $this->pagelinks[] = $link;
52                     $this->pos++;
53                 }
54             }
55             $links->free();
56             unset($links);
57             if ($this->pagelinks) return array_pop($this->pagelinks);
58         }
59         return false;
60     }
61
62     function free() {
63         unset($this->_allpages_array);
64         $this->_allpages->free();
65         unset($this->_allpages);
66         unset($this->_backend);
67     }
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:
77 ?>