From 653b7d63bc0bc952e804525b9a52c0b6f84c5b71 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Wed, 12 Feb 2014 09:30:22 -0800 Subject: [PATCH] Support interwiki searches in API Adds new SearchResult methods for getting namespace text Bug: 60975 Change-Id: I381c8681cabfa9ff7a73cbe0286ed32d267a5990 --- includes/api/ApiQuerySearch.php | 49 ++++++++++++++++++++++++++++++-- includes/search/SearchEngine.php | 7 +++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/includes/api/ApiQuerySearch.php b/includes/api/ApiQuerySearch.php index 4fedebce67..1132a60de6 100644 --- a/includes/api/ApiQuerySearch.php +++ b/includes/api/ApiQuerySearch.php @@ -63,6 +63,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { $limit = $params['limit']; $query = $params['search']; $what = $params['what']; + $interwiki = $params['interwiki']; $searchInfo = array_flip( $params['info'] ); $prop = array_flip( $params['prop'] ); @@ -197,10 +198,52 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { $result = $matches->next(); } + $hasInterwikiResults = false; + if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) { + $matches = $matches->getInterwikiResults(); + $iwprefixes = array(); + $hasInterwikiResults = true; + + // Include number of results if requested + if ( isset( $searchInfo['totalhits'] ) ) { + $totalhits = $matches->getTotalHits(); + if ( $totalhits !== null ) { + $apiResult->addValue( array( 'query', 'interwikisearchinfo' ), + 'totalhits', $totalhits ); + } + } + + $result = $matches->next(); + while ( $result ) { + $title = $result->getTitle(); + $vals = array( + 'namespace' => $result->getInterwikiNamespaceText(), + 'title' => $title->getText(), + 'url' => $title->getFullUrl(), + ); + + // Add item to results and see whether it fits + $fit = $apiResult->addValue( array( 'query', 'interwiki' . $this->getModuleName(), $result->getInterwikiPrefix() ), + null, $vals ); + if ( !$fit ) { + // We hit the limit. We can't really provide any meaningful + // pagination info so just bail out + break; + } + + $result = $matches->next(); + } + } + if ( is_null( $resultPageSet ) ) { $apiResult->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' ); + if ( $hasInterwikiResults ) { + $apiResult->setIndexedTagName_internal( array( + 'query', 'interwiki' . $this->getModuleName() + ), 'p' ); + } } else { $resultPageSet->populateFromTitles( $titles ); } @@ -264,7 +307,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { ApiBase::PARAM_MIN => 1, ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1, ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2 - ) + ), + 'interwiki' => false, ); $alternatives = SearchEngine::getSearchTypes(); @@ -303,7 +347,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { ), 'redirects' => 'Include redirect pages in the search', 'offset' => 'Use this value to continue paging (return by query)', - 'limit' => 'How many total pages to return' + 'limit' => 'How many total pages to return', + 'interwiki' => 'Include interwiki results in the search, if available' ); if ( count( SearchEngine::getSearchTypes() ) > 1 ) { diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index 882919feba..debf01b4aa 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -974,6 +974,13 @@ class SearchResult { return ''; } + /** + * @return string interwiki namespace of the title (since we likely can't resolve it locally) + */ + function getInterwikiNamespaceText() { + return ''; + } + /** * Did this match file contents (eg: PDF/DJVU)? */ -- 2.20.1