3 * Created on Oct 13, 2006
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>
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.
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.
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
27 use MediaWiki\MediaWikiServices
;
32 class ApiOpenSearch
extends ApiBase
{
34 private $format = null;
38 * Get the output format
42 protected function getFormat() {
43 if ( $this->format
=== null ) {
44 $params = $this->extractRequestParams();
45 $format = $params['format'];
47 $allowedParams = $this->getAllowedParams();
48 if ( !in_array( $format, $allowedParams['format'][ApiBase
::PARAM_TYPE
] ) ) {
49 $format = $allowedParams['format'][ApiBase
::PARAM_DFLT
];
52 if ( substr( $format, -2 ) === 'fm' ) {
53 $this->format
= substr( $format, 0, -2 );
56 $this->format
= $format;
63 public function getCustomPrinter() {
64 switch ( $this->getFormat() ) {
66 return new ApiOpenSearchFormatJson(
67 $this->getMain(), $this->fm
, $this->getParameter( 'warningsaserror' )
71 $printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm
);
72 $printer->setRootElement( 'SearchSuggestion' );
76 ApiBase
::dieDebug( __METHOD__
, "Unsupported format '{$this->getFormat()}'" );
80 public function execute() {
81 $params = $this->extractRequestParams();
82 $search = $params['search'];
83 $limit = $params['limit'];
84 $namespaces = $params['namespace'];
85 $suggest = $params['suggest'];
87 if ( $params['redirects'] === null ) {
88 // Backwards compatibility, don't resolve for JSON.
89 $resolveRedir = $this->getFormat() !== 'json';
91 $resolveRedir = $params['redirects'] === 'resolve';
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 );
102 // Allow hooks to populate extracts and images
103 Hooks
::run( 'ApiOpenSearchSuggest', [ &$results ] );
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 );
114 // Populate result object
115 $this->populateResult( $search, $results );
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.
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 ) );
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;
142 if ( $resolveRedir ) {
143 // Query for redirects
145 $lb = new LinkBatch( $titles );
146 if ( !$lb->isEmpty() ) {
147 $db = $this->getDB();
149 [ 'page', 'redirect' ],
150 [ 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ],
153 'rd_interwiki IS NULL OR rd_interwiki = ' . $db->addQuotes( '' ),
154 $lb->constructSet( 'page', $db ),
158 foreach ( $res as $row ) {
159 $redirects[$row->page_namespace
][$row->page_title
] =
160 [ $row->rd_namespace
, $row->rd_title
];
164 // Bypass any redirects
166 foreach ( $titles as $title ) {
167 $ns = $title->getNamespace();
168 $dbkey = $title->getDBkey();
170 if ( isset( $redirects[$ns][$dbkey] ) ) {
171 list( $ns, $dbkey ) = $redirects[$ns][$dbkey];
173 $title = Title
::makeTitle( $ns, $dbkey );
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;
182 $results[$resultId] = [
184 'redirect from' => $from,
186 'extract trimmed' => false,
188 'url' => wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
),
193 foreach ( $titles as $title ) {
194 $resultId = $title->getArticleID();
195 if ( $resultId === 0 ) {
196 $resultId = $nextSpecialPageId;
197 $nextSpecialPageId -= 1;
199 $results[$resultId] = [
201 'redirect from' => null,
203 'extract trimmed' => false,
205 'url' => wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
),
212 * @param string $search
213 * @param array &$results
215 protected function populateResult( $search, &$results ) {
216 $result = $this->getResult();
218 switch ( $this->getFormat() ) {
220 // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
221 $result->addArrayType( null, 'array' );
222 $result->addValue( null, 0, strval( $search ) );
226 foreach ( $results as $r ) {
227 $terms[] = $r['title']->getPrefixedText();
228 $descriptions[] = strval( $r['extract'] );
231 $result->addValue( null, 1, $terms );
232 $result->addValue( null, 2, $descriptions );
233 $result->addValue( null, 3, $urls );
237 // http://msdn.microsoft.com/en-us/library/cc891508%28v=vs.85%29.aspx
246 foreach ( $results as $r ) {
248 'Text' => $r['title']->getPrefixedText(),
251 if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
252 $item['Description'] = $r['extract'];
254 if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
255 $item['Image'] = array_intersect_key( $r['image'], $imageKeys );
257 ApiResult
::setSubelementsList( $item, array_keys( $item ) );
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 );
269 ApiBase
::dieDebug( __METHOD__
, "Unsupported format '{$this->getFormat()}'" );
273 public function getAllowedParams() {
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
284 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
285 ApiBase
::PARAM_TYPE
=> 'namespace',
286 ApiBase
::PARAM_ISMULTI
=> true
290 ApiBase
::PARAM_TYPE
=> [ 'return', 'resolve' ],
293 ApiBase
::PARAM_DFLT
=> 'json',
294 ApiBase
::PARAM_TYPE
=> [ 'json', 'jsonfm', 'xml', 'xmlfm' ],
296 'warningsaserror' => false,
300 protected function getExamplesMessages() {
302 'action=opensearch&search=Te'
303 => 'apihelp-opensearch-example-te',
307 public function getHelpUrls() {
308 return 'https://www.mediawiki.org/wiki/API:Opensearch';
312 * Trim an extract to a sensible length.
314 * Adapted from Extension:OpenSearchXml, which adapted it from
315 * Extension:ActiveAbstract.
317 * @param string $text
318 * @param int $length Target length; actual result will continue to the end of a sentence.
321 public static function trimExtract( $text, $length ) {
322 static $regex = null;
324 if ( $regex === null ) {
326 '([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
327 '。', // full-width ideographic full-stop
328 '.', '!', '?', // double-width roman forms
329 '。', // half-width ideographic full stop
331 $endgroup = implode( '|', $endchars );
332 $end = "(?:$endgroup)";
333 $sentence = ".{{$length},}?$end+";
334 $regex = "/^($sentence)/u";
338 if ( preg_match( $regex, $text, $matches ) ) {
339 return trim( $matches[1] );
341 // Just return the first line
342 return trim( explode( "\n", $text )[0] );
347 * Fetch the template for a type.
349 * @param string $type MIME type
351 * @throws MWException
353 public static function getOpenSearchTemplate( $type ) {
354 $config = MediaWikiServices
::getInstance()->getSearchEngineConfig();
355 $template = $config->getConfig()->get( 'OpenSearchTemplate' );
357 if ( $template && $type === 'application/x-suggestions+json' ) {
361 $ns = implode( '|', $config->defaultNamespaces() );
367 case 'application/x-suggestions+json':
368 return $config->getConfig()->get( 'CanonicalServer' ) . wfScript( 'api' )
369 . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
371 case 'application/x-suggestions+xml':
372 return $config->getConfig()->get( 'CanonicalServer' ) . wfScript( 'api' )
373 . '?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
376 throw new MWException( __METHOD__
. ": Unknown type '$type'" );
381 class ApiOpenSearchFormatJson
extends ApiFormatJson
{
382 private $warningsAsError = false;
384 public function __construct( ApiMain
$main, $fm, $warningsAsError ) {
385 parent
::__construct( $main, "json$fm" );
386 $this->warningsAsError
= $warningsAsError;
389 public function execute() {
390 if ( !$this->getResult()->getResultData( 'error' ) ) {
391 $result = $this->getResult();
393 // Ignore warnings or treat as errors, as requested
394 $warnings = $result->removeValue( 'warnings', null );
395 if ( $this->warningsAsError
&& $warnings ) {
397 'Warnings cannot be represented in OpenSearch JSON format', 'warnings', 0,
398 [ 'warnings' => $warnings ]
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' ]
407 foreach ( $remove as $key ) {
408 $result->removeValue( $key, null );