ApiFormatJson.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * Created on Sep 19, 2006
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiFormatBase.php');
  27. }
  28. /**
  29. * @ingroup API
  30. */
  31. class ApiFormatJson extends ApiFormatBase {
  32. private $mIsRaw;
  33. public function __construct($main, $format) {
  34. parent :: __construct($main, $format);
  35. $this->mIsRaw = ($format === 'rawfm');
  36. }
  37. public function getMimeType() {
  38. return 'application/json';
  39. }
  40. public function getNeedsRawData() {
  41. return $this->mIsRaw;
  42. }
  43. public function execute() {
  44. $prefix = $suffix = "";
  45. $params = $this->extractRequestParams();
  46. $callback = $params['callback'];
  47. if(!is_null($callback)) {
  48. $prefix = preg_replace("/[^][.\\'\\\"_A-Za-z0-9]/", "", $callback ) . "(";
  49. $suffix = ")";
  50. }
  51. // Some versions of PHP have a broken json_encode, see PHP bug
  52. // 46944. Test encoding an affected character (U+20000) to
  53. // avoid this.
  54. if (!function_exists('json_encode') || $this->getIsHtml() || strtolower(json_encode("\xf0\xa0\x80\x80")) != '"\ud840\udc00"') {
  55. $json = new Services_JSON();
  56. $this->printText($prefix . $json->encode($this->getResultData(), $this->getIsHtml()) . $suffix);
  57. } else {
  58. $this->printText($prefix . json_encode($this->getResultData()) . $suffix);
  59. }
  60. }
  61. public function getAllowedParams() {
  62. return array (
  63. 'callback' => null
  64. );
  65. }
  66. public function getParamDescription() {
  67. return array (
  68. 'callback' => 'If specified, wraps the output into a given function call. For safety, all user-specific data will be restricted.',
  69. );
  70. }
  71. public function getDescription() {
  72. if ($this->mIsRaw)
  73. return 'Output data with the debuging elements in JSON format' . parent :: getDescription();
  74. else
  75. return 'Output data in JSON format' . parent :: getDescription();
  76. }
  77. public function getVersion() {
  78. return __CLASS__ . ': $Id: ApiFormatJson.php 48713 2009-03-23 19:58:07Z catrope $';
  79. }
  80. }