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 into a PHP associative array.
34 * @deprecated since 1.25 Use getData instead.
37 public function getJsonData() {
38 wfDeprecated( __METHOD__
, '1.25' );
39 return FormatJson
::decode( $this->getNativeData(), true );
43 * Decodes the JSON string.
45 * Note that this parses it without casting objects to associative arrays.
46 * Objects and arrays are kept as distinguishable types in the PHP values.
50 public function getData() {
51 if ( $this->jsonParse
=== null ) {
52 $this->jsonParse
= FormatJson
::parse( $this->getNativeData() );
54 return $this->jsonParse
;
58 * @return bool Whether content is valid.
60 public function isValid() {
61 return $this->getData()->isGood();
67 * If called before validation, it may return JSON "null".
71 public function beautifyJSON() {
72 return FormatJson
::encode( $this->getData()->getValue(), true, FormatJson
::UTF8_OK
);
76 * Beautifies JSON prior to save.
78 * @param Title $title Title
79 * @param User $user User
80 * @param ParserOptions $popts
83 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
84 // FIXME: WikiPage::doEditContent invokes PST before validation. As such, native data
85 // may be invalid (though PST result is discarded later in that case).
86 if ( !$this->isValid() ) {
90 return new static( self
::normalizeLineEndings( $this->beautifyJSON() ) );
94 * Set the HTML and add the appropriate styles.
98 * @param ParserOptions $options
99 * @param bool $generateHtml
100 * @param ParserOutput &$output
102 protected function fillParserOutput( Title
$title, $revId,
103 ParserOptions
$options, $generateHtml, ParserOutput
&$output
105 // FIXME: WikiPage::doEditContent generates parser output before validation.
106 // As such, native data may be invalid (though output is discarded later in that case).
107 if ( $generateHtml && $this->isValid() ) {
108 $output->setText( $this->rootValueTable( $this->getData()->getValue() ) );
109 $output->addModuleStyles( 'mediawiki.content.json' );
111 $output->setText( '' );
116 * Construct HTML table representation of any JSON value.
118 * See also valueCell, which is similar.
121 * @return string HTML.
123 protected function rootValueTable( $val ) {
124 if ( is_object( $val ) ) {
125 return $this->objectTable( $val );
128 if ( is_array( $val ) ) {
129 // Wrap arrays in another array so that they're visually boxed in a container.
130 // Otherwise they are visually indistinguishable from a single value.
131 return $this->arrayTable( [ $val ] );
134 return Html
::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
135 Html
::rawElement( 'tbody', [],
136 Html
::rawElement( 'tr', [],
137 Html
::element( 'td', [], $this->primitiveValue( $val ) )
144 * Create HTML table representing a JSON object.
146 * @param stdClass $mapping
147 * @return string HTML
149 protected function objectTable( $mapping ) {
153 foreach ( $mapping as $key => $val ) {
154 $rows[] = $this->objectRow( $key, $val );
158 $rows[] = Html
::rawElement( 'tr', [],
159 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
160 wfMessage( 'content-json-empty-object' )->text()
164 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
165 Html
::rawElement( 'tbody', [], implode( '', $rows ) )
170 * Create HTML table row representing one object property.
174 * @return string HTML.
176 protected function objectRow( $key, $val ) {
177 $th = Html
::element( 'th', [], $key );
178 $td = $this->valueCell( $val );
179 return Html
::rawElement( 'tr', [], $th . $td );
183 * Create HTML table representing a JSON array.
185 * @param array $mapping
186 * @return string HTML
188 protected function arrayTable( $mapping ) {
192 foreach ( $mapping as $val ) {
193 $rows[] = $this->arrayRow( $val );
197 $rows[] = Html
::rawElement( 'tr', [],
198 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
199 wfMessage( 'content-json-empty-array' )->text()
203 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
204 Html
::rawElement( 'tbody', [], implode( "\n", $rows ) )
209 * Create HTML table row representing the value in an array.
212 * @return string HTML.
214 protected function arrayRow( $val ) {
215 $td = $this->valueCell( $val );
216 return Html
::rawElement( 'tr', [], $td );
220 * Construct HTML table cell representing any JSON value.
223 * @return string HTML.
225 protected function valueCell( $val ) {
226 if ( is_object( $val ) ) {
227 return Html
::rawElement( 'td', [], $this->objectTable( $val ) );
230 if ( is_array( $val ) ) {
231 return Html
::rawElement( 'td', [], $this->arrayTable( $val ) );
234 return Html
::element( 'td', [ 'class' => 'value' ], $this->primitiveValue( $val ) );
238 * Construct text representing a JSON primitive value.
241 * @return string Text.
243 protected function primitiveValue( $val ) {
244 if ( is_string( $val ) ) {
245 // Don't FormatJson::encode for strings since we want quotes
246 // and new lines to render visually instead of escaped.
247 return '"' . $val . '"';
249 return FormatJson
::encode( $val );