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