MediaWiki  master
JsonContentHandler.php
Go to the documentation of this file.
1 <?php
23 
37 
42  public function __construct( $modelId = CONTENT_MODEL_JSON ) {
43  parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
44  }
45 
49  protected function getContentClass() {
50  return JsonContent::class;
51  }
52 
53  public function makeEmptyContent() {
54  $class = $this->getContentClass();
55  return new $class( '{}' );
56  }
57 
58  public function preSaveTransform(
60  PreSaveTransformParams $pstParams
61  ): Content {
62  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
63  $content,
64  $pstParams
65  );
66 
67  if ( $shouldCallDeprecatedMethod ) {
68  return $this->callDeprecatedContentPST(
69  $content,
70  $pstParams
71  );
72  }
73 
74  '@phan-var JsonContent $content';
75 
76  // FIXME: WikiPage::doEditContent invokes PST before validation. As such, native data
77  // may be invalid (though PST result is discarded later in that case).
78  if ( !$content->isValid() ) {
79  return $content;
80  }
81 
82  $contentClass = $this->getContentClass();
83  return new $contentClass( JsonContent::normalizeLineEndings( $content->beautifyJSON() ) );
84  }
85 
94  protected function fillParserOutput(
96  ContentParseParams $cpoParams,
97  ParserOutput &$parserOutput
98  ) {
99  '@phan-var JsonContent $content';
100  // FIXME: WikiPage::doEditContent generates parser output before validation.
101  // As such, native data may be invalid (though output is discarded later in that case).
102  if ( $cpoParams->getGenerateHtml() && $content->isValid() ) {
103  $parserOutput->setText( $content->rootValueTable( $content->getData()->getValue() ) );
104  $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
105  } else {
106  $parserOutput->setText( null );
107  }
108  }
109 }
const CONTENT_FORMAT_JSON
For future use with the API, and for use by extensions.
Definition: Defines.php:238
const CONTENT_MODEL_JSON
Definition: Defines.php:214
Content handler for code content such as CSS, JavaScript, JSON, etc.
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Content handler for JSON text.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$parserOutput)
Set the HTML and add the appropriate styles.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
__construct( $modelId=CONTENT_MODEL_JSON)
makeEmptyContent()
Creates an empty TextContent object.
setText( $text)
Set the text of the ParserOutput.
addModuleStyles( $modules)
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Base interface for content objects.
Definition: Content.php:35
$content
Definition: router.php:76