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