* @file
*/
+use MediaWiki\MediaWikiServices;
+
/**
* Query module to perform full text search within wiki titles and content
*
* @ingroup API
*/
class ApiQuerySearch extends ApiQueryGeneratorBase {
+ use SearchApi;
+
+ /** @var array list of api allowed params */
+ private $allowedParams;
/**
* When $wgSearchType is null, $wgSearchAlternatives[0] is null. Null isn't
global $wgContLang;
$params = $this->extractRequestParams();
+ if ( isset( $params['backend'] ) && $params['backend'] == self::BACKEND_NULL_PARAM ) {
+ unset( $params['backend'] );
+ }
+
// Extract parameters
- $limit = $params['limit'];
$query = $params['search'];
$what = $params['what'];
$interwiki = $params['interwiki'];
}
// Create search engine instance and set options
- $search = isset( $params['backend'] ) && $params['backend'] != self::BACKEND_NULL_PARAM ?
- SearchEngine::create( $params['backend'] ) : SearchEngine::create();
- $search->setLimitOffset( $limit + 1, $params['offset'] );
- $search->setNamespaces( $params['namespace'] );
+ $search = $this->buildSearchEngine( $params );
$search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] );
$query = $search->transformSearchTerm( $query );
} elseif ( $what == 'nearmatch' ) {
// near matches must receive the user input as provided, otherwise
// the near matches within namespaces are lost.
- $matches = SearchEngine::getNearMatchResultSet( $params['search'] );
+ $matches = $search->getNearMatcher( $this->getConfig() )
+ ->getNearMatchResultSet( $params['search'] );
} else {
// We default to title searches; this is a terrible legacy
// of the way we initially set up the MySQL fulltext-based
$titles = [];
$count = 0;
$result = $matches->next();
+ $limit = $params['limit'];
while ( $result ) {
if ( ++$count > $limit ) {
}
public function getAllowedParams() {
- $params = [
+ if ( $this->allowedParams !== null ) {
+ return $this->allowedParams;
+ }
+
+ $this->allowedParams = [
'search' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true
'enablerewrites' => false,
];
- $alternatives = SearchEngine::getSearchTypes();
+ $searchConfig = MediaWikiServices::getInstance()->getSearchEngineConfig();
+ $alternatives = $searchConfig->getSearchTypes();
if ( count( $alternatives ) > 1 ) {
if ( $alternatives[0] === null ) {
$alternatives[0] = self::BACKEND_NULL_PARAM;
}
- $params['backend'] = [
- ApiBase::PARAM_DFLT => $this->getConfig()->get( 'SearchType' ),
+ $this->allowedParams['backend'] = [
+ ApiBase::PARAM_DFLT => $searchConfig->getSearchType(),
ApiBase::PARAM_TYPE => $alternatives,
];
+ // @todo: support profile selection when multiple
+ // backends are available. The solution could be to
+ // merge all possible profiles and let ApiBase
+ // subclasses do the check. Making ApiHelp and ApiSandbox
+ // comprehensive might be more difficult.
+ } else {
+ $profileParam = $this->buildProfileApiParam( SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
+ 'apihelp-query+search-param-qiprofile' );
+ if ( $profileParam ) {
+ $this->allowedParams['qiprofile'] = $profileParam;
+ }
}
- return $params;
+ return $this->allowedParams;
+ }
+
+ public function getSearchProfileParams() {
+ if ( isset( $this->getAllowedParams()['qiprofile'] ) ) {
+ return [ SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE => 'qiprofile' ];
+ }
+ return [];
}
protected function getExamplesMessages() {