ApiOpenSearchFormatJson.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  4. * Copyright © 2008 Brion Vibber <brion@wikimedia.org>
  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. * @ingroup API
  26. */
  27. class ApiOpenSearchFormatJson extends ApiFormatJson {
  28. private $warningsAsError = false;
  29. public function __construct( ApiMain $main, $fm, $warningsAsError ) {
  30. parent::__construct( $main, "json$fm" );
  31. $this->warningsAsError = $warningsAsError;
  32. }
  33. public function execute() {
  34. $result = $this->getResult();
  35. if ( !$result->getResultData( 'error' ) && !$result->getResultData( 'errors' ) ) {
  36. // Ignore warnings or treat as errors, as requested
  37. $warnings = $result->removeValue( 'warnings', null );
  38. if ( $this->warningsAsError && $warnings ) {
  39. $this->dieWithError(
  40. 'apierror-opensearch-json-warnings',
  41. 'warnings',
  42. [ 'warnings' => $warnings ]
  43. );
  44. }
  45. // Ignore any other unexpected keys (e.g. from $wgDebugToolbar)
  46. $remove = array_keys( array_diff_key(
  47. $result->getResultData(),
  48. [ 0 => 'search', 1 => 'terms', 2 => 'descriptions', 3 => 'urls' ]
  49. ) );
  50. foreach ( $remove as $key ) {
  51. $result->removeValue( $key, null );
  52. }
  53. }
  54. parent::execute();
  55. }
  56. }