wiki.techinc.nl/includes/content/JsonContent.php
Petr Pchelko b782a7e66d Move Content::preSaveTransform to ContentHandler
Create ContentTransformer to access ContentHandler::preSaveTransform through the service.
Prepare object to hold a data that required for ContentHandler::preSaveTranform params.

This will require making a semi-backwards-incompatible
change no matter what, we don't really have a great way
of hard-deprecating overriding methods.

However, with the ContentHandler calling Content and
Content calling ContentHandler, and with the ProxyContent
trick to stop infinite recursion, it doesn't matter whether
callers use Content or ContentHandler. This will allow us
to naturally convert all callers. But won't really allow
hard-deprecation.

Bug: T287156
Change-Id: If6a2025868ceca3a3b6f11baec39695e47292e40
2021-07-29 18:06:02 +03:00

223 lines
5.3 KiB
PHP

<?php
/**
* JSON Content Model
*
* @file
*
* @author Ori Livneh <ori@wikimedia.org>
* @author Kunal Mehta <legoktm@gmail.com>
*/
/**
* Represents the content of a JSON content.
* @since 1.24
*/
class JsonContent extends TextContent {
/**
* @since 1.25
* @var Status
*/
protected $jsonParse;
/**
* @param string $text JSON
* @param string $modelId
*/
public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
parent::__construct( $text, $modelId );
}
/**
* Decodes the JSON string.
*
* Note that this parses it without casting objects to associative arrays.
* Objects and arrays are kept as distinguishable types in the PHP values.
*
* @return Status
*/
public function getData() {
if ( $this->jsonParse === null ) {
$this->jsonParse = FormatJson::parse( $this->getText() );
}
return $this->jsonParse;
}
/**
* @return bool Whether content is valid.
*/
public function isValid() {
return $this->getData()->isGood();
}
/**
* Pretty-print JSON.
*
* If called before validation, it may return JSON "null".
*
* @return string
*/
public function beautifyJSON() {
return FormatJson::encode( $this->getData()->getValue(), true, FormatJson::UTF8_OK );
}
/**
* Set the HTML and add the appropriate styles.
*
* @param Title $title
* @param int $revId
* @param ParserOptions $options
* @param bool $generateHtml
* @param ParserOutput &$output
*/
protected function fillParserOutput( Title $title, $revId,
ParserOptions $options, $generateHtml, ParserOutput &$output
) {
// FIXME: WikiPage::doEditContent generates parser output before validation.
// As such, native data may be invalid (though output is discarded later in that case).
if ( $generateHtml && $this->isValid() ) {
$output->setText( $this->rootValueTable( $this->getData()->getValue() ) );
$output->addModuleStyles( 'mediawiki.content.json' );
} else {
$output->setText( '' );
}
}
/**
* Construct HTML table representation of any JSON value.
*
* See also valueCell, which is similar.
*
* @param mixed $val
* @return string HTML.
*/
protected function rootValueTable( $val ) {
if ( is_object( $val ) ) {
return $this->objectTable( $val );
}
if ( is_array( $val ) ) {
// Wrap arrays in another array so that they're visually boxed in a container.
// Otherwise they are visually indistinguishable from a single value.
return $this->arrayTable( [ $val ] );
}
return Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
Html::rawElement( 'tbody', [],
Html::rawElement( 'tr', [],
Html::element( 'td', [], $this->primitiveValue( $val ) )
)
)
);
}
/**
* Create HTML table representing a JSON object.
*
* @param stdClass $mapping
* @return string HTML
*/
protected function objectTable( $mapping ) {
$rows = [];
$empty = true;
foreach ( $mapping as $key => $val ) {
$rows[] = $this->objectRow( $key, $val );
$empty = false;
}
if ( $empty ) {
$rows[] = Html::rawElement( 'tr', [],
Html::element( 'td', [ 'class' => 'mw-json-empty' ],
wfMessage( 'content-json-empty-object' )->text()
)
);
}
return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
Html::rawElement( 'tbody', [], implode( '', $rows ) )
);
}
/**
* Create HTML table row representing one object property.
*
* @param string $key
* @param mixed $val
* @return string HTML.
*/
protected function objectRow( $key, $val ) {
$thContent = Html::element( 'span', [], $key );
$th = Html::rawElement( 'th', [], $thContent );
$td = $this->valueCell( $val );
return Html::rawElement( 'tr', [], $th . $td );
}
/**
* Create HTML table representing a JSON array.
*
* @param array $mapping
* @return string HTML
*/
protected function arrayTable( $mapping ) {
$rows = [];
$empty = true;
foreach ( $mapping as $val ) {
$rows[] = $this->arrayRow( $val );
$empty = false;
}
if ( $empty ) {
$rows[] = Html::rawElement( 'tr', [],
Html::element( 'td', [ 'class' => 'mw-json-empty' ],
wfMessage( 'content-json-empty-array' )->text()
)
);
}
return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
);
}
/**
* Create HTML table row representing the value in an array.
*
* @param mixed $val
* @return string HTML.
*/
protected function arrayRow( $val ) {
$td = $this->valueCell( $val );
return Html::rawElement( 'tr', [], $td );
}
/**
* Construct HTML table cell representing any JSON value.
*
* @param mixed $val
* @return string HTML.
*/
protected function valueCell( $val ) {
if ( is_object( $val ) ) {
return Html::rawElement( 'td', [], $this->objectTable( $val ) );
}
if ( is_array( $val ) ) {
return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
}
return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
}
/**
* Construct text representing a JSON primitive value.
*
* @param mixed $val
* @return string Text.
*/
protected function primitiveValue( $val ) {
if ( is_string( $val ) ) {
// Don't FormatJson::encode for strings since we want quotes
// and new lines to render visually instead of escaped.
return '"' . $val . '"';
}
return FormatJson::encode( $val );
}
}