MediaWiki  1.29.2
JsonContent.php
Go to the documentation of this file.
1 <?php
15 class 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 }
JsonContent\beautifyJSON
beautifyJSON()
Pretty-print JSON.
Definition: JsonContent.php:70
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:33
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:237
JsonContent\isValid
isValid()
Definition: JsonContent.php:59
ParserOutput
Definition: ParserOutput.php:24
JsonContent\arrayTable
arrayTable( $mapping)
Create HTML table representing a JSON array.
Definition: JsonContent.php:187
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:82
$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:246
JsonContent\arrayRow
arrayRow( $val)
Create HTML table row representing the value in an array.
Definition: JsonContent.php:213
$generateHtml
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
JsonContent\objectTable
objectTable( $mapping)
Create HTML table representing a JSON object.
Definition: JsonContent.php:148
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:122
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:934
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1128
JsonContent\fillParserOutput
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Set the HTML and add the appropriate styles.
Definition: JsonContent.php:101
JsonContent\objectRow
objectRow( $key, $val)
Create HTML table row representing one object property.
Definition: JsonContent.php:175
$output
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:429
JsonContent\valueCell
valueCell( $val)
Construct HTML table cell representing any JSON value.
Definition: JsonContent.php:224
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:36
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
JsonContent\getData
getData()
Decodes the JSON string.
Definition: JsonContent.php:49
Title
Represents a title within MediaWiki.
Definition: Title.php:39
JsonContent\primitiveValue
primitiveValue( $val)
Construct text representing a JSON primitive value.
Definition: JsonContent.php:242
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
$revId
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
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:26
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:50
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
TextContent\getNativeData
getNativeData()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:119