MediaWiki  REL1_19
ApiFormatJson.php
Go to the documentation of this file.
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                         # Prepend a comment to try to avoid attacks against content
00066                         # sniffers, such as bug 68187.
00067                         $prefix = ( "/**/$prefix" );
00068                         $suffix = ')';
00069                 }
00070                 $this->printText(
00071                         $prefix .
00072                         FormatJson::encode( $this->getResultData(), $this->getIsHtml() ) .
00073                         $suffix
00074                 );
00075         }
00076 
00077         public function getAllowedParams() {
00078                 return array(
00079                         'callback'  => null,
00080                 );
00081         }
00082 
00083         public function getParamDescription() {
00084                 return array(
00085                         'callback' => 'If specified, wraps the output into a given function call. For safety, all user-specific data will be restricted.',
00086                 );
00087         }
00088 
00089         public function getDescription() {
00090                 if ( $this->mIsRaw ) {
00091                         return 'Output data with the debuging elements in JSON format' . parent::getDescription();
00092                 } else {
00093                         return 'Output data in JSON format' . parent::getDescription();
00094                 }
00095         }
00096 
00097         public function getVersion() {
00098                 return __CLASS__ . ': $Id$';
00099         }
00100 }