Quote search text for regexp to avoid some errors with +foo etc
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?
2 # See search.doc
3
4 class SearchEngine {
5 /* private */ var $mUsertext, $mSearchterms;
6 /* private */ var $mTitlecond, $mTextcond;
7
8 var $doSearchRedirects = true;
9 var $addtoquery = array();
10 var $namespacesToSearch = array();
11 var $alternateTitle;
12
13 function SearchEngine( $text )
14 {
15 # We display the query, so let's strip it for safety
16 #
17 global $wgDBmysql4;
18 $lc = SearchEngine::legalSearchChars() . "()";
19 if( $wgDBmysql4 ) $lc .= "\"~<>*+-";
20 $this->mUsertext = trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
21 $this->mSearchterms = array();
22 }
23
24 function queryNamespaces()
25 {
26 $namespaces = implode( ",", $this->namespacesToSearch );
27 if ($namespaces == "") {
28 $namespaces = "0";
29 }
30 return "AND cur_namespace IN (" . $namespaces . ")";
31 #return "1";
32 }
33
34 function searchRedirects()
35 {
36 if ( $this->doSearchRedirects ) return "";
37 return "AND cur_is_redirect=0 ";
38 }
39
40 /* private */ function initNamespaceCheckbox( $i )
41 {
42 global $wgUser, $wgNamespacesToBeSearchedDefault;
43
44
45 if ($wgUser->getID()) {
46 // User is logged in so we retrieve his default namespaces
47 return $wgUser->getOption( "searchNs".$i );
48 }
49 else {
50 // User is not logged in so we give him the global default namespaces
51 return $wgNamespacesToBeSearchedDefault[ $i ];
52 }
53 }
54
55 # Display the "power search" footer. Does not actually perform the search,
56 # that is done by showResults()
57 function powersearch()
58 {
59 global $wgUser, $wgOut, $wgLang, $wgTitle;
60
61 $search = $_REQUEST['search'];
62 $searchx = $_REQUEST['searchx'];
63 $listredirs = $_REQUEST['redirs'];
64
65 $ret = wfMsg("powersearchtext"); # Text to be returned
66 $tempText = ""; # Temporary text, for substitution into $ret
67
68 if( isset( $_REQUEST["searchx"] ) ) {
69 $this->addtoquery["searchx"] = "1";
70 }
71
72 # Do namespace checkboxes
73 $namespaces = $wgLang->getNamespaces();
74 foreach ( $namespaces as $i => $namespace ) {
75 # Skip virtual namespaces
76 if ( $i < 0 ) {
77 continue;
78 }
79
80 $formVar = "ns$i";
81
82 # Initialise checkboxValues, either from defaults or from
83 # a previous invocation
84 if ( !isset( $searchx ) ) {
85 $checkboxValue = $this->initNamespaceCheckbox( $i );
86 } else {
87 $checkboxValue = $_REQUEST[$formVar];
88 }
89
90 $checked = "";
91 if ( $checkboxValue == 1 ) {
92 $checked = " checked";
93 $this->addtoquery["ns{$i}"] = 1;
94 array_push( $this->namespacesToSearch, $i );
95 }
96 $name = str_replace( "_", " ", $namespaces[$i] );
97 if ( "" == $name ) {
98 $name = wfMsg( "blanknamespace" );
99 }
100
101 if ( $tempText !== "" ) {
102 $tempText .= " ";
103 }
104 $tempText .= "<input type=checkbox value=\"1\" name=\"" .
105 "ns{$i}\"{$checked}>{$name}\n";
106 }
107 $ret = str_replace ( "$1", $tempText, $ret );
108
109 # List redirects checkbox
110
111 $checked = "";
112 if ( $listredirs == 1 ) {
113 $this->addtoquery["redirs"] = 1;
114 $checked = " checked";
115 }
116 $tempText = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
117 $ret = str_replace( "$2", $tempText, $ret );
118
119 # Search field
120
121 $tempText = "<input type=text name=\"search\" value=\"" .
122 htmlspecialchars( $search ) ."\" width=80>\n";
123 $ret = str_replace( "$3", $tempText, $ret );
124
125 # Searchx button
126
127 $tempText = "<input type=submit name=\"searchx\" value=\"" .
128 wfMsg("powersearch") . "\">\n";
129 $ret = str_replace( "$9", $tempText, $ret );
130
131 $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
132 "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
133
134 if ( isset ( $searchx ) ) {
135 if ( ! $listredirs ) {
136 $this->doSearchRedirects = false;
137 }
138 }
139 return $ret;
140 }
141
142 # Perform the search and construct the results page
143 function showResults()
144 {
145 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
146 global $wgInputEncoding;
147 $fname = "SearchEngine::showResults";
148
149 $search = $_REQUEST['search'];
150
151 $powersearch = $this->powersearch(); /* Need side-effects here? */
152
153 $wgOut->setPageTitle( wfMsg( "searchresults" ) );
154 $q = wfMsg( "searchquery", htmlspecialchars( $this->mUsertext ) );
155 $wgOut->setSubtitle( $q );
156 $wgOut->setArticleFlag( false );
157 $wgOut->setRobotpolicy( "noindex,nofollow" );
158
159 $sk = $wgUser->getSkin();
160 $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
161 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
162 $wgOut->addHTML( $text );
163
164 $this->parseQuery();
165 if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
166 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
167 "<p>" . wfMsg( "badquerytext" ) );
168 return;
169 }
170 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
171
172 $searchnamespaces = $this->queryNamespaces();
173 $redircond = $this->searchRedirects();
174
175 if ( $wgDisableTextSearch ) {
176 $wgOut->addHTML( wfMsg( "searchdisabled", htmlspecialchars( $search ), $wgInputEncoding ) );
177 } else {
178 $sql = "SELECT cur_id,cur_namespace,cur_title," .
179 "cur_text FROM cur,searchindex " .
180 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
181 "{$searchnamespaces} {$redircond}" .
182 "LIMIT {$offset}, {$limit}";
183 $res1 = wfQuery( $sql, DB_READ, $fname );
184 $num = wfNumRows($res1);
185
186 $sk = $wgUser->getSkin();
187 $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
188 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
189 $wgOut->addHTML( $text );
190
191 $this->parseQuery();
192 if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
193 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
194 "<p>" . wfMsg( "badquerytext" ) );
195 return;
196 }
197 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
198
199 $searchnamespaces = $this->queryNamespaces();
200 $redircond = $this->searchRedirects();
201
202 $sql = "SELECT cur_id,cur_namespace,cur_title," .
203 "cur_text FROM cur,searchindex " .
204 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
205 "{$searchnamespaces} {$redircond}" .
206 "LIMIT {$offset}, {$limit}";
207 $res1 = wfQuery( $sql, DB_READ, $fname );
208 $num = wfNumRows($res1);
209
210 $sql = "SELECT cur_id,cur_namespace,cur_title," .
211 "cur_text FROM cur,searchindex " .
212 "WHERE cur_id=si_page AND {$this->mTextcond} " .
213 "{$searchnamespaces} {$redircond} " .
214 "LIMIT {$offset}, {$limit}";
215 $res2 = wfQuery( $sql, DB_READ, $fname );
216 $num = $num + wfNumRows($res2);
217
218 if ( $num == $limit ) {
219 $top = wfShowingResults( $offset, $limit);
220 } else {
221 $top = wfShowingResultsNum( $offset, $limit, $num );
222 }
223 $wgOut->addHTML( "<p>{$top}\n" );
224
225 # For powersearch
226
227 $a2l = "" ;
228 $akk = array_keys( $this->addtoquery ) ;
229 foreach ( $akk AS $ak ) {
230 $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
231 }
232
233 $sl = wfViewPrevNext( $offset, $limit, "",
234 "search=" . wfUrlencode( $this->mUsertext ) . $a2l );
235 $wgOut->addHTML( "<br>{$sl}\n" );
236
237 $foundsome = false;
238
239 if ( 0 == wfNumRows( $res1 ) ) {
240 $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
241 "</h2>\n" );
242 } else {
243 $foundsome = true;
244 $off = $offset + 1;
245 $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
246 "</h2>\n<ol start='{$off}'>" );
247
248 while ( $row = wfFetchObject( $res1 ) ) {
249 $this->showHit( $row );
250 }
251 wfFreeResult( $res1 );
252 $wgOut->addHTML( "</ol>\n" );
253 }
254
255 if ( 0 == wfNumRows( $res2 ) ) {
256 $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
257 "</h2>\n" );
258 } else {
259 $foundsome = true;
260 $off = $offset + 1;
261 $wgOut->addHTML( "<h2>" . wfMsg( "textmatches" ) . "</h2>\n" .
262 "<ol start='{$off}'>" );
263 while ( $row = wfFetchObject( $res2 ) ) {
264 $this->showHit( $row );
265 }
266 wfFreeResult( $res2 );
267 $wgOut->addHTML( "</ol>\n" );
268 }
269 if ( ! $foundsome ) {
270 $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
271 }
272 $wgOut->addHTML( "<p>{$sl}\n" );
273 $wgOut->addHTML( $powersearch );
274 }
275 }
276
277 function legalSearchChars()
278 {
279 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
280 return $lc;
281 }
282
283 function parseQuery()
284 {
285 global $wgDBminWordLen, $wgLang, $wgDBmysql4;
286
287 if( $wgDBmysql4 ) {
288 # Use cleaner boolean search if available
289 return $this->parseQuery4();
290 }
291
292 $lc = SearchEngine::legalSearchChars() . "()";
293 $q = preg_replace( "/([()])/", " \\1 ", $this->mUsertext );
294 $q = preg_replace( "/\\s+/", " ", $q );
295 $w = explode( " ", strtolower( trim( $q ) ) );
296
297 $last = $cond = "";
298 foreach ( $w as $word ) {
299 $word = $wgLang->stripForSearch( $word );
300 if ( "and" == $word || "or" == $word || "not" == $word
301 || "(" == $word || ")" == $word ) {
302 $cond .= " " . strtoupper( $word );
303 $last = "";
304 } else if ( strlen( $word ) < $wgDBminWordLen ) {
305 continue;
306 } else if ( FulltextStoplist::inList( $word ) ) {
307 continue;
308 } else {
309 if ( "" != $last ) { $cond .= " AND"; }
310 $cond .= " (MATCH (##field##) AGAINST ('" .
311 wfStrencode( $word ). "'))";
312 $last = $word;
313 $word = preg_quote( $word );
314 array_push( $this->mSearchterms, "\\b" . $word . "\\b" );
315 }
316 }
317 if ( 0 == count( $this->mSearchterms ) ) { return; }
318
319 $this->mTitlecond = "(" . str_replace( "##field##",
320 "si_title", $cond ) . " )";
321
322 $this->mTextcond = "(" . str_replace( "##field##",
323 "si_text", $cond ) . " AND (cur_is_redirect=0) )";
324 }
325
326 function parseQuery4()
327 {
328 # FIXME: not ready yet! Do not use.
329
330 global $wgLang;
331 $lc = SearchEngine::legalSearchChars();
332 #$q = preg_replace( "/([+-]?)([$lc]+)/e",
333 # "\"$1\" . \$wgLang->stripForSearch(\"$2\")",
334 # $this->mUsertext );
335
336 $q = $this->mUsertext;
337 $qq = wfStrencode( $wgLang->stripForSearch( $q ) );
338 $this->mSearchterms = preg_split( '/\s+/', $q );
339 $this->mSearchterms = array_map( "preg_quote", $this->mSearchterms );
340
341 $this->mTitlecond = " MATCH(si_title) AGAINST('$qq' IN BOOLEAN MODE)";
342 $this->mTextcond = " (MATCH(si_text) AGAINST('$qq' IN BOOLEAN MODE) AND cur_is_redirect=0)";
343 }
344
345 function showHit( $row )
346 {
347 global $wgUser, $wgOut;
348
349 $t = Title::makeName( $row->cur_namespace, $row->cur_title );
350 $sk = $wgUser->getSkin();
351
352 $contextlines = $wgUser->getOption( "contextlines" );
353 if ( "" == $contextlines ) { $contextlines = 5; }
354 $contextchars = $wgUser->getOption( "contextchars" );
355 if ( "" == $contextchars ) { $contextchars = 50; }
356
357 $link = $sk->makeKnownLink( $t, "" );
358 $size = wfMsg( "nbytes", strlen( $row->cur_text ) );
359 $wgOut->addHTML( "<li>{$link} ({$size})" );
360
361 $lines = explode( "\n", $row->cur_text );
362 $pat1 = "/(.*)(" . implode( "|", $this->mSearchterms ) . ")(.*)/i";
363 $lineno = 0;
364
365 foreach ( $lines as $line ) {
366 if ( 0 == $contextlines ) { break; }
367 --$contextlines;
368 ++$lineno;
369 wfDebug( "Search highlight pattern is '$pat1'\n" );
370 if ( ! preg_match( $pat1, $line, $m ) ) { continue; }
371
372 $pre = $m[1];
373 if ( 0 == $contextchars ) { $pre = "..."; }
374 else {
375 if ( strlen( $pre ) > $contextchars ) {
376 $pre = "..." . substr( $pre, -$contextchars );
377 }
378 }
379 $pre = wfEscapeHTML( $pre );
380
381 if ( count( $m ) < 3 ) { $post = ""; }
382 else { $post = $m[3]; }
383
384 if ( 0 == $contextchars ) { $post = "..."; }
385 else {
386 if ( strlen( $post ) > $contextchars ) {
387 $post = substr( $post, 0, $contextchars ) . "...";
388 }
389 }
390 $post = wfEscapeHTML( $post );
391 $found = wfEscapeHTML( $m[2] );
392
393 $line = "{$pre}{$found}{$post}";
394 $pat2 = "/(" . implode( "|", $this->mSearchterms ) . ")/i";
395 $line = preg_replace( $pat2,
396 "<font color='red'>\\1</font>", $line );
397
398 $wgOut->addHTML( "<br><small>{$lineno}: {$line}</small>\n" );
399 }
400 $wgOut->addHTML( "</li>\n" );
401 }
402
403 function goResult()
404 {
405 global $wgOut, $wgDisableTextSearch;
406 $fname = "SearchEngine::goResult";
407
408 $search = $_REQUEST['search'];
409
410 # First try to go to page as entered.
411 #
412 $t = Title::newFromText( $search );
413
414 # If the string cannot be used to create a title
415 if( false == $t ){
416 $this->showResults();
417 return;
418 }
419
420 if ( 0 != $t->getArticleID() ) {
421 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
422 return;
423 }
424
425 # Now try all lower case (i.e. first letter capitalized)
426 #
427 $t = Title::newFromText( strtolower( $search ) );
428 if ( 0 != $t->getArticleID() ) {
429 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
430 return;
431 }
432
433 # Now try capitalized string
434 #
435 $t = Title::newFromText( ucwords( strtolower( $search ) ) );
436 if ( 0 != $t->getArticleID() ) {
437 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
438 return;
439 }
440
441 # Now try all upper case
442 #
443 $t = Title::newFromText( strtoupper( $search ) );
444 if ( 0 != $t->getArticleID() ) {
445 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
446 return;
447 }
448
449 # Try a near match
450 #
451 if( !$wgDisableTextSearch ) {
452 $this->parseQuery();
453 $sql = "SELECT cur_id,cur_title,cur_namespace,si_page FROM cur,searchindex " .
454 "WHERE cur_id=si_page AND {$this->mTitlecond} ORDER BY cur_namespace LIMIT 1";
455
456 if ( "" != $this->mTitlecond ) {
457 $res = wfQuery( $sql, DB_READ, $fname );
458 }
459 if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
460 $s = wfFetchObject( $res );
461
462 $t = Title::makeTitle( $s->cur_namespace, $s->cur_title );
463 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
464 return;
465 }
466 }
467 $wgOut->addHTML( wfMsg("nogomatch",
468 htmlspecialchars( wfLocalUrl( ucfirst($this->mUsertext), "action=edit") ) )
469 . "\n<p>" );
470 $this->showResults();
471 }
472 }
473
474 ?>