Update comment for r64876
[lhc/web/wiklou.git] / includes / api / ApiFormatJson.php
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiFormatBase.php' );
30 }
31
32 /**
33 * API JSON output formatter
34 * @ingroup API
35 */
36 class ApiFormatJson extends ApiFormatBase {
37
38 private $mIsRaw;
39
40 public function __construct( $main, $format ) {
41 parent::__construct( $main, $format );
42 $this->mIsRaw = ( $format === 'rawfm' );
43 }
44
45 public function getMimeType() {
46 $params = $this->extractRequestParams();
47 // callback:
48 if ( $params['callback'] ) {
49 return 'text/javascript';
50 }
51 return 'application/json';
52 }
53
54 public function getNeedsRawData() {
55 return $this->mIsRaw;
56 }
57
58 public function getWantsHelp() {
59 // Help is always ugly in JSON
60 return false;
61 }
62
63 public function execute() {
64 $prefix = $suffix = '';
65
66 $params = $this->extractRequestParams();
67 $callback = $params['callback'];
68 if ( !is_null( $callback ) ) {
69 $prefix = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback ) . '(';
70 $suffix = ')';
71 }
72 $this->printText(
73 $prefix .
74 FormatJson::encode( $this->getResultData(), $this->getIsHtml() ) .
75 $suffix
76 );
77 }
78
79 public function getAllowedParams() {
80 return array(
81 'callback' => null,
82 );
83 }
84
85 public function getParamDescription() {
86 return array(
87 'callback' => 'If specified, wraps the output into a given function call. For safety, all user-specific data will be restricted.',
88 );
89 }
90
91 public function getDescription() {
92 if ( $this->mIsRaw ) {
93 return 'Output data with the debuging elements in JSON format' . parent::getDescription();
94 } else {
95 return 'Output data in JSON format' . parent::getDescription();
96 }
97 }
98
99 public function getVersion() {
100 return __CLASS__ . ': $Id$';
101 }
102 }