ApiFormatJson.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API JSON output formatter
  24. * @ingroup API
  25. */
  26. class ApiFormatJson extends ApiFormatBase {
  27. private $isRaw;
  28. public function __construct( ApiMain $main, $format ) {
  29. parent::__construct( $main, $format );
  30. $this->isRaw = ( $format === 'rawfm' );
  31. if ( $this->getMain()->getCheck( 'callback' ) ) {
  32. # T94015: jQuery appends a useless '_' parameter in jsonp mode.
  33. # Mark the parameter as used in that case to avoid a warning that's
  34. # outside the control of the end user.
  35. # (and do it here because ApiMain::reportUnusedParams() gets called
  36. # before our ::execute())
  37. $this->getMain()->markParamsUsed( '_' );
  38. }
  39. }
  40. public function getMimeType() {
  41. $params = $this->extractRequestParams();
  42. // callback:
  43. if ( isset( $params['callback'] ) ) {
  44. return 'text/javascript';
  45. }
  46. return 'application/json';
  47. }
  48. public function execute() {
  49. $params = $this->extractRequestParams();
  50. $opt = 0;
  51. if ( $this->isRaw ) {
  52. $opt |= FormatJson::ALL_OK;
  53. $transform = [];
  54. } else {
  55. switch ( $params['formatversion'] ) {
  56. case 1:
  57. $opt |= $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK;
  58. $transform = [
  59. 'BC' => [],
  60. 'Types' => [ 'AssocAsObject' => true ],
  61. 'Strip' => 'all',
  62. ];
  63. break;
  64. case 2:
  65. case 'latest':
  66. $opt |= $params['ascii'] ? FormatJson::XMLMETA_OK : FormatJson::ALL_OK;
  67. $transform = [
  68. 'Types' => [ 'AssocAsObject' => true ],
  69. 'Strip' => 'all',
  70. ];
  71. break;
  72. default:
  73. // Should have been caught during parameter validation
  74. // @codeCoverageIgnoreStart
  75. $this->dieDebug( __METHOD__, 'Unknown value for \'formatversion\'' );
  76. // @codeCoverageIgnoreEnd
  77. }
  78. }
  79. $data = $this->getResult()->getResultData( null, $transform );
  80. $json = FormatJson::encode( $data, $this->getIsHtml(), $opt );
  81. if ( $json === false ) {
  82. // This should never happen, but it's a bug which could crop up
  83. // if you use ApiResult::NO_VALIDATE for instance.
  84. // @codeCoverageIgnoreStart
  85. $this->dieDebug( __METHOD__, 'Unable to encode API result as JSON' );
  86. // @codeCoverageIgnoreEnd
  87. }
  88. // T68776: OutputHandler::mangleFlashPolicy() avoids a nasty bug in
  89. // Flash, but what it does isn't friendly for the API, so we need to
  90. // work around it.
  91. if ( preg_match( '/\<\s*cross-domain-policy(?=\s|\>)/i', $json ) ) {
  92. $json = preg_replace(
  93. '/\<(\s*cross-domain-policy(?=\s|\>))/i', '\\u003C$1', $json
  94. );
  95. }
  96. if ( isset( $params['callback'] ) ) {
  97. $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $params['callback'] );
  98. # Prepend a comment to try to avoid attacks against content
  99. # sniffers, such as T70187.
  100. $this->printText( "/**/$callback($json)" );
  101. } else {
  102. $this->printText( $json );
  103. }
  104. }
  105. public function getAllowedParams() {
  106. if ( $this->isRaw ) {
  107. return parent::getAllowedParams();
  108. }
  109. $ret = parent::getAllowedParams() + [
  110. 'callback' => [
  111. ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-callback',
  112. ],
  113. 'utf8' => [
  114. ApiBase::PARAM_DFLT => false,
  115. ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-utf8',
  116. ],
  117. 'ascii' => [
  118. ApiBase::PARAM_DFLT => false,
  119. ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-ascii',
  120. ],
  121. 'formatversion' => [
  122. ApiBase::PARAM_TYPE => [ '1', '2', 'latest' ],
  123. ApiBase::PARAM_DFLT => '1',
  124. ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-formatversion',
  125. ],
  126. ];
  127. return $ret;
  128. }
  129. }