]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/dumb/TextSearchIter.php
trailing_spaces
[SourceForge/phpwiki.git] / lib / WikiDB / backend / dumb / TextSearchIter.php
1 <?php // -*-php-*-
2 // $Id$
3
4 class WikiDB_backend_dumb_TextSearchIter
5 extends WikiDB_backend_iterator
6 {
7     function WikiDB_backend_dumb_TextSearchIter(&$backend, &$pages, $search, $fulltext=false,
8                                                 $options=array())
9     {
10         $this->_backend = &$backend;
11         $this->_pages = $pages;
12         $this->_fulltext = $fulltext;
13         $this->_search  =& $search;
14         $this->_index   = 0;
15         $this->_stoplist =& $search->_stoplist;
16         $this->stoplisted = array();
17
18         $this->_from = 0;
19         if (isset($options['limit']))  // extract from,count from limit
20             list($this->_from, $this->_count) = WikiDB_backend::limit($options['limit']);
21         else
22             $this->_count = 0;
23         if (isset($options['exclude'])) $this->_exclude = $options['exclude'];
24         else $this->_exclude = false;
25     }
26
27     function _get_content(&$page) {
28         $backend = &$this->_backend;
29         $pagename = $page['pagename'];
30
31         if (!isset($page['versiondata'])) {
32             $version = $backend->get_latest_version($pagename);
33             $page['versiondata'] = $backend->get_versiondata($pagename, $version, true);
34         }
35         return $page['versiondata']['%content'];
36     }
37
38     function _match(&$page) {
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         $pages = &$this->_pages;
61         while ($page = $pages->next()) {
62             if ($score = $this->_match($page)) {
63                 $this->_index++;
64                 if (($this->_from > 0) and ($this->_index <= $this->_from))
65                     // not yet reached the offset
66                     continue;
67                 /*if ($this->_count and ($this->_index > $this->_count)) {
68                     // reached the limit, but need getTotal
69                     $this->_count++;
70                     return false;
71                 }*/
72                 if (is_array($page))
73                     $page['score'] = $score;
74                 else
75                     $page->score = $score;
76                 return $page;
77             }
78         }
79         return false;
80     }
81
82     function free() {
83         $this->_pages->free();
84     }
85 };
86
87 // Local Variables:
88 // mode: php
89 // tab-width: 8
90 // c-basic-offset: 4
91 // c-hanging-comment-ender-p: nil
92 // indent-tabs-mode: nil
93 // End:
94 ?>