[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/api/ -> ApiFormatJson.php (source)

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on Sep 19, 2006
   6   *
   7   * Copyright © 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   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22   * http://www.gnu.org/copyleft/gpl.html
  23   *
  24   * @file
  25   */
  26  
  27  /**
  28   * API JSON output formatter
  29   * @ingroup API
  30   */
  31  class ApiFormatJson extends ApiFormatBase {
  32  
  33      private $mIsRaw;
  34  
  35  	public function __construct( ApiMain $main, $format ) {
  36          parent::__construct( $main, $format );
  37          $this->mIsRaw = ( $format === 'rawfm' );
  38      }
  39  
  40  	public function getMimeType() {
  41          $params = $this->extractRequestParams();
  42          // callback:
  43          if ( $params['callback'] ) {
  44              return 'text/javascript';
  45          }
  46  
  47          return 'application/json';
  48      }
  49  
  50  	public function getNeedsRawData() {
  51          return $this->mIsRaw;
  52      }
  53  
  54  	public function getWantsHelp() {
  55          // Help is always ugly in JSON
  56          return false;
  57      }
  58  
  59  	public function execute() {
  60          $params = $this->extractRequestParams();
  61          $json = FormatJson::encode(
  62              $this->getResultData(),
  63              $this->getIsHtml(),
  64              $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK
  65          );
  66  
  67          // Bug 66776: wfMangleFlashPolicy() is needed to avoid a nasty bug in
  68          // Flash, but what it does isn't friendly for the API, so we need to
  69          // work around it.
  70          if ( preg_match( '/\<\s*cross-domain-policy\s*\>/i', $json ) ) {
  71              $json = preg_replace(
  72                  '/\<(\s*cross-domain-policy\s*)\>/i', '\\u003C$1\\u003E', $json
  73              );
  74          }
  75  
  76          $callback = $params['callback'];
  77          if ( $callback !== null ) {
  78              $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback );
  79              # Prepend a comment to try to avoid attacks against content
  80              # sniffers, such as bug 68187.
  81              $this->printText( "/**/$callback($json)" );
  82          } else {
  83              $this->printText( $json );
  84          }
  85      }
  86  
  87  	public function getAllowedParams() {
  88          return array(
  89              'callback' => null,
  90              'utf8' => false,
  91          );
  92      }
  93  
  94  	public function getParamDescription() {
  95          return array(
  96              'callback' => 'If specified, wraps the output into a given function ' .
  97                  'call. For safety, all user-specific data will be restricted.',
  98              'utf8' => 'If specified, encodes most (but not all) non-ASCII ' .
  99                  'characters as UTF-8 instead of replacing them with hexadecimal escape sequences.',
 100          );
 101      }
 102  
 103  	public function getDescription() {
 104          if ( $this->mIsRaw ) {
 105              return 'Output data with the debugging elements in JSON format' . parent::getDescription();
 106          }
 107  
 108          return 'Output data in JSON format' . parent::getDescription();
 109      }
 110  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1