Merge "Follow-up cdc93a62bf: add serialize/unserialize tests for RedisBagOStuff"
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
1 <?php
2 /**
3 * Created on Oct 13, 2006
4 *
5 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 * Copyright © 2008 Brion Vibber <brion@wikimedia.org>
7 * Copyright © 2014 Brad Jorsch <bjorsch@wikimedia.org>
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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * @ingroup API
31 */
32 class ApiOpenSearch extends ApiBase {
33
34 private $format = null;
35 private $fm = null;
36
37 /**
38 * Get the output format
39 *
40 * @return string
41 */
42 protected function getFormat() {
43 if ( $this->format === null ) {
44 $params = $this->extractRequestParams();
45 $format = $params['format'];
46
47 $allowedParams = $this->getAllowedParams();
48 if ( !in_array( $format, $allowedParams['format'][ApiBase::PARAM_TYPE] ) ) {
49 $format = $allowedParams['format'][ApiBase::PARAM_DFLT];
50 }
51
52 if ( substr( $format, -2 ) === 'fm' ) {
53 $this->format = substr( $format, 0, -2 );
54 $this->fm = 'fm';
55 } else {
56 $this->format = $format;
57 $this->fm = '';
58 }
59 }
60 return $this->format;
61 }
62
63 public function getCustomPrinter() {
64 switch ( $this->getFormat() ) {
65 case 'json':
66 return new ApiOpenSearchFormatJson(
67 $this->getMain(), $this->fm, $this->getParameter( 'warningsaserror' )
68 );
69
70 case 'xml':
71 $printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm );
72 $printer->setRootElement( 'SearchSuggestion' );
73 return $printer;
74
75 default:
76 ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
77 }
78 }
79
80 public function execute() {
81 $params = $this->extractRequestParams();
82 $search = $params['search'];
83 $limit = $params['limit'];
84 $namespaces = $params['namespace'];
85 $suggest = $params['suggest'];
86
87 if ( $params['redirects'] === null ) {
88 // Backwards compatibility, don't resolve for JSON.
89 $resolveRedir = $this->getFormat() !== 'json';
90 } else {
91 $resolveRedir = $params['redirects'] === 'resolve';
92 }
93
94 $results = [];
95
96 if ( !$suggest || $this->getConfig()->get( 'EnableOpenSearchSuggest' ) ) {
97 // Open search results may be stored for a very long time
98 $this->getMain()->setCacheMaxAge( $this->getConfig()->get( 'SearchSuggestCacheExpiry' ) );
99 $this->getMain()->setCacheMode( 'public' );
100 $this->search( $search, $limit, $namespaces, $resolveRedir, $results );
101
102 // Allow hooks to populate extracts and images
103 Hooks::run( 'ApiOpenSearchSuggest', [ &$results ] );
104
105 // Trim extracts, if necessary
106 $length = $this->getConfig()->get( 'OpenSearchDescriptionLength' );
107 foreach ( $results as &$r ) {
108 if ( is_string( $r['extract'] ) && !$r['extract trimmed'] ) {
109 $r['extract'] = self::trimExtract( $r['extract'], $length );
110 }
111 }
112 }
113
114 // Populate result object
115 $this->populateResult( $search, $results );
116 }
117
118 /**
119 * Perform the search
120 *
121 * @param string $search Text to search
122 * @param int $limit Maximum items to return
123 * @param array $namespaces Namespaces to search
124 * @param bool $resolveRedir Whether to resolve redirects
125 * @param array &$results Put results here. Keys have to be integers.
126 */
127 protected function search( $search, $limit, $namespaces, $resolveRedir, &$results ) {
128 $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
129 $searchEngine->setLimitOffset( $limit );
130 $searchEngine->setNamespaces( $namespaces );
131 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
132
133 if ( !$titles ) {
134 return;
135 }
136
137 // Special pages need unique integer ids in the return list, so we just
138 // assign them negative numbers because those won't clash with the
139 // always positive articleIds that non-special pages get.
140 $nextSpecialPageId = -1;
141
142 if ( $resolveRedir ) {
143 // Query for redirects
144 $redirects = [];
145 $lb = new LinkBatch( $titles );
146 if ( !$lb->isEmpty() ) {
147 $db = $this->getDB();
148 $res = $db->select(
149 [ 'page', 'redirect' ],
150 [ 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ],
151 [
152 'rd_from = page_id',
153 'rd_interwiki IS NULL OR rd_interwiki = ' . $db->addQuotes( '' ),
154 $lb->constructSet( 'page', $db ),
155 ],
156 __METHOD__
157 );
158 foreach ( $res as $row ) {
159 $redirects[$row->page_namespace][$row->page_title] =
160 [ $row->rd_namespace, $row->rd_title ];
161 }
162 }
163
164 // Bypass any redirects
165 $seen = [];
166 foreach ( $titles as $title ) {
167 $ns = $title->getNamespace();
168 $dbkey = $title->getDBkey();
169 $from = null;
170 if ( isset( $redirects[$ns][$dbkey] ) ) {
171 list( $ns, $dbkey ) = $redirects[$ns][$dbkey];
172 $from = $title;
173 $title = Title::makeTitle( $ns, $dbkey );
174 }
175 if ( !isset( $seen[$ns][$dbkey] ) ) {
176 $seen[$ns][$dbkey] = true;
177 $resultId = $title->getArticleID();
178 if ( $resultId === 0 ) {
179 $resultId = $nextSpecialPageId;
180 $nextSpecialPageId -= 1;
181 }
182 $results[$resultId] = [
183 'title' => $title,
184 'redirect from' => $from,
185 'extract' => false,
186 'extract trimmed' => false,
187 'image' => false,
188 'url' => wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ),
189 ];
190 }
191 }
192 } else {
193 foreach ( $titles as $title ) {
194 $resultId = $title->getArticleID();
195 if ( $resultId === 0 ) {
196 $resultId = $nextSpecialPageId;
197 $nextSpecialPageId -= 1;
198 }
199 $results[$resultId] = [
200 'title' => $title,
201 'redirect from' => null,
202 'extract' => false,
203 'extract trimmed' => false,
204 'image' => false,
205 'url' => wfExpandUrl( $title->getFullURL(), PROTO_CURRENT ),
206 ];
207 }
208 }
209 }
210
211 /**
212 * @param string $search
213 * @param array &$results
214 */
215 protected function populateResult( $search, &$results ) {
216 $result = $this->getResult();
217
218 switch ( $this->getFormat() ) {
219 case 'json':
220 // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
221 $result->addArrayType( null, 'array' );
222 $result->addValue( null, 0, strval( $search ) );
223 $terms = [];
224 $descriptions = [];
225 $urls = [];
226 foreach ( $results as $r ) {
227 $terms[] = $r['title']->getPrefixedText();
228 $descriptions[] = strval( $r['extract'] );
229 $urls[] = $r['url'];
230 }
231 $result->addValue( null, 1, $terms );
232 $result->addValue( null, 2, $descriptions );
233 $result->addValue( null, 3, $urls );
234 break;
235
236 case 'xml':
237 // http://msdn.microsoft.com/en-us/library/cc891508%28v=vs.85%29.aspx
238 $imageKeys = [
239 'source' => true,
240 'alt' => true,
241 'width' => true,
242 'height' => true,
243 'align' => true,
244 ];
245 $items = [];
246 foreach ( $results as $r ) {
247 $item = [
248 'Text' => $r['title']->getPrefixedText(),
249 'Url' => $r['url'],
250 ];
251 if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
252 $item['Description'] = $r['extract'];
253 }
254 if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
255 $item['Image'] = array_intersect_key( $r['image'], $imageKeys );
256 }
257 ApiResult::setSubelementsList( $item, array_keys( $item ) );
258 $items[] = $item;
259 }
260 ApiResult::setIndexedTagName( $items, 'Item' );
261 $result->addValue( null, 'version', '2.0' );
262 $result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
263 $result->addValue( null, 'Query', strval( $search ) );
264 $result->addSubelementsList( null, 'Query' );
265 $result->addValue( null, 'Section', $items );
266 break;
267
268 default:
269 ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
270 }
271 }
272
273 public function getAllowedParams() {
274 return [
275 'search' => null,
276 'limit' => [
277 ApiBase::PARAM_DFLT => $this->getConfig()->get( 'OpenSearchDefaultLimit' ),
278 ApiBase::PARAM_TYPE => 'limit',
279 ApiBase::PARAM_MIN => 1,
280 ApiBase::PARAM_MAX => 100,
281 ApiBase::PARAM_MAX2 => 100
282 ],
283 'namespace' => [
284 ApiBase::PARAM_DFLT => NS_MAIN,
285 ApiBase::PARAM_TYPE => 'namespace',
286 ApiBase::PARAM_ISMULTI => true
287 ],
288 'suggest' => false,
289 'redirects' => [
290 ApiBase::PARAM_TYPE => [ 'return', 'resolve' ],
291 ],
292 'format' => [
293 ApiBase::PARAM_DFLT => 'json',
294 ApiBase::PARAM_TYPE => [ 'json', 'jsonfm', 'xml', 'xmlfm' ],
295 ],
296 'warningsaserror' => false,
297 ];
298 }
299
300 protected function getExamplesMessages() {
301 return [
302 'action=opensearch&search=Te'
303 => 'apihelp-opensearch-example-te',
304 ];
305 }
306
307 public function getHelpUrls() {
308 return 'https://www.mediawiki.org/wiki/API:Opensearch';
309 }
310
311 /**
312 * Trim an extract to a sensible length.
313 *
314 * Adapted from Extension:OpenSearchXml, which adapted it from
315 * Extension:ActiveAbstract.
316 *
317 * @param string $text
318 * @param int $length Target length; actual result will continue to the end of a sentence.
319 * @return string
320 */
321 public static function trimExtract( $text, $length ) {
322 static $regex = null;
323
324 if ( $regex === null ) {
325 $endchars = [
326 '([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
327 '。', // full-width ideographic full-stop
328 '.', '!', '?', // double-width roman forms
329 '。', // half-width ideographic full stop
330 ];
331 $endgroup = implode( '|', $endchars );
332 $end = "(?:$endgroup)";
333 $sentence = ".{{$length},}?$end+";
334 $regex = "/^($sentence)/u";
335 }
336
337 $matches = [];
338 if ( preg_match( $regex, $text, $matches ) ) {
339 return trim( $matches[1] );
340 } else {
341 // Just return the first line
342 return trim( explode( "\n", $text )[0] );
343 }
344 }
345
346 /**
347 * Fetch the template for a type.
348 *
349 * @param string $type MIME type
350 * @return string
351 * @throws MWException
352 */
353 public static function getOpenSearchTemplate( $type ) {
354 $config = MediaWikiServices::getInstance()->getSearchEngineConfig();
355 $template = $config->getConfig()->get( 'OpenSearchTemplate' );
356
357 if ( $template && $type === 'application/x-suggestions+json' ) {
358 return $template;
359 }
360
361 $ns = implode( '|', $config->defaultNamespaces() );
362 if ( !$ns ) {
363 $ns = '0';
364 }
365
366 switch ( $type ) {
367 case 'application/x-suggestions+json':
368 return $config->getConfig()->get( 'CanonicalServer' ) . wfScript( 'api' )
369 . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
370
371 case 'application/x-suggestions+xml':
372 return $config->getConfig()->get( 'CanonicalServer' ) . wfScript( 'api' )
373 . '?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
374
375 default:
376 throw new MWException( __METHOD__ . ": Unknown type '$type'" );
377 }
378 }
379 }
380
381 class ApiOpenSearchFormatJson extends ApiFormatJson {
382 private $warningsAsError = false;
383
384 public function __construct( ApiMain $main, $fm, $warningsAsError ) {
385 parent::__construct( $main, "json$fm" );
386 $this->warningsAsError = $warningsAsError;
387 }
388
389 public function execute() {
390 if ( !$this->getResult()->getResultData( 'error' ) ) {
391 $result = $this->getResult();
392
393 // Ignore warnings or treat as errors, as requested
394 $warnings = $result->removeValue( 'warnings', null );
395 if ( $this->warningsAsError && $warnings ) {
396 $this->dieUsage(
397 'Warnings cannot be represented in OpenSearch JSON format', 'warnings', 0,
398 [ 'warnings' => $warnings ]
399 );
400 }
401
402 // Ignore any other unexpected keys (e.g. from $wgDebugToolbar)
403 $remove = array_keys( array_diff_key(
404 $result->getResultData(),
405 [ 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ]
406 ) );
407 foreach ( $remove as $key ) {
408 $result->removeValue( $key, null );
409 }
410 }
411
412 parent::execute();
413 }
414 }