Followup comment to r81507. The api is using a legacy langlinks method no modern...
[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 private function run( $resultPageSet = null ) {
52 global $wgContLang;
53 $params = $this->extractRequestParams();
54
55 // Extract parameters
56 $limit = $params['limit'];
57 $query = $params['search'];
58 $what = $params['what'];
59 $searchInfo = array_flip( $params['info'] );
60 $prop = array_flip( $params['prop'] );
61
62 // Create search engine instance and set options
63 $search = SearchEngine::create();
64 $search->setLimitOffset( $limit + 1, $params['offset'] );
65 $search->setNamespaces( $params['namespace'] );
66 $search->showRedirects = $params['redirects'];
67
68 // Perform the actual search
69 if ( $what == 'text' ) {
70 $matches = $search->searchText( $query );
71 } elseif ( $what == 'title' ) {
72 $matches = $search->searchTitle( $query );
73 } elseif ( $what == 'nearmatch' ) {
74 $matches = SearchEngine::getNearMatchResultSet( $query );
75 } else {
76 // We default to title searches; this is a terrible legacy
77 // of the way we initially set up the MySQL fulltext-based
78 // search engine with separate title and text fields.
79 // In the future, the default should be for a combined index.
80 $what = 'title';
81 $matches = $search->searchTitle( $query );
82
83 // Not all search engines support a separate title search,
84 // for instance the Lucene-based engine we use on Wikipedia.
85 // In this case, fall back to full-text search (which will
86 // include titles in it!)
87 if ( is_null( $matches ) ) {
88 $what = 'text';
89 $matches = $search->searchText( $query );
90 }
91 }
92 if ( is_null( $matches ) ) {
93 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
94 }
95
96 // Add search meta data to result
97 if ( isset( $searchInfo['totalhits'] ) ) {
98 $totalhits = $matches->getTotalHits();
99 if ( $totalhits !== null ) {
100 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
101 'totalhits', $totalhits );
102 }
103 }
104 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
105 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
106 'suggestion', $matches->getSuggestionQuery() );
107 }
108
109 // Add the search results to the result
110 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
111 $titles = array();
112 $count = 0;
113 while ( $result = $matches->next() ) {
114 if ( ++ $count > $limit ) {
115 // We've reached the one extra which shows that there are additional items to be had. Stop here...
116 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
117 break;
118 }
119
120 // Silently skip broken and missing titles
121 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
122 continue;
123 }
124
125 $title = $result->getTitle();
126 if ( is_null( $resultPageSet ) ) {
127 $vals = array();
128 ApiQueryBase::addTitleInfo( $vals, $title );
129
130 if ( isset( $prop['snippet'] ) ) {
131 $vals['snippet'] = $result->getTextSnippet( $terms );
132 }
133 if ( isset( $prop['size'] ) ) {
134 $vals['size'] = $result->getByteSize();
135 }
136 if ( isset( $prop['wordcount'] ) ) {
137 $vals['wordcount'] = $result->getWordCount();
138 }
139 if ( isset( $prop['timestamp'] ) ) {
140 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
141 }
142 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
143 $vals['score'] = $result->getScore();
144 }
145 if ( isset( $prop['titlesnippet'] ) ) {
146 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
147 }
148 if ( !is_null( $result->getRedirectTitle() ) ) {
149 if ( isset( $prop['redirecttitle'] ) ) {
150 $vals['redirecttitle'] = $result->getRedirectTitle();
151 }
152 if ( isset( $prop['redirectsnippet'] ) ) {
153 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
154 }
155 }
156 if ( !is_null( $result->getSectionTitle() ) ) {
157 if ( isset( $prop['sectiontitle'] ) ) {
158 $vals['sectiontitle'] = $result->getSectionTitle();
159 }
160 if ( isset( $prop['sectionsnippet'] ) ) {
161 $vals['sectionsnippet'] = $result->getSectionSnippet();
162 }
163 }
164 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
165 $vals['hasrelated'] = "";
166 }
167
168 // Add item to results and see whether it fits
169 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
170 null, $vals );
171 if ( !$fit ) {
172 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
173 break;
174 }
175 } else {
176 $titles[] = $title;
177 }
178 }
179
180 if ( is_null( $resultPageSet ) ) {
181 $this->getResult()->setIndexedTagName_internal( array(
182 'query', $this->getModuleName()
183 ), 'p' );
184 } else {
185 $resultPageSet->populateFromTitles( $titles );
186 }
187 }
188
189 public function getCacheMode( $params ) {
190 return 'public';
191 }
192
193 public function getAllowedParams() {
194 return array(
195 'search' => array(
196 ApiBase::PARAM_TYPE => 'string',
197 ApiBase::PARAM_REQUIRED => true
198 ),
199 'namespace' => array(
200 ApiBase::PARAM_DFLT => 0,
201 ApiBase::PARAM_TYPE => 'namespace',
202 ApiBase::PARAM_ISMULTI => true,
203 ),
204 'what' => array(
205 ApiBase::PARAM_DFLT => null,
206 ApiBase::PARAM_TYPE => array(
207 'title',
208 'text',
209 'nearmatch',
210 )
211 ),
212 'info' => array(
213 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
214 ApiBase::PARAM_TYPE => array(
215 'totalhits',
216 'suggestion',
217 ),
218 ApiBase::PARAM_ISMULTI => true,
219 ),
220 'prop' => array(
221 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
222 ApiBase::PARAM_TYPE => array(
223 'size',
224 'wordcount',
225 'timestamp',
226 'score',
227 'snippet',
228 'titlesnippet',
229 'redirecttitle',
230 'redirectsnippet',
231 'sectiontitle',
232 'sectionsnippet',
233 'hasrelated',
234 ),
235 ApiBase::PARAM_ISMULTI => true,
236 ),
237 'redirects' => false,
238 'offset' => 0,
239 'limit' => array(
240 ApiBase::PARAM_DFLT => 10,
241 ApiBase::PARAM_TYPE => 'limit',
242 ApiBase::PARAM_MIN => 1,
243 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
244 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
245 )
246 );
247 }
248
249 public function getParamDescription() {
250 return array(
251 'search' => 'Search for all page titles (or content) that has this value',
252 'namespace' => 'The namespace(s) to enumerate',
253 'what' => 'Search inside the text or titles',
254 'info' => 'What metadata to return',
255 'prop' => array(
256 'What properties to return',
257 ' size - Adds the size of the page in bytes',
258 ' wordcount - Adds the word count of the page',
259 ' timestamp - Adds the timestamp of when the page was last edited',
260 ' score - Adds the score (if any) from the search engine',
261 ' snippet - Adds a parsed snippet of the page',
262 ' titlesnippet - Adds a parsed snippet of the page title',
263 ' redirectsnippet - Adds a parsed snippet of the redirect',
264 ' redirecttitle - Adds a parsed snippet of the redirect title',
265 ' sectionsnippet - Adds a parsed snippet of the matching section',
266 ' sectiontitle - Adds a parsed snippet of the matching section title',
267 ' hasrelated - Indicates whether a related search is available',
268 ),
269 'redirects' => 'Include redirect pages in the search',
270 'offset' => 'Use this value to continue paging (return by query)',
271 'limit' => 'How many total pages to return'
272 );
273 }
274
275 public function getDescription() {
276 return 'Perform a full text search';
277 }
278
279 public function getPossibleErrors() {
280 return array_merge( parent::getPossibleErrors(), array(
281 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
282 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
283 ) );
284 }
285
286 protected function getExamples() {
287 return array(
288 'api.php?action=query&list=search&srsearch=meaning',
289 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
290 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
291 );
292 }
293
294 public function getVersion() {
295 return __CLASS__ . ': $Id$';
296 }
297 }