ApiOpenSearch: Ignore any other unexpected keys in json output
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 28 May 2015 14:38:19 +0000 (10:38 -0400)
committerUmherirrender <umherirrender_de.wp@web.de>
Fri, 29 May 2015 20:29:07 +0000 (20:29 +0000)
Iad295d2f fixed warnings to not break the opensearch json output. But
other stuff like $wgDebugToolbar can do the same thing, so let's strip
all unrecognized keys to avoid that problem.

Bug: T100635
Change-Id: I20b6502482af537ccb703f400508960038213a91

includes/api/ApiOpenSearch.php

index a93b7cc..0adb464 100644 (file)
@@ -384,13 +384,25 @@ class ApiOpenSearchFormatJson extends ApiFormatJson {
 
        public function execute() {
                if ( !$this->getResult()->getResultData( 'error' ) ) {
-                       $warnings = $this->getResult()->removeValue( 'warnings', null );
+                       $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();