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