OpenSearch: Error for unsupported formats and adding format=jsonfm
authorTimo Tijhof <ttijhof@wikimedia.org>
Wed, 27 Mar 2013 21:31:05 +0000 (22:31 +0100)
committerTimo Tijhof <ttijhof@wikimedia.org>
Thu, 28 Mar 2013 20:05:10 +0000 (21:05 +0100)
Previously it would just completely ignore the format parameter.
Making it always serve JSON no matter the value of the format
parameter.

Now it properly errors when doing (for example) format=txt or format=xml.

Also adding support for jsonfm.

Change-Id: Ia98f54f41f39006312fb49ecd718f0f161f27c37

RELEASE-NOTES-1.21
includes/api/ApiOpenSearch.php

index ecb37d5..f8ce4a2 100644 (file)
@@ -284,6 +284,8 @@ production.
 * The JSON output formatter now leaves forward slashes unescaped to improve human
   readability of URLs and similar strings. Also, a "utf8" option is now provided
   to use UTF-8 encoding instead of hex escape codes for most non-ASCII characters.
+* action=opensearch no longer silently ignores the format parameter.
+* action=opensearch now supports format=jsonfm.
 
 === API internal changes in 1.21 ===
 * For debugging only, a new global $wgDebugAPI removes many API restrictions when true.
index caf361a..315ace3 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 /**
- *
- *
  * Created on Oct 13, 2006
  *
  * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  */
 class ApiOpenSearch extends ApiBase {
 
+       /**
+        * Override built-in handling of format parameter.
+        * Only JSON is supported.
+        *
+        * @return ApiFormatBase
+        */
        public function getCustomPrinter() {
-               return $this->getMain()->createPrinterByName( 'json' );
+               $params = $this->extractRequestParams();
+               $format = $params['format'];
+               $allowed = array( 'json', 'jsonfm' );
+               if ( in_array( $format, $allowed ) ) {
+                       return $this->getMain()->createPrinterByName( $format );
+               }
+               return $this->getMain()->createPrinterByName( $allowed[0] );
        }
 
        public function execute() {
@@ -94,6 +104,10 @@ class ApiOpenSearch extends ApiBase {
                                ApiBase::PARAM_ISMULTI => true
                        ),
                        'suggest' => false,
+                       'format' => array(
+                               ApiBase::PARAM_DFLT => 'json',
+                               ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
+                       )
                );
        }
 
@@ -103,6 +117,7 @@ class ApiOpenSearch extends ApiBase {
                        'limit' => 'Maximum amount of results to return',
                        'namespace' => 'Namespaces to search',
                        'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
+                       'format' => 'The format of the output',
                );
        }