MediaWiki master
VueContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
9use InvalidArgumentException;
18use StatusValue;
19use WikiPage;
20
28
29 private array $textModelsToParse;
30
31 private ?VueComponentParser $vueComponentParser = null;
32
33 public function __construct(
34 string $modelId,
35 Config $config,
36 private readonly ParserFactory $parserFactory,
37 ) {
38 parent::__construct( $modelId, [ CONTENT_FORMAT_VUE ] );
39 $this->textModelsToParse = $config->get( MainConfigNames::TextModelsToParse ) ?? [];
40 }
41
45 protected function getContentClass() {
46 return VueContent::class;
47 }
48
50 public function makeEmptyContent() {
51 $class = $this->getContentClass();
52 return new $class( "<template>\n</template>\n\n<script>\n</script>\n\n<style>\n</style>\n" );
53 }
54
70 protected function fillParserOutput(
71 Content $content,
72 ContentParseParams $cpoParams,
73 ParserOutput &$output
74 ) {
75 '@phan-var VueContent $content';
76 if ( in_array( $content->getModel(), $this->textModelsToParse ) ) {
77 // parse just to get links etc into the database, HTML is replaced below.
78 $output = $this->parserFactory->getInstance()->parse(
79 $content->getText(),
80 $cpoParams->getPage(),
81 WikiPage::makeParserOptionsFromTitleAndModel(
82 $cpoParams->getPage(),
83 $content->getModel(),
84 'canonical'
85 ),
86 true,
87 true,
88 $cpoParams->getRevId()
89 );
90 }
91
92 if ( $cpoParams->getGenerateHtml() ) {
93 // Return Vue code wrapped in a <pre> tag.
94 $html = Html::element(
95 'pre',
96 [ 'class' => 'mw-code mw-vue', 'dir' => 'ltr' ],
97 "\n" . $content->getText() . "\n"
98 ) . "\n";
99 } else {
100 $html = null;
101 }
102
103 $output->clearWrapperDivClass();
104 $output->setRawText( $html );
105 // Suppress the TOC (T307691)
106 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
107 $output->setSections( [] );
108 }
109
115 public function validateSave( Content $content, ValidationParams $validationParams ): StatusValue {
116 $this->vueComponentParser ??= new VueComponentParser;
117 try {
118 $parsedComponent = $this->vueComponentParser->parse( $content->serialize() );
119 } catch ( InvalidArgumentException $e ) {
120 // TODO: i18n for error messages
121 return StatusValue::newFatal( 'vue-invalid-content', $e->getMessage() );
122 }
123 if ( $parsedComponent['styleLang'] === 'less' ) {
124 return StatusValue::newFatal( 'vue-less-notsupported' );
125 }
126 return StatusValue::newGood();
127 }
128}
const CONTENT_FORMAT_VUE
For Vue pages.
Definition Defines.php:259
Content handler for pages with source code as content (e.g.
Content handler for Vue pages.
validateSave(Content $content, ValidationParams $validationParams)
__construct(string $modelId, Config $config, private readonly ParserFactory $parserFactory,)
makeEmptyContent()
Creates an empty TextContent object.1.21Content A new TextContent object with empty text.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
A class containing constants representing the names of configuration variables.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config::get()
ParserOutput is a rendering of a Content object or a message.
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setRawText(?string $text)
Set the raw text of the ParserOutput.
setSections(array $sectionArray)
setOutputFlag(ParserOutputFlags|string $name, bool $val=true)
Provides a uniform interface to various boolean flags stored in the ParserOutput.
Parser for Vue single file components (.vue files).
parse(string $html, array $options=[])
Parse a Vue single file component, and extract the script, template and style parts.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Interface for configuration instances.
Definition Config.php:18
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
Content objects represent page content, e.g.
Definition Content.php:28
serialize( $format=null)
Serialize this Content object.
getModel()
Get the content model ID.
element(SerializerNode $parent, SerializerNode $node, $contents)