Merge "Html::closeElement: Don't omit closing tags."
[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 /**
28 * API JSON output formatter
29 * @ingroup API
30 */
31 class ApiFormatJson extends ApiFormatBase {
32
33 private $mIsRaw;
34
35 public function __construct( ApiMain $main, $format ) {
36 parent::__construct( $main, $format );
37 $this->mIsRaw = ( $format === 'rawfm' );
38 }
39
40 public function getMimeType() {
41 $params = $this->extractRequestParams();
42 // callback:
43 if ( $params['callback'] ) {
44 return 'text/javascript';
45 }
46
47 return 'application/json';
48 }
49
50 public function getNeedsRawData() {
51 return $this->mIsRaw;
52 }
53
54 public function getWantsHelp() {
55 // Help is always ugly in JSON
56 return false;
57 }
58
59 public function execute() {
60 $params = $this->extractRequestParams();
61 $json = FormatJson::encode(
62 $this->getResultData(),
63 $this->getIsHtml(),
64 $params['utf8'] ? FormatJson::ALL_OK : FormatJson::XMLMETA_OK
65 );
66 $callback = $params['callback'];
67 if ( $callback !== null ) {
68 $callback = preg_replace( "/[^][.\\'\\\"_A-Za-z0-9]/", '', $callback );
69 # Prepend a comment to try to avoid attacks against content
70 # sniffers, such as bug 68187.
71 $this->printText( "/**/$callback($json)" );
72 } else {
73 $this->printText( $json );
74 }
75 }
76
77 public function getAllowedParams() {
78 return array(
79 'callback' => null,
80 'utf8' => false,
81 );
82 }
83
84 public function getParamDescription() {
85 return array(
86 'callback' => 'If specified, wraps the output into a given function ' .
87 'call. For safety, all user-specific data will be restricted.',
88 'utf8' => 'If specified, encodes most (but not all) non-ASCII ' .
89 'characters as UTF-8 instead of replacing them with hexadecimal escape sequences.',
90 );
91 }
92
93 public function getDescription() {
94 if ( $this->mIsRaw ) {
95 return 'Output data with the debugging elements in JSON format' . parent::getDescription();
96 }
97
98 return 'Output data in JSON format' . parent::getDescription();
99 }
100 }