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