Fix prefix search for special pages
[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 $db = $this->getDb();
139 $lb = new LinkBatch( $titles );
140 $res = $db->select(
141 array( 'page', 'redirect' ),
142 array( 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ),
143 array(
144 'rd_from = page_id',
145 'rd_interwiki IS NULL OR rd_interwiki = ' . $db->addQuotes( '' ),
146 $lb->constructSet( 'page', $db ),
147 ),
148 __METHOD__
149 );
150 $redirects = array();
151 foreach ( $res as $row ) {
152 $redirects[$row->page_namespace][$row->page_title] =
153 array( $row->rd_namespace, $row->rd_title );
154 }
155
156 // Bypass any redirects
157 $seen = array();
158 foreach ( $titles as $title ) {
159 $ns = $title->getNamespace();
160 $dbkey = $title->getDBkey();
161 $from = null;
162 if ( isset( $redirects[$ns][$dbkey] ) ) {
163 list( $ns, $dbkey ) = $redirects[$ns][$dbkey];
164 $from = $title;
165 $title = Title::makeTitle( $ns, $dbkey );
166 }
167 if ( !isset( $seen[$ns][$dbkey] ) ) {
168 $seen[$ns][$dbkey] = true;
169 $resultId = $title->getArticleId();
170 if ( $resultId === 0 ) {
171 $resultId = $nextSpecialPageId;
172 $nextSpecialPageId -= 1;
173 }
174 $results[$resultId] = array(
175 'title' => $title,
176 'redirect from' => $from,
177 'extract' => false,
178 'extract trimmed' => false,
179 'image' => false,
180 'url' => wfExpandUrl( $title->getFullUrl(), PROTO_CURRENT ),
181 );
182 }
183 }
184 } else {
185 foreach ( $titles as $title ) {
186 $resultId = $title->getArticleId();
187 if ( $resultId === 0 ) {
188 $resultId = $nextSpecialPageId;
189 $nextSpecialPageId -= 1;
190 }
191 $results[$resultId] = array(
192 'title' => $title,
193 'redirect from' => null,
194 'extract' => false,
195 'extract trimmed' => false,
196 'image' => false,
197 'url' => wfExpandUrl( $title->getFullUrl(), PROTO_CURRENT ),
198 );
199 }
200 }
201 }
202
203 /**
204 * @param string $search
205 * @param array &$results
206 */
207 protected function populateResult( $search, &$results ) {
208 $result = $this->getResult();
209
210 switch ( $this->getFormat() ) {
211 case 'json':
212 // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
213 $result->addValue( null, 0, strval( $search ) );
214 $terms = array();
215 $descriptions = array();
216 $urls = array();
217 foreach ( $results as $r ) {
218 $terms[] = $r['title']->getPrefixedText();
219 $descriptions[] = strval( $r['extract'] );
220 $urls[] = $r['url'];
221 }
222 $result->addValue( null, 1, $terms );
223 $result->addValue( null, 2, $descriptions );
224 $result->addValue( null, 3, $urls );
225 break;
226
227 case 'xml':
228 // http://msdn.microsoft.com/en-us/library/cc891508%28v=vs.85%29.aspx
229 $imageKeys = array(
230 'source' => true,
231 'alt' => true,
232 'width' => true,
233 'height' => true,
234 'align' => true,
235 );
236 $items = array();
237 foreach ( $results as $r ) {
238 $item = array();
239 $result->setContent( $item, $r['title']->getPrefixedText(), 'Text' );
240 $result->setContent( $item, $r['url'], 'Url' );
241 if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
242 $result->setContent( $item, $r['extract'], 'Description' );
243 }
244 if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
245 $item['Image'] = array_intersect_key( $r['image'], $imageKeys );
246 }
247 $items[] = $item;
248 }
249 $result->setIndexedTagName( $items, 'Item' );
250 $result->addValue( null, 'version', '2.0' );
251 $result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
252 $query = array();
253 $result->setContent( $query, strval( $search ) );
254 $result->addValue( null, 'Query', $query );
255 $result->addValue( null, 'Section', $items );
256 break;
257
258 default:
259 ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
260 }
261 }
262
263 public function getAllowedParams() {
264 return array(
265 'search' => null,
266 'limit' => array(
267 ApiBase::PARAM_DFLT => $this->getConfig()->get( 'OpenSearchDefaultLimit' ),
268 ApiBase::PARAM_TYPE => 'limit',
269 ApiBase::PARAM_MIN => 1,
270 ApiBase::PARAM_MAX => 100,
271 ApiBase::PARAM_MAX2 => 100
272 ),
273 'namespace' => array(
274 ApiBase::PARAM_DFLT => NS_MAIN,
275 ApiBase::PARAM_TYPE => 'namespace',
276 ApiBase::PARAM_ISMULTI => true
277 ),
278 'suggest' => false,
279 'redirects' => array(
280 ApiBase::PARAM_TYPE => array( 'return', 'resolve' ),
281 ),
282 'format' => array(
283 ApiBase::PARAM_DFLT => 'json',
284 ApiBase::PARAM_TYPE => array( 'json', 'jsonfm', 'xml', 'xmlfm' ),
285 )
286 );
287 }
288
289 protected function getExamplesMessages() {
290 return array(
291 'action=opensearch&search=Te'
292 => 'apihelp-opensearch-example-te',
293 );
294 }
295
296 public function getHelpUrls() {
297 return 'https://www.mediawiki.org/wiki/API:Opensearch';
298 }
299
300 /**
301 * Trim an extract to a sensible length.
302 *
303 * Adapted from Extension:OpenSearchXml, which adapted it from
304 * Extension:ActiveAbstract.
305 *
306 * @param string $text
307 * @param int $len Target length; actual result will continue to the end of a sentence.
308 * @return string
309 */
310 public static function trimExtract( $text, $length ) {
311 static $regex = null;
312
313 if ( $regex === null ) {
314 $endchars = array(
315 '([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
316 '。', // full-width ideographic full-stop
317 '.', '!', '?', // double-width roman forms
318 '。', // half-width ideographic full stop
319 );
320 $endgroup = implode( '|', $endchars );
321 $end = "(?:$endgroup)";
322 $sentence = ".{{$length},}?$end+";
323 $regex = "/^($sentence)/u";
324 }
325
326 $matches = array();
327 if ( preg_match( $regex, $text, $matches ) ) {
328 return trim( $matches[1] );
329 } else {
330 // Just return the first line
331 $lines = explode( "\n", $text );
332 return trim( $lines[0] );
333 }
334 }
335
336 /**
337 * Fetch the template for a type.
338 *
339 * @param string $type MIME type
340 * @return string
341 */
342 public static function getOpenSearchTemplate( $type ) {
343 global $wgOpenSearchTemplate, $wgCanonicalServer;
344
345 if ( $wgOpenSearchTemplate && $type === 'application/x-suggestions+json' ) {
346 return $wgOpenSearchTemplate;
347 }
348
349 $ns = implode( '|', SearchEngine::defaultNamespaces() );
350 if ( !$ns ) {
351 $ns = "0";
352 }
353
354 switch ( $type ) {
355 case 'application/x-suggestions+json':
356 return $wgCanonicalServer . wfScript( 'api' )
357 . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
358
359 case 'application/x-suggestions+xml':
360 return $wgCanonicalServer . wfScript( 'api' )
361 . '?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
362
363 default:
364 throw new MWException( __METHOD__ . ": Unknown type '$type'" );
365 }
366 }
367 }