Break up SearchEngine.php into a couple of other files
[lhc/web/wiklou.git] / includes / search / SearchResultSet.php
1 <?php
2 /**
3 * Search result sets
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Search
22 */
23
24 /**
25 * @ingroup Search
26 */
27 class SearchResultSet {
28 /**
29 * Fetch an array of regular expression fragments for matching
30 * the search terms as parsed by this engine in a text extract.
31 * STUB
32 *
33 * @return Array
34 */
35 function termMatches() {
36 return array();
37 }
38
39 function numRows() {
40 return 0;
41 }
42
43 /**
44 * Return true if results are included in this result set.
45 * STUB
46 *
47 * @return Boolean
48 */
49 function hasResults() {
50 return false;
51 }
52
53 /**
54 * Some search modes return a total hit count for the query
55 * in the entire article database. This may include pages
56 * in namespaces that would not be matched on the given
57 * settings.
58 *
59 * Return null if no total hits number is supported.
60 *
61 * @return Integer
62 */
63 function getTotalHits() {
64 return null;
65 }
66
67 /**
68 * Some search modes return a suggested alternate term if there are
69 * no exact hits. Returns true if there is one on this set.
70 *
71 * @return Boolean
72 */
73 function hasSuggestion() {
74 return false;
75 }
76
77 /**
78 * @return String: suggested query, null if none
79 */
80 function getSuggestionQuery() {
81 return null;
82 }
83
84 /**
85 * @return String: HTML highlighted suggested query, '' if none
86 */
87 function getSuggestionSnippet() {
88 return '';
89 }
90
91 /**
92 * Return information about how and from where the results were fetched,
93 * should be useful for diagnostics and debugging
94 *
95 * @return String
96 */
97 function getInfo() {
98 return null;
99 }
100
101 /**
102 * Return a result set of hits on other (multiple) wikis associated with this one
103 *
104 * @return SearchResultSet
105 */
106 function getInterwikiResults() {
107 return null;
108 }
109
110 /**
111 * Check if there are results on other wikis
112 *
113 * @return Boolean
114 */
115 function hasInterwikiResults() {
116 return $this->getInterwikiResults() != null;
117 }
118
119 /**
120 * Fetches next search result, or false.
121 * STUB
122 *
123 * @return SearchResult
124 */
125 function next() {
126 return false;
127 }
128
129 /**
130 * Frees the result set, if applicable.
131 */
132 function free() {
133 // ...
134 }
135
136 /**
137 * Did the search contain search syntax? If so, Special:Search won't offer
138 * the user a link to a create a page named by the search string because the
139 * name would contain the search syntax.
140 */
141 public function searchContainedSyntax() {
142 return false;
143 }
144 }
145
146 /**
147 * This class is used for different SQL-based search engines shipped with MediaWiki
148 * @ingroup Search
149 */
150 class SqlSearchResultSet extends SearchResultSet {
151
152 protected $mResultSet;
153
154 function __construct( $resultSet, $terms ) {
155 $this->mResultSet = $resultSet;
156 $this->mTerms = $terms;
157 }
158
159 function termMatches() {
160 return $this->mTerms;
161 }
162
163 function numRows() {
164 if ( $this->mResultSet === false ) {
165 return false;
166 }
167
168 return $this->mResultSet->numRows();
169 }
170
171 function next() {
172 if ( $this->mResultSet === false ) {
173 return false;
174 }
175
176 $row = $this->mResultSet->fetchObject();
177 if ( $row === false ) {
178 return false;
179 }
180
181 return SearchResult::newFromRow( $row );
182 }
183
184 function free() {
185 if ( $this->mResultSet === false ) {
186 return false;
187 }
188
189 $this->mResultSet->free();
190 }
191 }
192
193 /**
194 * A SearchResultSet wrapper for SearchEngine::getNearMatch
195 */
196 class SearchNearMatchResultSet extends SearchResultSet {
197 private $fetched = false;
198
199 /**
200 * @param $match mixed Title if matched, else null
201 */
202 public function __construct( $match ) {
203 $this->result = $match;
204 }
205
206 public function hasResult() {
207 return (bool)$this->result;
208 }
209
210 public function numRows() {
211 return $this->hasResults() ? 1 : 0;
212 }
213
214 public function next() {
215 if ( $this->fetched || !$this->result ) {
216 return false;
217 }
218 $this->fetched = true;
219 return SearchResult::newFromTitle( $this->result );
220 }
221 }