Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / api / ApiQueryPrefixSearch.php
blob46538e0eb1a5653c8c01250cc319d7564e613e96
1 <?php
2 use MediaWiki\MediaWikiServices;
4 /**
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
20 * @file
21 * @since 1.23
24 /**
25 * @ingroup API
27 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
28 use SearchApi;
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() {
38 $this->run();
41 public function executeGenerator( $resultPageSet ) {
42 $this->run( $resultPageSet );
45 /**
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'];
62 return $current;
63 } );
64 if ( count( $titles ) > $limit ) {
65 $this->setContinueEnumParameter( 'offset', $offset + $limit );
66 array_pop( $titles );
68 $resultPageSet->populateFromTitles( $titles );
69 foreach ( $titles as $index => $title ) {
70 $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
72 } else {
73 $result = $this->getResult();
74 $count = 0;
75 foreach ( $titles as $title ) {
76 if ( ++$count > $limit ) {
77 $this->setContinueEnumParameter( 'offset', $offset + $limit );
78 break;
80 $vals = [
81 'ns' => intval( $title->getNamespace() ),
82 'title' => $title->getPrefixedText(),
84 if ( $title->isSpecialPage() ) {
85 $vals['special'] = true;
86 } else {
87 $vals['pageid'] = intval( $title->getArticleID() );
89 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
90 if ( !$fit ) {
91 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
92 break;
95 $result->addIndexedTagName(
96 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
101 public function getCacheMode( $params ) {
102 return 'public';
105 public function getAllowedParams() {
106 if ( $this->allowedParams !== null ) {
107 return $this->allowedParams;
109 $this->allowedParams = [
110 'search' => [
111 ApiBase::PARAM_TYPE => 'string',
112 ApiBase::PARAM_REQUIRED => true,
114 'namespace' => [
115 ApiBase::PARAM_DFLT => NS_MAIN,
116 ApiBase::PARAM_TYPE => 'namespace',
117 ApiBase::PARAM_ISMULTI => true,
119 'limit' => [
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,
127 'offset' => [
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' ];
144 return [];
147 protected function getExamplesMessages() {
148 return [
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';