correct typo in r102393
[mediawiki.git] / includes / api / ApiQuerySearch.php
blob8f7b725e3db66d6671c3f0dc88e9b1ef9dd7a363
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
32 /**
33 * Query module to perform full text search within wiki titles and content
35 * @ingroup API
37 class ApiQuerySearch extends ApiQueryGeneratorBase {
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'sr' );
43 public function execute() {
44 $this->run();
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
55 private function run( $resultPageSet = null ) {
56 global $wgContLang;
57 $params = $this->extractRequestParams();
59 // Extract parameters
60 $limit = $params['limit'];
61 $query = $params['search'];
62 $what = $params['what'];
63 $searchInfo = array_flip( $params['info'] );
64 $prop = array_flip( $params['prop'] );
66 // Create search engine instance and set options
67 $search = SearchEngine::create();
68 $search->setLimitOffset( $limit + 1, $params['offset'] );
69 $search->setNamespaces( $params['namespace'] );
70 $search->showRedirects = $params['redirects'];
72 $query = $search->transformSearchTerm( $query );
73 $query = $search->replacePrefixes( $query );
75 // Perform the actual search
76 if ( $what == 'text' ) {
77 $matches = $search->searchText( $query );
78 } elseif ( $what == 'title' ) {
79 $matches = $search->searchTitle( $query );
80 } elseif ( $what == 'nearmatch' ) {
81 $matches = SearchEngine::getNearMatchResultSet( $query );
82 } else {
83 // We default to title searches; this is a terrible legacy
84 // of the way we initially set up the MySQL fulltext-based
85 // search engine with separate title and text fields.
86 // In the future, the default should be for a combined index.
87 $what = 'title';
88 $matches = $search->searchTitle( $query );
90 // Not all search engines support a separate title search,
91 // for instance the Lucene-based engine we use on Wikipedia.
92 // In this case, fall back to full-text search (which will
93 // include titles in it!)
94 if ( is_null( $matches ) ) {
95 $what = 'text';
96 $matches = $search->searchText( $query );
99 if ( is_null( $matches ) ) {
100 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
103 $apiResult = $this->getResult();
104 // Add search meta data to result
105 if ( isset( $searchInfo['totalhits'] ) ) {
106 $totalhits = $matches->getTotalHits();
107 if ( $totalhits !== null ) {
108 $apiResult->addValue( array( 'query', 'searchinfo' ),
109 'totalhits', $totalhits );
112 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
113 $apiResult->addValue( array( 'query', 'searchinfo' ),
114 'suggestion', $matches->getSuggestionQuery() );
117 // Add the search results to the result
118 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
119 $titles = array();
120 $count = 0;
121 $result = $matches->next();
123 while ( $result ) {
124 if ( ++ $count > $limit ) {
125 // We've reached the one extra which shows that there are additional items to be had. Stop here...
126 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
127 break;
130 // Silently skip broken and missing titles
131 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
132 continue;
135 $title = $result->getTitle();
136 if ( is_null( $resultPageSet ) ) {
137 $vals = array();
138 ApiQueryBase::addTitleInfo( $vals, $title );
140 if ( isset( $prop['snippet'] ) ) {
141 $vals['snippet'] = $result->getTextSnippet( $terms );
143 if ( isset( $prop['size'] ) ) {
144 $vals['size'] = $result->getByteSize();
146 if ( isset( $prop['wordcount'] ) ) {
147 $vals['wordcount'] = $result->getWordCount();
149 if ( isset( $prop['timestamp'] ) ) {
150 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
152 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
153 $vals['score'] = $result->getScore();
155 if ( isset( $prop['titlesnippet'] ) ) {
156 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
158 if ( !is_null( $result->getRedirectTitle() ) ) {
159 if ( isset( $prop['redirecttitle'] ) ) {
160 $vals['redirecttitle'] = $result->getRedirectTitle();
162 if ( isset( $prop['redirectsnippet'] ) ) {
163 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
166 if ( !is_null( $result->getSectionTitle() ) ) {
167 if ( isset( $prop['sectiontitle'] ) ) {
168 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
170 if ( isset( $prop['sectionsnippet'] ) ) {
171 $vals['sectionsnippet'] = $result->getSectionSnippet();
174 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
175 $vals['hasrelated'] = "";
178 // Add item to results and see whether it fits
179 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
180 null, $vals );
181 if ( !$fit ) {
182 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
183 break;
185 } else {
186 $titles[] = $title;
189 $result = $matches->next();
192 if ( is_null( $resultPageSet ) ) {
193 $apiResult->setIndexedTagName_internal( array(
194 'query', $this->getModuleName()
195 ), 'p' );
196 } else {
197 $resultPageSet->populateFromTitles( $titles );
201 public function getCacheMode( $params ) {
202 return 'public';
205 public function getAllowedParams() {
206 return array(
207 'search' => array(
208 ApiBase::PARAM_TYPE => 'string',
209 ApiBase::PARAM_REQUIRED => true
211 'namespace' => array(
212 ApiBase::PARAM_DFLT => 0,
213 ApiBase::PARAM_TYPE => 'namespace',
214 ApiBase::PARAM_ISMULTI => true,
216 'what' => array(
217 ApiBase::PARAM_DFLT => null,
218 ApiBase::PARAM_TYPE => array(
219 'title',
220 'text',
221 'nearmatch',
224 'info' => array(
225 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
226 ApiBase::PARAM_TYPE => array(
227 'totalhits',
228 'suggestion',
230 ApiBase::PARAM_ISMULTI => true,
232 'prop' => array(
233 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
234 ApiBase::PARAM_TYPE => array(
235 'size',
236 'wordcount',
237 'timestamp',
238 'score',
239 'snippet',
240 'titlesnippet',
241 'redirecttitle',
242 'redirectsnippet',
243 'sectiontitle',
244 'sectionsnippet',
245 'hasrelated',
247 ApiBase::PARAM_ISMULTI => true,
249 'redirects' => false,
250 'offset' => 0,
251 'limit' => array(
252 ApiBase::PARAM_DFLT => 10,
253 ApiBase::PARAM_TYPE => 'limit',
254 ApiBase::PARAM_MIN => 1,
255 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
256 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
261 public function getParamDescription() {
262 return array(
263 'search' => 'Search for all page titles (or content) that has this value',
264 'namespace' => 'The namespace(s) to enumerate',
265 'what' => 'Search inside the text or titles',
266 'info' => 'What metadata to return',
267 'prop' => array(
268 'What properties to return',
269 ' size - Adds the size of the page in bytes',
270 ' wordcount - Adds the word count of the page',
271 ' timestamp - Adds the timestamp of when the page was last edited',
272 ' score - Adds the score (if any) from the search engine',
273 ' snippet - Adds a parsed snippet of the page',
274 ' titlesnippet - Adds a parsed snippet of the page title',
275 ' redirectsnippet - Adds a parsed snippet of the redirect title',
276 ' redirecttitle - Adds the title of the matching redirect',
277 ' sectionsnippet - Adds a parsed snippet of the matching section title',
278 ' sectiontitle - Adds the title of the matching section',
279 ' hasrelated - Indicates whether a related search is available',
281 'redirects' => 'Include redirect pages in the search',
282 'offset' => 'Use this value to continue paging (return by query)',
283 'limit' => 'How many total pages to return'
287 public function getDescription() {
288 return 'Perform a full text search';
291 public function getPossibleErrors() {
292 return array_merge( parent::getPossibleErrors(), array(
293 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
294 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
295 ) );
298 public function getExamples() {
299 return array(
300 'api.php?action=query&list=search&srsearch=meaning',
301 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
302 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
306 public function getHelpUrls() {
307 return 'http://www.mediawiki.org/wiki/API:Search';
310 public function getVersion() {
311 return __CLASS__ . ': $Id$';