MediaWiki REL1_28
JsonContent.php
Go to the documentation of this file.
1<?php
15class JsonContent extends TextContent {
16
21 protected $jsonParse;
22
26 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
27 parent::__construct( $text, $modelId );
28 }
29
36 public function getJsonData() {
37 wfDeprecated( __METHOD__, '1.25' );
38 return FormatJson::decode( $this->getNativeData(), true );
39 }
40
49 public function getData() {
50 if ( $this->jsonParse === null ) {
51 $this->jsonParse = FormatJson::parse( $this->getNativeData() );
52 }
53 return $this->jsonParse;
54 }
55
59 public function isValid() {
60 return $this->getData()->isGood();
61 }
62
70 public function beautifyJSON() {
71 return FormatJson::encode( $this->getData()->getValue(), true, FormatJson::UTF8_OK );
72 }
73
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;
87 }
88
89 return new static( self::normalizeLineEndings( $this->beautifyJSON() ) );
90 }
91
101 protected function fillParserOutput( Title $title, $revId,
103 ) {
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( '' );
111 }
112 }
113
122 protected function rootValueTable( $val ) {
123 if ( is_object( $val ) ) {
124 return $this->objectTable( $val );
125 }
126
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 ] );
131 }
132
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 ) )
137 )
138 )
139 );
140 }
141
148 protected function objectTable( $mapping ) {
149 $rows = [];
150 $empty = true;
151
152 foreach ( $mapping as $key => $val ) {
153 $rows[] = $this->objectRow( $key, $val );
154 $empty = false;
155 }
156 if ( $empty ) {
157 $rows[] = Html::rawElement( 'tr', [],
158 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
159 wfMessage( 'content-json-empty-object' )->text()
160 )
161 );
162 }
163 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
164 Html::rawElement( 'tbody', [], implode( '', $rows ) )
165 );
166 }
167
175 protected function objectRow( $key, $val ) {
176 $th = Html::element( 'th', [], $key );
177 $td = $this->valueCell( $val );
178 return Html::rawElement( 'tr', [], $th . $td );
179 }
180
187 protected function arrayTable( $mapping ) {
188 $rows = [];
189 $empty = true;
190
191 foreach ( $mapping as $val ) {
192 $rows[] = $this->arrayRow( $val );
193 $empty = false;
194 }
195 if ( $empty ) {
196 $rows[] = Html::rawElement( 'tr', [],
197 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
198 wfMessage( 'content-json-empty-array' )->text()
199 )
200 );
201 }
202 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
203 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
204 );
205 }
206
213 protected function arrayRow( $val ) {
214 $td = $this->valueCell( $val );
215 return Html::rawElement( 'tr', [], $td );
216 }
217
224 protected function valueCell( $val ) {
225 if ( is_object( $val ) ) {
226 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
227 }
228
229 if ( is_array( $val ) ) {
230 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
231 }
232
233 return Html::element( 'td', [ 'class' => 'value' ], $this->primitiveValue( $val ) );
234 }
235
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 . '"';
247 }
248 return FormatJson::encode( $val );
249 }
250}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Represents the content of a JSON content.
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.
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Beautifies JSON prior to save.
__construct( $text, $modelId=CONTENT_MODEL_JSON)
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Set the HTML and add the appropriate styles.
getJsonData()
Decodes the JSON into a PHP associative array.
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.
Set options of the Parser.
addModuleStyles( $modules)
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:40
Content object implementation for representing flat text.
getNativeData()
Returns the text represented by this Content object, as a string.
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Represents a title within MediaWiki.
Definition Title.php:36
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const CONTENT_MODEL_JSON
Definition Defines.php:243
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context $revId
Definition hooks.txt:1095
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition hooks.txt:1102
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition hooks.txt:1096
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state $generateHtml
Definition hooks.txt:1099
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37