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.
* @private
*/
function queryNamespaces() {
+ if( is_null($this->namespaces) )
+ return ''; # search all
$namespaces = implode( ',', $this->namespaces );
if ($namespaces == '') {
$namespaces = '0';
$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";
$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) {
wfProfileOut( $fname );
return;
}
- $textMatches = $search->searchText( $term );
+ $textMatches = $search->searchText( $rewritten );
// did you mean...
if($textMatches && $textMatches->hasSuggestion()){
'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'''",
'search-redirect',
'search-section',
'search-suggest',
+ 'searchall',
'showingresults',
'showingresultsnum',
'showingresultstotal',