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
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'sr' );
38 public function execute() {
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
47 * @param $resultPageSet ApiPageSet
50 private function run( $resultPageSet = null ) {
52 $params = $this->extractRequestParams();
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 );
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.
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 ) ) {
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() );
116 $result = $matches->next();
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'] );
125 // Silently skip broken and missing titles
126 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
127 $result = $matches->next();
131 $title = $result->getTitle();
132 if ( is_null( $resultPageSet ) ) {
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() ),
178 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
185 $result = $matches->next();
188 if ( is_null( $resultPageSet ) ) {
189 $apiResult->setIndexedTagName_internal( array(
190 'query', $this->getModuleName()
193 $resultPageSet->populateFromTitles( $titles );
197 public function getCacheMode( $params ) {
201 public function getAllowedParams() {
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,
213 ApiBase
::PARAM_DFLT
=> null,
214 ApiBase
::PARAM_TYPE
=> array(
221 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
222 ApiBase
::PARAM_TYPE
=> array(
226 ApiBase
::PARAM_ISMULTI
=> true,
229 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
230 ApiBase
::PARAM_TYPE
=> array(
243 ApiBase
::PARAM_ISMULTI
=> true,
245 'redirects' => false,
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() {
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',
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() {
290 'snippet' => 'string'
295 'wordcount' => array(
296 'wordcount' => 'integer'
298 'timestamp' => array(
299 'timestamp' => 'timestamp'
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' ),
351 public function getExamples() {
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';