| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
| 4 | * Copyright © 2008 Brooke Vibber <[email protected]> |
| 5 | * Copyright © 2014 Wikimedia Foundation and contributors |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * http://www.gnu.org/copyleft/gpl.html |
| 21 | * |
| 22 | * @file |
| 23 | */ |
| 24 | |
| 25 | namespace MediaWiki\Api; |
| 26 | |
| 27 | /** |
| 28 | * @ingroup API |
| 29 | */ |
| 30 | class ApiOpenSearchFormatJson extends ApiFormatJson { |
| 31 | /** @var bool */ |
| 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 | |
| 66 | /** @deprecated class alias since 1.43 */ |
| 67 | class_alias( ApiOpenSearchFormatJson::class, 'ApiOpenSearchFormatJson' ); |