ApiOpenSearch: Don't output warnings in JSON mode
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 30 Apr 2015 19:39:16 +0000 (15:39 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 30 Apr 2015 19:40:46 +0000 (15:40 -0400)
The OpenSearch JSON format doesn't have any way to report warnings. So
by default we just drop them, but a new option will cause them to be
converted to errors instead.

Bug: T97617
Change-Id: I632bbb99a3d0f04baa5aff211fc52c3f34b13b78

autoload.php
includes/api/ApiOpenSearch.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json

index 81ee8b1..a285049 100644 (file)
@@ -60,6 +60,7 @@ $wgAutoloadLocalClasses = array(
        'ApiModuleManager' => __DIR__ . '/includes/api/ApiModuleManager.php',
        'ApiMove' => __DIR__ . '/includes/api/ApiMove.php',
        'ApiOpenSearch' => __DIR__ . '/includes/api/ApiOpenSearch.php',
+       'ApiOpenSearchFormatJson' => __DIR__ . '/includes/api/ApiOpenSearch.php',
        'ApiOptions' => __DIR__ . '/includes/api/ApiOptions.php',
        'ApiPageSet' => __DIR__ . '/includes/api/ApiPageSet.php',
        'ApiParamInfo' => __DIR__ . '/includes/api/ApiParamInfo.php',
index 36026c2..16d8b55 100644 (file)
@@ -61,7 +61,9 @@ class ApiOpenSearch extends ApiBase {
        public function getCustomPrinter() {
                switch ( $this->getFormat() ) {
                        case 'json':
-                               return $this->getMain()->createPrinterByName( 'json' . $this->fm );
+                               return new ApiOpenSearchFormatJson(
+                                       $this->getMain(), $this->fm, $this->getParameter( 'warningsaserror' )
+                               );
 
                        case 'xml':
                                $printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm );
@@ -212,7 +214,7 @@ class ApiOpenSearch extends ApiBase {
                switch ( $this->getFormat() ) {
                        case 'json':
                                // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
-                               $result->addArrayType( null, 'BCarray' );
+                               $result->addArrayType( null, 'array' );
                                $result->addValue( null, 0, strval( $search ) );
                                $terms = array();
                                $descriptions = array();
@@ -286,7 +288,8 @@ class ApiOpenSearch extends ApiBase {
                        'format' => array(
                                ApiBase::PARAM_DFLT => 'json',
                                ApiBase::PARAM_TYPE => array( 'json', 'jsonfm', 'xml', 'xmlfm' ),
-                       )
+                       ),
+                       'warningsaserror' => false,
                );
        }
 
@@ -370,3 +373,26 @@ class ApiOpenSearch extends ApiBase {
                }
        }
 }
+
+class ApiOpenSearchFormatJson extends ApiFormatJson {
+       private $warningsAsError = false;
+
+       public function __construct( ApiMain $main, $fm, $warningsAsError ) {
+               parent::__construct( $main, "json$fm" );
+               $this->warningsAsError = $warningsAsError;
+       }
+
+       public function execute() {
+               if ( !$this->getResult()->getResultData( 'error' ) ) {
+                       $warnings = $this->getResult()->removeValue( 'warnings' );
+                       if ( $this->warningsAsError && $warnings ) {
+                               $this->dieUsage(
+                                       'Warnings cannot be represented in OpenSearch JSON format', 'warnings', 0,
+                                       array( 'warnings' => $warnings )
+                               );
+                       }
+               }
+
+               parent::execute();
+       }
+}
index 87064a2..9b1501b 100644 (file)
        "apihelp-opensearch-param-suggest": "Do nothing if <var>[[mw:Manual:$wgEnableOpenSearchSuggest|$wgEnableOpenSearchSuggest]]</var> is false.",
        "apihelp-opensearch-param-redirects": "How to handle redirects:\n;return:Return the redirect itself.\n;resolve:Return the target page. May return fewer than $1limit results.\nFor historical reasons, the default is \"return\" for $1format=json and \"resolve\" for other formats.",
        "apihelp-opensearch-param-format": "The format of the output.",
+       "apihelp-opensearch-param-warningsaserror": "If warnings are raised with <kbd>format=json</kbd>, return an API error instead of ignoring them.",
        "apihelp-opensearch-example-te": "Find pages beginning with <kbd>Te</kbd>.",
 
        "apihelp-options-description": "Change preferences of the current user.\n\nOnly options which are registered in core or in one of installed extensions, or options with keys prefixed with \"userjs-\" (intended to be used by user scripts), can be set.",
index 876f598..8d3fcc7 100644 (file)
        "apihelp-opensearch-param-suggest": "{{doc-apihelp-param|opensearch|suggest}}",
        "apihelp-opensearch-param-redirects": "{{doc-apihelp-param|opensearch|redirects}}",
        "apihelp-opensearch-param-format": "{{doc-apihelp-param|opensearch|format}}",
+       "apihelp-opensearch-param-warningsaserror": "{{doc-apihelp-param|opensearch|warningsaserror}}",
        "apihelp-opensearch-example-te": "{{doc-apihelp-example|opensearch}}",
        "apihelp-options-description": "{{doc-apihelp-description|options}}",
        "apihelp-options-param-reset": "{{doc-apihelp-param|options|reset}}",