MediaWiki master
ApiOpenSearchFormatJson.php
Go to the documentation of this file.
1<?php
29 private $warningsAsError;
30
31 public function __construct( ApiMain $main, $fm, $warningsAsError ) {
32 parent::__construct( $main, "json$fm" );
33 $this->warningsAsError = $warningsAsError;
34 }
35
36 public function execute() {
37 $result = $this->getResult();
38 if ( !$result->getResultData( 'error' ) && !$result->getResultData( 'errors' ) ) {
39 // Ignore warnings or treat as errors, as requested
40 $warnings = $result->removeValue( 'warnings', null );
41 if ( $this->warningsAsError && $warnings ) {
42 $this->dieWithError(
43 'apierror-opensearch-json-warnings',
44 'warnings',
45 [ 'warnings' => $warnings ]
46 );
47 }
48
49 // Ignore any other unexpected keys (e.g. from $wgDebugToolbar)
50 $remove = array_keys( array_diff_key(
51 $result->getResultData(),
52 [ 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ]
53 ) );
54 foreach ( $remove as $key ) {
55 $result->removeValue( $key, null );
56 }
57 }
58
59 parent::execute();
60 }
61}
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1540
getResult()
Get the result object.
Definition ApiBase.php:681
API JSON output formatter.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:67
__construct(ApiMain $main, $fm, $warningsAsError)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.