wiki.techinc.nl/includes/api/ApiQueryPrefixSearch.php
dcausse 31680aaddc Expose SearchEngine specific profiles
This patch introduces a way for SearchEngine implementations to expose
specific search profiles useful to fine-tune the various behaviors related to
search.

A SearchEngine can expose a list of profiles by overriding
SearchEngine::getProfiles( $profileType ), profileType stands for the type of
profile being customized. Two types are added in this patch:
- completion: exposed by ApiQueryPrefixSearch and ApiOpenSearch to control
  the behavior of the algorithm behind "search as you type" suggestions.
- fulltext query independent profiles: exposed by ApiQuerySearch to customize
  query indpendent ranking profiles (e.g. boost by templates/incoming
  links/popularity/...)

This patch allows api consumers that might have been confused by fuzzy
suggestions to switch to stricter profiles and to officialize the behavior
behind the hidden param cirrusUseCompletionSuggester. Or to control the
fulltext ranking behaviors like cirrusBoostLinks=(yes|no).

The list of profiles can be discovered by using ApiSandbox/ApiHelp and is totally
controlled by search engine implementations.

Bug: T132477
Change-Id: I66be724d8975976c98c91badbf421f237e014f89
2016-05-30 20:43:53 +02:00

157 lines
4.4 KiB
PHP

<?php
use MediaWiki\MediaWikiServices;
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.23
*/
/**
* @ingroup API
*/
class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
use SearchApi;
/** @var array list of api allowed params */
private $allowedParams;
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'ps' );
}
public function execute() {
$this->run();
}
public function executeGenerator( $resultPageSet ) {
$this->run( $resultPageSet );
}
/**
* @param ApiPageSet $resultPageSet
*/
private function run( $resultPageSet = null ) {
$params = $this->extractRequestParams();
$search = $params['search'];
$limit = $params['limit'];
$offset = $params['offset'];
$searchEngine = $this->buildSearchEngine( $params );
$titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
if ( $resultPageSet ) {
$resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
$current['index'] = $new['index'];
}
return $current;
} );
if ( count( $titles ) > $limit ) {
$this->setContinueEnumParameter( 'offset', $offset + $limit );
array_pop( $titles );
}
$resultPageSet->populateFromTitles( $titles );
foreach ( $titles as $index => $title ) {
$resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
}
} else {
$result = $this->getResult();
$count = 0;
foreach ( $titles as $title ) {
if ( ++$count > $limit ) {
$this->setContinueEnumParameter( 'offset', $offset + $limit );
break;
}
$vals = [
'ns' => intval( $title->getNamespace() ),
'title' => $title->getPrefixedText(),
];
if ( $title->isSpecialPage() ) {
$vals['special'] = true;
} else {
$vals['pageid'] = intval( $title->getArticleID() );
}
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
if ( !$fit ) {
$this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
break;
}
}
$result->addIndexedTagName(
[ 'query', $this->getModuleName() ], $this->getModulePrefix()
);
}
}
public function getCacheMode( $params ) {
return 'public';
}
public function getAllowedParams() {
if ( $this->allowedParams !== null ) {
return $this->allowedParams;
}
$this->allowedParams = [
'search' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'namespace' => [
ApiBase::PARAM_DFLT => NS_MAIN,
ApiBase::PARAM_TYPE => 'namespace',
ApiBase::PARAM_ISMULTI => true,
],
'limit' => [
ApiBase::PARAM_DFLT => 10,
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_MIN => 1,
// Non-standard value for compatibility with action=opensearch
ApiBase::PARAM_MAX => 100,
ApiBase::PARAM_MAX2 => 200,
],
'offset' => [
ApiBase::PARAM_DFLT => 0,
ApiBase::PARAM_TYPE => 'integer',
],
];
$profileParam = $this->buildProfileApiParam( SearchEngine::COMPLETION_PROFILE_TYPE,
'apihelp-query+prefixsearch-param-profile' );
if ( $profileParam ) {
$this->allowedParams['profile'] = $profileParam;
}
return $this->allowedParams;
}
public function getSearchProfileParams() {
if ( isset( $this->getAllowedParams()['profile'] ) ) {
return [ SearchEngine::COMPLETION_PROFILE_TYPE => 'profile' ];
}
return [];
}
protected function getExamplesMessages() {
return [
'action=query&list=prefixsearch&pssearch=meaning'
=> 'apihelp-query+prefixsearch-example-simple',
];
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
}
}