MediaWiki 1.42.1
JsonContent.php
Go to the documentation of this file.
1<?php
13
22class JsonContent extends TextContent {
23
28 protected $jsonParse;
29
35 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
36 parent::__construct( $text, $modelId );
37 }
38
47 public function getData() {
48 $this->jsonParse ??= FormatJson::parse( $this->getText() );
49 return $this->jsonParse;
50 }
51
55 public function isValid() {
56 return $this->getData()->isGood();
57 }
58
66 public function beautifyJSON() {
67 return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
68 }
69
78 public function rootValueTable( $val ) {
79 if ( is_object( $val ) ) {
80 return $this->objectTable( $val );
81 }
82
83 if ( is_array( $val ) ) {
84 // Wrap arrays in another array so that they're visually boxed in a container.
85 // Otherwise they are visually indistinguishable from a single value.
86 return $this->arrayTable( [ $val ] );
87 }
88
89 return Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
90 Html::rawElement( 'tbody', [],
91 Html::rawElement( 'tr', [],
92 Html::element( 'td', [], $this->primitiveValue( $val ) )
93 )
94 )
95 );
96 }
97
104 protected function objectTable( $mapping ) {
105 $rows = [];
106 $empty = true;
107
108 foreach ( $mapping as $key => $val ) {
109 $rows[] = $this->objectRow( $key, $val );
110 $empty = false;
111 }
112 if ( $empty ) {
113 $rows[] = Html::rawElement( 'tr', [],
114 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
115 wfMessage( 'content-json-empty-object' )->text()
116 )
117 );
118 }
119 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
120 Html::rawElement( 'tbody', [], implode( '', $rows ) )
121 );
122 }
123
131 protected function objectRow( $key, $val ) {
132 $thContent = Html::element( 'span', [], $key );
133 $th = Html::rawElement( 'th', [], $thContent );
134 $td = $this->valueCell( $val );
135 return Html::rawElement( 'tr', [], $th . $td );
136 }
137
144 protected function arrayTable( $mapping ) {
145 $rows = [];
146 $empty = true;
147
148 foreach ( $mapping as $val ) {
149 $rows[] = $this->arrayRow( $val );
150 $empty = false;
151 }
152 if ( $empty ) {
153 $rows[] = Html::rawElement( 'tr', [],
154 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
155 wfMessage( 'content-json-empty-array' )->text()
156 )
157 );
158 }
159 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
160 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
161 );
162 }
163
170 protected function arrayRow( $val ) {
171 $td = $this->valueCell( $val );
172 return Html::rawElement( 'tr', [], $td );
173 }
174
181 protected function valueCell( $val ) {
182 if ( is_object( $val ) ) {
183 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
184 }
185
186 if ( is_array( $val ) ) {
187 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
188 }
189
190 return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
191 }
192
199 protected function primitiveValue( $val ) {
200 if ( is_string( $val ) ) {
201 // Don't FormatJson::encode for strings since we want quotes
202 // and new lines to render visually instead of escaped.
203 return '"' . $val . '"';
204 }
205 return FormatJson::encode( $val );
206 }
207}
const CONTENT_MODEL_JSON
Definition Defines.php:224
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
JSON text content that can be viewed and edit directly by users.
objectRow( $key, $val)
Create HTML table row representing one object property.
objectTable( $mapping)
Create HTML table representing a JSON object.
valueCell( $val)
Construct HTML table cell representing any JSON value.
arrayRow( $val)
Create HTML table row representing the value in an array.
Status $jsonParse
primitiveValue( $val)
Construct text representing a JSON primitive value.
__construct( $text, $modelId=CONTENT_MODEL_JSON)
beautifyJSON()
Pretty-print JSON.
rootValueTable( $val)
Construct HTML table representation of any JSON value.
getData()
Decodes the JSON string.
arrayTable( $mapping)
Create HTML table representing a JSON array.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.