2 use MediaWiki\MediaWikiServices
;
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
27 class ApiQueryPrefixSearch
extends ApiQueryGeneratorBase
{
30 /** @var array list of api allowed params */
31 private $allowedParams;
33 public function __construct( $query, $moduleName ) {
34 parent
::__construct( $query, $moduleName, 'ps' );
37 public function execute() {
41 public function executeGenerator( $resultPageSet ) {
42 $this->run( $resultPageSet );
46 * @param ApiPageSet $resultPageSet
48 private function run( $resultPageSet = null ) {
49 $params = $this->extractRequestParams();
50 $search = $params['search'];
51 $limit = $params['limit'];
52 $offset = $params['offset'];
54 $searchEngine = $this->buildSearchEngine( $params );
55 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
57 if ( $resultPageSet ) {
58 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
59 if ( !isset( $current['index'] ) ||
$new['index'] < $current['index'] ) {
60 $current['index'] = $new['index'];
64 if ( count( $titles ) > $limit ) {
65 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
68 $resultPageSet->populateFromTitles( $titles );
69 foreach ( $titles as $index => $title ) {
70 $resultPageSet->setGeneratorData( $title, [ 'index' => $index +
$offset +
1 ] );
73 $result = $this->getResult();
75 foreach ( $titles as $title ) {
76 if ( ++
$count > $limit ) {
77 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
81 'ns' => intval( $title->getNamespace() ),
82 'title' => $title->getPrefixedText(),
84 if ( $title->isSpecialPage() ) {
85 $vals['special'] = true;
87 $vals['pageid'] = intval( $title->getArticleID() );
89 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
91 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
95 $result->addIndexedTagName(
96 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
101 public function getCacheMode( $params ) {
105 public function getAllowedParams() {
106 if ( $this->allowedParams
!== null ) {
107 return $this->allowedParams
;
109 $this->allowedParams
= [
111 ApiBase
::PARAM_TYPE
=> 'string',
112 ApiBase
::PARAM_REQUIRED
=> true,
115 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
116 ApiBase
::PARAM_TYPE
=> 'namespace',
117 ApiBase
::PARAM_ISMULTI
=> true,
120 ApiBase
::PARAM_DFLT
=> 10,
121 ApiBase
::PARAM_TYPE
=> 'limit',
122 ApiBase
::PARAM_MIN
=> 1,
123 // Non-standard value for compatibility with action=opensearch
124 ApiBase
::PARAM_MAX
=> 100,
125 ApiBase
::PARAM_MAX2
=> 200,
128 ApiBase
::PARAM_DFLT
=> 0,
129 ApiBase
::PARAM_TYPE
=> 'integer',
132 $profileParam = $this->buildProfileApiParam( SearchEngine
::COMPLETION_PROFILE_TYPE
,
133 'apihelp-query+prefixsearch-param-profile' );
134 if ( $profileParam ) {
135 $this->allowedParams
['profile'] = $profileParam;
137 return $this->allowedParams
;
140 public function getSearchProfileParams() {
141 if ( isset( $this->getAllowedParams()['profile'] ) ) {
142 return [ SearchEngine
::COMPLETION_PROFILE_TYPE
=> 'profile' ];
147 protected function getExamplesMessages() {
149 'action=query&list=prefixsearch&pssearch=meaning'
150 => 'apihelp-query+prefixsearch-example-simple',
154 public function getHelpUrls() {
155 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';