MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\Content;
8
9use MediaWiki\Config\Config;
10use MediaWiki\Content\Renderer\ContentParseParams;
11use MediaWiki\Content\Transform\PreSaveTransformParams;
12use MediaWiki\MainConfigNames;
13use MediaWiki\MediaWikiServices;
14use MediaWiki\Page\WikiPage;
15use MediaWiki\Parser\ParserFactory;
16use MediaWiki\Parser\ParserOutput;
17use MediaWiki\Parser\ParserOutputFlags;
18use MediaWiki\Title\Title;
19use MediaWiki\User\Options\UserOptionsLookup;
20use Wikimedia\Minify\CSSMin;
21
29
30 private readonly array $textModelsToParse;
31
32 public function __construct(
33 string $modelId,
34 Config $config,
35 private readonly ParserFactory $parserFactory,
36 private readonly UserOptionsLookup $userOptionsLookup,
37 private readonly CodeHighlighter $codeHighlighter,
38 ) {
39 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
40 $this->textModelsToParse = $config->get( MainConfigNames::TextModelsToParse ) ?? [];
41 }
42
46 protected function getContentClass() {
47 return CssContent::class;
48 }
49
51 public function supportsRedirects() {
52 return true;
53 }
54
63 public function makeRedirectContent( Title $destination, $text = '' ) {
64 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
65 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
66 $class = $this->getContentClass();
67
68 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
69 }
70
71 public function preSaveTransform(
72 Content $content,
73 PreSaveTransformParams $pstParams
74 ): Content {
75 '@phan-var CssContent $content';
76
77 // @todo Make pre-save transformation optional for script pages (T34858)
78 if ( !$this->userOptionsLookup->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
79 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
80 $popts = clone $pstParams->getParserOptions();
81 $popts->setPreSaveTransform( false );
82 }
83
84 $text = $content->getText();
85 $pst = $this->parserFactory->getInstance()->preSaveTransform(
86 $text,
87 $pstParams->getPage(),
88 $pstParams->getUser(),
89 $pstParams->getParserOptions()
90 );
91
92 $class = $this->getContentClass();
93 return new $class( $pst );
94 }
95
99 protected function fillParserOutput(
100 Content $content,
101 ContentParseParams $cpoParams,
102 ParserOutput &$output
103 ) {
104 '@phan-var CssContent $content';
105
106 $parsoidParserFactory = MediaWikiServices::getInstance()->getParsoidParserFactory();
107 $parserOptions = WikiPage::makeParserOptionsFromTitleAndModel(
108 $cpoParams->getPage(),
109 $content->getModel(),
110 'canonical'
111 );
112 $this->collectMetadata(
113 $content, $cpoParams, $output, $this->textModelsToParse, $this->parserFactory,
114 $parsoidParserFactory, $parserOptions
115 );
116
117 if ( $cpoParams->getGenerateHtml() ) {
118 $highlightOutput = $this->codeHighlighter->highlight( $content->getText(), new CodeHighlighterOptions(
119 language: 'css',
120 classes: [ 'mw-css' ],
121 includeLineNumbers: true,
122 includeLineLinks: true,
123 ) );
124 $html = $highlightOutput->getHtml();
125 $highlightOutput->getMetadata()->addToParserOutput( $output );
126 } else {
127 $html = null;
128 }
129
130 $output->clearWrapperDivClass();
131 $output->setContentHolderText( $html );
132 // Suppress the TOC (T307691)
133 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
134 $output->setSections( [] );
135 }
136}
137
138class_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.
collectMetadata(TextContent $content, ContentParseParams $cpoParams, ParserOutput $output, array $textModelsToParse, ParserFactory $parserFactory, ParsoidParserFactory $parsoidParserFactory, ParserOptions $parserOptions)
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config\get().
static getInstance()
Returns the global default instance of the top level service locator.
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:2113
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.