move '(Main)' from hardcoded string to language file
[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
41
42 /* private */ function initNamespaceCheckbox( $i )
43 {
44 global $wgUser, $wgNamespacesToBeSearchedDefault;
45
46
47 if ($wgUser->getRights()) {
48 // User is logged in so we retrieve his default namespaces
49 return $wgUser->getOption( "searchNs".$i );
50 }
51 else {
52 // User is not logged in so we give him the global default namespaces
53 return $wgNamespacesToBeSearchedDefault[ $i ];
54 }
55 }
56
57
58
59 function powersearch()
60 {
61 global $wgUser, $wgOut, $wgLang, $wgTitle;
62 $nscb = array();
63
64 $search = $_REQUEST['search'];
65 $searchx = $_REQUEST['searchx'];
66 $listredirs = $_REQUEST['redirs'];
67
68
69 if ( ! isset ( $searchx ) ) { /* First time here */
70 $listredirs = 1;
71 for ($i = 0; ($i <= 7); $i++)
72 {
73 $nscb[$i] = $this->initNamespaceCheckbox($i);
74 }
75 } else {
76 $nscb[0] = $_REQUEST['ns0'];
77 $nscb[1] = $_REQUEST['ns1'];
78 $nscb[2] = $_REQUEST['ns2'];
79 $nscb[3] = $_REQUEST['ns3'];
80 $nscb[4] = $_REQUEST['ns4'];
81 $nscb[5] = $_REQUEST['ns5'];
82 $nscb[6] = $_REQUEST['ns6'];
83 $nscb[7] = $_REQUEST['ns7'];
84 }
85
86 $this->checkboxes["searchx"] = 1;
87 $ret = wfMsg("powersearchtext");
88
89 # Determine namespace checkboxes
90
91 $ns = $wgLang->getNamespaces();
92 array_shift( $ns ); /* Skip "Special" */
93
94 $r1 = "";
95 for ( $i = 0; $i < count( $ns ); ++$i ) {
96 $checked = "";
97 if ( $nscb[$i] == 1 ) {
98 $checked = " checked";
99 $this->addtoquery["ns{$i}"] = 1;
100 array_push( $this->namespacesToSearch, $i );
101 }
102 $name = str_replace( "_", " ", $ns[$i] );
103 if ( "" == $name ) { $name = wfMsg( "blanknamespace" ); }
104
105 if ( 0 != $i ) { $r1 .= " "; }
106 $r1 .= "<input type=checkbox value=\"1\" name=\"" .
107 "ns{$i}\"{$checked}>{$name}\n";
108 }
109 $ret = str_replace ( "$1", $r1, $ret );
110
111 # List redirects checkbox
112
113 $checked = "";
114 if ( $listredirs == 1 ) {
115 $this->addtoquery["redirs"] = 1;
116 $checked = " checked";
117 }
118 $r2 = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
119 $ret = str_replace( "$2", $r2, $ret );
120
121 # Search field
122
123 $r3 = "<input type=text name=\"search\" value=\"" .
124 htmlspecialchars( $search ) ."\" width=80>\n";
125 $ret = str_replace( "$3", $r3, $ret );
126
127 # Searchx button
128
129 $r9 = "<input type=submit name=\"searchx\" value=\"" .
130 wfMsg("powersearch") . "\">\n";
131 $ret = str_replace( "$9", $r9, $ret );
132
133 $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
134 "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
135
136 if ( isset ( $searchx ) ) {
137 if ( ! $listredirs ) { $this->doSearchRedirects = false; }
138 }
139 return $ret;
140 }
141
142 function showResults()
143 {
144 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
145 $fname = "SearchEngine::showResults";
146
147 $search = $_REQUEST['search'];
148
149 $powersearch = $this->powersearch(); /* Need side-effects here? */
150
151 $wgOut->setPageTitle( wfMsg( "searchresults" ) );
152 $q = wfMsg( "searchquery", htmlspecialchars( $this->mUsertext ) );
153 $wgOut->setSubtitle( $q );
154 $wgOut->setArticleFlag( false );
155 $wgOut->setRobotpolicy( "noindex,nofollow" );
156
157 $sk = $wgUser->getSkin();
158 $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
159 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
160 $wgOut->addHTML( $text );
161
162 $this->parseQuery();
163 if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
164 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
165 "<p>" . wfMsg( "badquerytext" ) );
166 return;
167 }
168 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
169
170 $searchnamespaces = $this->queryNamespaces();
171 $redircond = $this->searchRedirects();
172
173 $sql = "SELECT cur_id,cur_namespace,cur_title," .
174 "cur_text FROM cur,searchindex " .
175 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
176 "{$searchnamespaces} {$redircond}" .
177 "LIMIT {$offset}, {$limit}";
178 $res1 = wfQuery( $sql, $fname );
179 $num = wfNumRows($res1);
180
181 if ( $wgDisableTextSearch ) {
182 $res2 = 0;
183 } else {
184 $sql = "SELECT cur_id,cur_namespace,cur_title," .
185 "cur_text FROM cur,searchindex " .
186 "WHERE cur_id=si_page AND {$this->mTextcond} " .
187 "{$searchnamespaces} {$redircond} " .
188 "LIMIT {$offset}, {$limit}";
189 $res2 = wfQuery( $sql, $fname );
190 $num = $num + wfNumRows($res2);
191 }
192
193 if ( $num == $limit ) {
194 $top = wfShowingResults( $offset, $limit);
195 } else {
196 $top = wfShowingResultsNum( $offset, $limit, $num );
197 }
198 $wgOut->addHTML( "<p>{$top}\n" );
199
200 # For powersearch
201
202 $a2l = "" ;
203 $akk = array_keys( $this->addtoquery ) ;
204 foreach ( $akk AS $ak ) {
205 $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
206 }
207
208 $sl = wfViewPrevNext( $offset, $limit, "",
209 "search=" . wfUrlencode( $this->mUsertext ) . $a2l );
210 $wgOut->addHTML( "<br>{$sl}\n" );
211
212 $foundsome = false;
213
214 if ( 0 == wfNumRows( $res1 ) ) {
215 $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
216 "</h2>\n" );
217 } else {
218 $foundsome = true;
219 $off = $offset + 1;
220 $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
221 "</h2>\n<ol start='{$off}'>" );
222
223 while ( $row = wfFetchObject( $res1 ) ) {
224 $this->showHit( $row );
225 }
226 wfFreeResult( $res1 );
227 $wgOut->addHTML( "</ol>\n" );
228 }
229
230 if ( $wgDisableTextSearch ) {
231 $wgOut->addHTML( str_replace( "$1",
232 htmlspecialchars( $search ), wfMsg( "searchdisabled" ) ) );
233 } else {
234 if ( 0 == wfNumRows( $res2 ) ) {
235 $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
236 "</h2>\n" );
237 } else {
238 $foundsome = true;
239 $off = $offset + 1;
240 $wgOut->addHTML( "<h2>" . wfMsg( "textmatches" ) . "</h2>\n" .
241 "<ol start='{$off}'>" );
242 while ( $row = wfFetchObject( $res2 ) ) {
243 $this->showHit( $row );
244 }
245 wfFreeResult( $res2 );
246 $wgOut->addHTML( "</ol>\n" );
247 }
248 }
249 if ( ! $foundsome ) {
250 $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
251 }
252 $wgOut->addHTML( "<p>{$sl}\n" );
253 $wgOut->addHTML( $powersearch );
254 }
255
256 function legalSearchChars()
257 {
258 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
259 return $lc;
260 }
261
262 function parseQuery()
263 {
264 global $wgDBminWordLen, $wgLang, $wgDBmysql4;
265
266 if( $wgDBmysql4 ) {
267 # Use cleaner boolean search if available
268 return $this->parseQuery4();
269 }
270
271 $lc = SearchEngine::legalSearchChars() . "()";
272 $q = preg_replace( "/([()])/", " \\1 ", $this->mUsertext );
273 $q = preg_replace( "/\\s+/", " ", $q );
274 $w = explode( " ", strtolower( trim( $q ) ) );
275
276 $last = $cond = "";
277 foreach ( $w as $word ) {
278 $word = $wgLang->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 wfStrencode( $word ). "'))";
291 $last = $word;
292 array_push( $this->mSearchterms, "\\b" . $word . "\\b" );
293 }
294 }
295 if ( 0 == count( $this->mSearchterms ) ) { return; }
296
297 $this->mTitlecond = "(" . str_replace( "##field##",
298 "si_title", $cond ) . " )";
299
300 $this->mTextcond = "(" . str_replace( "##field##",
301 "si_text", $cond ) . " AND (cur_is_redirect=0) )";
302 }
303
304 function parseQuery4()
305 {
306 # FIXME: not ready yet! Do not use.
307
308 global $wgLang;
309 $lc = SearchEngine::legalSearchChars();
310 #$q = preg_replace( "/([+-]?)([$lc]+)/e",
311 # "\"$1\" . \$wgLang->stripForSearch(\"$2\")",
312 # $this->mUsertext );
313
314 $q = $this->mUsertext;
315 $qq = wfStrencode( $wgLang->stripForSearch( $q ) );
316 $this->mSearchterms = preg_split( '/\s+/', $q );
317 $this->mTitlecond = " MATCH(si_title) AGAINST('$qq' IN BOOLEAN MODE)";
318 $this->mTextcond = " (MATCH(si_text) AGAINST('$qq' IN BOOLEAN MODE) AND cur_is_redirect=0)";
319 }
320
321 function showHit( $row )
322 {
323 global $wgUser, $wgOut;
324
325 $t = Title::makeName( $row->cur_namespace, $row->cur_title );
326 $sk = $wgUser->getSkin();
327
328 $contextlines = $wgUser->getOption( "contextlines" );
329 if ( "" == $contextlines ) { $contextlines = 5; }
330 $contextchars = $wgUser->getOption( "contextchars" );
331 if ( "" == $contextchars ) { $contextchars = 50; }
332
333 $link = $sk->makeKnownLink( $t, "" );
334 $size = str_replace( "$1", strlen( $row->cur_text ), WfMsg( "nbytes" ) );
335 $wgOut->addHTML( "<li>{$link} ({$size})" );
336
337 $lines = explode( "\n", $row->cur_text );
338 $pat1 = "/(.*)(" . implode( "|", $this->mSearchterms ) . ")(.*)/i";
339 $lineno = 0;
340
341 foreach ( $lines as $line ) {
342 if ( 0 == $contextlines ) { break; }
343 --$contextlines;
344 ++$lineno;
345 if ( ! preg_match( $pat1, $line, $m ) ) { continue; }
346
347 $pre = $m[1];
348 if ( 0 == $contextchars ) { $pre = "..."; }
349 else {
350 if ( strlen( $pre ) > $contextchars ) {
351 $pre = "..." . substr( $pre, -$contextchars );
352 }
353 }
354 $pre = wfEscapeHTML( $pre );
355
356 if ( count( $m ) < 3 ) { $post = ""; }
357 else { $post = $m[3]; }
358
359 if ( 0 == $contextchars ) { $post = "..."; }
360 else {
361 if ( strlen( $post ) > $contextchars ) {
362 $post = substr( $post, 0, $contextchars ) . "...";
363 }
364 }
365 $post = wfEscapeHTML( $post );
366 $found = wfEscapeHTML( $m[2] );
367
368 $line = "{$pre}{$found}{$post}";
369 $pat2 = "/(" . implode( "|", $this->mSearchterms ) . ")/i";
370 $line = preg_replace( $pat2,
371 "<font color='red'>\\1</font>", $line );
372
373 $wgOut->addHTML( "<br><small>{$lineno}: {$line}</small>\n" );
374 }
375 $wgOut->addHTML( "</li>\n" );
376 }
377
378 function goResult()
379 {
380 global $wgOut, $wgArticle, $wgTitle;
381 $fname = "SearchEngine::goResult";
382
383 $search = $_REQUEST['search'];
384
385 # First try to go to page as entered
386 #
387 $wgArticle = new Article();
388 $wgTitle = Title::newFromText( $search );
389
390 if ( 0 != $wgArticle->getID() ) {
391 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
392 return;
393 }
394
395 # Now try all lower case (i.e. first letter capitalized)
396 #
397 $wgTitle = Title::newFromText( strtolower( $search ) );
398 if ( 0 != $wgArticle->getID() ) {
399 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
400 return;
401 }
402
403 # Now try capitalized string
404 #
405 $wgTitle=Title::newFromText( ucwords( strtolower( $search ) ) );
406 if ( 0 != $wgArticle->getID() ) {
407 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
408 return;
409 }
410
411 # Now try all upper case
412 #
413 $wgTitle = Title::newFromText( strtoupper( $search ) );
414 if ( 0 != $wgArticle->getID() ) {
415 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
416 return;
417 }
418
419 # Try a near match
420 #
421 $this->parseQuery();
422 $sql = "SELECT cur_id,cur_title,cur_namespace,si_page FROM cur,searchindex " .
423 "WHERE cur_id=si_page AND {$this->mTitlecond} ORDER BY cur_namespace LIMIT 1";
424
425 if ( "" != $this->mTitlecond ) {
426 $res = wfQuery( $sql, $fname );
427 }
428 if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
429 $s = wfFetchObject( $res );
430
431 $wgTitle = Title::newFromDBkey( $s->cur_title );
432 $wgTitle->setNamespace( $s->cur_namespace );
433 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
434 return;
435 }
436 $wgOut->addHTML( wfMsg("nogomatch",
437 htmlspecialchars( wfLocalUrl( ucfirst($this->mUsertext), "action=edit") ) )
438 . "\n<p>" );
439 $this->showResults();
440 }
441 }
442