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
26 public function __construct( $text, $modelId = CONTENT_MODEL_JSON
) {
27 parent
::__construct( $text, $modelId );
31 * Decodes the JSON into a PHP associative array.
33 * @deprecated since 1.25 Use getData instead.
36 public function getJsonData() {
37 wfDeprecated( __METHOD__
, '1.25' );
38 return FormatJson
::decode( $this->getNativeData(), true );
42 * Decodes the JSON string.
44 * Note that this parses it without casting objects to associative arrays.
45 * Objects and arrays are kept as distinguishable types in the PHP values.
49 public function getData() {
50 if ( $this->jsonParse
=== null ) {
51 $this->jsonParse
= FormatJson
::parse( $this->getNativeData() );
53 return $this->jsonParse
;
57 * @return bool Whether content is valid.
59 public function isValid() {
60 return $this->getData()->isGood();
66 * If called before validation, it may return JSON "null".
70 public function beautifyJSON() {
71 return FormatJson
::encode( $this->getData()->getValue(), true, FormatJson
::UTF8_OK
);
75 * Beautifies JSON prior to save.
77 * @param Title $title Title
78 * @param User $user User
79 * @param ParserOptions $popts
82 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
83 // FIXME: WikiPage::doEditContent invokes PST before validation. As such, native data
84 // may be invalid (though PST result is discarded later in that case).
85 if ( !$this->isValid() ) {
89 return new static( $this->beautifyJSON() );
93 * Set the HTML and add the appropriate styles.
97 * @param ParserOptions $options
98 * @param bool $generateHtml
99 * @param ParserOutput $output
101 protected function fillParserOutput( Title
$title, $revId,
102 ParserOptions
$options, $generateHtml, ParserOutput
&$output
104 // FIXME: WikiPage::doEditContent generates parser output before validation.
105 // As such, native data may be invalid (though output is discarded later in that case).
106 if ( $generateHtml && $this->isValid() ) {
107 $output->setText( $this->rootValueTable( $this->getData()->getValue() ) );
108 $output->addModuleStyles( 'mediawiki.content.json' );
110 $output->setText( '' );
115 * Construct HTML table representation of any JSON value.
117 * See also valueCell, which is similar.
120 * @return string HTML.
122 protected function rootValueTable( $val ) {
123 if ( is_object( $val ) ) {
124 return $this->objectTable( $val );
127 if ( is_array( $val ) ) {
128 // Wrap arrays in another array so that they're visually boxed in a container.
129 // Otherwise they are visually indistinguishable from a single value.
130 return $this->arrayTable( [ $val ] );
133 return Html
::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
134 Html
::rawElement( 'tbody', [],
135 Html
::rawElement( 'tr', [],
136 Html
::element( 'td', [], $this->primitiveValue( $val ) )
143 * Create HTML table representing a JSON object.
145 * @param stdClass $mapping
146 * @return string HTML
148 protected function objectTable( $mapping ) {
152 foreach ( $mapping as $key => $val ) {
153 $rows[] = $this->objectRow( $key, $val );
157 $rows[] = Html
::rawElement( 'tr', [],
158 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
159 wfMessage( 'content-json-empty-object' )->text()
163 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
164 Html
::rawElement( 'tbody', [], implode( '', $rows ) )
169 * Create HTML table row representing one object property.
173 * @return string HTML.
175 protected function objectRow( $key, $val ) {
176 $th = Html
::element( 'th', [], $key );
177 $td = $this->valueCell( $val );
178 return Html
::rawElement( 'tr', [], $th . $td );
182 * Create HTML table representing a JSON array.
184 * @param array $mapping
185 * @return string HTML
187 protected function arrayTable( $mapping ) {
191 foreach ( $mapping as $val ) {
192 $rows[] = $this->arrayRow( $val );
196 $rows[] = Html
::rawElement( 'tr', [],
197 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
198 wfMessage( 'content-json-empty-array' )->text()
202 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
203 Html
::rawElement( 'tbody', [], implode( "\n", $rows ) )
208 * Create HTML table row representing the value in an array.
211 * @return string HTML.
213 protected function arrayRow( $val ) {
214 $td = $this->valueCell( $val );
215 return Html
::rawElement( 'tr', [], $td );
219 * Construct HTML table cell representing any JSON value.
222 * @return string HTML.
224 protected function valueCell( $val ) {
225 if ( is_object( $val ) ) {
226 return Html
::rawElement( 'td', [], $this->objectTable( $val ) );
229 if ( is_array( $val ) ) {
230 return Html
::rawElement( 'td', [], $this->arrayTable( $val ) );
233 return Html
::element( 'td', [ 'class' => 'value' ], $this->primitiveValue( $val ) );
237 * Construct text representing a JSON primitive value.
240 * @return string Text.
242 protected function primitiveValue( $val ) {
243 if ( is_string( $val ) ) {
244 // Don't FormatJson::encode for strings since we want quotes
245 // and new lines to render visually instead of escaped.
246 return '"' . $val . '"';
248 return FormatJson
::encode( $val );