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
This commit is contained in:
Robert Stojnić 2008-03-23 17:29:43 +00:00
parent fda41877b9
commit 7532064d27
6 changed files with 47 additions and 8 deletions

View file

@ -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.

View file

@ -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';

View file

@ -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";

View file

@ -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()){

View file

@ -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'''",

View file

@ -685,6 +685,7 @@ $wgMessageStructure = array(
'search-redirect',
'search-section',
'search-suggest',
'searchall',
'showingresults',
'showingresultsnum',
'showingresultstotal',