MediaWiki master
VueContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
9use InvalidArgumentException;
18use StatusValue;
19
27
28 private readonly array $textModelsToParse;
29
30 private ?VueComponentParser $vueComponentParser = null;
31
32 public function __construct(
33 string $modelId,
34 Config $config,
35 private readonly ParserFactory $parserFactory,
36 private readonly CodeHighlighter $codeHighlighter,
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 $highlightOutput = $this->codeHighlighter->highlight( $content->getText(), new CodeHighlighterOptions(
94 language: 'vue',
95 classes: [ 'mw-vue' ],
96 includeLineNumbers: true,
97 includeLineLinks: true,
98 ) );
99 $html = $highlightOutput->getHtml();
100 $highlightOutput->getMetadata()->addToParserOutput( $output );
101 } else {
102 $html = null;
103 }
104
105 $output->clearWrapperDivClass();
106 $output->setContentHolderText( $html );
107 // Suppress the TOC (T307691)
108 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
109 $output->setSections( [] );
110 }
111
117 public function validateSave( Content $content, ValidationParams $validationParams ): StatusValue {
118 $this->vueComponentParser ??= new VueComponentParser;
119 try {
120 $parsedComponent = $this->vueComponentParser->parse( $content->serialize() );
121 } catch ( InvalidArgumentException $e ) {
122 // TODO: i18n for error messages
123 return StatusValue::newFatal( 'vue-invalid-content', $e->getMessage() );
124 }
125 if ( $parsedComponent['styleLang'] === 'less' ) {
126 return StatusValue::newFatal( 'vue-less-notsupported' );
127 }
128 return StatusValue::newGood();
129 }
130}
const CONTENT_FORMAT_VUE
For Vue pages.
Definition Defines.php:259
Content handler for pages with source code as content (e.g.
Service for syntax highlighting.
Content handler for Vue pages.
validateSave(Content $content, ValidationParams $validationParams)
__construct(string $modelId, Config $config, private readonly ParserFactory $parserFactory, private readonly CodeHighlighter $codeHighlighter,)
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.
A class containing constants representing the names of configuration variables.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config::get()
Base representation for an editable wiki page.
Definition WikiPage.php:83
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...
setContentHolderText(?string $text)
Sets the body fragment 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.