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
17 $lc = SearchEngine
::legalSearchChars() . "()";
18 $this->mUsertext
= trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
19 $this->mSearchterms
= array();
22 function queryNamespaces()
24 return "cur_namespace IN (" . implode( ",", $this->namespacesToSearch
) . ")";
28 function searchRedirects()
30 if ( $this->doSearchRedirects
) return "";
31 return "AND cur_is_redirect=0 ";
34 function powersearch()
36 global $wgUser, $wgOut, $wgLang, $wgTitle;
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'];
51 if ( ! isset ( $searchx ) ) { /* First time here */
52 $nscb[0] = $listredirs = 1; /* All others should be unset */
54 $this->checkboxes
["searchx"] = 1;
55 $ret = wfMsg("powersearchtext");
57 # Determine namespace checkboxes
59 $ns = $wgLang->getNamespaces();
60 array_shift( $ns ); /* Skip "Special" */
63 for ( $i = 0; $i < count( $ns ); ++
$i ) {
65 if ( $nscb[$i] == 1 ) {
66 $checked = " checked";
67 $this->addtoquery
["ns{$i}"] = 1;
68 array_push( $this->namespacesToSearch
, $i );
70 $name = str_replace( "_", " ", $ns[$i] );
71 if ( "" == $name ) { $name = "(Main)"; }
73 if ( 0 != $i ) { $r1 .= " "; }
74 $r1 .= "<input type=checkbox value=\"1\" name=\"" .
75 "ns{$i}\"{$checked}>{$name}\n";
77 $ret = str_replace ( "$1", $r1, $ret );
79 # List redirects checkbox
82 if ( $listredirs == 1 ) {
83 $this->addtoquery
["redirs"] = 1;
84 $checked = " checked";
86 $r2 = "<input type=checkbox value=1 name=\"redirs\"{$checked}>\n";
87 $ret = str_replace( "$2", $r2, $ret );
91 $r3 = "<input type=text name=\"search\" value=\"" .
92 htmlspecialchars( $search ) ."\" width=80>\n";
93 $ret = str_replace( "$3", $r3, $ret );
97 $r9 = "<input type=submit name=\"searchx\" value=\"" .
98 wfMsg("powersearch") . "\">\n";
99 $ret = str_replace( "$9", $r9, $ret );
101 $ret = "<br><br>\n<form id=\"powersearch\" method=\"get\" " .
102 "action=\"" . wfLocalUrl( "" ) . "\">\n{$ret}\n</form>\n";
104 if ( isset ( $searchx ) ) {
105 if ( ! $listredirs ) { $this->doSearchRedirects
= false; }
110 function showResults()
112 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgDisableTextSearch;
113 $fname = "SearchEngine::showResults";
115 $offset = $_REQUEST['offset'];
116 $limit = $_REQUEST['limit'];
117 $search = $_REQUEST['search'];
119 $powersearch = $this->powersearch(); /* Need side-effects here? */
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" );
128 $sk = $wgUser->getSkin();
129 $text = str_replace( "$1", $sk->makeKnownLink(
130 wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ),
131 wfMsg( "searchresulttext" ) );
132 $wgOut->addHTML( $text );
135 if ( "" == $this->mTitlecond ||
"" == $this->mTextcond
) {
136 $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
137 "<p>" . wfMsg( "badquerytext" ) );
140 if ( ! isset( $limit ) ) {
141 $limit = $wgUser->getOption( "searchlimit" );
142 if ( ! $limit ) { $limit = 20; }
144 if ( ! $offset ) { $offset = 0; }
146 $searchnamespaces = $this->queryNamespaces();
147 $redircond = $this->searchRedirects();
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);
157 if ( $wgDisableTextSearch ) {
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);
169 if ( $num == $limit ) {
170 $top = wfShowingResults( $offset, $limit);
172 $top = wfShowingResultsNum( $offset, $limit, $num );
174 $wgOut->addHTML( "<p>{$top}\n" );
179 $akk = array_keys( $this->addtoquery
) ;
180 foreach ( $akk AS $ak ) {
181 $a2l .= "&{$ak}={$this->addtoquery[$ak]}" ;
184 $sl = wfViewPrevNext( $offset, $limit, "",
185 "search=" . wfUrlencode( $this->mUsertext
) . $a2l );
186 $wgOut->addHTML( "<br>{$sl}\n" );
190 if ( 0 == wfNumRows( $res1 ) ) {
191 $wgOut->addHTML( "<h2>" . wfMsg( "notitlematches" ) .
196 $wgOut->addHTML( "<h2>" . wfMsg( "titlematches" ) .
197 "</h2>\n<ol start='{$off}'>" );
199 while ( $row = wfFetchObject( $res1 ) ) {
200 $this->showHit( $row );
202 wfFreeResult( $res1 );
203 $wgOut->addHTML( "</ol>\n" );
206 if ( $wgDisableTextSearch ) {
207 $wgOut->addHTML( str_replace( "$1",
208 htmlspecialchars( $search ), wfMsg( "searchdisabled" ) ) );
210 if ( 0 == wfNumRows( $res2 ) ) {
211 $wgOut->addHTML( "<h2>" . wfMsg( "notextmatches" ) .
216 $wgOut->addHTML( "<h2>" . wfMsg( "textmatches" ) . "</h2>\n" .
217 "<ol start='{$off}'>" );
218 while ( $row = wfFetchObject( $res2 ) ) {
219 $this->showHit( $row );
221 wfFreeResult( $res2 );
222 $wgOut->addHTML( "</ol>\n" );
225 if ( ! $foundsome ) {
226 $wgOut->addHTML( "<p>" . wfMsg( "nonefound" ) . "\n" );
228 $wgOut->addHTML( "<p>{$sl}\n" );
229 $wgOut->addHTML( $powersearch );
232 function legalSearchChars()
234 $lc = "A-Za-z_'0-9\\x80-\\xFF\\-";
238 function parseQuery()
240 global $wgDBminWordLen, $wgLang;
242 $lc = SearchEngine
::legalSearchChars() . "()";
243 $q = preg_replace( "/([()])/", " \\1 ", $this->mUsertext
);
244 $q = preg_replace( "/\\s+/", " ", $q );
245 $w = explode( " ", strtolower( trim( $q ) ) );
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 );
254 } else if ( strlen( $word ) < $wgDBminWordLen ) {
256 } else if ( FulltextStoplist
::inList( $word ) ) {
259 if ( "" != $last ) { $cond .= " AND"; }
260 $cond .= " (MATCH (##field##) AGAINST ('" .
261 wfStrencode( $word ). "'))";
263 array_push( $this->mSearchterms
, "\\b" . $word . "\\b" );
266 if ( 0 == count( $this->mSearchterms
) ) { return; }
268 # To disable boolean:
269 # $cond = "MATCH (##field##) AGAINST('" . wfStrencode( $q ) . "')";
271 $this->mTitlecond
= "(" . str_replace( "##field##",
272 "si_title", $cond ) . " )";
274 $this->mTextcond
= "(" . str_replace( "##field##",
275 "si_text", $cond ) . " AND (cur_is_redirect=0) )";
278 function showHit( $row )
280 global $wgUser, $wgOut;
282 $t = Title
::makeName( $row->cur_namespace
, $row->cur_title
);
283 $sk = $wgUser->getSkin();
285 $contextlines = $wgUser->getOption( "contextlines" );
286 if ( "" == $contextlines ) { $contextlines = 5; }
287 $contextchars = $wgUser->getOption( "contextchars" );
288 if ( "" == $contextchars ) { $contextchars = 50; }
290 $link = $sk->makeKnownLink( $t, "" );
291 $size = str_replace( "$1", strlen( $row->cur_text
), WfMsg( "nbytes" ) );
292 $wgOut->addHTML( "<li>{$link} ({$size})" );
294 $lines = explode( "\n", $row->cur_text
);
295 $pat1 = "/(.*)(" . implode( "|", $this->mSearchterms
) . ")(.*)/i";
298 foreach ( $lines as $line ) {
299 if ( 0 == $contextlines ) { break; }
302 if ( ! preg_match( $pat1, $line, $m ) ) { continue; }
305 if ( 0 == $contextchars ) { $pre = "..."; }
307 if ( strlen( $pre ) > $contextchars ) {
308 $pre = "..." . substr( $pre, -$contextchars );
311 $pre = wfEscapeHTML( $pre );
313 if ( count( $m ) < 3 ) { $post = ""; }
314 else { $post = $m[3]; }
316 if ( 0 == $contextchars ) { $post = "..."; }
318 if ( strlen( $post ) > $contextchars ) {
319 $post = substr( $post, 0, $contextchars ) . "...";
322 $post = wfEscapeHTML( $post );
323 $found = wfEscapeHTML( $m[2] );
325 $line = "{$pre}{$found}{$post}";
326 $pat2 = "/(" . implode( "|", $this->mSearchterms
) . ")/i";
327 $line = preg_replace( $pat2,
328 "<font color='red'>\\1</font>", $line );
330 $wgOut->addHTML( "<br><small>{$lineno}: {$line}</small>\n" );
332 $wgOut->addHTML( "</li>\n" );
337 global $wgOut, $wgArticle, $wgTitle;
338 $fname = "SearchEngine::goResult";
340 $search = $_REQUEST['search'];
342 # First try to go to page as entered
344 $wgArticle = new Article();
345 $wgTitle = Title
::newFromText( $search );
347 if ( 0 != $wgArticle->getID() ) {
352 # Now try all lower case (i.e. first letter capitalized)
354 $wgTitle = Title
::newFromText( strtolower( $search ) );
355 if ( 0 != $wgArticle->getID() ) {
360 # Now try capitalized string
362 $wgTitle=Title
::newFromText( ucwords( strtolower( $search ) ) );
363 if ( 0 != $wgArticle->getID() ) {
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";
374 if ( "" != $this->mTitlecond
) {
375 $res = wfQuery( $sql, $fname );
377 if ( isset( $res ) && 0 != wfNumRows( $res ) ) {
378 $s = wfFetchObject( $res );
380 $wgTitle = Title
::newFromDBkey( $s->cur_title
);
381 $wgTitle->setNamespace( $s->cur_namespace
);
385 $wgOut->addHTML( wfMsg("nogomatch") . "\n<p>" );
386 $this->showResults();