Follow-up r90742: the parser should call getFunctionLang()
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to perform full text search within wiki titles and content
34 *
35 * @ingroup API
36 */
37 class ApiQuerySearch extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'sr' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
54 */
55 private function run( $resultPageSet = null ) {
56 global $wgContLang;
57 $params = $this->extractRequestParams();
58
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'] );
65
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'];
71
72 // Perform the actual search
73 if ( $what == 'text' ) {
74 $matches = $search->searchText( $query );
75 } elseif ( $what == 'title' ) {
76 $matches = $search->searchTitle( $query );
77 } elseif ( $what == 'nearmatch' ) {
78 $matches = SearchEngine::getNearMatchResultSet( $query );
79 } else {
80 // We default to title searches; this is a terrible legacy
81 // of the way we initially set up the MySQL fulltext-based
82 // search engine with separate title and text fields.
83 // In the future, the default should be for a combined index.
84 $what = 'title';
85 $matches = $search->searchTitle( $query );
86
87 // Not all search engines support a separate title search,
88 // for instance the Lucene-based engine we use on Wikipedia.
89 // In this case, fall back to full-text search (which will
90 // include titles in it!)
91 if ( is_null( $matches ) ) {
92 $what = 'text';
93 $matches = $search->searchText( $query );
94 }
95 }
96 if ( is_null( $matches ) ) {
97 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
98 }
99
100 // Add search meta data to result
101 if ( isset( $searchInfo['totalhits'] ) ) {
102 $totalhits = $matches->getTotalHits();
103 if ( $totalhits !== null ) {
104 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
105 'totalhits', $totalhits );
106 }
107 }
108 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
109 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
110 'suggestion', $matches->getSuggestionQuery() );
111 }
112
113 // Add the search results to the result
114 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
115 $titles = array();
116 $count = 0;
117 $result = $matches->next();
118 while ( $result ) {
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'] );
122 break;
123 }
124
125 // Silently skip broken and missing titles
126 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
127 continue;
128 }
129
130 $title = $result->getTitle();
131 if ( is_null( $resultPageSet ) ) {
132 $vals = array();
133 ApiQueryBase::addTitleInfo( $vals, $title );
134
135 if ( isset( $prop['snippet'] ) ) {
136 $vals['snippet'] = $result->getTextSnippet( $terms );
137 }
138 if ( isset( $prop['size'] ) ) {
139 $vals['size'] = $result->getByteSize();
140 }
141 if ( isset( $prop['wordcount'] ) ) {
142 $vals['wordcount'] = $result->getWordCount();
143 }
144 if ( isset( $prop['timestamp'] ) ) {
145 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
146 }
147 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
148 $vals['score'] = $result->getScore();
149 }
150 if ( isset( $prop['titlesnippet'] ) ) {
151 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
152 }
153 if ( !is_null( $result->getRedirectTitle() ) ) {
154 if ( isset( $prop['redirecttitle'] ) ) {
155 $vals['redirecttitle'] = $result->getRedirectTitle();
156 }
157 if ( isset( $prop['redirectsnippet'] ) ) {
158 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
159 }
160 }
161 if ( !is_null( $result->getSectionTitle() ) ) {
162 if ( isset( $prop['sectiontitle'] ) ) {
163 $vals['sectiontitle'] = $result->getSectionTitle();
164 }
165 if ( isset( $prop['sectionsnippet'] ) ) {
166 $vals['sectionsnippet'] = $result->getSectionSnippet();
167 }
168 }
169 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
170 $vals['hasrelated'] = "";
171 }
172
173 // Add item to results and see whether it fits
174 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
175 null, $vals );
176 if ( !$fit ) {
177 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
178 break;
179 }
180 } else {
181 $titles[] = $title;
182 }
183
184 $result = $matches->next();
185 }
186
187 if ( is_null( $resultPageSet ) ) {
188 $this->getResult()->setIndexedTagName_internal( array(
189 'query', $this->getModuleName()
190 ), 'p' );
191 } else {
192 $resultPageSet->populateFromTitles( $titles );
193 }
194 }
195
196 public function getCacheMode( $params ) {
197 return 'public';
198 }
199
200 public function getAllowedParams() {
201 return array(
202 'search' => array(
203 ApiBase::PARAM_TYPE => 'string',
204 ApiBase::PARAM_REQUIRED => true
205 ),
206 'namespace' => array(
207 ApiBase::PARAM_DFLT => 0,
208 ApiBase::PARAM_TYPE => 'namespace',
209 ApiBase::PARAM_ISMULTI => true,
210 ),
211 'what' => array(
212 ApiBase::PARAM_DFLT => null,
213 ApiBase::PARAM_TYPE => array(
214 'title',
215 'text',
216 'nearmatch',
217 )
218 ),
219 'info' => array(
220 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
221 ApiBase::PARAM_TYPE => array(
222 'totalhits',
223 'suggestion',
224 ),
225 ApiBase::PARAM_ISMULTI => true,
226 ),
227 'prop' => array(
228 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
229 ApiBase::PARAM_TYPE => array(
230 'size',
231 'wordcount',
232 'timestamp',
233 'score',
234 'snippet',
235 'titlesnippet',
236 'redirecttitle',
237 'redirectsnippet',
238 'sectiontitle',
239 'sectionsnippet',
240 'hasrelated',
241 ),
242 ApiBase::PARAM_ISMULTI => true,
243 ),
244 'redirects' => false,
245 'offset' => 0,
246 'limit' => array(
247 ApiBase::PARAM_DFLT => 10,
248 ApiBase::PARAM_TYPE => 'limit',
249 ApiBase::PARAM_MIN => 1,
250 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
251 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
252 )
253 );
254 }
255
256 public function getParamDescription() {
257 return array(
258 'search' => 'Search for all page titles (or content) that has this value',
259 'namespace' => 'The namespace(s) to enumerate',
260 'what' => 'Search inside the text or titles',
261 'info' => 'What metadata to return',
262 'prop' => array(
263 'What properties to return',
264 ' size - Adds the size of the page in bytes',
265 ' wordcount - Adds the word count of the page',
266 ' timestamp - Adds the timestamp of when the page was last edited',
267 ' score - Adds the score (if any) from the search engine',
268 ' snippet - Adds a parsed snippet of the page',
269 ' titlesnippet - Adds a parsed snippet of the page title',
270 ' redirectsnippet - Adds a parsed snippet of the redirect',
271 ' redirecttitle - Adds a parsed snippet of the redirect title',
272 ' sectionsnippet - Adds a parsed snippet of the matching section',
273 ' sectiontitle - Adds a parsed snippet of the matching section title',
274 ' hasrelated - Indicates whether a related search is available',
275 ),
276 'redirects' => 'Include redirect pages in the search',
277 'offset' => 'Use this value to continue paging (return by query)',
278 'limit' => 'How many total pages to return'
279 );
280 }
281
282 public function getDescription() {
283 return 'Perform a full text search';
284 }
285
286 public function getPossibleErrors() {
287 return array_merge( parent::getPossibleErrors(), array(
288 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
289 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
290 ) );
291 }
292
293 protected function getExamples() {
294 return array(
295 'api.php?action=query&list=search&srsearch=meaning',
296 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
297 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
298 );
299 }
300
301 public function getVersion() {
302 return __CLASS__ . ': $Id$';
303 }
304 }