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 $matches = SearchEngine
::getNearMatchResultSet( $query );
97 // We default to title searches; this is a terrible legacy
98 // of the way we initially set up the MySQL fulltext-based
99 // search engine with separate title and text fields.
100 // In the future, the default should be for a combined index.
102 $matches = $search->searchTitle( $query );
104 // Not all search engines support a separate title search,
105 // for instance the Lucene-based engine we use on Wikipedia.
106 // In this case, fall back to full-text search (which will
107 // include titles in it!)
108 if ( is_null( $matches ) ) {
110 $matches = $search->searchText( $query );
113 if ( is_null( $matches ) ) {
114 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
115 } elseif ( $matches instanceof Status
&& !$matches->isGood() ) {
116 $this->dieUsage( $matches->getWikiText(), 'search-error' );
119 if ( $resultPageSet === null ) {
120 $apiResult = $this->getResult();
121 // Add search meta data to result
122 if ( isset( $searchInfo['totalhits'] ) ) {
123 $totalhits = $matches->getTotalHits();
124 if ( $totalhits !== null ) {
125 $apiResult->addValue( array( 'query', 'searchinfo' ),
126 'totalhits', $totalhits );
129 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
130 $apiResult->addValue( array( 'query', 'searchinfo' ),
131 'suggestion', $matches->getSuggestionQuery() );
135 // Add the search results to the result
136 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
139 $result = $matches->next();
142 if ( ++
$count > $limit ) {
143 // We've reached the one extra which shows that there are
144 // additional items to be had. Stop here...
145 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
149 // Silently skip broken and missing titles
150 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
151 $result = $matches->next();
155 $title = $result->getTitle();
156 if ( $resultPageSet === null ) {
158 ApiQueryBase
::addTitleInfo( $vals, $title );
160 if ( isset( $prop['snippet'] ) ) {
161 $vals['snippet'] = $result->getTextSnippet( $terms );
163 if ( isset( $prop['size'] ) ) {
164 $vals['size'] = $result->getByteSize();
166 if ( isset( $prop['wordcount'] ) ) {
167 $vals['wordcount'] = $result->getWordCount();
169 if ( isset( $prop['timestamp'] ) ) {
170 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $result->getTimestamp() );
172 if ( isset( $prop['titlesnippet'] ) ) {
173 $vals['titlesnippet'] = $result->getTitleSnippet();
175 if ( !is_null( $result->getRedirectTitle() ) ) {
176 if ( isset( $prop['redirecttitle'] ) ) {
177 $vals['redirecttitle'] = $result->getRedirectTitle()->getPrefixedText();
179 if ( isset( $prop['redirectsnippet'] ) ) {
180 $vals['redirectsnippet'] = $result->getRedirectSnippet();
183 if ( !is_null( $result->getSectionTitle() ) ) {
184 if ( isset( $prop['sectiontitle'] ) ) {
185 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
187 if ( isset( $prop['sectionsnippet'] ) ) {
188 $vals['sectionsnippet'] = $result->getSectionSnippet();
192 // Add item to results and see whether it fits
193 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
196 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
203 $result = $matches->next();
206 $hasInterwikiResults = false;
208 if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) {
209 foreach ( $matches->getInterwikiResults() as $matches ) {
210 $matches = $matches->getInterwikiResults();
211 $hasInterwikiResults = true;
213 // Include number of results if requested
214 if ( $resultPageSet === null && isset( $searchInfo['totalhits'] ) ) {
215 $totalhits +
= $matches->getTotalHits();
218 $result = $matches->next();
220 $title = $result->getTitle();
222 if ( $resultPageSet === null ) {
224 'namespace' => $result->getInterwikiNamespaceText(),
225 'title' => $title->getText(),
226 'url' => $title->getFullUrl(),
229 // Add item to results and see whether it fits
230 $fit = $apiResult->addValue(
231 array( 'query', 'interwiki' . $this->getModuleName(), $result->getInterwikiPrefix() ),
237 // We hit the limit. We can't really provide any meaningful
238 // pagination info so just bail out
245 $result = $matches->next();
248 if ( $totalhits !== null ) {
249 $apiResult->addValue( array( 'query', 'interwikisearchinfo' ),
250 'totalhits', $totalhits );
254 if ( $resultPageSet === null ) {
255 $apiResult->setIndexedTagName_internal( array(
256 'query', $this->getModuleName()
258 if ( $hasInterwikiResults ) {
259 $apiResult->setIndexedTagName_internal( array(
260 'query', 'interwiki' . $this->getModuleName()
264 $resultPageSet->populateFromTitles( $titles );
265 $offset = $params['offset'] +
1;
266 foreach ( $titles as $index => $title ) {
267 $resultPageSet->setGeneratorData( $title, array( 'index' => $index +
$offset ) );
272 public function getCacheMode( $params ) {
276 public function getAllowedParams() {
279 ApiBase
::PARAM_TYPE
=> 'string',
280 ApiBase
::PARAM_REQUIRED
=> true
282 'namespace' => array(
283 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
284 ApiBase
::PARAM_TYPE
=> 'namespace',
285 ApiBase
::PARAM_ISMULTI
=> true,
288 ApiBase
::PARAM_DFLT
=> null,
289 ApiBase
::PARAM_TYPE
=> array(
296 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
297 ApiBase
::PARAM_TYPE
=> array(
301 ApiBase
::PARAM_ISMULTI
=> true,
304 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
305 ApiBase
::PARAM_TYPE
=> array(
318 ApiBase
::PARAM_ISMULTI
=> true,
321 ApiBase
::PARAM_DFLT
=> 0,
322 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
325 ApiBase
::PARAM_DFLT
=> 10,
326 ApiBase
::PARAM_TYPE
=> 'limit',
327 ApiBase
::PARAM_MIN
=> 1,
328 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_SML1
,
329 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_SML2
331 'interwiki' => false,
334 $alternatives = SearchEngine
::getSearchTypes();
335 if ( count( $alternatives ) > 1 ) {
336 if ( $alternatives[0] === null ) {
337 $alternatives[0] = self
::BACKEND_NULL_PARAM
;
339 $params['backend'] = array(
340 ApiBase
::PARAM_DFLT
=> $this->getConfig()->get( 'SearchType' ),
341 ApiBase
::PARAM_TYPE
=> $alternatives,
348 protected function getExamplesMessages() {
350 'action=query&list=search&srsearch=meaning'
351 => 'apihelp-query+search-example-simple',
352 'action=query&list=search&srwhat=text&srsearch=meaning'
353 => 'apihelp-query+search-example-text',
354 'action=query&generator=search&gsrsearch=meaning&prop=info'
355 => 'apihelp-query+search-example-generator',
359 public function getHelpUrls() {
360 return 'https://www.mediawiki.org/wiki/API:Search';