Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / api / ApiQueryPrefixSearch.php
1 <?php
2
3 /**
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.
8 *
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.
13 *
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
18 *
19 * @file
20 * @since 1.23
21 */
22
23 /**
24 * @ingroup API
25 */
26 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
27 use SearchApi;
28
29 /** @var array list of api allowed params */
30 private $allowedParams;
31
32 public function __construct( $query, $moduleName ) {
33 parent::__construct( $query, $moduleName, 'ps' );
34 }
35
36 public function execute() {
37 $this->run();
38 }
39
40 public function executeGenerator( $resultPageSet ) {
41 $this->run( $resultPageSet );
42 }
43
44 /**
45 * @param ApiPageSet $resultPageSet
46 */
47 private function run( $resultPageSet = null ) {
48 $params = $this->extractRequestParams();
49 $search = $params['search'];
50 $limit = $params['limit'];
51 $offset = $params['offset'];
52
53 $searchEngine = $this->buildSearchEngine( $params );
54 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
55
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'];
60 }
61 return $current;
62 } );
63 if ( count( $titles ) > $limit ) {
64 $this->setContinueEnumParameter( 'offset', $offset + $limit );
65 array_pop( $titles );
66 }
67 $resultPageSet->populateFromTitles( $titles );
68 foreach ( $titles as $index => $title ) {
69 $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
70 }
71 } else {
72 $result = $this->getResult();
73 $count = 0;
74 foreach ( $titles as $title ) {
75 if ( ++$count > $limit ) {
76 $this->setContinueEnumParameter( 'offset', $offset + $limit );
77 break;
78 }
79 $vals = [
80 'ns' => intval( $title->getNamespace() ),
81 'title' => $title->getPrefixedText(),
82 ];
83 if ( $title->isSpecialPage() ) {
84 $vals['special'] = true;
85 } else {
86 $vals['pageid'] = intval( $title->getArticleID() );
87 }
88 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
89 if ( !$fit ) {
90 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
91 break;
92 }
93 }
94 $result->addIndexedTagName(
95 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
96 );
97 }
98 }
99
100 public function getCacheMode( $params ) {
101 return 'public';
102 }
103
104 public function getAllowedParams() {
105 if ( $this->allowedParams !== null ) {
106 return $this->allowedParams;
107 }
108 $this->allowedParams = $this->buildCommonApiParams();
109
110 return $this->allowedParams;
111 }
112
113 public function getSearchProfileParams() {
114 return [
115 'profile' => [
116 'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
117 'help-message' => 'apihelp-query+prefixsearch-param-profile',
118 ],
119 ];
120 }
121
122 protected function getExamplesMessages() {
123 return [
124 'action=query&list=prefixsearch&pssearch=meaning'
125 => 'apihelp-query+prefixsearch-example-simple',
126 ];
127 }
128
129 public function getHelpUrls() {
130 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
131 }
132 }