MediaWiki master
ApiOpenSearchFormatJson.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Api;
26
32 private $warningsAsError;
33
34 public function __construct( ApiMain $main, string $fm, $warningsAsError ) {
35 parent::__construct( $main, "json$fm" );
36 $this->warningsAsError = $warningsAsError;
37 }
38
39 public function execute() {
40 $result = $this->getResult();
41 if ( !$result->getResultData( 'error' ) && !$result->getResultData( 'errors' ) ) {
42 // Ignore warnings or treat as errors, as requested
43 $warnings = $result->removeValue( 'warnings', null );
44 if ( $this->warningsAsError && $warnings ) {
45 $this->dieWithError(
46 'apierror-opensearch-json-warnings',
47 'warnings',
48 [ 'warnings' => $warnings ]
49 );
50 }
51
52 // Ignore any other unexpected keys (e.g. from $wgDebugToolbar)
53 $remove = array_keys( array_diff_key(
54 $result->getResultData(),
55 [ 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ]
56 ) );
57 foreach ( $remove as $key ) {
58 $result->removeValue( $key, null );
59 }
60 }
61
62 parent::execute();
63 }
64}
65
67class_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:78
__construct(ApiMain $main, string $fm, $warningsAsError)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.