MediaWiki  1.34.4
ApiOpenSearchFormatJson.php
Go to the documentation of this file.
1 <?php
29  private $warningsAsError = false;
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 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiFormatJson
API JSON output formatter.
Definition: ApiFormatJson.php:27
ApiOpenSearchFormatJson\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiOpenSearchFormatJson.php:36
ApiOpenSearchFormatJson\$warningsAsError
$warningsAsError
Definition: ApiOpenSearchFormatJson.php:29
ApiOpenSearchFormatJson
Definition: ApiOpenSearchFormatJson.php:28
ApiOpenSearchFormatJson\__construct
__construct(ApiMain $main, $fm, $warningsAsError)
Definition: ApiOpenSearchFormatJson.php:31