MediaWiki  1.27.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( $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 }
arrayTable($mapping)
Create HTML table representing a JSON array.
getNativeData()
Returns the text represented by this Content object, as a string.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Beautifies JSON prior to save.
Definition: JsonContent.php:82
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:1004
Set options of the Parser.
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:210
arrayRow($val)
Create HTML table row representing the value in an array.
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:1004
addModuleStyles($modules)
Status $jsonParse
Definition: JsonContent.php:21
Represents a title within MediaWiki.
Definition: Title.php:34
const CONTENT_MODEL_JSON
Definition: Defines.php:281
static parse($value, $options=0)
Decodes a JSON string.
Definition: FormatJson.php:201
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Definition: FormatJson.php:34
primitiveValue($val)
Construct text representing a JSON primitive value.
Content object implementation for representing flat text.
Definition: TextContent.php:35
rootValueTable($val)
Construct HTML table representation of any JSON value.
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:1004
fillParserOutput(Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output)
Set the HTML and add the appropriate styles.
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 noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing 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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
static encode($value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:127
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
getJsonData()
Decodes the JSON into a PHP associative array.
Definition: JsonContent.php:36
getData()
Decodes the JSON string.
Definition: JsonContent.php:49
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
objectRow($key, $val)
Create HTML table row representing one object property.
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
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 local account $user
Definition: hooks.txt:242
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:1004
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
Represents the content of a JSON content.
Definition: JsonContent.php:15
objectTable($mapping)
Create HTML table representing a JSON object.
valueCell($val)
Construct HTML table cell representing any JSON value.
__construct($text, $modelId=CONTENT_MODEL_JSON)
Definition: JsonContent.php:26
beautifyJSON()
Pretty-print JSON.
Definition: JsonContent.php:70
static decode($value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
static element($element, $attribs=[], $contents= '')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:230