7 * @author Ori Livneh <ori@wikimedia.org>
8 * @author Kunal Mehta <legoktm@gmail.com>
11 namespace MediaWiki\Content
;
13 use MediaWiki\Html\Html
;
14 use MediaWiki\Json\FormatJson
;
15 use MediaWiki\Status\Status
;
18 * JSON text content that can be viewed and edit directly by users.
25 class JsonContent
extends TextContent
{
34 * @param string $text JSON
35 * @param string $modelId
38 public function __construct( $text, $modelId = CONTENT_MODEL_JSON
) {
39 parent
::__construct( $text, $modelId );
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 $this->jsonParse ??
= FormatJson
::parse( $this->getText() );
52 return $this->jsonParse
;
56 * @return bool Whether content is valid.
58 public function isValid() {
59 return $this->getData()->isGood();
65 * If called before validation, it may return JSON "null".
69 public function beautifyJSON() {
70 return FormatJson
::encode( $this->getData()->getValue(), "\t", FormatJson
::UTF8_OK
);
74 * Construct HTML table representation of any JSON value.
76 * See also valueCell, which is similar.
79 * @return string HTML.
81 public function rootValueTable( $val ) {
82 if ( is_object( $val ) ) {
83 $table = $this->objectTable( $val );
85 } elseif ( is_array( $val ) ) {
86 // Wrap arrays in another array so that they're visually boxed in a container.
87 // Otherwise they are visually indistinguishable from a single value.
88 $table = $this->arrayTable( [ $val ] );
91 $table = Html
::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
92 Html
::rawElement( 'tbody', [],
93 Html
::rawElement( 'tr', [],
94 Html
::element( 'td', [], $this->primitiveValue( $val ) )
100 return Html
::rawElement( 'div', [ 'class' => 'noresize' ], $table );
104 * Create HTML table representing a JSON object.
106 * @param \stdClass $mapping
107 * @return string HTML
109 protected function objectTable( $mapping ) {
113 foreach ( $mapping as $key => $val ) {
114 $rows[] = $this->objectRow( $key, $val );
118 $rows[] = Html
::rawElement( 'tr', [],
119 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
120 wfMessage( 'content-json-empty-object' )->text()
124 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
125 Html
::rawElement( 'tbody', [], implode( '', $rows ) )
130 * Create HTML table row representing one object property.
134 * @return string HTML.
136 protected function objectRow( $key, $val ) {
137 $thContent = Html
::element( 'span', [], $key );
138 $th = Html
::rawElement( 'th', [], $thContent );
139 $td = $this->valueCell( $val );
140 return Html
::rawElement( 'tr', [], $th . $td );
144 * Create HTML table representing a JSON array.
146 * @param array $mapping
147 * @return string HTML
149 protected function arrayTable( $mapping ) {
153 foreach ( $mapping as $val ) {
154 $rows[] = $this->arrayRow( $val );
158 $rows[] = Html
::rawElement( 'tr', [],
159 Html
::element( 'td', [ 'class' => 'mw-json-empty' ],
160 wfMessage( 'content-json-empty-array' )->text()
164 return Html
::rawElement( 'table', [ 'class' => 'mw-json' ],
165 Html
::rawElement( 'tbody', [], implode( "\n", $rows ) )
170 * Create HTML table row representing the value in an array.
173 * @return string HTML.
175 protected function arrayRow( $val ) {
176 $td = $this->valueCell( $val );
177 return Html
::rawElement( 'tr', [], $td );
181 * Construct HTML table cell representing any JSON value.
184 * @return string HTML.
186 protected function valueCell( $val ) {
187 if ( is_object( $val ) ) {
188 return Html
::rawElement( 'td', [], $this->objectTable( $val ) );
191 if ( is_array( $val ) ) {
192 return Html
::rawElement( 'td', [], $this->arrayTable( $val ) );
195 return Html
::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
199 * Construct text representing a JSON primitive value.
202 * @return string Text.
204 protected function primitiveValue( $val ) {
205 if ( is_string( $val ) ) {
206 // Don't FormatJson::encode for strings since we want quotes
207 // and new lines to render visually instead of escaped.
208 return '"' . $val . '"';
210 return FormatJson
::encode( $val );
213 /** @deprecated class alias since 1.43 */
214 class_alias( JsonContent
::class, 'JsonContent' );