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
{
28 public function __construct( $query, $moduleName ) {
29 parent
::__construct( $query, $moduleName, 'ps' );
32 public function execute() {
36 public function executeGenerator( $resultPageSet ) {
37 $this->run( $resultPageSet );
41 * @param ApiPageSet $resultPageSet
43 private function run( $resultPageSet = null ) {
44 $params = $this->extractRequestParams();
45 $search = $params['search'];
46 $limit = $params['limit'];
47 $namespaces = $params['namespace'];
48 $offset = $params['offset'];
50 $searchEngine = MediaWikiServices
::getInstance()->newSearchEngine();
51 $searchEngine->setLimitOffset( $limit +
1, $offset );
52 $searchEngine->setNamespaces( $namespaces );
53 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
55 if ( $resultPageSet ) {
56 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
57 if ( !isset( $current['index'] ) ||
$new['index'] < $current['index'] ) {
58 $current['index'] = $new['index'];
62 if ( count( $titles ) > $limit ) {
63 $this->setContinueEnumParameter( 'offset', $offset +
$params['limit'] );
66 $resultPageSet->populateFromTitles( $titles );
67 foreach ( $titles as $index => $title ) {
68 $resultPageSet->setGeneratorData( $title, [ 'index' => $index +
$offset +
1 ] );
71 $result = $this->getResult();
73 foreach ( $titles as $title ) {
74 if ( ++
$count > $limit ) {
75 $this->setContinueEnumParameter( 'offset', $offset +
$params['limit'] );
79 'ns' => intval( $title->getNamespace() ),
80 'title' => $title->getPrefixedText(),
82 if ( $title->isSpecialPage() ) {
83 $vals['special'] = true;
85 $vals['pageid'] = intval( $title->getArticleID() );
87 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
89 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
93 $result->addIndexedTagName(
94 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
99 public function getCacheMode( $params ) {
103 public function getAllowedParams() {
106 ApiBase
::PARAM_TYPE
=> 'string',
107 ApiBase
::PARAM_REQUIRED
=> true,
110 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
111 ApiBase
::PARAM_TYPE
=> 'namespace',
112 ApiBase
::PARAM_ISMULTI
=> true,
115 ApiBase
::PARAM_DFLT
=> 10,
116 ApiBase
::PARAM_TYPE
=> 'limit',
117 ApiBase
::PARAM_MIN
=> 1,
118 // Non-standard value for compatibility with action=opensearch
119 ApiBase
::PARAM_MAX
=> 100,
120 ApiBase
::PARAM_MAX2
=> 200,
123 ApiBase
::PARAM_DFLT
=> 0,
124 ApiBase
::PARAM_TYPE
=> 'integer',
129 protected function getExamplesMessages() {
131 'action=query&list=prefixsearch&pssearch=meaning'
132 => 'apihelp-query+prefixsearch-example-simple',
136 public function getHelpUrls() {
137 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';