95f84839a3e90f58d2a6bdb17baef3c73c647f39
[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
47 $searcher = new TitlePrefixSearch;
48 $titles = $searcher->searchWithVariants( $search, $limit, $namespaces );
49 if ( $resultPageSet ) {
50 $resultPageSet->populateFromTitles( $titles );
51 /** @todo If this module gets an 'offset' parameter, use it here */
52 $offset = 1;
53 foreach ( $titles as $index => $title ) {
54 $resultPageSet->setGeneratorData( $title, array( 'index' => $index + $offset ) );
55 }
56 } else {
57 $result = $this->getResult();
58 foreach ( $titles as $title ) {
59 if ( !$limit-- ) {
60 break;
61 }
62 $vals = array(
63 'ns' => intval( $title->getNamespace() ),
64 'title' => $title->getPrefixedText(),
65 );
66 if ( $title->isSpecialPage() ) {
67 $vals['special'] = '';
68 } else {
69 $vals['pageid'] = intval( $title->getArticleId() );
70 }
71 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
72 if ( !$fit ) {
73 break;
74 }
75 }
76 $result->setIndexedTagName_internal(
77 array( 'query', $this->getModuleName() ), $this->getModulePrefix()
78 );
79 }
80 }
81
82 public function getCacheMode( $params ) {
83 return 'public';
84 }
85
86 public function getAllowedParams() {
87 return array(
88 'search' => array(
89 ApiBase::PARAM_TYPE => 'string',
90 ApiBase::PARAM_REQUIRED => true,
91 ),
92 'namespace' => array(
93 ApiBase::PARAM_DFLT => NS_MAIN,
94 ApiBase::PARAM_TYPE => 'namespace',
95 ApiBase::PARAM_ISMULTI => true,
96 ),
97 'limit' => array(
98 ApiBase::PARAM_DFLT => 10,
99 ApiBase::PARAM_TYPE => 'limit',
100 ApiBase::PARAM_MIN => 1,
101 // Non-standard value for compatibility with action=opensearch
102 ApiBase::PARAM_MAX => 100,
103 ApiBase::PARAM_MAX2 => 200,
104 ),
105 );
106 }
107
108 protected function getExamplesMessages() {
109 return array(
110 'action=query&list=prefixsearch&pssearch=meaning'
111 => 'apihelp-query+prefixsearch-example-simple',
112 );
113 }
114
115 public function getHelpUrls() {
116 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
117 }
118 }