Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / content / JsonContent.php
blob40d9277470f82b3385ce43cd32bab5d2c4ef60f4
1 <?php
2 /**
3 * JSON Content Model
5 * @file
7 * @author Ori Livneh <ori@wikimedia.org>
8 * @author Kunal Mehta <legoktm@gmail.com>
9 */
11 /**
12 * Represents the content of a JSON content.
13 * @since 1.24
15 class JsonContent extends TextContent {
17 /**
18 * @since 1.25
19 * @var Status
21 protected $jsonParse;
23 /**
24 * @param string $text JSON
26 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
27 parent::__construct( $text, $modelId );
30 /**
31 * Decodes the JSON into a PHP associative array.
33 * @deprecated since 1.25 Use getData instead.
34 * @return array|null
36 public function getJsonData() {
37 wfDeprecated( __METHOD__, '1.25' );
38 return FormatJson::decode( $this->getNativeData(), true );
41 /**
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.
47 * @return Status
49 public function getData() {
50 if ( $this->jsonParse === null ) {
51 $this->jsonParse = FormatJson::parse( $this->getNativeData() );
53 return $this->jsonParse;
56 /**
57 * @return bool Whether content is valid.
59 public function isValid() {
60 return $this->getData()->isGood();
63 /**
64 * Pretty-print JSON.
66 * If called before validation, it may return JSON "null".
68 * @return string
70 public function beautifyJSON() {
71 return FormatJson::encode( $this->getData()->getValue(), true, FormatJson::UTF8_OK );
74 /**
75 * Beautifies JSON prior to save.
77 * @param Title $title Title
78 * @param User $user User
79 * @param ParserOptions $popts
80 * @return JsonContent
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() ) {
86 return $this;
89 return new static( $this->beautifyJSON() );
92 /**
93 * Set the HTML and add the appropriate styles.
95 * @param Title $title
96 * @param int $revId
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' );
109 } else {
110 $output->setText( '' );
115 * Construct HTML table representation of any JSON value.
117 * See also valueCell, which is similar.
119 * @param mixed $val
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 ) {
149 $rows = [];
150 $empty = true;
152 foreach ( $mapping as $key => $val ) {
153 $rows[] = $this->objectRow( $key, $val );
154 $empty = false;
156 if ( $empty ) {
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.
171 * @param string $key
172 * @param mixed $val
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 ) {
188 $rows = [];
189 $empty = true;
191 foreach ( $mapping as $val ) {
192 $rows[] = $this->arrayRow( $val );
193 $empty = false;
195 if ( $empty ) {
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.
210 * @param mixed $val
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.
221 * @param mixed $val
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.
239 * @param mixed $val
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 );