Merge "tests: Remove unused TableCleanupTest class"
[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 /**
30 * Types of interwiki results
31 */
32 /**
33 * Results that are displayed only together with existing main wiki results
34 * @var int
35 */
36 const SECONDARY_RESULTS = 0;
37 /**
38 * Results that can displayed even if no existing main wiki results exist
39 * @var int
40 */
41 const INLINE_RESULTS = 1;
42
43 protected $containedSyntax = false;
44
45 public function __construct( $containedSyntax = false ) {
46 $this->containedSyntax = $containedSyntax;
47 }
48
49 /**
50 * Fetch an array of regular expression fragments for matching
51 * the search terms as parsed by this engine in a text extract.
52 * STUB
53 *
54 * @return array
55 */
56 function termMatches() {
57 return array();
58 }
59
60 function numRows() {
61 return 0;
62 }
63
64 /**
65 * Some search modes return a total hit count for the query
66 * in the entire article database. This may include pages
67 * in namespaces that would not be matched on the given
68 * settings.
69 *
70 * Return null if no total hits number is supported.
71 *
72 * @return int
73 */
74 function getTotalHits() {
75 return null;
76 }
77
78 /**
79 * Some search modes will run an alternative query that it thinks gives
80 * a better result than the provided search. Returns true if this has
81 * occured.
82 *
83 * @return bool
84 */
85 function hasRewrittenQuery() {
86 return false;
87 }
88
89 /**
90 * @return string|null The search the query was internally rewritten to,
91 * or null when the result of the original query was returned.
92 */
93 function getQueryAfterRewrite() {
94 return null;
95 }
96
97 /**
98 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
99 * and with changes highlighted. Null when the query was not rewritten.
100 */
101 function getQueryAfterRewriteSnippet() {
102 return null;
103 }
104
105 /**
106 * Some search modes return a suggested alternate term if there are
107 * no exact hits. Returns true if there is one on this set.
108 *
109 * @return bool
110 */
111 function hasSuggestion() {
112 return false;
113 }
114
115 /**
116 * @return string|null Suggested query, null if none
117 */
118 function getSuggestionQuery() {
119 return null;
120 }
121
122 /**
123 * @return string HTML highlighted suggested query, '' if none
124 */
125 function getSuggestionSnippet() {
126 return '';
127 }
128
129 /**
130 * Return a result set of hits on other (multiple) wikis associated with this one
131 *
132 * @return SearchResultSet
133 */
134 function getInterwikiResults( $type = self::SECONDARY_RESULTS ) {
135 return null;
136 }
137
138 /**
139 * Check if there are results on other wikis
140 *
141 * @return bool
142 */
143 function hasInterwikiResults( $type = self::SECONDARY_RESULTS ) {
144 return false;
145 }
146
147 /**
148 * Fetches next search result, or false.
149 * STUB
150 *
151 * @return SearchResult
152 */
153 function next() {
154 return false;
155 }
156
157 /**
158 * Frees the result set, if applicable.
159 */
160 function free() {
161 // ...
162 }
163
164 /**
165 * Did the search contain search syntax? If so, Special:Search won't offer
166 * the user a link to a create a page named by the search string because the
167 * name would contain the search syntax.
168 * @return bool
169 */
170 public function searchContainedSyntax() {
171 return $this->containedSyntax;
172 }
173 }
174
175 /**
176 * This class is used for different SQL-based search engines shipped with MediaWiki
177 * @ingroup Search
178 */
179 class SqlSearchResultSet extends SearchResultSet {
180 protected $resultSet;
181 protected $terms;
182 protected $totalHits;
183
184 function __construct( $resultSet, $terms, $total = null ) {
185 $this->resultSet = $resultSet;
186 $this->terms = $terms;
187 $this->totalHits = $total;
188 }
189
190 function termMatches() {
191 return $this->terms;
192 }
193
194 function numRows() {
195 if ( $this->resultSet === false ) {
196 return false;
197 }
198
199 return $this->resultSet->numRows();
200 }
201
202 function next() {
203 if ( $this->resultSet === false ) {
204 return false;
205 }
206
207 $row = $this->resultSet->fetchObject();
208 if ( $row === false ) {
209 return false;
210 }
211
212 return SearchResult::newFromTitle(
213 Title::makeTitle( $row->page_namespace, $row->page_title )
214 );
215 }
216
217 function free() {
218 if ( $this->resultSet === false ) {
219 return false;
220 }
221
222 $this->resultSet->free();
223 }
224
225 function getTotalHits() {
226 if ( !is_null( $this->totalHits ) ) {
227 return $this->totalHits;
228 } else {
229 // Special:Search expects a number here.
230 return $this->numRows();
231 }
232 }
233 }
234
235 /**
236 * A SearchResultSet wrapper for SearchEngine::getNearMatch
237 */
238 class SearchNearMatchResultSet extends SearchResultSet {
239 private $fetched = false;
240
241 /**
242 * @param Title|null $match Title if matched, else null
243 */
244 public function __construct( $match ) {
245 $this->result = $match;
246 }
247
248 public function numRows() {
249 return $this->result ? 1 : 0;
250 }
251
252 public function next() {
253 if ( $this->fetched || !$this->result ) {
254 return false;
255 }
256 $this->fetched = true;
257 return SearchResult::newFromTitle( $this->result );
258 }
259 }