Remove score display from search engine
[mediawiki.git] / includes / api / ApiQuerySearch.php
blob5ed587334606d7e434f19f23d7f190add35344bc
1 <?php
2 /**
5 * Created on July 30, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query module to perform full text search within wiki titles and content
30 * @ingroup API
32 class ApiQuerySearch extends ApiQueryGeneratorBase {
34 /**
35 * When $wgSearchType is null, $wgSearchAlternatives[0] is null. Null isn't
36 * a valid option for an array for PARAM_TYPE, so we'll use a fake name
37 * that can't possibly be a class name and describes what the null behavior
38 * does
40 const BACKEND_NULL_PARAM = 'database-backed';
42 public function __construct( ApiQuery $query, $moduleName ) {
43 parent::__construct( $query, $moduleName, 'sr' );
46 public function execute() {
47 $this->run();
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
54 /**
55 * @param ApiPageSet $resultPageSet
56 * @return void
58 private function run( $resultPageSet = null ) {
59 global $wgContLang;
60 $params = $this->extractRequestParams();
62 // Extract parameters
63 $limit = $params['limit'];
64 $query = $params['search'];
65 $what = $params['what'];
66 $interwiki = $params['interwiki'];
67 $searchInfo = array_flip( $params['info'] );
68 $prop = array_flip( $params['prop'] );
70 // Create search engine instance and set options
71 $search = isset( $params['backend'] ) && $params['backend'] != self::BACKEND_NULL_PARAM ?
72 SearchEngine::create( $params['backend'] ) : SearchEngine::create();
73 $search->setLimitOffset( $limit + 1, $params['offset'] );
74 $search->setNamespaces( $params['namespace'] );
76 $query = $search->transformSearchTerm( $query );
77 $query = $search->replacePrefixes( $query );
79 // Perform the actual search
80 if ( $what == 'text' ) {
81 $matches = $search->searchText( $query );
82 } elseif ( $what == 'title' ) {
83 $matches = $search->searchTitle( $query );
84 } elseif ( $what == 'nearmatch' ) {
85 $matches = SearchEngine::getNearMatchResultSet( $query );
86 } else {
87 // We default to title searches; this is a terrible legacy
88 // of the way we initially set up the MySQL fulltext-based
89 // search engine with separate title and text fields.
90 // In the future, the default should be for a combined index.
91 $what = 'title';
92 $matches = $search->searchTitle( $query );
94 // Not all search engines support a separate title search,
95 // for instance the Lucene-based engine we use on Wikipedia.
96 // In this case, fall back to full-text search (which will
97 // include titles in it!)
98 if ( is_null( $matches ) ) {
99 $what = 'text';
100 $matches = $search->searchText( $query );
103 if ( is_null( $matches ) ) {
104 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
105 } elseif ( $matches instanceof Status && !$matches->isGood() ) {
106 $this->dieUsage( $matches->getWikiText(), 'search-error' );
109 $apiResult = $this->getResult();
110 // Add search meta data to result
111 if ( isset( $searchInfo['totalhits'] ) ) {
112 $totalhits = $matches->getTotalHits();
113 if ( $totalhits !== null ) {
114 $apiResult->addValue( array( 'query', 'searchinfo' ),
115 'totalhits', $totalhits );
118 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
119 $apiResult->addValue( array( 'query', 'searchinfo' ),
120 'suggestion', $matches->getSuggestionQuery() );
123 // Add the search results to the result
124 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
125 $titles = array();
126 $count = 0;
127 $result = $matches->next();
129 while ( $result ) {
130 if ( ++$count > $limit ) {
131 // We've reached the one extra which shows that there are
132 // additional items to be had. Stop here...
133 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
134 break;
137 // Silently skip broken and missing titles
138 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
139 $result = $matches->next();
140 continue;
143 $title = $result->getTitle();
144 if ( is_null( $resultPageSet ) ) {
145 $vals = array();
146 ApiQueryBase::addTitleInfo( $vals, $title );
148 if ( isset( $prop['snippet'] ) ) {
149 $vals['snippet'] = $result->getTextSnippet( $terms );
151 if ( isset( $prop['size'] ) ) {
152 $vals['size'] = $result->getByteSize();
154 if ( isset( $prop['wordcount'] ) ) {
155 $vals['wordcount'] = $result->getWordCount();
157 if ( isset( $prop['timestamp'] ) ) {
158 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
160 if ( isset( $prop['titlesnippet'] ) ) {
161 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
163 if ( !is_null( $result->getRedirectTitle() ) ) {
164 if ( isset( $prop['redirecttitle'] ) ) {
165 $vals['redirecttitle'] = $result->getRedirectTitle();
167 if ( isset( $prop['redirectsnippet'] ) ) {
168 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
171 if ( !is_null( $result->getSectionTitle() ) ) {
172 if ( isset( $prop['sectiontitle'] ) ) {
173 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
175 if ( isset( $prop['sectionsnippet'] ) ) {
176 $vals['sectionsnippet'] = $result->getSectionSnippet();
179 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
180 $vals['hasrelated'] = '';
183 // Add item to results and see whether it fits
184 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
185 null, $vals );
186 if ( !$fit ) {
187 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
188 break;
190 } else {
191 $titles[] = $title;
194 $result = $matches->next();
197 $hasInterwikiResults = false;
198 if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) {
199 $matches = $matches->getInterwikiResults();
200 $iwprefixes = array();
201 $hasInterwikiResults = true;
203 // Include number of results if requested
204 if ( isset( $searchInfo['totalhits'] ) ) {
205 $totalhits = $matches->getTotalHits();
206 if ( $totalhits !== null ) {
207 $apiResult->addValue( array( 'query', 'interwikisearchinfo' ),
208 'totalhits', $totalhits );
212 $result = $matches->next();
213 while ( $result ) {
214 $title = $result->getTitle();
215 $vals = array(
216 'namespace' => $result->getInterwikiNamespaceText(),
217 'title' => $title->getText(),
218 'url' => $title->getFullUrl(),
221 // Add item to results and see whether it fits
222 $fit = $apiResult->addValue(
223 array( 'query', 'interwiki' . $this->getModuleName(), $result->getInterwikiPrefix() ),
224 null,
225 $vals
228 if ( !$fit ) {
229 // We hit the limit. We can't really provide any meaningful
230 // pagination info so just bail out
231 break;
234 $result = $matches->next();
238 if ( is_null( $resultPageSet ) ) {
239 $apiResult->setIndexedTagName_internal( array(
240 'query', $this->getModuleName()
241 ), 'p' );
242 if ( $hasInterwikiResults ) {
243 $apiResult->setIndexedTagName_internal( array(
244 'query', 'interwiki' . $this->getModuleName()
245 ), 'p' );
247 } else {
248 $resultPageSet->populateFromTitles( $titles );
252 public function getCacheMode( $params ) {
253 return 'public';
256 public function getAllowedParams() {
257 $params = array(
258 'search' => array(
259 ApiBase::PARAM_TYPE => 'string',
260 ApiBase::PARAM_REQUIRED => true
262 'namespace' => array(
263 ApiBase::PARAM_DFLT => NS_MAIN,
264 ApiBase::PARAM_TYPE => 'namespace',
265 ApiBase::PARAM_ISMULTI => true,
267 'what' => array(
268 ApiBase::PARAM_DFLT => null,
269 ApiBase::PARAM_TYPE => array(
270 'title',
271 'text',
272 'nearmatch',
275 'info' => array(
276 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
277 ApiBase::PARAM_TYPE => array(
278 'totalhits',
279 'suggestion',
281 ApiBase::PARAM_ISMULTI => true,
283 'prop' => array(
284 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
285 ApiBase::PARAM_TYPE => array(
286 'size',
287 'wordcount',
288 'timestamp',
289 'score',
290 'snippet',
291 'titlesnippet',
292 'redirecttitle',
293 'redirectsnippet',
294 'sectiontitle',
295 'sectionsnippet',
296 'hasrelated',
298 ApiBase::PARAM_ISMULTI => true,
300 'offset' => 0,
301 'limit' => array(
302 ApiBase::PARAM_DFLT => 10,
303 ApiBase::PARAM_TYPE => 'limit',
304 ApiBase::PARAM_MIN => 1,
305 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
306 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
308 'interwiki' => false,
311 $alternatives = SearchEngine::getSearchTypes();
312 if ( count( $alternatives ) > 1 ) {
313 if ( $alternatives[0] === null ) {
314 $alternatives[0] = self::BACKEND_NULL_PARAM;
316 $params['backend'] = array(
317 ApiBase::PARAM_DFLT => $this->getConfig()->get( 'SearchType' ),
318 ApiBase::PARAM_TYPE => $alternatives,
322 return $params;
325 public function getParamDescription() {
326 $descriptions = array(
327 'search' => 'Search for all page titles (or content) that has this value',
328 'namespace' => 'The namespace(s) to enumerate',
329 'what' => 'Search inside the text or titles',
330 'info' => 'What metadata to return',
331 'prop' => array(
332 'What properties to return',
333 ' size - Adds the size of the page in bytes',
334 ' wordcount - Adds the word count of the page',
335 ' timestamp - Adds the timestamp of when the page was last edited',
336 ' score - Adds the score (if any) from the search engine',
337 ' snippet - Adds a parsed snippet of the page',
338 ' titlesnippet - Adds a parsed snippet of the page title',
339 ' redirectsnippet - Adds a parsed snippet of the redirect title',
340 ' redirecttitle - Adds the title of the matching redirect',
341 ' sectionsnippet - Adds a parsed snippet of the matching section title',
342 ' sectiontitle - Adds the title of the matching section',
343 ' hasrelated - Indicates whether a related search is available',
345 'offset' => 'Use this value to continue paging (return by query)',
346 'limit' => 'How many total pages to return',
347 'interwiki' => 'Include interwiki results in the search, if available'
350 if ( count( SearchEngine::getSearchTypes() ) > 1 ) {
351 $descriptions['backend'] = 'Which search backend to use, if not the default';
354 return $descriptions;
357 public function getResultProperties() {
358 return array(
359 '' => array(
360 'ns' => 'namespace',
361 'title' => 'string'
363 'snippet' => array(
364 'snippet' => 'string'
366 'size' => array(
367 'size' => 'integer'
369 'wordcount' => array(
370 'wordcount' => 'integer'
372 'timestamp' => array(
373 'timestamp' => 'timestamp'
375 'score' => array(
376 'score' => array(
377 ApiBase::PROP_TYPE => 'string',
378 ApiBase::PROP_NULLABLE => true
381 'titlesnippet' => array(
382 'titlesnippet' => 'string'
384 'redirecttitle' => array(
385 'redirecttitle' => array(
386 ApiBase::PROP_TYPE => 'string',
387 ApiBase::PROP_NULLABLE => true
390 'redirectsnippet' => array(
391 'redirectsnippet' => array(
392 ApiBase::PROP_TYPE => 'string',
393 ApiBase::PROP_NULLABLE => true
396 'sectiontitle' => array(
397 'sectiontitle' => array(
398 ApiBase::PROP_TYPE => 'string',
399 ApiBase::PROP_NULLABLE => true
402 'sectionsnippet' => array(
403 'sectionsnippet' => array(
404 ApiBase::PROP_TYPE => 'string',
405 ApiBase::PROP_NULLABLE => true
408 'hasrelated' => array(
409 'hasrelated' => 'boolean'
414 public function getDescription() {
415 return 'Perform a full text search.';
418 public function getPossibleErrors() {
419 return array_merge( parent::getPossibleErrors(), array(
420 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
421 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
422 array( 'code' => 'search-error', 'info' => 'search error has occurred' ),
423 ) );
426 public function getExamples() {
427 return array(
428 'api.php?action=query&list=search&srsearch=meaning',
429 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
430 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
434 public function getHelpUrls() {
435 return 'https://www.mediawiki.org/wiki/API:Search';