From: Robert Stojnić Date: Sun, 23 Mar 2008 17:29:43 +0000 (+0000) Subject: Search backend: X-Git-Tag: 1.31.0-rc.0~48861 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=7532064d279d2c1fbbbe867b4720b191675a1fa3;p=lhc%2Fweb%2Fwiklou.git Search backend: * add "all:" prefix that searches all namespaces (port from LuceneSearch) * added a simplistic replacePrefixes so that now image:something will always search the image namespace --- diff --git a/includes/SearchEngine.php b/includes/SearchEngine.php index 739e8a1084..a2c92a426e 100644 --- a/includes/SearchEngine.php +++ b/includes/SearchEngine.php @@ -176,6 +176,37 @@ class SearchEngine { function setNamespaces( $namespaces ) { $this->namespaces = $namespaces; } + + /** + * Parse some common prefixes: all (search everything) + * or namespace names + * + * @param string $query + */ + function replacePrefixes( $query ){ + global $wgContLang; + + if( strpos($query,':') === false ) + return $query; // nothing to do + + $parsed = $query; + $allkeyword = wfMsg('searchall').":"; + if( strncmp($query, $allkeyword, strlen($allkeyword)) == 0 ){ + $this->namespaces = null; + $parsed = substr($query,strlen($allkeyword)); + } else if( strpos($query,':') !== false ) { + $prefix = substr($query,0,strpos($query,':')); + $index = $wgContLang->getNsIndex($prefix); + if($index !== false){ + $this->namespaces = array($index); + $parsed = substr($query,strlen($prefix)+1); + } + } + if(trim($parsed) == '') + return $query; // prefix was the whole query + + return $parsed; + } /** * Make a list of searchable namespaces and their canonical names. diff --git a/includes/SearchMySQL.php b/includes/SearchMySQL.php index e6e312cbd9..ecbb528680 100644 --- a/includes/SearchMySQL.php +++ b/includes/SearchMySQL.php @@ -115,6 +115,8 @@ class SearchMySQL extends SearchEngine { * @private */ function queryNamespaces() { + if( is_null($this->namespaces) ) + return ''; # search all $namespaces = implode( ',', $this->namespaces ); if ($namespaces == '') { $namespaces = '0'; diff --git a/includes/SearchPostgres.php b/includes/SearchPostgres.php index 59110a5a73..18a02a8f50 100644 --- a/includes/SearchPostgres.php +++ b/includes/SearchPostgres.php @@ -174,11 +174,13 @@ class SearchPostgres extends SearchEngine { $query .= ' AND page_is_redirect = 0'; ## Namespaces - defaults to 0 - if ( count($this->namespaces) < 1) - $query .= ' AND page_namespace = 0'; - else { - $namespaces = implode( ',', $this->namespaces ); - $query .= " AND page_namespace IN ($namespaces)"; + if( !is_null($this->namespaces) ){ // null -> search all + if ( count($this->namespaces) < 1) + $query .= ' AND page_namespace = 0'; + else { + $namespaces = implode( ',', $this->namespaces ); + $query .= " AND page_namespace IN ($namespaces)"; + } } $query .= " ORDER BY score DESC, page_id DESC"; diff --git a/includes/SpecialSearch.php b/includes/SpecialSearch.php index 434ea33a2a..84d1568a7d 100644 --- a/includes/SpecialSearch.php +++ b/includes/SpecialSearch.php @@ -159,8 +159,10 @@ class SpecialSearch { $search = SearchEngine::create(); $search->setLimitOffset( $this->limit, $this->offset ); $search->setNamespaces( $this->namespaces ); - $search->showRedirects = $this->searchRedirects; - $titleMatches = $search->searchTitle( $term ); + $search->showRedirects = $this->searchRedirects; + $rewritten = $search->replacePrefixes($term); + + $titleMatches = $search->searchTitle( $rewritten ); // Sometimes the search engine knows there are too many hits if ($titleMatches instanceof SearchResultTooMany) { @@ -170,7 +172,7 @@ class SpecialSearch { wfProfileOut( $fname ); return; } - $textMatches = $search->searchText( $term ); + $textMatches = $search->searchText( $rewritten ); // did you mean... if($textMatches && $textMatches->hasSuggestion()){ diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 29bdb41ddb..01482e066a 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -1236,6 +1236,7 @@ Make sure that this change will maintain historical page continuity. 'search-redirect' => '(redirect $1)', 'search-section' => '(section $1)', 'search-suggest' => 'Did you mean: $1', +'searchall' => 'all', 'showingresults' => "Showing below up to {{PLURAL:$1|'''1''' result|'''$1''' results}} starting with #'''$2'''.", 'showingresultsnum' => "Showing below {{PLURAL:$3|'''1''' result|'''$3''' results}} starting with #'''$2'''.", 'showingresultstotal' => "Showing below results '''$1 - $2''' of '''$3'''", diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 07bc8d36b9..73b66ae867 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -685,6 +685,7 @@ $wgMessageStructure = array( 'search-redirect', 'search-section', 'search-suggest', + 'searchall', 'showingresults', 'showingresultsnum', 'showingresultstotal',