MediaWiki
REL1_20
|
00001 <?php 00031 class ApiFormatJson extends ApiFormatBase { 00032 00033 private $mIsRaw; 00034 00035 public function __construct( $main, $format ) { 00036 parent::__construct( $main, $format ); 00037 $this->mIsRaw = ( $format === 'rawfm' ); 00038 } 00039 00040 public function getMimeType() { 00041 $params = $this->extractRequestParams(); 00042 // callback: 00043 if ( $params['callback'] ) { 00044 return 'text/javascript'; 00045 } 00046 return 'application/json'; 00047 } 00048 00049 public function getNeedsRawData() { 00050 return $this->mIsRaw; 00051 } 00052 00053 public function getWantsHelp() { 00054 // Help is always ugly in JSON 00055 return false; 00056 } 00057 00058 public function execute() { 00059 $prefix = $suffix = ''; 00060 00061 $params = $this->extractRequestParams(); 00062 $callback = $params['callback']; 00063 if ( !is_null( $callback ) ) { 00064 $prefix = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback ) . '('; 00065 $suffix = ')'; 00066 } 00067 $this->printText( 00068 $prefix . 00069 FormatJson::encode( $this->getResultData(), $this->getIsHtml() ) . 00070 $suffix 00071 ); 00072 } 00073 00074 public function getAllowedParams() { 00075 return array( 00076 'callback' => null, 00077 ); 00078 } 00079 00080 public function getParamDescription() { 00081 return array( 00082 'callback' => 'If specified, wraps the output into a given function call. For safety, all user-specific data will be restricted.', 00083 ); 00084 } 00085 00086 public function getDescription() { 00087 if ( $this->mIsRaw ) { 00088 return 'Output data with the debuging elements in JSON format' . parent::getDescription(); 00089 } else { 00090 return 'Output data in JSON format' . parent::getDescription(); 00091 } 00092 } 00093 00094 public function getVersion() { 00095 return __CLASS__ . ': $Id$'; 00096 } 00097 }