MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
20use Wikimedia\Minify\CSSMin;
21
29
30 private array $textModelsToParse;
31 private ParserFactory $parserFactory;
32 private UserOptionsLookup $userOptionsLookup;
33
34 public function __construct(
35 string $modelId,
36 Config $config,
37 ParserFactory $parserFactory,
38 UserOptionsLookup $userOptionsLookup
39 ) {
40 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
41 $this->textModelsToParse = $config->get( MainConfigNames::TextModelsToParse ) ?? [];
42 $this->parserFactory = $parserFactory;
43 $this->userOptionsLookup = $userOptionsLookup;
44 }
45
49 protected function getContentClass() {
50 return CssContent::class;
51 }
52
54 public function supportsRedirects() {
55 return true;
56 }
57
66 public function makeRedirectContent( Title $destination, $text = '' ) {
67 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
68 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
69 $class = $this->getContentClass();
70
71 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
72 }
73
74 public function preSaveTransform(
75 Content $content,
76 PreSaveTransformParams $pstParams
77 ): Content {
78 '@phan-var CssContent $content';
79
80 // @todo Make pre-save transformation optional for script pages (T34858)
81 if ( !$this->userOptionsLookup->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
82 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
83 $popts = clone $pstParams->getParserOptions();
84 $popts->setPreSaveTransform( false );
85 }
86
87 $text = $content->getText();
88 $pst = $this->parserFactory->getInstance()->preSaveTransform(
89 $text,
90 $pstParams->getPage(),
91 $pstParams->getUser(),
92 $pstParams->getParserOptions()
93 );
94
95 $class = $this->getContentClass();
96 return new $class( $pst );
97 }
98
102 protected function fillParserOutput(
103 Content $content,
104 ContentParseParams $cpoParams,
105 ParserOutput &$output
106 ) {
107 '@phan-var CssContent $content';
108 if ( in_array( $content->getModel(), $this->textModelsToParse ) ) {
109 // parse just to get links etc into the database, HTML is replaced below.
110 $output = $this->parserFactory->getInstance()
111 ->parse(
112 $content->getText(),
113 $cpoParams->getPage(),
114 WikiPage::makeParserOptionsFromTitleAndModel(
115 $cpoParams->getPage(),
116 $content->getModel(),
117 'canonical'
118 ),
119 true,
120 true,
121 $cpoParams->getRevId()
122 );
123 }
124
125 if ( $cpoParams->getGenerateHtml() ) {
126 // Return CSS wrapped in a <pre> tag.
127 $html = Html::element(
128 'pre',
129 [ 'class' => [ 'mw-code', 'mw-css' ], 'dir' => 'ltr' ],
130 "\n" . $content->getText() . "\n"
131 ) . "\n";
132 } else {
133 $html = null;
134 }
135
136 $output->clearWrapperDivClass();
137 $output->setRawText( $html );
138 // Suppress the TOC (T307691)
139 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
140 $output->setSections( [] );
141 }
142}
144class_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.
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, ParserFactory $parserFactory, UserOptionsLookup $userOptionsLookup)
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid CSS.
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()
Base representation for an editable wiki page.
Definition WikiPage.php:82
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.
Represents a title within MediaWiki.
Definition Title.php:70
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.
element(SerializerNode $parent, SerializerNode $node, $contents)