Escape input on a few error messages
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain site class
4 * See search.doc
5 * @package MediaWiki
6 */
7
8 /**
9 *
10 */
11 define( 'MW_SEARCH_OK', true );
12 define( 'MW_SEARCH_BAD_QUERY', false );
13
14 /**
15 * @todo document
16 * @package MediaWiki
17 */
18 class SearchEngine {
19 /* private */ var $rawText, $filteredText, $searchTerms;
20 /* private */ var $titleCond, $textCond;
21
22 var $doSearchRedirects = true;
23 var $addToQuery = array();
24 var $namespacesToSearch = array();
25 var $alternateTitle;
26 var $allTitles = false;
27
28 function SearchEngine( $text ) {
29 $this->rawText = trim( $text );
30
31 # We display the query, so let's strip it for safety
32 #
33 global $wgDBmysql4;
34 $lc = SearchEngine::legalSearchChars() . '()';
35 if( $wgDBmysql4 ) {
36 $lc .= "\"~<>*+-";
37 }
38 $this->filteredText = trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
39 $this->searchTerms = array();
40 $this->strictMatching = true; # Google-style, add '+' on all terms
41
42 $this->db =& wfGetDB( DB_SLAVE );
43 }
44
45 /**
46 * Return a partial WHERE clause to limit the search to the given namespaces
47 */
48 function queryNamespaces() {
49 $namespaces = implode( ',', $this->namespacesToSearch );
50 if ($namespaces == '') {
51 $namespaces = '0';
52 }
53 return "AND cur_namespace IN (" . $namespaces . ')';
54 }
55
56 /**
57 * Return a partial WHERE clause to include or exclude redirects from results
58 */
59 function searchRedirects() {
60 if ( $this->doSearchRedirects ) {
61 return '';
62 } else {
63 return 'AND cur_is_redirect=0 ';
64 }
65 }
66
67 /**
68 * @access private
69 */ function initNamespaceCheckbox( $i ) {
70 global $wgUser, $wgNamespacesToBeSearchedDefault;
71
72 if ($wgUser->getID()) {
73 // User is logged in so we retrieve his default namespaces
74 return $wgUser->getOption( 'searchNs'.$i );
75 } else {
76 // User is not logged in so we give him the global default namespaces
77 return !empty($wgNamespacesToBeSearchedDefault[ $i ]);
78 }
79 }
80
81 /**
82 * Display the "power search" footer. Does not actually perform the search,
83 * that is done by showResults()
84 */
85 function powersearch() {
86 global $wgUser, $wgOut, $wgContLang, $wgTitle, $wgRequest;
87 $sk =& $wgUser->getSkin();
88
89 $search = $this->rawText;
90 $searchx = $wgRequest->getVal( 'searchx' );
91 $listredirs = $wgRequest->getVal( 'redirs' );
92
93 $ret = wfMsg('powersearchtext'); # Text to be returned
94 $tempText = ''; # Temporary text, for substitution into $ret
95
96 if( isset( $_REQUEST['searchx'] ) ) {
97 $this->addToQuery['searchx'] = '1';
98 }
99
100 # Do namespace checkboxes
101 $namespaces = $wgContLang->getNamespaces();
102 foreach ( $namespaces as $i => $namespace ) {
103 # Skip virtual namespaces
104 if ( $i < 0 ) {
105 continue;
106 }
107
108 $formVar = 'ns'.$i;
109
110 # Initialise checkboxValues, either from defaults or from
111 # a previous invocation
112 if ( !isset( $searchx ) ) {
113 $checkboxValue = $this->initNamespaceCheckbox( $i );
114 } else {
115 $checkboxValue = $wgRequest->getVal( $formVar );
116 }
117
118 $checked = '';
119 if ( $checkboxValue == 1 ) {
120 $checked = ' checked="checked"';
121 $this->addToQuery['ns'.$i] = 1;
122 array_push( $this->namespacesToSearch, $i );
123 }
124 $name = str_replace( '_', ' ', $namespaces[$i] );
125 if ( '' == $name ) {
126 $name = wfMsg( 'blanknamespace' );
127 }
128
129 if ( $tempText !== '' ) {
130 $tempText .= ' ';
131 }
132 $tempText .= "<input type='checkbox' value=\"1\" name=\"" .
133 "ns{$i}\"{$checked} />{$name}\n";
134 }
135 $ret = str_replace ( '$1', $tempText, $ret );
136
137 # List redirects checkbox
138
139 $checked = '';
140 if ( $listredirs == 1 ) {
141 $this->addToQuery['redirs'] = 1;
142 $checked = ' checked="checked"';
143 }
144 $tempText = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
145 $ret = str_replace( '$2', $tempText, $ret );
146
147 # Search field
148
149 $tempText = "<input type='text' name=\"search\" value=\"" .
150 htmlspecialchars( $search ) ."\" width=\"80\" />\n";
151 $ret = str_replace( "$3", $tempText, $ret );
152
153 # Searchx button
154
155 $tempText = '<input type="submit" name="searchx" value="' .
156 wfMsg('powersearch') . "\" />\n";
157 $ret = str_replace( '$9', $tempText, $ret );
158
159 $action = $sk->escapeSearchLink();
160 $ret = "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
161 "action=\"$action\">\n{$ret}\n</form>\n";
162
163 if ( isset ( $searchx ) ) {
164 if ( ! $listredirs ) {
165 $this->doSearchRedirects = false;
166 }
167 }
168 return $ret;
169 }
170
171 function setupPage() {
172 global $wgOut;
173 $wgOut->setPageTitle( wfMsg( 'searchresults' ) );
174 $wgOut->setSubtitle( wfMsg( 'searchquery', htmlspecialchars( $this->rawText ) ) );
175 $wgOut->setArticleRelated( false );
176 $wgOut->setRobotpolicy( 'noindex,nofollow' );
177 }
178
179 /**
180 * Perform the search and construct the results page
181 */
182 function showResults() {
183 global $wgUser, $wgTitle, $wgOut, $wgLang;
184 global $wgDisableTextSearch, $wgInputEncoding;
185 $fname = 'SearchEngine::showResults';
186
187 $search = $this->rawText;
188
189 $powersearch = $this->powersearch(); /* Need side-effects here? */
190
191 $this->setupPage();
192
193 $sk = $wgUser->getSkin();
194 $wgOut->addWikiText( wfMsg( 'searchresulttext' ) );
195
196 if ( !$this->parseQuery() ) {
197 $wgOut->addWikiText(
198 '==' . wfMsg( 'badquery' ) . "==\n" .
199 wfMsg( 'badquerytext' ) );
200 return;
201 }
202 list( $limit, $offset ) = wfCheckLimits( 20, 'searchlimit' );
203
204 if ( $wgDisableTextSearch ) {
205 $wgOut->addHTML( wfMsg( 'searchdisabled' ) );
206 $wgOut->addHTML( wfMsg( 'googlesearch',
207 htmlspecialchars( $this->rawText ),
208 htmlspecialchars( $wgInputEncoding ) ) );
209 return;
210 }
211
212 $titleMatches = $this->getMatches( $this->titleCond, $limit, $offset );
213 $textMatches = $this->getMatches( $this->textCond, $limit, $offset );
214
215 $sk = $wgUser->getSkin();
216
217 $num = count( $titleMatches ) + count( $textMatches );
218 if ( $num >= $limit ) {
219 $top = wfShowingResults( $offset, $limit );
220 } else {
221 $top = wfShowingResultsNum( $offset, $limit, $num );
222 }
223 $wgOut->addHTML( "<p>{$top}</p>\n" );
224
225 # For powersearch
226 $a2l = '';
227 $akk = array_keys( $this->addToQuery );
228 foreach ( $akk AS $ak ) {
229 $a2l .= "&{$ak}={$this->addToQuery[$ak]}" ;
230 }
231
232 $prevnext = wfViewPrevNext( $offset, $limit, 'Special:Search',
233 'search=' . wfUrlencode( $this->filteredText ) . $a2l );
234 $wgOut->addHTML( "<br />{$prevnext}\n" );
235
236 $foundsome = $this->showMatches( $titleMatches, $offset, 'notitlematches', 'titlematches' )
237 || $this->showMatches( $textMatches, $offset, 'notextmatches', 'textmatches' );
238
239 if ( !$foundsome ) {
240 $wgOut->addWikiText( wfMsg( 'nonefound' ) );
241 }
242 $wgOut->addHTML( "<p>{$prevnext}</p>\n" );
243 $wgOut->addHTML( $powersearch );
244 }
245
246 function legalSearchChars() {
247 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
248 return $lc;
249 }
250
251 function parseQuery() {
252 global $wgDBmysql4;
253 if (strlen($this->filteredText) < 1)
254 return MW_SEARCH_BAD_QUERY;
255
256 if( $wgDBmysql4 ) {
257 # Use cleaner boolean search if available
258 return $this->parseQuery4();
259 } else {
260 # Fall back to ugly hack with multiple search clauses
261 return $this->parseQuery3();
262 }
263 }
264
265 function parseQuery3() {
266 global $wgDBminWordLen, $wgContLang;
267
268 # on non mysql4 database: get list of words we don't want to search for
269 require_once( 'FulltextStoplist.php' );
270
271 $lc = SearchEngine::legalSearchChars() . '()';
272 $q = preg_replace( "/([()])/", " \\1 ", $this->filteredText );
273 $q = preg_replace( "/\\s+/", " ", $q );
274 $w = explode( ' ', trim( $q ) );
275
276 $last = $cond = '';
277 foreach ( $w as $word ) {
278 $word = $wgContLang->stripForSearch( $word );
279 if ( 'and' == $word || 'or' == $word || 'not' == $word
280 || '(' == $word || ')' == $word ) {
281 $cond .= ' ' . strtoupper( $word );
282 $last = '';
283 } else if ( strlen( $word ) < $wgDBminWordLen ) {
284 continue;
285 } else if ( FulltextStoplist::inList( $word ) ) {
286 continue;
287 } else {
288 if ( '' != $last ) { $cond .= ' AND'; }
289 $cond .= " (MATCH (##field##) AGAINST ('" .
290 $this->db->strencode( $word ). "'))";
291 $last = $word;
292 array_push( $this->searchTerms, "\\b" . $word . "\\b" );
293 }
294 }
295 if ( 0 == count( $this->searchTerms ) ) {
296 return MW_SEARCH_BAD_QUERY;
297 }
298
299 $this->titleCond = '(' . str_replace( '##field##',
300 'si_title', $cond ) . ' )';
301
302 $this->textCond = '(' . str_replace( '##field##',
303 'si_text', $cond ) . ' AND (cur_is_redirect=0) )';
304
305 return MW_SEARCH_OK;
306 }
307
308 function parseQuery4() {
309 global $wgContLang;
310 $lc = SearchEngine::legalSearchChars();
311 $searchon = '';
312 $this->searchTerms = array();
313
314 # FIXME: This doesn't handle parenthetical expressions.
315 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
316 $this->filteredText, $m, PREG_SET_ORDER ) ) {
317 foreach( $m as $terms ) {
318 if( $searchon !== '' ) $searchon .= ' ';
319 if( $this->strictMatching && ($terms[1] == '') ) {
320 $terms[1] = '+';
321 }
322 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
323 if( !empty( $terms[3] ) ) {
324 $regexp = preg_quote( $terms[3] );
325 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
326 } else {
327 $regexp = preg_quote( str_replace( '"', '', $terms[2] ) );
328 }
329 $this->searchTerms[] = $regexp;
330 }
331 wfDebug( "Would search with '$searchon'\n" );
332 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
333 } else {
334 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
335 }
336
337 $searchon = $this->db->strencode( $searchon );
338 $this->titleCond = " MATCH(si_title) AGAINST('$searchon' IN BOOLEAN MODE)";
339 $this->textCond = " (MATCH(si_text) AGAINST('$searchon' IN BOOLEAN MODE) AND cur_is_redirect=0)";
340 return MW_SEARCH_OK;
341 }
342
343 function &getMatches( $cond, $limit, $offset = 0 ) {
344 $searchindex = $this->db->tableName( 'searchindex' );
345 $cur = $this->db->tableName( 'cur' );
346 $searchnamespaces = $this->queryNamespaces();
347 $redircond = $this->searchRedirects();
348
349 $sql = "SELECT cur_id,cur_namespace,cur_title," .
350 "cur_text FROM $cur,$searchindex " .
351 "WHERE cur_id=si_page AND {$cond} " .
352 "{$searchnamespaces} {$redircond} " .
353 $this->db->limitResult( $limit, $offset );
354
355 $res = $this->db->query( $sql, 'SearchEngine::getMatches' );
356 $matches = array();
357 while ( $row = $this->db->fetchObject( $res ) ) {
358 $matches[] = $row;
359 }
360 $this->db->freeResult( $res );
361
362 return $matches;
363 }
364
365 function showMatches( &$matches, $offset, $msgEmpty, $msgFound ) {
366 global $wgOut;
367 if ( 0 == count( $matches ) ) {
368 $wgOut->addHTML( "<h2>" . wfMsg( $msgEmpty ) .
369 "</h2>\n" );
370 return false;
371 } else {
372 $off = $offset + 1;
373 $wgOut->addHTML( "<h2>" . wfMsg( $msgFound ) .
374 "</h2>\n<ol start='{$off}'>" );
375
376 foreach( $matches as $row ) {
377 $this->showHit( $row );
378 }
379 $wgOut->addHTML( "</ol>\n" );
380 return true;
381 }
382 }
383
384 function showHit( $row ) {
385 global $wgUser, $wgOut, $wgContLang;
386
387 $t = Title::makeName( $row->cur_namespace, $row->cur_title );
388 if( is_null( $t ) ) {
389 $wgOut->addHTML( "<!-- Broken link in search result -->\n" );
390 return;
391 }
392 $sk = $wgUser->getSkin();
393
394 $contextlines = $wgUser->getOption( 'contextlines' );
395 if ( '' == $contextlines ) { $contextlines = 5; }
396 $contextchars = $wgUser->getOption( 'contextchars' );
397 if ( '' == $contextchars ) { $contextchars = 50; }
398
399 $link = $sk->makeKnownLink( $t, '' );
400 $size = wfMsg( 'nbytes', strlen( $row->cur_text ) );
401 $wgOut->addHTML( "<li>{$link} ({$size})" );
402
403 $lines = explode( "\n", $row->cur_text );
404 $pat1 = "/(.*)(" . implode( "|", $this->searchTerms ) . ")(.*)/i";
405 $lineno = 0;
406
407 foreach ( $lines as $line ) {
408 if ( 0 == $contextlines ) {
409 break;
410 }
411 --$contextlines;
412 ++$lineno;
413 if ( ! preg_match( $pat1, $line, $m ) ) {
414 continue;
415 }
416
417 $pre = $wgContLang->truncate( $m[1], -$contextchars, '...' );
418
419 if ( count( $m ) < 3 ) {
420 $post = '';
421 } else {
422 $post = $wgContLang->truncate( $m[3], $contextchars, '...' );
423 }
424
425 $found = $m[2];
426
427 $line = htmlspecialchars( $pre . $found . $post );
428 $pat2 = '/(' . implode( '|', $this->searchTerms ) . ")/i";
429 $line = preg_replace( $pat2,
430 "<span class='searchmatch'>\\1</span>", $line );
431
432 $wgOut->addHTML( "<br /><small>{$lineno}: {$line}</small>\n" );
433 }
434 $wgOut->addHTML( "</li>\n" );
435 }
436
437 function getNearMatch() {
438 # Exact match? No need to look further.
439 $title = Title::newFromText( $this->rawText );
440 if ( $title->getNamespace() == NS_SPECIAL || 0 != $title->getArticleID() ) {
441 return $title;
442 }
443
444 # Now try all lower case (i.e. first letter capitalized)
445 #
446 $title = Title::newFromText( strtolower( $this->rawText ) );
447 if ( 0 != $title->getArticleID() ) {
448 return $title;
449 }
450
451 # Now try capitalized string
452 #
453 $title = Title::newFromText( ucwords( strtolower( $this->rawText ) ) );
454 if ( 0 != $title->getArticleID() ) {
455 return $title;
456 }
457
458 # Now try all upper case
459 #
460 $title = Title::newFromText( strtoupper( $this->rawText ) );
461 if ( 0 != $title->getArticleID() ) {
462 return $title;
463 }
464
465 # Entering an IP address goes to the contributions page
466 if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $this->rawText ) ) {
467 $title = Title::makeTitle( NS_SPECIAL, "Contributions/" . $this->rawText );
468 return $title;
469 }
470
471 return NULL;
472 }
473
474 function goResult() {
475 global $wgOut, $wgGoToEdit;
476 global $wgDisableTextSearch;
477 $fname = 'SearchEngine::goResult';
478
479 # Try to go to page as entered.
480 #
481 $t = Title::newFromText( $this->rawText );
482
483 # If the string cannot be used to create a title
484 if( is_null( $t ) ){
485 $this->showResults();
486 return;
487 }
488
489 # If there's an exact or very near match, jump right there.
490 $t = $this->getNearMatch();
491 if( !is_null( $t ) ) {
492 $wgOut->redirect( $t->getFullURL() );
493 return;
494 }
495
496 # No match, generate an edit URL
497 $t = Title::newFromText( $this->rawText );
498
499 # If the feature is enabled, go straight to the edit page
500 if ( $wgGoToEdit ) {
501 $wgOut->redirect( $t->getFullURL( 'action=edit' ) );
502 return;
503 }
504
505 if( $t ) {
506 $editurl = $t->escapeLocalURL( 'action=edit' );
507 } else {
508 $editurl = ''; # ??
509 }
510 $wgOut->addHTML( '<p>' . wfMsg('nogomatch', $editurl, htmlspecialchars( $this->rawText ) ) . "</p>\n" );
511
512 # Try a fuzzy title search
513 $anyhit = false;
514 global $wgDisableFuzzySearch;
515 if(! $wgDisableFuzzySearch ){
516 foreach( array(NS_MAIN, NS_PROJECT, NS_USER, NS_IMAGE, NS_MEDIAWIKI) as $namespace){
517 $anyhit |= SearchEngine::doFuzzyTitleSearch( $this->rawText, $namespace );
518 }
519 }
520
521 if( ! $anyhit ){
522 return $this->showResults();
523 }
524 }
525
526 /**
527 * @static
528 */
529 function doFuzzyTitleSearch( $search, $namespace ){
530 global $wgContLang, $wgOut;
531
532 $this->setupPage();
533
534 $sstr = ucfirst($search);
535 $sstr = str_replace(' ', '_', $sstr);
536 $fuzzymatches = SearchEngine::fuzzyTitles( $sstr, $namespace );
537 $fuzzymatches = array_slice($fuzzymatches, 0, 10);
538 $slen = strlen( $search );
539 $wikitext = '';
540 foreach($fuzzymatches as $res){
541 $t = str_replace('_', ' ', $res[1]);
542 $tfull = $wgContLang->getNsText( $namespace ) . ":$t|$t";
543 if( $namespace == NS_MAIN )
544 $tfull = $t;
545 $distance = $res[0];
546 $closeness = (strlen( $search ) - $distance) / strlen( $search );
547 $percent = intval( $closeness * 100 ) . '%';
548 $stars = str_repeat('*', ceil(5 * $closeness) );
549 $wikitext .= "* [[$tfull]] $percent ($stars)\n";
550 }
551 if( $wikitext ){
552 if( $namespace != NS_MAIN )
553 $wikitext = '=== ' . $wgContLang->getNsText( $namespace ) . " ===\n" . $wikitext;
554 $wgOut->addWikiText( $wikitext );
555 return true;
556 }
557 return false;
558 }
559
560 /**
561 * @static
562 */
563 function fuzzyTitles( $sstr, $namespace = NS_MAIN ){
564 $span = 0.10; // weed on title length before doing levenshtein.
565 $tolerance = 0.35; // allowed percentage of erronous characters
566 $slen = strlen($sstr);
567 $tolerance_count = ceil($tolerance * $slen);
568 $spanabs = ceil($slen * (1 + $span)) - $slen;
569 # print "Word: $sstr, len = $slen, range = [$min, $max], tolerance_count = $tolerance_count<BR>\n";
570 $result = array();
571 $cnt = 0;
572 for( $i=0; $i <= $spanabs; $i++ ){
573 $titles = SearchEngine::getTitlesByLength( $slen + $i, $namespace );
574 if( $i != 0) {
575 $titles = array_merge($titles, SearchEngine::getTitlesByLength( $slen - $i, $namespace ) );
576 }
577 foreach($titles as $t){
578 $d = levenshtein($sstr, $t);
579 if($d < $tolerance_count)
580 $result[] = array($d, $t);
581 $cnt++;
582 }
583 }
584 usort($result, 'SearchEngine_pcmp');
585 return $result;
586 }
587
588 /**
589 * static
590 */
591 function getTitlesByLength($aLength, $aNamespace = 0){
592 global $wgMemc, $wgDBname;
593 $fname = 'SearchEngin::getTitlesByLength';
594
595 // to avoid multiple costly SELECTs in case of no memcached
596 if( $this->allTitles ){
597 if( isset( $this->allTitles[$aLength][$aNamespace] ) ){
598 return $this->allTitles[$aLength][$aNamespace];
599 } else {
600 return array();
601 }
602 }
603
604 $mkey = "$wgDBname:titlesbylength:$aLength:$aNamespace";
605 $mkeyts = "$wgDBname:titlesbylength:createtime";
606 $ts = $wgMemc->get( $mkeyts );
607 $result = $wgMemc->get( $mkey );
608
609 if( time() - $ts < 3600 ){
610 // note: in case of insufficient memcached space, we return
611 // an empty list instead of starting to hit the DB.
612 return is_array( $result ) ? $result : array();
613 }
614
615 $wgMemc->set( $mkeyts, time() );
616
617 $res = $this->db->select( 'cur', array( 'cur_title', 'cur_namespace' ), false, $fname );
618 $titles = array(); // length, ns, [titles]
619 while( $obj = $this->db->fetchObject( $res ) ){
620 $title = $obj->cur_title;
621 $ns = $obj->cur_namespace;
622 $len = strlen( $title );
623 $titles[$len][$ns][] = $title;
624 }
625 foreach($titles as $length => $length_arr){
626 foreach($length_arr as $ns => $title_arr){
627 $mkey = "$wgDBname:titlesbylength:$length:$ns";
628 $wgMemc->set( $mkey, $title_arr, 3600 * 24 );
629 }
630 }
631 $this->allTitles = $titles;
632 if( isset( $titles[$aLength][$aNamespace] ) )
633 return $titles[$aLength][$aNamespace];
634 else
635 return array();
636 }
637 }
638
639 /**
640 * @access private
641 * @static
642 */
643 function SearchEngine_pcmp($a, $b){ return $a[0] - $b[0]; }
644
645 ?>