MediaWiki master
ApiOpenSearchFormatJson.php
Go to the documentation of this file.
1<?php
11namespace MediaWiki\Api;
12
17
18 private bool $warningsAsError;
19
20 public function __construct( ApiMain $main, string $format, bool $warningsAsError ) {
21 parent::__construct( $main, $format );
22 $this->warningsAsError = $warningsAsError;
23 }
24
25 public function execute() {
26 $result = $this->getResult();
27 if ( !$result->getResultData( 'error' ) && !$result->getResultData( 'errors' ) ) {
28 // Ignore warnings or treat as errors, as requested
29 $warnings = $result->removeValue( 'warnings', null );
30 if ( $this->warningsAsError && $warnings ) {
31 $this->dieWithError(
32 'apierror-opensearch-json-warnings',
33 'warnings',
34 [ 'warnings' => $warnings ]
35 );
36 }
37
38 // Ignore any other unexpected keys (e.g. from $wgDebugToolbar)
39 $remove = array_keys( array_diff_key(
40 $result->getResultData(),
41 [ 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ]
42 ) );
43 foreach ( $remove as $key ) {
44 $result->removeValue( $key, null );
45 }
46 }
47
48 parent::execute();
49 }
50}
51
53class_alias( ApiOpenSearchFormatJson::class, 'ApiOpenSearchFormatJson' );
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getResult()
Get the result object.
Definition ApiBase.php:682
API JSON output formatter.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:65
__construct(ApiMain $main, string $format, bool $warningsAsError)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.