MediaWiki  REL1_22
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         $params = $this->extractRequestParams();
00060         $json = FormatJson::encode(
00061             $this->getResultData(),
00062             $this->getIsHtml(),
00063             $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK
00064         );
00065         $callback = $params['callback'];
00066         if ( $callback !== null ) {
00067             $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback );
00068             # Prepend a comment to try to avoid attacks against content
00069             # sniffers, such as bug 68187.
00070             $this->printText( "/**/$callback($json)" );
00071         } else {
00072             $this->printText( $json );
00073         }
00074     }
00075 
00076     public function getAllowedParams() {
00077         return array(
00078             'callback' => null,
00079             'utf8' => false,
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             'utf8' => 'If specified, encodes most (but not all) non-ASCII characters as UTF-8 instead of replacing them with hexadecimal escape sequences.',
00087         );
00088     }
00089 
00090     public function getDescription() {
00091         if ( $this->mIsRaw ) {
00092             return 'Output data with the debugging elements in JSON format' . parent::getDescription();
00093         } else {
00094             return 'Output data in JSON format' . parent::getDescription();
00095         }
00096     }
00097 }