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
26 class ApiQueryPrefixSearch
extends ApiQueryGeneratorBase
{
29 /** @var array list of api allowed params */
30 private $allowedParams;
32 public function __construct( $query, $moduleName ) {
33 parent
::__construct( $query, $moduleName, 'ps' );
36 public function execute() {
40 public function executeGenerator( $resultPageSet ) {
41 $this->run( $resultPageSet );
45 * @param ApiPageSet $resultPageSet
47 private function run( $resultPageSet = null ) {
48 $params = $this->extractRequestParams();
49 $search = $params['search'];
50 $limit = $params['limit'];
51 $offset = $params['offset'];
53 $searchEngine = $this->buildSearchEngine( $params );
54 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
56 if ( $resultPageSet ) {
57 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
58 if ( !isset( $current['index'] ) ||
$new['index'] < $current['index'] ) {
59 $current['index'] = $new['index'];
63 if ( count( $titles ) > $limit ) {
64 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
67 $resultPageSet->populateFromTitles( $titles );
68 foreach ( $titles as $index => $title ) {
69 $resultPageSet->setGeneratorData( $title, [ 'index' => $index +
$offset +
1 ] );
72 $result = $this->getResult();
74 foreach ( $titles as $title ) {
75 if ( ++
$count > $limit ) {
76 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
80 'ns' => intval( $title->getNamespace() ),
81 'title' => $title->getPrefixedText(),
83 if ( $title->isSpecialPage() ) {
84 $vals['special'] = true;
86 $vals['pageid'] = intval( $title->getArticleID() );
88 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
90 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
94 $result->addIndexedTagName(
95 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
100 public function getCacheMode( $params ) {
104 public function getAllowedParams() {
105 if ( $this->allowedParams
!== null ) {
106 return $this->allowedParams
;
108 $this->allowedParams
= [
110 ApiBase
::PARAM_TYPE
=> 'string',
111 ApiBase
::PARAM_REQUIRED
=> true,
114 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
115 ApiBase
::PARAM_TYPE
=> 'namespace',
116 ApiBase
::PARAM_ISMULTI
=> true,
119 ApiBase
::PARAM_DFLT
=> 10,
120 ApiBase
::PARAM_TYPE
=> 'limit',
121 ApiBase
::PARAM_MIN
=> 1,
122 // Non-standard value for compatibility with action=opensearch
123 ApiBase
::PARAM_MAX
=> 100,
124 ApiBase
::PARAM_MAX2
=> 200,
127 ApiBase
::PARAM_DFLT
=> 0,
128 ApiBase
::PARAM_TYPE
=> 'integer',
131 $profileParam = $this->buildProfileApiParam( SearchEngine
::COMPLETION_PROFILE_TYPE
,
132 'apihelp-query+prefixsearch-param-profile' );
133 if ( $profileParam ) {
134 $this->allowedParams
['profile'] = $profileParam;
136 return $this->allowedParams
;
139 public function getSearchProfileParams() {
140 if ( isset( $this->getAllowedParams()['profile'] ) ) {
141 return [ SearchEngine
::COMPLETION_PROFILE_TYPE
=> 'profile' ];
146 protected function getExamplesMessages() {
148 'action=query&list=prefixsearch&pssearch=meaning'
149 => 'apihelp-query+prefixsearch-example-simple',
153 public function getHelpUrls() {
154 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';