4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
26 use SearchEngineConfig
;
27 use SearchEngineFactory
;
32 class ApiQueryPrefixSearch
extends ApiQueryGeneratorBase
{
33 use \MediaWiki\Api\SearchApi
;
35 public function __construct(
38 SearchEngineConfig
$searchEngineConfig,
39 SearchEngineFactory
$searchEngineFactory
41 parent
::__construct( $query, $moduleName, 'ps' );
42 // Services needed in SearchApi trait
43 $this->searchEngineConfig
= $searchEngineConfig;
44 $this->searchEngineFactory
= $searchEngineFactory;
47 public function execute() {
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
56 * @param ApiPageSet|null $resultPageSet
58 private function run( $resultPageSet = null ) {
59 $params = $this->extractRequestParams();
60 $search = $params['search'];
61 $limit = $params['limit'];
62 $offset = $params['offset'];
64 $searchEngine = $this->buildSearchEngine( $params );
65 $suggestions = $searchEngine->completionSearchWithVariants( $search );
66 $titles = $searchEngine->extractTitles( $suggestions );
68 if ( $suggestions->hasMoreResults() ) {
69 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
72 if ( $resultPageSet ) {
73 $resultPageSet->setRedirectMergePolicy( static function ( array $current, array $new ) {
74 if ( !isset( $current['index'] ) ||
$new['index'] < $current['index'] ) {
75 $current['index'] = $new['index'];
79 $resultPageSet->populateFromTitles( $titles );
80 foreach ( $titles as $index => $title ) {
81 $resultPageSet->setGeneratorData( $title, [ 'index' => $index +
$offset +
1 ] );
84 $result = $this->getResult();
86 foreach ( $titles as $title ) {
88 'ns' => $title->getNamespace(),
89 'title' => $title->getPrefixedText(),
91 if ( $title->isSpecialPage() ) {
92 $vals['special'] = true;
94 $vals['pageid'] = (int)$title->getArticleID();
96 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
99 $this->setContinueEnumParameter( 'offset', $offset +
$count );
103 $result->addIndexedTagName(
104 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
109 public function getCacheMode( $params ) {
113 public function getAllowedParams() {
114 return $this->buildCommonApiParams();
117 public function getSearchProfileParams() {
120 'profile-type' => SearchEngine
::COMPLETION_PROFILE_TYPE
,
121 'help-message' => 'apihelp-query+prefixsearch-param-profile',
126 protected function getExamplesMessages() {
128 'action=query&list=prefixsearch&pssearch=meaning'
129 => 'apihelp-query+prefixsearch-example-simple',
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
138 /** @deprecated class alias since 1.43 */
139 class_alias( ApiQueryPrefixSearch
::class, 'ApiQueryPrefixSearch' );