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
28 * Query module to perform full text search within wiki titles and content
32 class ApiQuerySearch
extends ApiQueryGeneratorBase
{
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
40 const BACKEND_NULL_PARAM
= 'database-backed';
42 public function __construct( ApiQuery
$query, $moduleName ) {
43 parent
::__construct( $query, $moduleName, 'sr' );
46 public function execute() {
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
55 * @param ApiPageSet $resultPageSet
58 private function run( $resultPageSet = null ) {
60 $params = $this->extractRequestParams();
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 // Deprecated parameters
71 if ( isset( $prop['hasrelated'] ) ) {
72 $this->logFeatureUsage( 'action=search&srprop=hasrelated' );
73 $this->setWarning( 'srprop=hasrelated has been deprecated' );
75 if ( isset( $prop['score'] ) ) {
76 $this->logFeatureUsage( 'action=search&srprop=score' );
77 $this->setWarning( 'srprop=score has been deprecated' );
80 // Create search engine instance and set options
81 $search = isset( $params['backend'] ) && $params['backend'] != self
::BACKEND_NULL_PARAM ?
82 SearchEngine
::create( $params['backend'] ) : SearchEngine
::create();
83 $search->setLimitOffset( $limit +
1, $params['offset'] );
84 $search->setNamespaces( $params['namespace'] );
86 $query = $search->transformSearchTerm( $query );
87 $query = $search->replacePrefixes( $query );
89 // Perform the actual search
90 if ( $what == 'text' ) {
91 $matches = $search->searchText( $query );
92 } elseif ( $what == 'title' ) {
93 $matches = $search->searchTitle( $query );
94 } elseif ( $what == 'nearmatch' ) {
95 // near matches must receive the user input as provided, otherwise
96 // the near matches within namespaces are lost.
97 $matches = SearchEngine
::getNearMatchResultSet( $params['search'] );
99 // We default to title searches; this is a terrible legacy
100 // of the way we initially set up the MySQL fulltext-based
101 // search engine with separate title and text fields.
102 // In the future, the default should be for a combined index.
104 $matches = $search->searchTitle( $query );
106 // Not all search engines support a separate title search,
107 // for instance the Lucene-based engine we use on Wikipedia.
108 // In this case, fall back to full-text search (which will
109 // include titles in it!)
110 if ( is_null( $matches ) ) {
112 $matches = $search->searchText( $query );
115 if ( is_null( $matches ) ) {
116 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
117 } elseif ( $matches instanceof Status
&& !$matches->isGood() ) {
118 $this->dieUsage( $matches->getWikiText(), 'search-error' );
121 if ( $resultPageSet === null ) {
122 $apiResult = $this->getResult();
123 // Add search meta data to result
124 if ( isset( $searchInfo['totalhits'] ) ) {
125 $totalhits = $matches->getTotalHits();
126 if ( $totalhits !== null ) {
127 $apiResult->addValue( array( 'query', 'searchinfo' ),
128 'totalhits', $totalhits );
131 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
132 $apiResult->addValue( array( 'query', 'searchinfo' ),
133 'suggestion', $matches->getSuggestionQuery() );
134 $apiResult->addValue( array( 'query', 'searchinfo' ),
135 'suggestionsnippet', $matches->getSuggestionSnippet() );
139 // Add the search results to the result
140 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
143 $result = $matches->next();
146 if ( ++
$count > $limit ) {
147 // We've reached the one extra which shows that there are
148 // additional items to be had. Stop here...
149 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
153 // Silently skip broken and missing titles
154 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
155 $result = $matches->next();
159 $title = $result->getTitle();
160 if ( $resultPageSet === null ) {
162 ApiQueryBase
::addTitleInfo( $vals, $title );
164 if ( isset( $prop['snippet'] ) ) {
165 $vals['snippet'] = $result->getTextSnippet( $terms );
167 if ( isset( $prop['size'] ) ) {
168 $vals['size'] = $result->getByteSize();
170 if ( isset( $prop['wordcount'] ) ) {
171 $vals['wordcount'] = $result->getWordCount();
173 if ( isset( $prop['timestamp'] ) ) {
174 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $result->getTimestamp() );
176 if ( isset( $prop['titlesnippet'] ) ) {
177 $vals['titlesnippet'] = $result->getTitleSnippet();
179 if ( isset( $prop['categorysnippet'] ) ) {
180 $vals['categorysnippet'] = $result->getCategorySnippet();
182 if ( !is_null( $result->getRedirectTitle() ) ) {
183 if ( isset( $prop['redirecttitle'] ) ) {
184 $vals['redirecttitle'] = $result->getRedirectTitle()->getPrefixedText();
186 if ( isset( $prop['redirectsnippet'] ) ) {
187 $vals['redirectsnippet'] = $result->getRedirectSnippet();
190 if ( !is_null( $result->getSectionTitle() ) ) {
191 if ( isset( $prop['sectiontitle'] ) ) {
192 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
194 if ( isset( $prop['sectionsnippet'] ) ) {
195 $vals['sectionsnippet'] = $result->getSectionSnippet();
198 if ( isset( $prop['isfilematch'] ) ) {
199 $vals['isfilematch'] = $result->isFileMatch();
202 // Add item to results and see whether it fits
203 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
206 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
213 $result = $matches->next();
216 $hasInterwikiResults = false;
218 if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) {
219 foreach ( $matches->getInterwikiResults() as $matches ) {
220 $matches = $matches->getInterwikiResults();
221 $hasInterwikiResults = true;
223 // Include number of results if requested
224 if ( $resultPageSet === null && isset( $searchInfo['totalhits'] ) ) {
225 $totalhits +
= $matches->getTotalHits();
228 $result = $matches->next();
230 $title = $result->getTitle();
232 if ( $resultPageSet === null ) {
234 'namespace' => $result->getInterwikiNamespaceText(),
235 'title' => $title->getText(),
236 'url' => $title->getFullUrl(),
239 // Add item to results and see whether it fits
240 $fit = $apiResult->addValue(
241 array( 'query', 'interwiki' . $this->getModuleName(), $result->getInterwikiPrefix() ),
247 // We hit the limit. We can't really provide any meaningful
248 // pagination info so just bail out
255 $result = $matches->next();
258 if ( $totalhits !== null ) {
259 $apiResult->addValue( array( 'query', 'interwikisearchinfo' ),
260 'totalhits', $totalhits );
264 if ( $resultPageSet === null ) {
265 $apiResult->addIndexedTagName( array(
266 'query', $this->getModuleName()
268 if ( $hasInterwikiResults ) {
269 $apiResult->addIndexedTagName( array(
270 'query', 'interwiki' . $this->getModuleName()
274 $resultPageSet->populateFromTitles( $titles );
275 $offset = $params['offset'] +
1;
276 foreach ( $titles as $index => $title ) {
277 $resultPageSet->setGeneratorData( $title, array( 'index' => $index +
$offset ) );
282 public function getCacheMode( $params ) {
286 public function getAllowedParams() {
289 ApiBase
::PARAM_TYPE
=> 'string',
290 ApiBase
::PARAM_REQUIRED
=> true
292 'namespace' => array(
293 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
294 ApiBase
::PARAM_TYPE
=> 'namespace',
295 ApiBase
::PARAM_ISMULTI
=> true,
298 ApiBase
::PARAM_DFLT
=> null,
299 ApiBase
::PARAM_TYPE
=> array(
306 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
307 ApiBase
::PARAM_TYPE
=> array(
311 ApiBase
::PARAM_ISMULTI
=> true,
314 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
315 ApiBase
::PARAM_TYPE
=> array(
330 ApiBase
::PARAM_ISMULTI
=> true,
333 ApiBase
::PARAM_DFLT
=> 0,
334 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
337 ApiBase
::PARAM_DFLT
=> 10,
338 ApiBase
::PARAM_TYPE
=> 'limit',
339 ApiBase
::PARAM_MIN
=> 1,
340 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_SML1
,
341 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_SML2
343 'interwiki' => false,
346 $alternatives = SearchEngine
::getSearchTypes();
347 if ( count( $alternatives ) > 1 ) {
348 if ( $alternatives[0] === null ) {
349 $alternatives[0] = self
::BACKEND_NULL_PARAM
;
351 $params['backend'] = array(
352 ApiBase
::PARAM_DFLT
=> $this->getConfig()->get( 'SearchType' ),
353 ApiBase
::PARAM_TYPE
=> $alternatives,
360 protected function getExamplesMessages() {
362 'action=query&list=search&srsearch=meaning'
363 => 'apihelp-query+search-example-simple',
364 'action=query&list=search&srwhat=text&srsearch=meaning'
365 => 'apihelp-query+search-example-text',
366 'action=query&generator=search&gsrsearch=meaning&prop=info'
367 => 'apihelp-query+search-example-generator',
371 public function getHelpUrls() {
372 return 'https://www.mediawiki.org/wiki/API:Search';