]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dumb/TextSearchIter.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / backend / dumb / TextSearchIter.php
1 <?php
2 class WikiDB_backend_dumb_TextSearchIter
3     extends WikiDB_backend_iterator
4 {
5     function WikiDB_backend_dumb_TextSearchIter(&$backend, &$pages, $search, $fulltext = false,
6                                                 $options = array())
7     {
8         $this->_backend = &$backend;
9         $this->_pages = $pages;
10         $this->_fulltext = $fulltext;
11         $this->_search =& $search;
12         $this->_index = 0;
13         $this->_stoplist =& $search->_stoplist;
14         $this->stoplisted = array();
15
16         $this->_from = 0;
17         if (isset($options['limit'])) // extract from,count from limit
18             list($this->_from, $this->_count) = WikiDB_backend::limit($options['limit']);
19         else
20             $this->_count = 0;
21         if (isset($options['exclude'])) $this->_exclude = $options['exclude'];
22         else $this->_exclude = false;
23     }
24
25     function _get_content(&$page)
26     {
27         $backend = &$this->_backend;
28         $pagename = $page['pagename'];
29
30         if (!isset($page['versiondata'])) {
31             $version = $backend->get_latest_version($pagename);
32             $page['versiondata'] = $backend->get_versiondata($pagename, $version, true);
33         }
34         return $page['versiondata']['%content'];
35     }
36
37     function _match(&$page)
38     {
39         $text = $page['pagename'];
40         if ($result = $this->_search->match($text)) { // first match the pagename only
41             return $this->_search->score($text) * 2.0;
42         }
43
44         if ($this->_fulltext) {
45             // eliminate stoplist words from fulltext search
46             if (preg_match("/^" . $this->_stoplist . "$/i", $text)) {
47                 $this->stoplisted[] = $text;
48                 return $result;
49             }
50             $text .= "\n" . $this->_get_content($page);
51             // Todo: Bonus for meta keywords (* 1.5) and headers
52             if ($this->_search->match($text))
53                 return $this->_search->score($text);
54         } else {
55             return $result;
56         }
57     }
58
59     function next()
60     {
61         $pages = &$this->_pages;
62         while ($page = $pages->next()) {
63             if ($score = $this->_match($page)) {
64                 $this->_index++;
65                 if (($this->_from > 0) and ($this->_index <= $this->_from))
66                     // not yet reached the offset
67                     continue;
68                 /*if ($this->_count and ($this->_index > $this->_count)) {
69                     // reached the limit, but need getTotal
70                     $this->_count++;
71                     return false;
72                 }*/
73                 if (is_array($page))
74                     $page['score'] = $score;
75                 else
76                     $page->score = $score;
77                 return $page;
78             }
79         }
80         return false;
81     }
82
83     function free()
84     {
85         $this->_pages->free();
86     }
87 }
88
89 ;
90
91 // Local Variables:
92 // mode: php
93 // tab-width: 8
94 // c-basic-offset: 4
95 // c-hanging-comment-ender-p: nil
96 // indent-tabs-mode: nil
97 // End: