From: Brad Jorsch Date: Thu, 30 Apr 2015 19:39:16 +0000 (-0400) Subject: ApiOpenSearch: Don't output warnings in JSON mode X-Git-Tag: 1.31.0-rc.0~11395^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=663d555abd2b4422ae26e7aceba48ae3df8b2a95;p=lhc%2Fweb%2Fwiklou.git ApiOpenSearch: Don't output warnings in JSON mode 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 --- diff --git a/autoload.php b/autoload.php index 81ee8b1833..a285049985 100644 --- a/autoload.php +++ b/autoload.php @@ -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', diff --git a/includes/api/ApiOpenSearch.php b/includes/api/ApiOpenSearch.php index 36026c22f4..16d8b5504a 100644 --- a/includes/api/ApiOpenSearch.php +++ b/includes/api/ApiOpenSearch.php @@ -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(); + } +} diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 87064a21b6..9b1501b567 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -242,6 +242,7 @@ "apihelp-opensearch-param-suggest": "Do nothing if [[mw:Manual:$wgEnableOpenSearchSuggest|$wgEnableOpenSearchSuggest]] 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 format=json, return an API error instead of ignoring them.", "apihelp-opensearch-example-te": "Find pages beginning with Te.", "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.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 876f598083..8d3fcc764a 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -224,6 +224,7 @@ "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}}",