7 * @author Ori Livneh <ori@wikimedia.org>
8 * @author Kunal Mehta <legoktm@gmail.com>
12 * Represents the content of a JSON content.
15 class JsonContent
extends TextContent
{
24 * @param string $text JSON
25 * @param string $modelId
27 public function __construct( $text, $modelId = CONTENT_MODEL_JSON
) {
28 parent
::__construct( $text, $modelId );
32 * Decodes the JSON string.
34 * Note that this parses it without casting objects to associative arrays.
35 * Objects and arrays are kept as distinguishable types in the PHP values.
39 public function getData() {
40 if ( $this->jsonParse
=== null ) {
41 $this->jsonParse
= FormatJson
::parse( $this->getText() );
43 return $this->jsonParse
;
47 * @return bool Whether content is valid.
49 public function isValid() {
50 return $this->getData()->isGood();
56 * If called before validation, it may return JSON "null".
60 public function beautifyJSON() {
61 return FormatJson
::encode( $this->getData()->getValue(), true, FormatJson
::UTF8_OK
);
65 * Beautifies JSON prior to save.
69 * @param ParserOptions $popts
72 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
73 // FIXME: WikiPage::doEditContent invokes PST before validation. As such, native data
74 // may be invalid (though PST result is discarded later in that case).
75 if ( !$this->isValid() ) {
79 return new static( self
::normalizeLineEndings( $this->beautifyJSON() ) );
83 * Set the HTML and add the appropriate styles.
87 * @param ParserOptions $options
88 * @param bool $generateHtml
89 * @param ParserOutput &$output
91 protected function fillParserOutput( Title
$title, $revId,
92 ParserOptions
$options, $generateHtml, ParserOutput
&$output
94 // FIXME: WikiPage::doEditContent generates parser output before validation.
95 // As such, native data may be invalid (though output is discarded later in that case).
96 if ( $generateHtml && $this->isValid() ) {
97 $output->setText( $this->rootValueTable( $this->getData()->getValue() ) );
98 $output->addModuleStyles( 'mediawiki.content.json' );
100 $output->setText( '' );
105 * Construct HTML table representation of any JSON value.
107 * See also valueCell, which is similar.
110 * @return string HTML.
112 protected function rootValueTable( $val ) {
113 if ( is_object( $val ) ) {
114 return $this->objectTable( $val );
117 if ( is_array( $val ) ) {
118 // Wrap arrays in another array so that they're visually boxed in a container.
119 // Otherwise they are visually indistinguishable from a single value.
120 return $this->arrayTable( [ $val ] );
123 return Html
::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
124 Html
::rawElement( 'tbody', [],
125 Html
::rawElement( 'tr', [],
126 Html
::element( 'td', [], $this->primitiveValue( $val ) )
133 * Create HTML table representing a JSON object.
135 * @param stdClass $mapping
136 * @return string HTML
138 protected function objectTable( $mapping ) {
142 foreach ( $mapping as $key => $val ) {
143 $rows[] = $this->objectRow( $key, $val );
147 $rows[] = Html
::rawElement( 'tr', [],
148 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
149 wfMessage( 'content-json-empty-object' )->text()
153 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
154 Html
::rawElement( 'tbody', [], implode( '', $rows ) )
159 * Create HTML table row representing one object property.
163 * @return string HTML.
165 protected function objectRow( $key, $val ) {
166 $th = Html
::element( 'th', [], $key );
167 $td = $this->valueCell( $val );
168 return Html
::rawElement( 'tr', [], $th . $td );
172 * Create HTML table representing a JSON array.
174 * @param array $mapping
175 * @return string HTML
177 protected function arrayTable( $mapping ) {
181 foreach ( $mapping as $val ) {
182 $rows[] = $this->arrayRow( $val );
186 $rows[] = Html
::rawElement( 'tr', [],
187 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
188 wfMessage( 'content-json-empty-array' )->text()
192 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
193 Html
::rawElement( 'tbody', [], implode( "\n", $rows ) )
198 * Create HTML table row representing the value in an array.
201 * @return string HTML.
203 protected function arrayRow( $val ) {
204 $td = $this->valueCell( $val );
205 return Html
::rawElement( 'tr', [], $td );
209 * Construct HTML table cell representing any JSON value.
212 * @return string HTML.
214 protected function valueCell( $val ) {
215 if ( is_object( $val ) ) {
216 return Html
::rawElement( 'td', [], $this->objectTable( $val ) );
219 if ( is_array( $val ) ) {
220 return Html
::rawElement( 'td', [], $this->arrayTable( $val ) );
223 return Html
::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
227 * Construct text representing a JSON primitive value.
230 * @return string Text.
232 protected function primitiveValue( $val ) {
233 if ( is_string( $val ) ) {
234 // Don't FormatJson::encode for strings since we want quotes
235 // and new lines to render visually instead of escaped.
236 return '"' . $val . '"';
238 return FormatJson
::encode( $val );