X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiOpenSearch.php;h=0adb464d5ab20d1ea6fdf29f8a294447a2b5c4ba;hb=70e760ee4e37edad1be0573b37c17fe6899e4806;hp=8c03dceeff4c968b1be5dfd1001c86619c805c4e;hpb=9e18610457ce599db51d3b14af83c31053ea0ace;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiOpenSearch.php b/includes/api/ApiOpenSearch.php index 8c03dceeff..0adb464d5a 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 ); @@ -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,38 @@ 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' ) ) { + $result = $this->getResult(); + + // Ignore warnings or treat as errors, as requested + $warnings = $result->removeValue( 'warnings', null ); + if ( $this->warningsAsError && $warnings ) { + $this->dieUsage( + 'Warnings cannot be represented in OpenSearch JSON format', 'warnings', 0, + array( 'warnings' => $warnings ) + ); + } + + // Ignore any other unexpected keys (e.g. from $wgDebugToolbar) + $remove = array_keys( array_diff_key( + $result->getResultData(), + array( 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ) + ) ); + foreach ( $remove as $key ) { + $result->removeValue( $key, null ); + } + } + + parent::execute(); + } +}