6965eb7842d488fffec08eee62e9ab5b2849b399
5 /* private */ var $mUsertext, $mSearchterms;
6 /* private */ var $mTitlecond, $mTextcond;
8 var $doSearchRedirects = true;
9 var $addtoquery = array();
10 var $namespacesToSearch = array();
13 function SearchEngine( $text )
15 # We display the query, so let's strip it for safety
18 $lc = SearchEngine
::legalSearchChars() . "()";
19 if( $wgDBmysql4 ) $lc .= "\"~<>*+-";
20 $this->mUsertext
= trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
21 $this->mSearchterms
= array();
24 function queryNamespaces()
26 $namespaces = implode( ",", $this->namespacesToSearch
);
27 if ($namespaces == "") {
30 return "AND cur_namespace IN (" . $namespaces . ")";
34 function searchRedirects()
36 if ( $this->doSearchRedirects
) return "";
37 return "AND cur_is_redirect=0 ";
42 /* private */ function initNamespaceCheckbox( $i )
44 global $wgUser, $wgNamespacesToBeSearchedDefault;
47 if ($wgUser->getRights()) {
48 // User is logged in so we retrieve his default namespaces
49 return $wgUser->getOption( "searchNs".$i );
52 // User is not logged in so we give him the global default namespaces
53 return $wgNamespacesToBeSearchedDefault[ $i ];
59 function powersearch()
61 global $wgUser, $wgOut, $wgLang, $wgTitle;
64 $search = $_REQUEST['search'];
65 $searchx = $_REQUEST['searchx'];
66 $listredirs = $_REQUEST['redirs'];
69 if ( ! isset ( $searchx ) ) { /* First time here */
71 for ($i = 0; ($i <= 7); $i++
)
73 $nscb[$i] = $this->initNamespaceCheckbox($i);
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'];
86 $this->checkboxes
["searchx"] = 1;
87 $ret = wfMsg("powersearchtext");
89 # Determine namespace checkboxes
91 $ns = $wgLang->getNamespaces();
92 array_shift( $ns ); /* Skip "Special" */
95 for ( $i = 0; $i < count( $ns ); ++
$i ) {
97 if ( $nscb[$i] == 1 ) {
98 $checked = " checked";
99 $this->addtoquery
["ns{$i}"] = 1;
100 array_push( $this->namespacesToSearch
, $i );
102 $name = str_replace( "_", " ", $ns[$i] );
103 if ( "" == $name ) { $name = wfMsg( "blanknamespace" ); }
105 if ( 0 != $i ) { $r1 .= " "; }
106 $r1 .= "<input type=checkbox value=\"1\" name=\"" .
107 "ns{$i}\"{$checked}>{$name}\n";
109 $ret = str_replace ( "$1", $r1, $ret );
111 # List redirects checkbox
114 if ( $listredirs == 1 ) {
115 $this->addtoquery
["redirs"] = 1;
116 $checked = " checked";
118 $r2 = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
119 $ret = str_replace( "$2", $r2, $ret );
123 $r3 = "<input type=text name=\"search\" value=\"" .
124 htmlspecialchars( $search ) ."\" width=80>\n";
125 $ret = str_replace( "$3", $r3, $ret );
129 $r9 = "<input type=submit name=\"searchx\" value=\"" .
130 wfMsg("powersearch") . "\">\n";
131 $ret = str_replace( "$9", $r9, $ret );
133 $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
134 "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
136 if ( isset ( $searchx ) ) {
137 if ( ! $listredirs ) { $this->doSearchRedirects
= false; }
142 function showResults()
144 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
145 global $wgInputEncoding;
146 $fname = "SearchEngine::showResults";
148 $search = $_REQUEST['search'];
150 $powersearch = $this->powersearch(); /* Need side-effects here? */
152 $wgOut->setPageTitle( wfMsg( "searchresults" ) );
153 $q = wfMsg( "searchquery", htmlspecialchars( $this->mUsertext
) );
154 $wgOut->setSubtitle( $q );
155 $wgOut->setArticleFlag( false );
156 $wgOut->setRobotpolicy( "noindex,nofollow" );
158 $sk = $wgUser->getSkin();
159 $text = wfMsg( "searchresulttext", $sk->makeKnownLink(
160 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
161 $wgOut->addHTML( $text );
164 if ( "" == $this->mTitlecond ||
"" == $this->mTextcond
) {
165 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
166 "<p>" . wfMsg( "badquerytext" ) );
169 list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
171 $searchnamespaces = $this->queryNamespaces();
172 $redircond = $this->searchRedirects();
174 $sql = "SELECT cur_id,cur_namespace,cur_title," .
175 "cur_text FROM cur,searchindex " .
176 "WHERE cur_id=si_page AND {$this->mTitlecond} " .
177 "{$searchnamespaces} {$redircond}" .
178 "LIMIT {$offset}, {$limit}";
179 $res1 = wfQuery( $sql, DB_READ
, $fname );
180 $num = wfNumRows($res1);
182 if ( $wgDisableTextSearch ) {
185 $sql = "SELECT cur_id,cur_namespace,cur_title," .
186 "cur_text FROM cur,searchindex " .
187 "WHERE cur_id=si_page AND {$this->mTextcond} " .
188 "{$searchnamespaces} {$redircond} " .
189 "LIMIT {$offset}, {$limit}";
190 $res2 = wfQuery( $sql, DB_READ
, $fname );
191 $num = $num +
wfNumRows($res2);
194 if ( $num == $limit ) {
195 $top = wfShowingResults( $offset, $limit);
197 $top = wfShowingResultsNum( $offset, $limit, $num );
199 $wgOut->addHTML( "<p>{$top}\n" );
204 $akk = array_keys( $this->addtoquery
) ;
205 foreach ( $akk AS $ak ) {
206 $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
209 $sl = wfViewPrevNext( $offset, $limit, "",
210 "search=" . wfUrlencode( $this->mUsertext
) . $a2l );
211 $wgOut->addHTML( "<br>{$sl}\n" );
215 if ( 0 == wfNumRows( $res1 ) ) {
216 $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
221 $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
222 "</h2>\n<ol start='{$off}'>" );
224 while ( $row = wfFetchObject( $res1 ) ) {
225 $this->showHit( $row );
227 wfFreeResult( $res1 );
228 $wgOut->addHTML( "</ol>\n" );
231 if ( $wgDisableTextSearch ) {
232 $wgOut->addHTML( wfMsg( "searchdisabled", $search, $wgInputEncoding ) );
234 if ( 0 == wfNumRows( $res2 ) ) {
235 $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
240 $wgOut->addHTML( "<h2>" . wfMsg( "textmatches" ) . "</h2>\n" .
241 "<ol start='{$off}'>" );
242 while ( $row = wfFetchObject( $res2 ) ) {
243 $this->showHit( $row );
245 wfFreeResult( $res2 );
246 $wgOut->addHTML( "</ol>\n" );
249 if ( ! $foundsome ) {
250 $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
252 $wgOut->addHTML( "<p>{$sl}\n" );
253 $wgOut->addHTML( $powersearch );
256 function legalSearchChars()
258 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
262 function parseQuery()
264 global $wgDBminWordLen, $wgLang, $wgDBmysql4;
267 # Use cleaner boolean search if available
268 return $this->parseQuery4();
271 $lc = SearchEngine
::legalSearchChars() . "()";
272 $q = preg_replace( "/([()])/", " \\1 ", $this->mUsertext
);
273 $q = preg_replace( "/\\s+/", " ", $q );
274 $w = explode( " ", strtolower( trim( $q ) ) );
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 );
283 } else if ( strlen( $word ) < $wgDBminWordLen ) {
285 } else if ( FulltextStoplist
::inList( $word ) ) {
288 if ( "" != $last ) { $cond .= " AND"; }
289 $cond .= " (MATCH (##field##) AGAINST ('" .
290 wfStrencode( $word ). "'))";
292 array_push( $this->mSearchterms
, "\\b" . $word . "\\b" );
295 if ( 0 == count( $this->mSearchterms
) ) { return; }
297 $this->mTitlecond
= "(" . str_replace( "##field##",
298 "si_title", $cond ) . " )";
300 $this->mTextcond
= "(" . str_replace( "##field##",
301 "si_text", $cond ) . " AND (cur_is_redirect=0) )";
304 function parseQuery4()
306 # FIXME: not ready yet! Do not use.
309 $lc = SearchEngine
::legalSearchChars();
310 #$q = preg_replace( "/([+-]?)([$lc]+)/e",
311 # "\"$1\" . \$wgLang->stripForSearch(\"$2\")",
312 # $this->mUsertext );
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)";
321 function showHit( $row )
323 global $wgUser, $wgOut;
325 $t = Title
::makeName( $row->cur_namespace
, $row->cur_title
);
326 $sk = $wgUser->getSkin();
328 $contextlines = $wgUser->getOption( "contextlines" );
329 if ( "" == $contextlines ) { $contextlines = 5; }
330 $contextchars = $wgUser->getOption( "contextchars" );
331 if ( "" == $contextchars ) { $contextchars = 50; }
333 $link = $sk->makeKnownLink( $t, "" );
334 $size = str_replace( "$1", strlen( $row->cur_text
), WfMsg( "nbytes" ) );
335 $wgOut->addHTML( "<li>{$link} ({$size})" );
337 $lines = explode( "\n", $row->cur_text
);
338 $pat1 = "/(.*)(" . implode( "|", $this->mSearchterms
) . ")(.*)/i";
341 foreach ( $lines as $line ) {
342 if ( 0 == $contextlines ) { break; }
345 if ( ! preg_match( $pat1, $line, $m ) ) { continue; }
348 if ( 0 == $contextchars ) { $pre = "..."; }
350 if ( strlen( $pre ) > $contextchars ) {
351 $pre = "..." . substr( $pre, -$contextchars );
354 $pre = wfEscapeHTML( $pre );
356 if ( count( $m ) < 3 ) { $post = ""; }
357 else { $post = $m[3]; }
359 if ( 0 == $contextchars ) { $post = "..."; }
361 if ( strlen( $post ) > $contextchars ) {
362 $post = substr( $post, 0, $contextchars ) . "...";
365 $post = wfEscapeHTML( $post );
366 $found = wfEscapeHTML( $m[2] );
368 $line = "{$pre}{$found}{$post}";
369 $pat2 = "/(" . implode( "|", $this->mSearchterms
) . ")/i";
370 $line = preg_replace( $pat2,
371 "<font color='red'>\\1</font>", $line );
373 $wgOut->addHTML( "<br><small>{$lineno}: {$line}</small>\n" );
375 $wgOut->addHTML( "</li>\n" );
381 $fname = "SearchEngine::goResult";
383 $search = $_REQUEST['search'];
385 # First try to go to page as entered
387 $t = Title
::newFromText( $search );
389 if ( 0 != $t->getArticleID() ) {
390 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
394 # Now try all lower case (i.e. first letter capitalized)
396 $t = Title
::newFromText( strtolower( $search ) );
397 if ( 0 != $t->getArticleID() ) {
398 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
402 # Now try capitalized string
404 $t = Title
::newFromText( ucwords( strtolower( $search ) ) );
405 if ( 0 != $t->getArticleID() ) {
406 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
410 # Now try all upper case
412 $t = Title
::newFromText( strtoupper( $search ) );
413 if ( 0 != $t->getArticleID() ) {
414 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
421 $sql = "SELECT cur_id,cur_title,cur_namespace,si_page FROM cur,searchindex " .
422 "WHERE cur_id=si_page AND {$this->mTitlecond} ORDER BY cur_namespace LIMIT 1";
424 if ( "" != $this->mTitlecond
) {
425 $res = wfQuery( $sql, DB_READ
, $fname );
427 if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
428 $s = wfFetchObject( $res );
430 $t = Title
::makeTitle( $s->cur_namespace
, $s->cur_title
);
431 $wgOut->redirect( wfLocalUrl( $t->getPrefixedURL() ) );
434 $wgOut->addHTML( wfMsg("nogomatch",
435 htmlspecialchars( wfLocalUrl( ucfirst($this->mUsertext
), "action=edit") ) )
437 $this->showResults();