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