- /* private */ var $mRawtext, $mUsertext, $mSearchterms;
- /* private */ var $mTitlecond, $mTextcond;
-
- var $doSearchRedirects = true;
- var $addtoquery = array();
- var $namespacesToSearch = array();
- var $alternateTitle;
- var $all_titles = false;
-
- function SearchEngine( $text )
- {
- # We display the query, so let's strip it for safety
- #
- global $wgDBmysql4;
- $lc = SearchEngine::legalSearchChars() . "()";
- if( $wgDBmysql4 ) $lc .= "\"~<>*+-";
- $this->mRawtext = $text;
- $this->mUsertext = trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
- $this->mSearchterms = array();
- $this->mStrictMatching = true; # Google-style, add '+' on all terms
-
- $this->db =& wfGetDB( DB_SLAVE );
- }
-
- function queryNamespaces()
- {
- $namespaces = implode( ",", $this->namespacesToSearch );
- if ($namespaces == "") {
- $namespaces = "0";
- }
- return "AND cur_namespace IN (" . $namespaces . ")";
- }
-
- function searchRedirects()
- {
- if ( $this->doSearchRedirects ) {
- return "";
- } else {
- return "AND cur_is_redirect=0 ";
- }
- }
-
- /* private */ function initNamespaceCheckbox( $i )
- {
- global $wgUser, $wgNamespacesToBeSearchedDefault;
-
- if ($wgUser->getID()) {
- // User is logged in so we retrieve his default namespaces
- return $wgUser->getOption( "searchNs".$i );
- } else {
- // User is not logged in so we give him the global default namespaces
- return !empty($wgNamespacesToBeSearchedDefault[ $i ]);
- }
- }
-
- # Display the "power search" footer. Does not actually perform the search,
- # that is done by showResults()
- function powersearch()
- {
- global $wgUser, $wgOut, $wgLang, $wgTitle, $wgRequest;
- $sk =& $wgUser->getSkin();
-
- $search = $this->mRawtext;
- $searchx = $wgRequest->getVal( 'searchx' );
- $listredirs = $wgRequest->getVal( 'redirs' );
-
- $ret = wfMsg("powersearchtext"); # Text to be returned
- $tempText = ""; # Temporary text, for substitution into $ret
-
- if( isset( $_REQUEST["searchx"] ) ) {
- $this->addtoquery["searchx"] = "1";
- }
-
- # Do namespace checkboxes
- $namespaces = $wgLang->getNamespaces();
- foreach ( $namespaces as $i => $namespace ) {
- # Skip virtual namespaces
- if ( $i < 0 ) {
- continue;
- }
-
- $formVar = "ns$i";
-
- # Initialise checkboxValues, either from defaults or from
- # a previous invocation
- if ( !isset( $searchx ) ) {
- $checkboxValue = $this->initNamespaceCheckbox( $i );
- } else {
- $checkboxValue = $wgRequest->getVal( $formVar );
- }
-
- $checked = "";
- if ( $checkboxValue == 1 ) {
- $checked = " checked='checked'";
- $this->addtoquery["ns{$i}"] = 1;
- array_push( $this->namespacesToSearch, $i );
- }
- $name = str_replace( "_", " ", $namespaces[$i] );
- if ( "" == $name ) {
- $name = wfMsg( "blanknamespace" );
- }
-
- if ( $tempText !== "" ) {
- $tempText .= " ";
- }
- $tempText .= "<input type='checkbox' value=\"1\" name=\"" .
- "ns{$i}\"{$checked} />{$name}\n";
- }
- $ret = str_replace ( "$1", $tempText, $ret );
-
- # List redirects checkbox
-
- $checked = "";
- if ( $listredirs == 1 ) {
- $this->addtoquery["redirs"] = 1;
- $checked = " checked='checked'";
- }
- $tempText = "<input type='checkbox' value='1' name=\"redirs\"{$checked} />\n";
- $ret = str_replace( "$2", $tempText, $ret );
-
- # Search field
-
- $tempText = "<input type='text' name=\"search\" value=\"" .
- htmlspecialchars( $search ) ."\" width='80' />\n";
- $ret = str_replace( "$3", $tempText, $ret );
-
- # Searchx button
-
- $tempText = "<input type='submit' name=\"searchx\" value=\"" .
- wfMsg("powersearch") . "\" />\n";
- $ret = str_replace( "$9", $tempText, $ret );
-
- $action = $sk->escapeSearchLink();
- $ret = "<br /><br />\n<form id=\"powersearch\" method=\"get\" " .
- "action=\"$action\">\n{$ret}\n</form>\n";
-
- if ( isset ( $searchx ) ) {
- if ( ! $listredirs ) {
- $this->doSearchRedirects = false;
- }
- }
- return $ret;
- }
-
- function setupPage() {
- global $wgOut;
- $wgOut->setPageTitle( wfMsg( "searchresults" ) );
- $q = wfMsg( "searchquery", htmlspecialchars( $this->mRawtext ) );
- $wgOut->setSubtitle( $q );
- $wgOut->setArticleRelated( false );
- $wgOut->setRobotpolicy( "noindex,nofollow" );
- }
-
- # Perform the search and construct the results page
- function showResults()
- {
- global $wgUser, $wgTitle, $wgOut, $wgLang;
- global $wgDisableTextSearch, $wgInputEncoding;
- $fname = "SearchEngine::showResults";
-
- $search = $this->mRawtext;
-
- $powersearch = $this->powersearch(); /* Need side-effects here? */
-
- $this->setupPage();
-
- $sk = $wgUser->getSkin();
- $header = wfMsg( "searchresulttext", $sk->makeKnownLink(
- wfMsg( "searchhelppage" ), wfMsg( "searchingwikipedia" ) ) );
- $wgOut->addHTML( $header );
-
- $this->parseQuery();
- if ( "" == $this->mTitlecond || "" == $this->mTextcond ) {
- $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
- "<p>" . wfMsg( "badquerytext" ) . "</p>\n" );
- return;
- }
- list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
-
- $searchnamespaces = $this->queryNamespaces();
- $redircond = $this->searchRedirects();
-
- if ( $wgDisableTextSearch ) {
- $wgOut->addHTML( wfMsg( "searchdisabled" ) );
- $wgOut->addHTML( wfMsg( "googlesearch", htmlspecialchars( $search ), htmlspecialchars( $wgInputEncoding ) ) );
- } else {
- if( $this->parseQuery() == MW_SEARCH_BAD_QUERY ) {
- $wgOut->addHTML( "<h2>" . wfMsg( "badquery" ) . "</h2>\n" .
- "<p>" . wfMsg( "badquerytext" ) . "</p>\n" );
- return;
- }
-
- list( $limit, $offset ) = wfCheckLimits( 20, "searchlimit" );
- $titleMatches = $this->getMatches( $this->mTitlecond, $limit, $offset );
- $textMatches = $this->getMatches( $this->mTextcond, $limit, $offset );
-
- $sk = $wgUser->getSkin();
-
- $num = count( $titleMatches ) + count( $textMatches );
- if ( $num >= $limit ) {
- $top = wfShowingResults( $offset, $limit );
- } else {
- $top = wfShowingResultsNum( $offset, $limit, $num );
- }
- $wgOut->addHTML( "<p>{$top}</p>\n" );
-
- # For powersearch