MediaWiki  REL1_24
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( ApiMain $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 
00047         return 'application/json';
00048     }
00049 
00050     public function getNeedsRawData() {
00051         return $this->mIsRaw;
00052     }
00053 
00054     public function getWantsHelp() {
00055         // Help is always ugly in JSON
00056         return false;
00057     }
00058 
00059     public function execute() {
00060         $params = $this->extractRequestParams();
00061         $json = FormatJson::encode(
00062             $this->getResultData(),
00063             $this->getIsHtml(),
00064             $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK
00065         );
00066         $callback = $params['callback'];
00067         if ( $callback !== null ) {
00068             $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback );
00069             # Prepend a comment to try to avoid attacks against content
00070             # sniffers, such as bug 68187.
00071             $this->printText( "/**/$callback($json)" );
00072         } else {
00073             $this->printText( $json );
00074         }
00075     }
00076 
00077     public function getAllowedParams() {
00078         return array(
00079             'callback' => null,
00080             'utf8' => false,
00081         );
00082     }
00083 
00084     public function getParamDescription() {
00085         return array(
00086             'callback' => 'If specified, wraps the output into a given function ' .
00087                 'call. For safety, all user-specific data will be restricted.',
00088             'utf8' => 'If specified, encodes most (but not all) non-ASCII ' .
00089                 'characters as UTF-8 instead of replacing them with hexadecimal escape sequences.',
00090         );
00091     }
00092 
00093     public function getDescription() {
00094         if ( $this->mIsRaw ) {
00095             return 'Output data with the debugging elements in JSON format' . parent::getDescription();
00096         }
00097 
00098         return 'Output data in JSON format' . parent::getDescription();
00099     }
00100 }