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