MediaWiki  1.30.2
JsonContent.php
Go to the documentation of this file.
1 <?php
15 class JsonContent extends TextContent {
16 
21  protected $jsonParse;
22 
27  public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
28  parent::__construct( $text, $modelId );
29  }
30 
37  public function getJsonData() {
38  wfDeprecated( __METHOD__, '1.25' );
39  return FormatJson::decode( $this->getNativeData(), true );
40  }
41 
50  public function getData() {
51  if ( $this->jsonParse === null ) {
52  $this->jsonParse = FormatJson::parse( $this->getNativeData() );
53  }
54  return $this->jsonParse;
55  }
56 
60  public function isValid() {
61  return $this->getData()->isGood();
62  }
63 
71  public function beautifyJSON() {
72  return FormatJson::encode( $this->getData()->getValue(), true, FormatJson::UTF8_OK );
73  }
74 
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;
88  }
89 
90  return new static( self::normalizeLineEndings( $this->beautifyJSON() ) );
91  }
92 
102  protected function fillParserOutput( Title $title, $revId,
103  ParserOptions $options, $generateHtml, ParserOutput &$output
104  ) {
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( '' );
112  }
113  }
114 
123  protected function rootValueTable( $val ) {
124  if ( is_object( $val ) ) {
125  return $this->objectTable( $val );
126  }
127 
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 ] );
132  }
133 
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 ) )
138  )
139  )
140  );
141  }
142 
149  protected function objectTable( $mapping ) {
150  $rows = [];
151  $empty = true;
152 
153  foreach ( $mapping as $key => $val ) {
154  $rows[] = $this->objectRow( $key, $val );
155  $empty = false;
156  }
157  if ( $empty ) {
158  $rows[] = Html::rawElement( 'tr', [],
159  Html::element( 'td', [ 'class' => 'mw-json-empty' ],
160  wfMessage( 'content-json-empty-object' )->text()
161  )
162  );
163  }
164  return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
165  Html::rawElement( 'tbody', [], implode( '', $rows ) )
166  );
167  }
168 
176  protected function objectRow( $key, $val ) {
177  $th = Html::element( 'th', [], $key );
178  $td = $this->valueCell( $val );
179  return Html::rawElement( 'tr', [], $th . $td );
180  }
181 
188  protected function arrayTable( $mapping ) {
189  $rows = [];
190  $empty = true;
191 
192  foreach ( $mapping as $val ) {
193  $rows[] = $this->arrayRow( $val );
194  $empty = false;
195  }
196  if ( $empty ) {
197  $rows[] = Html::rawElement( 'tr', [],
198  Html::element( 'td', [ 'class' => 'mw-json-empty' ],
199  wfMessage( 'content-json-empty-array' )->text()
200  )
201  );
202  }
203  return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
204  Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
205  );
206  }
207 
214  protected function arrayRow( $val ) {
215  $td = $this->valueCell( $val );
216  return Html::rawElement( 'tr', [], $td );
217  }
218 
225  protected function valueCell( $val ) {
226  if ( is_object( $val ) ) {
227  return Html::rawElement( 'td', [], $this->objectTable( $val ) );
228  }
229 
230  if ( is_array( $val ) ) {
231  return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
232  }
233 
234  return Html::element( 'td', [ 'class' => 'value' ], $this->primitiveValue( $val ) );
235  }
236 
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 . '"';
248  }
249  return FormatJson::encode( $val );
250  }
251 }
JsonContent\beautifyJSON
beautifyJSON()
Pretty-print JSON.
Definition: JsonContent.php:71
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:40
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:240
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
JsonContent\isValid
isValid()
Definition: JsonContent.php:60
ParserOutput
Definition: ParserOutput.php:24
JsonContent\arrayTable
arrayTable( $mapping)
Create HTML table representing a JSON array.
Definition: JsonContent.php:188
text
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:12
JsonContent\preSaveTransform
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Beautifies JSON prior to save.
Definition: JsonContent.php:83
JsonContent\arrayRow
arrayRow( $val)
Create HTML table row representing the value in an array.
Definition: JsonContent.php:214
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2198
JsonContent\objectTable
objectTable( $mapping)
Create HTML table representing a JSON object.
Definition: JsonContent.php:149
FormatJson\UTF8_OK
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Definition: FormatJson.php:34
php
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:35
JsonContent\rootValueTable
rootValueTable( $val)
Construct HTML table representation of any JSON value.
Definition: JsonContent.php:123
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:127
JsonContent\$jsonParse
Status $jsonParse
Definition: JsonContent.php:21
JsonContent
Represents the content of a JSON content.
Definition: JsonContent.php:15
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1176
JsonContent\fillParserOutput
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Set the HTML and add the appropriate styles.
Definition: JsonContent.php:102
JsonContent\objectRow
objectRow( $key, $val)
Create HTML table row representing one object property.
Definition: JsonContent.php:176
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:437
JsonContent\valueCell
valueCell( $val)
Construct HTML table cell representing any JSON value.
Definition: JsonContent.php:225
FormatJson\parse
static parse( $value, $options=0)
Decodes a JSON string.
Definition: FormatJson.php:201
JsonContent\getJsonData
getJsonData()
Decodes the JSON into a PHP associative array.
Definition: JsonContent.php:37
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
JsonContent\getData
getData()
Decodes the JSON string.
Definition: JsonContent.php:50
Title
Represents a title within MediaWiki.
Definition: Title.php:39
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2581
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1965
JsonContent\primitiveValue
primitiveValue( $val)
Construct text representing a JSON primitive value.
Definition: JsonContent.php:243
as
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
Definition: distributors.txt:9
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
TextContent\normalizeLineEndings
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Definition: TextContent.php:163
wfMessage
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
JsonContent\__construct
__construct( $text, $modelId=CONTENT_MODEL_JSON)
Definition: JsonContent.php:27
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
TextContent\getNativeData
getNativeData()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:119