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