1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Content;
use MediaWiki\Html\Html;
use MediaWiki\Json\FormatJson;
use MediaWiki\Status\Status;
/**
* JSON text content that can be viewed and edit directly by users.
*
* @newable
* @stable to extend
* @since 1.24
* @ingroup Content
* @author Ori Livneh <ori@wikimedia.org>
* @author Kunal Mehta <legoktm@gmail.com>
*/
class JsonContent extends TextContent {
/**
* @since 1.25
* @var Status
*/
protected $jsonParse;
/**
* @param string $text JSON
* @param string $modelId
* @stable to call
*/
public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
parent::__construct( $text, $modelId );
}
/**
* Decodes the JSON string.
*
* Note that this parses it without casting objects to associative arrays.
* Objects and arrays are kept as distinguishable types in the PHP values.
*
* @return Status
*/
public function getData() {
$this->jsonParse ??= FormatJson::parse( $this->getText() );
return $this->jsonParse;
}
/**
* @return bool Whether content is valid.
*/
public function isValid() {
return $this->getData()->isGood();
}
/**
* Pretty-print JSON.
*
* If called before validation, it may return JSON "null".
*
* @return string
*/
public function beautifyJSON() {
return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
}
/**
* Construct HTML table representation of any JSON value.
*
* See also valueCell, which is similar.
*
* @param mixed $val
* @return string HTML.
*/
public function rootValueTable( $val ) {
if ( is_object( $val ) ) {
$table = $this->objectTable( $val );
} elseif ( is_array( $val ) ) {
// Wrap arrays in another array so that they're visually boxed in a container.
// Otherwise they are visually indistinguishable from a single value.
$table = $this->arrayTable( [ $val ] );
} else {
$table = Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
Html::rawElement( 'tbody', [],
Html::rawElement( 'tr', [],
Html::element( 'td', [], $this->primitiveValue( $val ) )
)
)
);
}
return Html::rawElement( 'div', [ 'class' => 'noresize' ], $table );
}
/**
* Create HTML table representing a JSON object.
*
* @param \stdClass $mapping
* @return string HTML
*/
protected function objectTable( $mapping ) {
$rows = [];
$empty = true;
foreach ( $mapping as $key => $val ) {
$rows[] = $this->objectRow( $key, $val );
$empty = false;
}
if ( $empty ) {
$rows[] = Html::rawElement( 'tr', [],
Html::element( 'td', [ 'class' => 'mw-json-empty' ],
wfMessage( 'content-json-empty-object' )->text()
)
);
}
return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
Html::rawElement( 'tbody', [], implode( '', $rows ) )
);
}
/**
* Create HTML table row representing one object property.
*
* @param string $key
* @param mixed $val
* @return string HTML.
*/
protected function objectRow( $key, $val ) {
$thContent = Html::element( 'span', [], $key );
$th = Html::rawElement( 'th', [], $thContent );
$td = $this->valueCell( $val );
return Html::rawElement( 'tr', [], $th . $td );
}
/**
* Create HTML table representing a JSON array.
*
* @param array $mapping
* @return string HTML
*/
protected function arrayTable( $mapping ) {
$rows = [];
$empty = true;
foreach ( $mapping as $val ) {
$rows[] = $this->arrayRow( $val );
$empty = false;
}
if ( $empty ) {
$rows[] = Html::rawElement( 'tr', [],
Html::element( 'td', [ 'class' => 'mw-json-empty' ],
wfMessage( 'content-json-empty-array' )->text()
)
);
}
return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
);
}
/**
* Create HTML table row representing the value in an array.
*
* @param mixed $val
* @return string HTML.
*/
protected function arrayRow( $val ) {
$td = $this->valueCell( $val );
return Html::rawElement( 'tr', [], $td );
}
/**
* Construct HTML table cell representing any JSON value.
*
* @param mixed $val
* @return string HTML.
*/
protected function valueCell( $val ) {
if ( is_object( $val ) ) {
return Html::rawElement( 'td', [], $this->objectTable( $val ) );
}
if ( is_array( $val ) ) {
return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
}
return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
}
/**
* Construct text representing a JSON primitive value.
*
* @param mixed $val
* @return string Text.
*/
protected function primitiveValue( $val ) {
if ( is_string( $val ) ) {
// Don't FormatJson::encode for strings since we want quotes
// and new lines to render visually instead of escaped.
return '"' . $val . '"';
}
return FormatJson::encode( $val );
}
}
/** @deprecated class alias since 1.43 */
class_alias( JsonContent::class, 'JsonContent' );
|