Merge "resourceloader: Reduce module_deps write slams after deployments"
[lhc/web/wiklou.git] / includes / api / ApiQueryPrefixSearch.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @since 1.23
20 */
21
22 /**
23 * @ingroup API
24 */
25 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
26 public function __construct( $query, $moduleName ) {
27 parent::__construct( $query, $moduleName, 'ps' );
28 }
29
30 public function execute() {
31 $this->run();
32 }
33
34 public function executeGenerator( $resultPageSet ) {
35 $this->run( $resultPageSet );
36 }
37
38 /**
39 * @param ApiPageSet $resultPageSet
40 */
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'];
47
48 $searchEngine = SearchEngine::create();
49 $searchEngine->setLimitOffset( $limit + 1, $offset );
50 $searchEngine->setNamespaces( $namespaces );
51 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
52
53 if ( $resultPageSet ) {
54 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
55 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
56 $current['index'] = $new['index'];
57 }
58 return $current;
59 } );
60 if ( count( $titles ) > $limit ) {
61 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
62 array_pop( $titles );
63 }
64 $resultPageSet->populateFromTitles( $titles );
65 foreach ( $titles as $index => $title ) {
66 $resultPageSet->setGeneratorData( $title, array( 'index' => $index + $offset + 1 ) );
67 }
68 } else {
69 $result = $this->getResult();
70 $count = 0;
71 foreach ( $titles as $title ) {
72 if ( ++$count > $limit ) {
73 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
74 break;
75 }
76 $vals = array(
77 'ns' => intval( $title->getNamespace() ),
78 'title' => $title->getPrefixedText(),
79 );
80 if ( $title->isSpecialPage() ) {
81 $vals['special'] = true;
82 } else {
83 $vals['pageid'] = intval( $title->getArticleId() );
84 }
85 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
86 if ( !$fit ) {
87 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
88 break;
89 }
90 }
91 $result->addIndexedTagName(
92 array( 'query', $this->getModuleName() ), $this->getModulePrefix()
93 );
94 }
95 }
96
97 public function getCacheMode( $params ) {
98 return 'public';
99 }
100
101 public function getAllowedParams() {
102 return array(
103 'search' => array(
104 ApiBase::PARAM_TYPE => 'string',
105 ApiBase::PARAM_REQUIRED => true,
106 ),
107 'namespace' => array(
108 ApiBase::PARAM_DFLT => NS_MAIN,
109 ApiBase::PARAM_TYPE => 'namespace',
110 ApiBase::PARAM_ISMULTI => true,
111 ),
112 'limit' => array(
113 ApiBase::PARAM_DFLT => 10,
114 ApiBase::PARAM_TYPE => 'limit',
115 ApiBase::PARAM_MIN => 1,
116 // Non-standard value for compatibility with action=opensearch
117 ApiBase::PARAM_MAX => 100,
118 ApiBase::PARAM_MAX2 => 200,
119 ),
120 'offset' => array(
121 ApiBase::PARAM_DFLT => 0,
122 ApiBase::PARAM_TYPE => 'integer',
123 ),
124 );
125 }
126
127 protected function getExamplesMessages() {
128 return array(
129 'action=query&list=prefixsearch&pssearch=meaning'
130 => 'apihelp-query+prefixsearch-example-simple',
131 );
132 }
133
134 public function getHelpUrls() {
135 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
136 }
137 }