5 /* private */ var $mUsertext, $mSearchterms;
6 /* private */ var $mTitlecond, $mTextcond;
8 var $doSearchRedirects = true;
9 var $addtoquery = array();
10 var $namespacesToSearch = array();
12 var $all_titles = false;
14 function SearchEngine( $text )
16 # We display the query, so let's strip it for safety
19 $lc = SearchEngine
::legalSearchChars() . "()";
20 if( $wgDBmysql4 ) $lc .= "\"~<>*+-";
21 $this->mUsertext
= trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
22 $this->mSearchterms
= array();
23 $this->mStrictMatching
= true; # Google-style, add '+' on all terms
26 function queryNamespaces()
28 $namespaces = implode( ",", $this->namespacesToSearch
);
29 if ($namespaces == "") {
32 return "AND cur_namespace IN (" . $namespaces . ")";
35 function searchRedirects()
37 if ( $this->doSearchRedirects
) {
40 return "AND cur_is_redirect=0 ";
44 /* private */ function initNamespaceCheckbox( $i )
46 global $wgUser, $wgNamespacesToBeSearchedDefault;
48 if ($wgUser->getID()) {
49 // User is logged in so we retrieve his default namespaces
50 return $wgUser->getOption( "searchNs".$i );
52 // User is not logged in so we give him the global default namespaces
53 return $wgNamespacesToBeSearchedDefault[ $i ];
57 # Display the "power search" footer. Does not actually perform the search,
58 # that is done by showResults()
59 function powersearch()
61 global $wgUser, $wgOut, $wgLang, $wgTitle;
63 $search = $_REQUEST['search'];
64 $searchx = $_REQUEST['searchx'];
65 $listredirs = $_REQUEST['redirs'];
67 $ret = wfMsg("powersearchtext"); # Text to be returned
68 $tempText = ""; # Temporary text, for substitution into $ret
70 if( isset( $_REQUEST["searchx"] ) ) {
71 $this->addtoquery
["searchx"] = "1";
74 # Do namespace checkboxes
75 $namespaces = $wgLang->getNamespaces();
76 foreach ( $namespaces as $i => $namespace ) {
77 # Skip virtual namespaces
84 # Initialise checkboxValues, either from defaults or from
85 # a previous invocation
86 if ( !isset( $searchx ) ) {
87 $checkboxValue = $this->initNamespaceCheckbox( $i );
89 $checkboxValue = $_REQUEST[$formVar];
93 if ( $checkboxValue == 1 ) {
94 $checked = " checked";
95 $this->addtoquery
["ns{$i}"] = 1;
96 array_push( $this->namespacesToSearch
, $i );
98 $name = str_replace( "_", " ", $namespaces[$i] );
100 $name = wfMsg( "blanknamespace" );
103 if ( $tempText !== "" ) {
106 $tempText .= "<input type=checkbox value=\"1\" name=\"" .
107 "ns{$i}\"{$checked}>{$name}\n";
109 $ret = str_replace ( "$1", $tempText, $ret );
111 # List redirects checkbox
114 if ( $listredirs == 1 ) {
115 $this->addtoquery
["redirs"] = 1;
116 $checked = " checked";
118 $tempText = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
119 $ret = str_replace( "$2", $tempText, $ret );
123 $tempText = "<input type=text name=\"search\" value=\"" .
124 htmlspecialchars( $search ) ."\" width=80>\n";
125 $ret = str_replace( "$3", $tempText, $ret );
129 $tempText = "<input type=submit name=\"searchx\" value=\"" .
130 wfMsg("powersearch") . "\">\n";
131 $ret = str_replace( "$9", $tempText, $ret );
133 $titleObj = NULL; # this does tricky stuff
135 $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
136 "action=\"" . $titleObj->getUrl() . "\">\n{$ret}\n</form>\n";
138 if ( isset ( $searchx ) ) {
139 if ( ! $listredirs ) {
140 $this->doSearchRedirects
= false;
146 # Perform the search and construct the results page
147 function showResults()
149 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
150 global $wgInputEncoding;
151 $fname = "SearchEngine::showResults";
153 $search = $_REQUEST['search'];
155 $powersearch = $this->powersearch(); /* Need side-effects here? */
157 $wgOut->setPageTitle( wfMsg( "searchresults" ) );
158 $q = wfMsg( "searchquery", htmlspecialchars( $this->mUsertext
) );
159 $wgOut->setSubtitle( $q );
160 $wgOut->setArticleRelated( false );
161 $wgOut->setRobotpolicy( "noindex,nofollow" );
163 $sk = $wgUser->getSkin();
164 $header = wfMsg( "searchresulttext", $sk->makeKnownLink(
165 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
166 $wgOut->addHTML( $header );
169 if ( "" == $this->mTitlecond ||
"" == $this->mTextcond
) {
170 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
171 "<p>" . wfMsg( "badquerytext" ) );
174 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
176 $searchnamespaces = $this->queryNamespaces();
177 $redircond = $this->searchRedirects();
179 if ( $wgDisableTextSearch ) {
180 $wgOut->addHTML( wfMsg( "searchdisabled" ) );
181 $wgOut->addHTML( wfMsg( "googlesearch", htmlspecialchars( $search ), $GLOBALS['wgInputEncoding'] ) );
183 $sql = "SELECT cur_id,cur_namespace,cur_title," .
184 "cur_text FROM cur,searchindex " .
185 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
186 "{$searchnamespaces} {$redircond}" .
187 "LIMIT {$offset}, {$limit}";
188 $res1 = wfQuery( $sql, DB_READ
, $fname );
189 $num = wfNumRows($res1);
191 $sk = $wgUser->getSkin();
195 if ( "" == $this->mTitlecond ||
"" == $this->mTextcond
) {
196 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
197 "<p>" . wfMsg( "badquerytext" ) );
200 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
202 $searchnamespaces = $this->queryNamespaces();
203 $redircond = $this->searchRedirects();
205 $sql = "SELECT cur_id,cur_namespace,cur_title," .
206 "cur_text FROM cur,searchindex " .
207 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
208 "{$searchnamespaces} {$redircond}" .
209 "LIMIT {$offset}, {$limit}";
210 $res1 = wfQuery( $sql, DB_READ
, $fname );
211 $num = wfNumRows($res1);
213 $sql = "SELECT cur_id,cur_namespace,cur_title," .
214 "cur_text FROM cur,searchindex " .
215 "WHERE cur_id=si_page AND {$this->mTextcond} " .
216 "{$searchnamespaces} {$redircond} " .
217 "LIMIT {$offset}, {$limit}";
218 $res2 = wfQuery( $sql, DB_READ
, $fname );
219 $num = $num +
wfNumRows($res2);
221 if ( $num == $limit ) {
222 $top = wfShowingResults( $offset, $limit);
224 $top = wfShowingResultsNum( $offset, $limit, $num );
226 $wgOut->addHTML( "<p>{$top}\n" );
231 $akk = array_keys( $this->addtoquery
) ;
232 foreach ( $akk AS $ak ) {
233 $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
236 $sl = wfViewPrevNext( $offset, $limit, "",
237 "search=" . wfUrlencode( $this->mUsertext
) . $a2l );
238 $wgOut->addHTML( "<br>{$sl}\n" );
242 if ( 0 == wfNumRows( $res1 ) ) {
243 $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
248 $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
249 "</h2>\n<ol start='{$off}'>" );
251 while ( $row = wfFetchObject( $res1 ) ) {
252 $this->showHit( $row );
254 wfFreeResult( $res1 );
255 $wgOut->addHTML( "</ol>\n" );
258 if ( 0 == wfNumRows( $res2 ) ) {
259 $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
264 $wgOut->addHTML( "<h2>" . wfMsg( "textmatches" ) . "</h2>\n" .
265 "<ol start='{$off}'>" );
266 while ( $row = wfFetchObject( $res2 ) ) {
267 $this->showHit( $row );
269 wfFreeResult( $res2 );
270 $wgOut->addHTML( "</ol>\n" );
272 if ( ! $foundsome ) {
273 $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
275 $wgOut->addHTML( "<p>{$sl}\n" );
276 $wgOut->addHTML( $powersearch );
280 function legalSearchChars()
282 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
286 function parseQuery()
288 global $wgDBminWordLen, $wgLang, $wgDBmysql4;
291 # Use cleaner boolean search if available
292 return $this->parseQuery4();
295 $lc = SearchEngine
::legalSearchChars() . "()";
296 $q = preg_replace( "/([()])/", " \\1 ", $this->mUsertext
);
297 $q = preg_replace( "/\\s+/", " ", $q );
298 $w = explode( " ", strtolower( trim( $q ) ) );
301 foreach ( $w as $word ) {
302 $word = $wgLang->stripForSearch( $word );
303 if ( "and" == $word ||
"or" == $word ||
"not" == $word
304 ||
"(" == $word ||
")" == $word ) {
305 $cond .= " " . strtoupper( $word );
307 } else if ( strlen( $word ) < $wgDBminWordLen ) {
309 } else if ( FulltextStoplist
::inList( $word ) ) {
312 if ( "" != $last ) { $cond .= " AND"; }
313 $cond .= " (MATCH (##field##) AGAINST ('" .
314 wfStrencode( $word ). "'))";
316 array_push( $this->mSearchterms
, "\\b" . $word . "\\b" );
319 if ( 0 == count( $this->mSearchterms
) ) { return; }
321 $this->mTitlecond
= "(" . str_replace( "##field##",
322 "si_title", $cond ) . " )";
324 $this->mTextcond
= "(" . str_replace( "##field##",
325 "si_text", $cond ) . " AND (cur_is_redirect=0) )";
328 function parseQuery4()
331 $lc = SearchEngine
::legalSearchChars();
333 $this->mSearchterms
= array();
335 # FIXME: This doesn't handle parenthetical expressions.
336 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
337 $this->mUsertext
, $m, PREG_SET_ORDER
) ) {
338 foreach( $m as $terms ) {
339 if( $searchon !== "" ) $searchon .= " ";
340 if( $this->mStrictMatching
&& ($terms[1] == "") ) {
343 $searchon .= $terms[1] . $wgLang->stripForSearch( $terms[2] );
345 $regexp = preg_quote( $terms[3] );
346 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
348 $regexp = preg_quote( str_replace( '"', '', $terms[2] ) );
350 $this->mSearchterms
[] = $regexp;
352 wfDebug( "Would search with '$searchon'\n" );
353 wfDebug( "Match with /\b" . implode( '\b|\b', $this->mSearchterms
) . "\b/\n" );
355 wfDebug( "Can't understand search query '$this->mUsertext'\n" );
358 $searchon = wfStrencode( $searchon );
359 $this->mTitlecond
= " MATCH(si_title) AGAINST('$searchon' IN BOOLEAN MODE)";
360 $this->mTextcond
= " (MATCH(si_text) AGAINST('$searchon' IN BOOLEAN MODE) AND cur_is_redirect=0)";
363 function showHit( $row )
365 global $wgUser, $wgOut;
367 $t = Title
::makeName( $row->cur_namespace
, $row->cur_title
);
368 $sk = $wgUser->getSkin();
370 $contextlines = $wgUser->getOption( "contextlines" );
371 if ( "" == $contextlines ) { $contextlines = 5; }
372 $contextchars = $wgUser->getOption( "contextchars" );
373 if ( "" == $contextchars ) { $contextchars = 50; }
375 $link = $sk->makeKnownLink( $t, "" );
376 $size = wfMsg( "nbytes", strlen( $row->cur_text
) );
377 $wgOut->addHTML( "<li>{$link} ({$size})" );
379 $lines = explode( "\n", $row->cur_text
);
380 $pat1 = "/(.*)(" . implode( "|", $this->mSearchterms
) . ")(.*)/i";
383 foreach ( $lines as $line ) {
384 if ( 0 == $contextlines ) { break; }
387 if ( ! preg_match( $pat1, $line, $m ) ) { continue; }
390 if ( 0 == $contextchars ) { $pre = "..."; }
392 if ( strlen( $pre ) > $contextchars ) {
393 $pre = "..." . substr( $pre, -$contextchars );
396 $pre = wfEscapeHTML( $pre );
398 if ( count( $m ) < 3 ) { $post = ""; }
399 else { $post = $m[3]; }
401 if ( 0 == $contextchars ) { $post = "..."; }
403 if ( strlen( $post ) > $contextchars ) {
404 $post = substr( $post, 0, $contextchars ) . "...";
407 $post = wfEscapeHTML( $post );
408 $found = wfEscapeHTML( $m[2] );
410 $line = "{$pre}{$found}{$post}";
411 $pat2 = "/(" . implode( "|", $this->mSearchterms
) . ")/i";
412 $line = preg_replace( $pat2,
413 "<font color='red'>\\1</font>", $line );
415 $wgOut->addHTML( "<br><small>{$lineno}: {$line}</small>\n" );
417 $wgOut->addHTML( "</li>\n" );
422 global $wgOut, $wgDisableTextSearch;
423 $fname = "SearchEngine::goResult";
425 $search = $_REQUEST['search'];
427 # First try to go to page as entered.
429 $t = Title
::newFromText( $search );
431 # If the string cannot be used to create a title
433 $this->showResults();
437 if ( 0 != $t->getArticleID() ) {
438 $wgOut->redirect( $t->getURL() );
442 # Now try all lower case (i.e. first letter capitalized)
444 $t = Title
::newFromText( strtolower( $search ) );
445 if ( 0 != $t->getArticleID() ) {
446 $wgOut->redirect( $t->getURL() );
450 # Now try capitalized string
452 $t = Title
::newFromText( ucwords( strtolower( $search ) ) );
453 if ( 0 != $t->getArticleID() ) {
454 $wgOut->redirect( $t->getURL() );
458 # Now try all upper case
460 $t = Title
::newFromText( strtoupper( $search ) );
461 if ( 0 != $t->getArticleID() ) {
462 $wgOut->redirect( $t->getURL() );
466 # No match, generate an edit URL
467 $t = Title
::newFromText( $this->mUsertext
);
468 $wgOut->addHTML( wfMsg("nogomatch", $t->getURL( "action=edit", true ) ) . "\n<p>" );
470 # Try a fuzzy title search
472 global $wgDisableFuzzySearch;
473 if(! $wgDisableFuzzySearch ){
474 foreach( array(NS_MAIN
, NS_WP
, NS_USER
, NS_IMAGE
, NS_MEDIAWIKI
) as $namespace){
475 $anyhit |
= SearchEngine
::doFuzzyTitleSearch( $search, $namespace );
480 return $this->showResults();
484 /* static */ function doFuzzyTitleSearch( $search, $namespace ){
485 global $wgLang, $wgOut;
486 $sstr = ucfirst($search);
487 $sstr = str_replace(" ", "_", $sstr);
488 $fuzzymatches = SearchEngine
::fuzzyTitles( $sstr, $namespace );
489 $fuzzymatches = array_slice($fuzzymatches, 0, 10);
490 $slen = strlen( $search );
492 foreach($fuzzymatches as $res){
493 $t = str_replace("_", " ", $res[1]);
494 $tfull = $wgLang->getNsText( $namespace ) . ":$t|$t";
495 if( $namespace == NS_MAIN
)
498 $closeness = (strlen( $search ) - $distance) / strlen( $search );
499 $percent = intval( $closeness * 100 ) . "%";
500 $stars = str_repeat("*", ceil(5 * $closeness) );
501 $wikitext .= "* [[$tfull]] $percent ($stars)\n";
504 if( $namespace != NS_MAIN
)
505 $wikitext = "=== " . $wgLang->getNsText( $namespace ) . " ===\n" . $wikitext;
506 $wgOut->addWikiText( $wikitext );
512 /* static */ function fuzzyTitles( $sstr, $namespace = NS_MAIN
){
513 $span = 0.10; // weed on title length before doing levenshtein.
514 $tolerance = 0.35; // allowed percentage of erronous characters
515 $slen = strlen($sstr);
516 $tolerance_count = ceil($tolerance * $slen);
517 $spanabs = ceil($slen * (1 +
$span)) - $slen;
518 # print "Word: $sstr, len = $slen, range = [$min, $max], tolerance_count = $tolerance_count<BR>\n";
520 for( $i=0; $i <= $spanabs; $i++
){
521 $titles = SearchEngine
::getTitlesByLength( $slen +
$i, $namespace );
523 $titles = array_merge($titles, SearchEngine
::getTitlesByLength( $slen - $i, $namespace ) );
524 foreach($titles as $t){
525 $d = levenshtein($sstr, $t);
526 if($d < $tolerance_count)
527 $result[] = array($d, $t);
531 usort($result, "SearchEngine_pcmp");
535 /* static */ function getTitlesByLength($aLength, $aNamespace = 0){
536 global $wgMemc, $wgDBname;
538 // to avoid multiple costly SELECTs in case of no memcached
539 if( $this->all_titles
){
540 if( isset( $this->all_titles
[$aLength][$aNamespace] ) ){
541 return $this->all_titles
[$aLength][$aNamespace];
547 $mkey = "$wgDBname:titlesbylength:$aLength:$aNamespace";
548 $mkeyts = "$wgDBname:titlesbylength:createtime";
549 $ts = $wgMemc->get( $mkeyts );
550 $result = $wgMemc->get( $mkey );
552 if( time() - $ts < 3600 ){
553 // note: in case of insufficient memcached space, we return
554 // an empty list instead of starting to hit the DB.
555 return is_array( $result ) ?
$result : array();
558 $wgMemc->set( $mkeyts, time() );
560 $res = wfQuery("SELECT cur_title, cur_namespace FROM cur", DB_READ
);
561 $titles = array(); // length, ns, [titles]
562 while( $obj = wfFetchObject( $res ) ){
563 $title = $obj->cur_title
;
564 $ns = $obj->cur_namespace
;
565 $len = strlen( $title );
566 $titles[$len][$ns][] = $title;
568 foreach($titles as $length => $length_arr){
569 foreach($length_arr as $ns => $title_arr){
570 $mkey = "$wgDBname:titlesbylength:$length:$ns";
571 $wgMemc->set( $mkey, $title_arr, 3600 * 24 );
574 $this->all_titles
= $titles;
575 if( isset( $titles[$aLength][$aNamespace] ) )
576 return $titles[$aLength][$aNamespace];
582 /* private static */ function SearchEngine_pcmp($a, $b){ return $a[0] - $b[0]; }