Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to perform full text search within wiki titles and content
29 *
30 * @ingroup API
31 */
32 class ApiQuerySearch extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'sr' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
49 */
50 private function run( $resultPageSet = null ) {
51 global $wgContLang;
52 $params = $this->extractRequestParams();
53
54 // Extract parameters
55 $limit = $params['limit'];
56 $query = $params['search'];
57 $what = $params['what'];
58 $searchInfo = array_flip( $params['info'] );
59 $prop = array_flip( $params['prop'] );
60
61 // Create search engine instance and set options
62 $search = SearchEngine::create();
63 $search->setLimitOffset( $limit + 1, $params['offset'] );
64 $search->setNamespaces( $params['namespace'] );
65 $search->showRedirects = $params['redirects'];
66
67 $query = $search->transformSearchTerm( $query );
68 $query = $search->replacePrefixes( $query );
69
70 // Perform the actual search
71 if ( $what == 'text' ) {
72 $matches = $search->searchText( $query );
73 } elseif ( $what == 'title' ) {
74 $matches = $search->searchTitle( $query );
75 } elseif ( $what == 'nearmatch' ) {
76 $matches = SearchEngine::getNearMatchResultSet( $query );
77 } else {
78 // We default to title searches; this is a terrible legacy
79 // of the way we initially set up the MySQL fulltext-based
80 // search engine with separate title and text fields.
81 // In the future, the default should be for a combined index.
82 $what = 'title';
83 $matches = $search->searchTitle( $query );
84
85 // Not all search engines support a separate title search,
86 // for instance the Lucene-based engine we use on Wikipedia.
87 // In this case, fall back to full-text search (which will
88 // include titles in it!)
89 if ( is_null( $matches ) ) {
90 $what = 'text';
91 $matches = $search->searchText( $query );
92 }
93 }
94 if ( is_null( $matches ) ) {
95 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
96 } elseif ( $matches instanceof Status && !$matches->isGood() ) {
97 $this->dieUsage( $matches->getWikiText(), 'search-error' );
98 }
99
100 $apiResult = $this->getResult();
101 // Add search meta data to result
102 if ( isset( $searchInfo['totalhits'] ) ) {
103 $totalhits = $matches->getTotalHits();
104 if ( $totalhits !== null ) {
105 $apiResult->addValue( array( 'query', 'searchinfo' ),
106 'totalhits', $totalhits );
107 }
108 }
109 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
110 $apiResult->addValue( array( 'query', 'searchinfo' ),
111 'suggestion', $matches->getSuggestionQuery() );
112 }
113
114 // Add the search results to the result
115 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
116 $titles = array();
117 $count = 0;
118 $result = $matches->next();
119
120 while ( $result ) {
121 if ( ++ $count > $limit ) {
122 // We've reached the one extra which shows that there are additional items to be had. Stop here...
123 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
124 break;
125 }
126
127 // Silently skip broken and missing titles
128 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
129 $result = $matches->next();
130 continue;
131 }
132
133 $title = $result->getTitle();
134 if ( is_null( $resultPageSet ) ) {
135 $vals = array();
136 ApiQueryBase::addTitleInfo( $vals, $title );
137
138 if ( isset( $prop['snippet'] ) ) {
139 $vals['snippet'] = $result->getTextSnippet( $terms );
140 }
141 if ( isset( $prop['size'] ) ) {
142 $vals['size'] = $result->getByteSize();
143 }
144 if ( isset( $prop['wordcount'] ) ) {
145 $vals['wordcount'] = $result->getWordCount();
146 }
147 if ( isset( $prop['timestamp'] ) ) {
148 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
149 }
150 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
151 $vals['score'] = $result->getScore();
152 }
153 if ( isset( $prop['titlesnippet'] ) ) {
154 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
155 }
156 if ( !is_null( $result->getRedirectTitle() ) ) {
157 if ( isset( $prop['redirecttitle'] ) ) {
158 $vals['redirecttitle'] = $result->getRedirectTitle();
159 }
160 if ( isset( $prop['redirectsnippet'] ) ) {
161 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
162 }
163 }
164 if ( !is_null( $result->getSectionTitle() ) ) {
165 if ( isset( $prop['sectiontitle'] ) ) {
166 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
167 }
168 if ( isset( $prop['sectionsnippet'] ) ) {
169 $vals['sectionsnippet'] = $result->getSectionSnippet();
170 }
171 }
172 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
173 $vals['hasrelated'] = '';
174 }
175
176 // Add item to results and see whether it fits
177 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
178 null, $vals );
179 if ( !$fit ) {
180 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
181 break;
182 }
183 } else {
184 $titles[] = $title;
185 }
186
187 $result = $matches->next();
188 }
189
190 if ( is_null( $resultPageSet ) ) {
191 $apiResult->setIndexedTagName_internal( array(
192 'query', $this->getModuleName()
193 ), 'p' );
194 } else {
195 $resultPageSet->populateFromTitles( $titles );
196 }
197 }
198
199 public function getCacheMode( $params ) {
200 return 'public';
201 }
202
203 public function getAllowedParams() {
204 return array(
205 'search' => array(
206 ApiBase::PARAM_TYPE => 'string',
207 ApiBase::PARAM_REQUIRED => true
208 ),
209 'namespace' => array(
210 ApiBase::PARAM_DFLT => NS_MAIN,
211 ApiBase::PARAM_TYPE => 'namespace',
212 ApiBase::PARAM_ISMULTI => true,
213 ),
214 'what' => array(
215 ApiBase::PARAM_DFLT => null,
216 ApiBase::PARAM_TYPE => array(
217 'title',
218 'text',
219 'nearmatch',
220 )
221 ),
222 'info' => array(
223 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
224 ApiBase::PARAM_TYPE => array(
225 'totalhits',
226 'suggestion',
227 ),
228 ApiBase::PARAM_ISMULTI => true,
229 ),
230 'prop' => array(
231 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
232 ApiBase::PARAM_TYPE => array(
233 'size',
234 'wordcount',
235 'timestamp',
236 'score',
237 'snippet',
238 'titlesnippet',
239 'redirecttitle',
240 'redirectsnippet',
241 'sectiontitle',
242 'sectionsnippet',
243 'hasrelated',
244 ),
245 ApiBase::PARAM_ISMULTI => true,
246 ),
247 'redirects' => false,
248 'offset' => 0,
249 'limit' => array(
250 ApiBase::PARAM_DFLT => 10,
251 ApiBase::PARAM_TYPE => 'limit',
252 ApiBase::PARAM_MIN => 1,
253 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
254 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
255 )
256 );
257 }
258
259 public function getParamDescription() {
260 return array(
261 'search' => 'Search for all page titles (or content) that has this value',
262 'namespace' => 'The namespace(s) to enumerate',
263 'what' => 'Search inside the text or titles',
264 'info' => 'What metadata to return',
265 'prop' => array(
266 'What properties to return',
267 ' size - Adds the size of the page in bytes',
268 ' wordcount - Adds the word count of the page',
269 ' timestamp - Adds the timestamp of when the page was last edited',
270 ' score - Adds the score (if any) from the search engine',
271 ' snippet - Adds a parsed snippet of the page',
272 ' titlesnippet - Adds a parsed snippet of the page title',
273 ' redirectsnippet - Adds a parsed snippet of the redirect title',
274 ' redirecttitle - Adds the title of the matching redirect',
275 ' sectionsnippet - Adds a parsed snippet of the matching section title',
276 ' sectiontitle - Adds the title of the matching section',
277 ' hasrelated - Indicates whether a related search is available',
278 ),
279 'redirects' => 'Include redirect pages in the search',
280 'offset' => 'Use this value to continue paging (return by query)',
281 'limit' => 'How many total pages to return'
282 );
283 }
284
285 public function getResultProperties() {
286 return array(
287 '' => array(
288 'ns' => 'namespace',
289 'title' => 'string'
290 ),
291 'snippet' => array(
292 'snippet' => 'string'
293 ),
294 'size' => array(
295 'size' => 'integer'
296 ),
297 'wordcount' => array(
298 'wordcount' => 'integer'
299 ),
300 'timestamp' => array(
301 'timestamp' => 'timestamp'
302 ),
303 'score' => array(
304 'score' => array(
305 ApiBase::PROP_TYPE => 'string',
306 ApiBase::PROP_NULLABLE => true
307 )
308 ),
309 'titlesnippet' => array(
310 'titlesnippet' => 'string'
311 ),
312 'redirecttitle' => array(
313 'redirecttitle' => array(
314 ApiBase::PROP_TYPE => 'string',
315 ApiBase::PROP_NULLABLE => true
316 )
317 ),
318 'redirectsnippet' => array(
319 'redirectsnippet' => array(
320 ApiBase::PROP_TYPE => 'string',
321 ApiBase::PROP_NULLABLE => true
322 )
323 ),
324 'sectiontitle' => array(
325 'sectiontitle' => array(
326 ApiBase::PROP_TYPE => 'string',
327 ApiBase::PROP_NULLABLE => true
328 )
329 ),
330 'sectionsnippet' => array(
331 'sectionsnippet' => array(
332 ApiBase::PROP_TYPE => 'string',
333 ApiBase::PROP_NULLABLE => true
334 )
335 ),
336 'hasrelated' => array(
337 'hasrelated' => 'boolean'
338 )
339 );
340 }
341
342 public function getDescription() {
343 return 'Perform a full text search';
344 }
345
346 public function getPossibleErrors() {
347 return array_merge( parent::getPossibleErrors(), array(
348 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
349 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
350 array( 'code' => 'search-error', 'info' => 'search error has occurred' ),
351 ) );
352 }
353
354 public function getExamples() {
355 return array(
356 'api.php?action=query&list=search&srsearch=meaning',
357 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
358 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
359 );
360 }
361
362 public function getHelpUrls() {
363 return 'https://www.mediawiki.org/wiki/API:Search';
364 }
365 }