Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / content / JsonContent.php
blob2b94f3f71e057c85e4b2d4ba3ff4426a436e339c
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
25 * @param string $modelId
27 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
28 parent::__construct( $text, $modelId );
31 /**
32 * Decodes the JSON into a PHP associative array.
34 * @deprecated since 1.25 Use getData instead.
35 * @return array|null
37 public function getJsonData() {
38 wfDeprecated( __METHOD__, '1.25' );
39 return FormatJson::decode( $this->getNativeData(), true );
42 /**
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.
48 * @return Status
50 public function getData() {
51 if ( $this->jsonParse === null ) {
52 $this->jsonParse = FormatJson::parse( $this->getNativeData() );
54 return $this->jsonParse;
57 /**
58 * @return bool Whether content is valid.
60 public function isValid() {
61 return $this->getData()->isGood();
64 /**
65 * Pretty-print JSON.
67 * If called before validation, it may return JSON "null".
69 * @return string
71 public function beautifyJSON() {
72 return FormatJson::encode( $this->getData()->getValue(), true, FormatJson::UTF8_OK );
75 /**
76 * Beautifies JSON prior to save.
78 * @param Title $title Title
79 * @param User $user User
80 * @param ParserOptions $popts
81 * @return JsonContent
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() ) {
87 return $this;
90 return new static( self::normalizeLineEndings( $this->beautifyJSON() ) );
93 /**
94 * Set the HTML and add the appropriate styles.
96 * @param Title $title
97 * @param int $revId
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' );
110 } else {
111 $output->setText( '' );
116 * Construct HTML table representation of any JSON value.
118 * See also valueCell, which is similar.
120 * @param mixed $val
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 ) {
150 $rows = [];
151 $empty = true;
153 foreach ( $mapping as $key => $val ) {
154 $rows[] = $this->objectRow( $key, $val );
155 $empty = false;
157 if ( $empty ) {
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.
172 * @param string $key
173 * @param mixed $val
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 ) {
189 $rows = [];
190 $empty = true;
192 foreach ( $mapping as $val ) {
193 $rows[] = $this->arrayRow( $val );
194 $empty = false;
196 if ( $empty ) {
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.
211 * @param mixed $val
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.
222 * @param mixed $val
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.
240 * @param mixed $val
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 );