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();
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;
207 if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) {
208 $matches = $matches->getInterwikiResults();
209 $hasInterwikiResults = true;
211 // Include number of results if requested
212 if ( $resultPageSet === null && isset( $searchInfo['totalhits'] ) ) {
213 $totalhits = $matches->getTotalHits();
214 if ( $totalhits !== null ) {
215 $apiResult->addValue( array( 'query', 'interwikisearchinfo' ),
216 'totalhits', $totalhits );
220 $result = $matches->next();
222 $title = $result->getTitle();
224 if ( $resultPageSet === null ) {
226 'namespace' => $result->getInterwikiNamespaceText(),
227 'title' => $title->getText(),
228 'url' => $title->getFullUrl(),
231 // Add item to results and see whether it fits
232 $fit = $apiResult->addValue(
233 array( 'query', 'interwiki' . $this->getModuleName(), $result->getInterwikiPrefix() ),
239 // We hit the limit. We can't really provide any meaningful
240 // pagination info so just bail out
247 $result = $matches->next();
251 if ( $resultPageSet === null ) {
252 $apiResult->setIndexedTagName_internal( array(
253 'query', $this->getModuleName()
255 if ( $hasInterwikiResults ) {
256 $apiResult->setIndexedTagName_internal( array(
257 'query', 'interwiki' . $this->getModuleName()
261 $resultPageSet->populateFromTitles( $titles );
262 $offset = $params['offset'] +
1;
263 foreach ( $titles as $index => $title ) {
264 $resultPageSet->setGeneratorData( $title, array( 'index' => $index +
$offset ) );
269 public function getCacheMode( $params ) {
273 public function getAllowedParams() {
276 ApiBase
::PARAM_TYPE
=> 'string',
277 ApiBase
::PARAM_REQUIRED
=> true
279 'namespace' => array(
280 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
281 ApiBase
::PARAM_TYPE
=> 'namespace',
282 ApiBase
::PARAM_ISMULTI
=> true,
285 ApiBase
::PARAM_DFLT
=> null,
286 ApiBase
::PARAM_TYPE
=> array(
293 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
294 ApiBase
::PARAM_TYPE
=> array(
298 ApiBase
::PARAM_ISMULTI
=> true,
301 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
302 ApiBase
::PARAM_TYPE
=> array(
315 ApiBase
::PARAM_ISMULTI
=> true,
318 ApiBase
::PARAM_DFLT
=> 0,
319 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
322 ApiBase
::PARAM_DFLT
=> 10,
323 ApiBase
::PARAM_TYPE
=> 'limit',
324 ApiBase
::PARAM_MIN
=> 1,
325 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_SML1
,
326 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_SML2
328 'interwiki' => false,
331 $alternatives = SearchEngine
::getSearchTypes();
332 if ( count( $alternatives ) > 1 ) {
333 if ( $alternatives[0] === null ) {
334 $alternatives[0] = self
::BACKEND_NULL_PARAM
;
336 $params['backend'] = array(
337 ApiBase
::PARAM_DFLT
=> $this->getConfig()->get( 'SearchType' ),
338 ApiBase
::PARAM_TYPE
=> $alternatives,
345 protected function getExamplesMessages() {
347 'action=query&list=search&srsearch=meaning'
348 => 'apihelp-query+search-example-simple',
349 'action=query&list=search&srwhat=text&srsearch=meaning'
350 => 'apihelp-query+search-example-text',
351 'action=query&generator=search&gsrsearch=meaning&prop=info'
352 => 'apihelp-query+search-example-generator',
356 public function getHelpUrls() {
357 return 'https://www.mediawiki.org/wiki/API:Search';