Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryPrefixSearch.php
blob7a31c4811b37b358273b58147cbbc2a2618ee0c7
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @since 1.23
22 /**
23 * @ingroup API
25 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
26 public function __construct( $query, $moduleName ) {
27 parent::__construct( $query, $moduleName, 'ps' );
30 public function execute() {
31 $this->run();
34 public function executeGenerator( $resultPageSet ) {
35 $this->run( $resultPageSet );
38 /**
39 * @param ApiPageSet $resultPageSet
41 private function run( $resultPageSet = null ) {
42 $params = $this->extractRequestParams();
43 $search = $params['search'];
44 $limit = $params['limit'];
45 $namespaces = $params['namespace'];
46 $offset = $params['offset'];
48 $searcher = new TitlePrefixSearch;
49 $titles = $searcher->searchWithVariants( $search, $limit + 1, $namespaces, $offset );
50 if ( $resultPageSet ) {
51 if ( count( $titles ) > $limit ) {
52 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
53 array_pop( $titles );
55 $resultPageSet->populateFromTitles( $titles );
56 foreach ( $titles as $index => $title ) {
57 $resultPageSet->setGeneratorData( $title, array( 'index' => $index + $offset + 1 ) );
59 } else {
60 $result = $this->getResult();
61 $count = 0;
62 foreach ( $titles as $title ) {
63 if ( ++$count > $limit ) {
64 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
65 break;
67 $vals = array(
68 'ns' => intval( $title->getNamespace() ),
69 'title' => $title->getPrefixedText(),
71 if ( $title->isSpecialPage() ) {
72 $vals['special'] = '';
73 } else {
74 $vals['pageid'] = intval( $title->getArticleId() );
76 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
77 if ( !$fit ) {
78 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
79 break;
82 $result->setIndexedTagName_internal(
83 array( 'query', $this->getModuleName() ), $this->getModulePrefix()
88 public function getCacheMode( $params ) {
89 return 'public';
92 public function getAllowedParams() {
93 return array(
94 'search' => array(
95 ApiBase::PARAM_TYPE => 'string',
96 ApiBase::PARAM_REQUIRED => true,
98 'namespace' => array(
99 ApiBase::PARAM_DFLT => NS_MAIN,
100 ApiBase::PARAM_TYPE => 'namespace',
101 ApiBase::PARAM_ISMULTI => true,
103 'limit' => array(
104 ApiBase::PARAM_DFLT => 10,
105 ApiBase::PARAM_TYPE => 'limit',
106 ApiBase::PARAM_MIN => 1,
107 // Non-standard value for compatibility with action=opensearch
108 ApiBase::PARAM_MAX => 100,
109 ApiBase::PARAM_MAX2 => 200,
111 'offset' => array(
112 ApiBase::PARAM_DFLT => 0,
113 ApiBase::PARAM_TYPE => 'integer',
118 protected function getExamplesMessages() {
119 return array(
120 'action=query&list=prefixsearch&pssearch=meaning'
121 => 'apihelp-query+prefixsearch-example-simple',
125 public function getHelpUrls() {
126 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';