Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiQuerySearch.php
blob861833919b235463c8d011e3134804bcd3b2af77
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 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'sr' );
38 public function execute() {
39 $this->run();
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
50 private function run( $resultPageSet = null ) {
51 global $wgContLang;
52 $params = $this->extractRequestParams();
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'] );
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'];
67 $query = $search->transformSearchTerm( $query );
68 $query = $search->replacePrefixes( $query );
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 );
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 );
94 if ( is_null( $matches ) ) {
95 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
98 $apiResult = $this->getResult();
99 // Add search meta data to result
100 if ( isset( $searchInfo['totalhits'] ) ) {
101 $totalhits = $matches->getTotalHits();
102 if ( $totalhits !== null ) {
103 $apiResult->addValue( array( 'query', 'searchinfo' ),
104 'totalhits', $totalhits );
107 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
108 $apiResult->addValue( array( 'query', 'searchinfo' ),
109 'suggestion', $matches->getSuggestionQuery() );
112 // Add the search results to the result
113 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
114 $titles = array();
115 $count = 0;
116 $result = $matches->next();
118 while ( $result ) {
119 if ( ++ $count > $limit ) {
120 // We've reached the one extra which shows that there are additional items to be had. Stop here...
121 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
122 break;
125 // Silently skip broken and missing titles
126 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
127 $result = $matches->next();
128 continue;
131 $title = $result->getTitle();
132 if ( is_null( $resultPageSet ) ) {
133 $vals = array();
134 ApiQueryBase::addTitleInfo( $vals, $title );
136 if ( isset( $prop['snippet'] ) ) {
137 $vals['snippet'] = $result->getTextSnippet( $terms );
139 if ( isset( $prop['size'] ) ) {
140 $vals['size'] = $result->getByteSize();
142 if ( isset( $prop['wordcount'] ) ) {
143 $vals['wordcount'] = $result->getWordCount();
145 if ( isset( $prop['timestamp'] ) ) {
146 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
148 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
149 $vals['score'] = $result->getScore();
151 if ( isset( $prop['titlesnippet'] ) ) {
152 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
154 if ( !is_null( $result->getRedirectTitle() ) ) {
155 if ( isset( $prop['redirecttitle'] ) ) {
156 $vals['redirecttitle'] = $result->getRedirectTitle();
158 if ( isset( $prop['redirectsnippet'] ) ) {
159 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
162 if ( !is_null( $result->getSectionTitle() ) ) {
163 if ( isset( $prop['sectiontitle'] ) ) {
164 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
166 if ( isset( $prop['sectionsnippet'] ) ) {
167 $vals['sectionsnippet'] = $result->getSectionSnippet();
170 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
171 $vals['hasrelated'] = '';
174 // Add item to results and see whether it fits
175 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
176 null, $vals );
177 if ( !$fit ) {
178 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
179 break;
181 } else {
182 $titles[] = $title;
185 $result = $matches->next();
188 if ( is_null( $resultPageSet ) ) {
189 $apiResult->setIndexedTagName_internal( array(
190 'query', $this->getModuleName()
191 ), 'p' );
192 } else {
193 $resultPageSet->populateFromTitles( $titles );
197 public function getCacheMode( $params ) {
198 return 'public';
201 public function getAllowedParams() {
202 return array(
203 'search' => array(
204 ApiBase::PARAM_TYPE => 'string',
205 ApiBase::PARAM_REQUIRED => true
207 'namespace' => array(
208 ApiBase::PARAM_DFLT => NS_MAIN,
209 ApiBase::PARAM_TYPE => 'namespace',
210 ApiBase::PARAM_ISMULTI => true,
212 'what' => array(
213 ApiBase::PARAM_DFLT => null,
214 ApiBase::PARAM_TYPE => array(
215 'title',
216 'text',
217 'nearmatch',
220 'info' => array(
221 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
222 ApiBase::PARAM_TYPE => array(
223 'totalhits',
224 'suggestion',
226 ApiBase::PARAM_ISMULTI => true,
228 'prop' => array(
229 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
230 ApiBase::PARAM_TYPE => array(
231 'size',
232 'wordcount',
233 'timestamp',
234 'score',
235 'snippet',
236 'titlesnippet',
237 'redirecttitle',
238 'redirectsnippet',
239 'sectiontitle',
240 'sectionsnippet',
241 'hasrelated',
243 ApiBase::PARAM_ISMULTI => true,
245 'redirects' => false,
246 'offset' => 0,
247 'limit' => array(
248 ApiBase::PARAM_DFLT => 10,
249 ApiBase::PARAM_TYPE => 'limit',
250 ApiBase::PARAM_MIN => 1,
251 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
252 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
257 public function getParamDescription() {
258 return array(
259 'search' => 'Search for all page titles (or content) that has this value',
260 'namespace' => 'The namespace(s) to enumerate',
261 'what' => 'Search inside the text or titles',
262 'info' => 'What metadata to return',
263 'prop' => array(
264 'What properties to return',
265 ' size - Adds the size of the page in bytes',
266 ' wordcount - Adds the word count of the page',
267 ' timestamp - Adds the timestamp of when the page was last edited',
268 ' score - Adds the score (if any) from the search engine',
269 ' snippet - Adds a parsed snippet of the page',
270 ' titlesnippet - Adds a parsed snippet of the page title',
271 ' redirectsnippet - Adds a parsed snippet of the redirect title',
272 ' redirecttitle - Adds the title of the matching redirect',
273 ' sectionsnippet - Adds a parsed snippet of the matching section title',
274 ' sectiontitle - Adds the title of the matching section',
275 ' hasrelated - Indicates whether a related search is available',
277 'redirects' => 'Include redirect pages in the search',
278 'offset' => 'Use this value to continue paging (return by query)',
279 'limit' => 'How many total pages to return'
283 public function getResultProperties() {
284 return array(
285 '' => array(
286 'ns' => 'namespace',
287 'title' => 'string'
289 'snippet' => array(
290 'snippet' => 'string'
292 'size' => array(
293 'size' => 'integer'
295 'wordcount' => array(
296 'wordcount' => 'integer'
298 'timestamp' => array(
299 'timestamp' => 'timestamp'
301 'score' => array(
302 'score' => array(
303 ApiBase::PROP_TYPE => 'string',
304 ApiBase::PROP_NULLABLE => true
307 'titlesnippet' => array(
308 'titlesnippet' => 'string'
310 'redirecttitle' => array(
311 'redirecttitle' => array(
312 ApiBase::PROP_TYPE => 'string',
313 ApiBase::PROP_NULLABLE => true
316 'redirectsnippet' => array(
317 'redirectsnippet' => array(
318 ApiBase::PROP_TYPE => 'string',
319 ApiBase::PROP_NULLABLE => true
322 'sectiontitle' => array(
323 'sectiontitle' => array(
324 ApiBase::PROP_TYPE => 'string',
325 ApiBase::PROP_NULLABLE => true
328 'sectionsnippet' => array(
329 'sectionsnippet' => array(
330 ApiBase::PROP_TYPE => 'string',
331 ApiBase::PROP_NULLABLE => true
334 'hasrelated' => array(
335 'hasrelated' => 'boolean'
340 public function getDescription() {
341 return 'Perform a full text search';
344 public function getPossibleErrors() {
345 return array_merge( parent::getPossibleErrors(), array(
346 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
347 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
348 ) );
351 public function getExamples() {
352 return array(
353 'api.php?action=query&list=search&srsearch=meaning',
354 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
355 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
359 public function getHelpUrls() {
360 return 'https://www.mediawiki.org/wiki/API:Search';