loadAllMessages to ensure that all localizations for group names defined by extension...
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
6 class SearchEngine {
7 var $limit = 10;
8 var $offset = 0;
9 var $searchTerms = array();
10 var $namespaces = array( NS_MAIN );
11 var $showRedirects = false;
12
13 /**
14 * Perform a full text search query and return a result set.
15 * If title searches are not supported or disabled, return null.
16 *
17 * @param string $term - Raw search term
18 * @return SearchResultSet
19 * @access public
20 * @abstract
21 */
22 function searchText( $term ) {
23 return null;
24 }
25
26 /**
27 * Perform a title-only search query and return a result set.
28 * If title searches are not supported or disabled, return null.
29 *
30 * @param string $term - Raw search term
31 * @return SearchResultSet
32 * @access public
33 * @abstract
34 */
35 function searchTitle( $term ) {
36 return null;
37 }
38
39 /**
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
42 *
43 * @param string $term
44 * @return Title
45 */
46 public static function getNearMatch( $searchterm ) {
47 global $wgContLang;
48
49 $allSearchTerms = array($searchterm);
50
51 if($wgContLang->hasVariants()){
52 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
53 }
54
55 foreach($allSearchTerms as $term){
56
57 # Exact match? No need to look further.
58 $title = Title::newFromText( $term );
59 if (is_null($title))
60 return NULL;
61
62 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
63 return $title;
64 }
65
66 # Now try all lower case (i.e. first letter capitalized)
67 #
68 $title = Title::newFromText( $wgContLang->lc( $term ) );
69 if ( $title->exists() ) {
70 return $title;
71 }
72
73 # Now try capitalized string
74 #
75 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
76 if ( $title->exists() ) {
77 return $title;
78 }
79
80 # Now try all upper case
81 #
82 $title = Title::newFromText( $wgContLang->uc( $term ) );
83 if ( $title->exists() ) {
84 return $title;
85 }
86
87 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
88 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
89 if ( $title->exists() ) {
90 return $title;
91 }
92
93 global $wgCapitalLinks, $wgContLang;
94 if( !$wgCapitalLinks ) {
95 // Catch differs-by-first-letter-case-only
96 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
97 if ( $title->exists() ) {
98 return $title;
99 }
100 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
101 if ( $title->exists() ) {
102 return $title;
103 }
104 }
105
106 // Give hooks a chance at better match variants
107 $title = null;
108 if( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
109 return $title;
110 }
111 }
112
113 $title = Title::newFromText( $searchterm );
114
115 # Entering an IP address goes to the contributions page
116 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
117 || User::isIP( trim( $searchterm ) ) ) {
118 return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
119 }
120
121
122 # Entering a user goes to the user page whether it's there or not
123 if ( $title->getNamespace() == NS_USER ) {
124 return $title;
125 }
126
127 # Go to images that exist even if there's no local page.
128 # There may have been a funny upload, or it may be on a shared
129 # file repository such as Wikimedia Commons.
130 if( $title->getNamespace() == NS_IMAGE ) {
131 $image = wfFindFile( $title );
132 if( $image ) {
133 return $title;
134 }
135 }
136
137 # MediaWiki namespace? Page may be "implied" if not customized.
138 # Just return it, with caps forced as the message system likes it.
139 if( $title->getNamespace() == NS_MEDIAWIKI ) {
140 return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
141 }
142
143 # Quoted term? Try without the quotes...
144 $matches = array();
145 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
146 return SearchEngine::getNearMatch( $matches[1] );
147 }
148
149 return NULL;
150 }
151
152 public static function legalSearchChars() {
153 return "A-Za-z_'0-9\\x80-\\xFF\\-";
154 }
155
156 /**
157 * Set the maximum number of results to return
158 * and how many to skip before returning the first.
159 *
160 * @param int $limit
161 * @param int $offset
162 * @access public
163 */
164 function setLimitOffset( $limit, $offset = 0 ) {
165 $this->limit = intval( $limit );
166 $this->offset = intval( $offset );
167 }
168
169 /**
170 * Set which namespaces the search should include.
171 * Give an array of namespace index numbers.
172 *
173 * @param array $namespaces
174 * @access public
175 */
176 function setNamespaces( $namespaces ) {
177 $this->namespaces = $namespaces;
178 }
179
180 /**
181 * Parse some common prefixes: all (search everything)
182 * or namespace names
183 *
184 * @param string $query
185 */
186 function replacePrefixes( $query ){
187 global $wgContLang;
188
189 if( strpos($query,':') === false )
190 return $query; // nothing to do
191
192 $parsed = $query;
193 $allkeyword = wfMsg('searchall').":";
194 if( strncmp($query, $allkeyword, strlen($allkeyword)) == 0 ){
195 $this->namespaces = null;
196 $parsed = substr($query,strlen($allkeyword));
197 } else if( strpos($query,':') !== false ) {
198 $prefix = substr($query,0,strpos($query,':'));
199 $index = $wgContLang->getNsIndex($prefix);
200 if($index !== false){
201 $this->namespaces = array($index);
202 $parsed = substr($query,strlen($prefix)+1);
203 }
204 }
205 if(trim($parsed) == '')
206 return $query; // prefix was the whole query
207
208 return $parsed;
209 }
210
211 /**
212 * Make a list of searchable namespaces and their canonical names.
213 * @return array
214 */
215 public static function searchableNamespaces() {
216 global $wgContLang;
217 $arr = array();
218 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
219 if( $ns >= NS_MAIN ) {
220 $arr[$ns] = $name;
221 }
222 }
223 return $arr;
224 }
225
226 /**
227 * Return a 'cleaned up' search string
228 *
229 * @return string
230 * @access public
231 */
232 function filter( $text ) {
233 $lc = $this->legalSearchChars();
234 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
235 }
236 /**
237 * Load up the appropriate search engine class for the currently
238 * active database backend, and return a configured instance.
239 *
240 * @return SearchEngine
241 */
242 public static function create() {
243 global $wgDBtype, $wgSearchType;
244 if( $wgSearchType ) {
245 $class = $wgSearchType;
246 } elseif( $wgDBtype == 'mysql' ) {
247 $class = 'SearchMySQL';
248 } else if ( $wgDBtype == 'postgres' ) {
249 $class = 'SearchPostgres';
250 } else if ( $wgDBtype == 'oracle' ) {
251 $class = 'SearchOracle';
252 } else {
253 $class = 'SearchEngineDummy';
254 }
255 $search = new $class( wfGetDB( DB_SLAVE ) );
256 $search->setLimitOffset(0,0);
257 return $search;
258 }
259
260 /**
261 * Create or update the search index record for the given page.
262 * Title and text should be pre-processed.
263 *
264 * @param int $id
265 * @param string $title
266 * @param string $text
267 * @abstract
268 */
269 function update( $id, $title, $text ) {
270 // no-op
271 }
272
273 /**
274 * Update a search index record's title only.
275 * Title should be pre-processed.
276 *
277 * @param int $id
278 * @param string $title
279 * @abstract
280 */
281 function updateTitle( $id, $title ) {
282 // no-op
283 }
284 }
285
286
287 /**
288 * @addtogroup Search
289 */
290 class SearchResultSet {
291 /**
292 * Fetch an array of regular expression fragments for matching
293 * the search terms as parsed by this engine in a text extract.
294 *
295 * @return array
296 * @access public
297 * @abstract
298 */
299 function termMatches() {
300 return array();
301 }
302
303 function numRows() {
304 return 0;
305 }
306
307 /**
308 * Return true if results are included in this result set.
309 * @return bool
310 * @abstract
311 */
312 function hasResults() {
313 return false;
314 }
315
316 /**
317 * Some search modes return a total hit count for the query
318 * in the entire article database. This may include pages
319 * in namespaces that would not be matched on the given
320 * settings.
321 *
322 * Return null if no total hits number is supported.
323 *
324 * @return int
325 * @access public
326 */
327 function getTotalHits() {
328 return null;
329 }
330
331 /**
332 * Some search modes return a suggested alternate term if there are
333 * no exact hits. Returns true if there is one on this set.
334 *
335 * @return bool
336 * @access public
337 */
338 function hasSuggestion() {
339 return false;
340 }
341
342 /**
343 * @return string suggested query, null if none
344 */
345 function getSuggestionQuery(){
346 return null;
347 }
348
349 /**
350 * @return string highlighted suggested query, '' if none
351 */
352 function getSuggestionSnippet(){
353 return '';
354 }
355
356 /**
357 * Fetches next search result, or false.
358 * @return SearchResult
359 * @access public
360 * @abstract
361 */
362 function next() {
363 return false;
364 }
365
366 /**
367 * Frees the result set, if applicable.
368 * @ access public
369 */
370 function free() {
371 // ...
372 }
373 }
374
375
376 /**
377 * @addtogroup Search
378 */
379 class SearchResultTooMany {
380 ## Some search engines may bail out if too many matches are found
381 }
382
383
384 /**
385 * @addtogroup Search
386 */
387 class SearchResult {
388
389 function SearchResult( $row ) {
390 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
391 }
392
393 /**
394 * @return Title
395 * @access public
396 */
397 function getTitle() {
398 return $this->mTitle;
399 }
400
401 /**
402 * @return double or null if not supported
403 */
404 function getScore() {
405 return null;
406 }
407
408 /**
409 * @return string highlighted text snippet, null if not supported
410 */
411 function getTextSnippet(){
412 return null;
413 }
414
415 /**
416 * @return string highlighted title, '' if not supported
417 */
418 function getTitleSnippet(){
419 return '';
420 }
421
422 /**
423 * @return string highlighted redirect name (redirect to this page), '' if none or not supported
424 */
425 function getRedirectSnippet(){
426 return '';
427 }
428
429 /**
430 * @return Title object for the redirect to this page, null if none or not supported
431 */
432 function getRedirectTitle(){
433 return null;
434 }
435
436 /**
437 * @return string highlighted relevant section name, null if none or not supported
438 */
439 function getSectionSnippet(){
440 return '';
441 }
442
443 /**
444 * @return Title object (pagename+fragment) for the section, null if none or not supported
445 */
446 function getSectionTitle(){
447 return null;
448 }
449
450 /**
451 * @return string timestamp, null if not supported
452 */
453 function getTimestamp(){
454 return null;
455 }
456
457 /**
458 * @return int number of words, null if not supported
459 */
460 function getWordCount(){
461 return null;
462 }
463
464 /**
465 * @return int size in bytes, null if not supported
466 */
467 function getByteSize(){
468 return null;
469 }
470 }
471
472 /**
473 * @addtogroup Search
474 */
475 class SearchEngineDummy {
476 function search( $term ) {
477 return null;
478 }
479 function setLimitOffset($l, $o) {}
480 function legalSearchChars() {}
481 function update() {}
482 function setnamespaces() {}
483 function searchtitle() {}
484 function searchtext() {}
485 }