Merge "Displaying search results for multiple wikis"
[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 $searcher = new TitlePrefixSearch;
49 $titles = $searcher->searchWithVariants( $search, $limit + 1, $namespaces, $offset );
50 if ( $resultPageSet ) {
51 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
52 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
53 $current['index'] = $new['index'];
54 }
55 return $current;
56 } );
57 if ( count( $titles ) > $limit ) {
58 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
59 array_pop( $titles );
60 }
61 $resultPageSet->populateFromTitles( $titles );
62 foreach ( $titles as $index => $title ) {
63 $resultPageSet->setGeneratorData( $title, array( 'index' => $index + $offset + 1 ) );
64 }
65 } else {
66 $result = $this->getResult();
67 $count = 0;
68 foreach ( $titles as $title ) {
69 if ( ++$count > $limit ) {
70 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
71 break;
72 }
73 $vals = array(
74 'ns' => intval( $title->getNamespace() ),
75 'title' => $title->getPrefixedText(),
76 );
77 if ( $title->isSpecialPage() ) {
78 $vals['special'] = true;
79 } else {
80 $vals['pageid'] = intval( $title->getArticleId() );
81 }
82 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
83 if ( !$fit ) {
84 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
85 break;
86 }
87 }
88 $result->addIndexedTagName(
89 array( 'query', $this->getModuleName() ), $this->getModulePrefix()
90 );
91 }
92 }
93
94 public function getCacheMode( $params ) {
95 return 'public';
96 }
97
98 public function getAllowedParams() {
99 return array(
100 'search' => array(
101 ApiBase::PARAM_TYPE => 'string',
102 ApiBase::PARAM_REQUIRED => true,
103 ),
104 'namespace' => array(
105 ApiBase::PARAM_DFLT => NS_MAIN,
106 ApiBase::PARAM_TYPE => 'namespace',
107 ApiBase::PARAM_ISMULTI => true,
108 ),
109 'limit' => array(
110 ApiBase::PARAM_DFLT => 10,
111 ApiBase::PARAM_TYPE => 'limit',
112 ApiBase::PARAM_MIN => 1,
113 // Non-standard value for compatibility with action=opensearch
114 ApiBase::PARAM_MAX => 100,
115 ApiBase::PARAM_MAX2 => 200,
116 ),
117 'offset' => array(
118 ApiBase::PARAM_DFLT => 0,
119 ApiBase::PARAM_TYPE => 'integer',
120 ),
121 );
122 }
123
124 protected function getExamplesMessages() {
125 return array(
126 'action=query&list=prefixsearch&pssearch=meaning'
127 => 'apihelp-query+prefixsearch-example-simple',
128 );
129 }
130
131 public function getHelpUrls() {
132 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
133 }
134 }