MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
19use Wikimedia\Minify\CSSMin;
20
28
29 private readonly array $textModelsToParse;
30
31 public function __construct(
32 string $modelId,
33 Config $config,
34 private readonly ParserFactory $parserFactory,
35 private readonly UserOptionsLookup $userOptionsLookup,
36 private readonly CodeHighlighter $codeHighlighter,
37 ) {
38 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
39 $this->textModelsToParse = $config->get( MainConfigNames::TextModelsToParse ) ?? [];
40 }
41
45 protected function getContentClass() {
46 return CssContent::class;
47 }
48
50 public function supportsRedirects() {
51 return true;
52 }
53
62 public function makeRedirectContent( Title $destination, $text = '' ) {
63 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
64 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
65 $class = $this->getContentClass();
66
67 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
68 }
69
70 public function preSaveTransform(
71 Content $content,
72 PreSaveTransformParams $pstParams
73 ): Content {
74 '@phan-var CssContent $content';
75
76 // @todo Make pre-save transformation optional for script pages (T34858)
77 if ( !$this->userOptionsLookup->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
78 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
79 $popts = clone $pstParams->getParserOptions();
80 $popts->setPreSaveTransform( false );
81 }
82
83 $text = $content->getText();
84 $pst = $this->parserFactory->getInstance()->preSaveTransform(
85 $text,
86 $pstParams->getPage(),
87 $pstParams->getUser(),
88 $pstParams->getParserOptions()
89 );
90
91 $class = $this->getContentClass();
92 return new $class( $pst );
93 }
94
98 protected function fillParserOutput(
99 Content $content,
100 ContentParseParams $cpoParams,
101 ParserOutput &$output
102 ) {
103 '@phan-var CssContent $content';
104 if ( in_array( $content->getModel(), $this->textModelsToParse ) ) {
105 // parse just to get links etc into the database, HTML is replaced below.
106 $output = $this->parserFactory->getInstance()
107 ->parse(
108 $content->getText(),
109 $cpoParams->getPage(),
110 WikiPage::makeParserOptionsFromTitleAndModel(
111 $cpoParams->getPage(),
112 $content->getModel(),
113 'canonical'
114 ),
115 true,
116 true,
117 $cpoParams->getRevId()
118 );
119 }
120
121 if ( $cpoParams->getGenerateHtml() ) {
122 $highlightOutput = $this->codeHighlighter->highlight( $content->getText(), new CodeHighlighterOptions(
123 language: 'css',
124 classes: [ 'mw-css' ],
125 includeLineNumbers: true,
126 includeLineLinks: true,
127 ) );
128 $html = $highlightOutput->getHtml();
129 $highlightOutput->getMetadata()->addToParserOutput( $output );
130 } else {
131 $html = null;
132 }
133
134 $output->clearWrapperDivClass();
135 $output->setContentHolderText( $html );
136 // Suppress the TOC (T307691)
137 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
138 $output->setSections( [] );
139 }
140}
142class_alias( CssContentHandler::class, 'CssContentHandler' );
const CONTENT_FORMAT_CSS
For CSS pages.
Definition Defines.php:257
const PROTO_RELATIVE
Definition Defines.php:219
Content handler for pages with source code as content (e.g.
Service for syntax highlighting.
Content handler for CSS pages.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
supportsRedirects()
Returns true if this content model supports redirects.This default implementation returns false....
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.Unless $generateHtml...
__construct(string $modelId, Config $config, private readonly ParserFactory $parserFactory, private readonly UserOptionsLookup $userOptionsLookup, private readonly CodeHighlighter $codeHighlighter,)
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid CSS.
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.
Represents a title within MediaWiki.
Definition Title.php:69
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2153
Provides access to user options.
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
getModel()
Get the content model ID.