MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Content;
25
34use Wikimedia\Minify\CSSMin;
35use WikiPage;
36
44
48 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
49 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
50 }
51
55 protected function getContentClass() {
56 return CssContent::class;
57 }
58
59 public function supportsRedirects() {
60 return true;
61 }
62
71 public function makeRedirectContent( Title $destination, $text = '' ) {
72 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
73 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
74 $class = $this->getContentClass();
75
76 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
77 }
78
79 public function preSaveTransform(
80 Content $content,
81 PreSaveTransformParams $pstParams
82 ): Content {
83 '@phan-var CssContent $content';
84
85 // @todo Make pre-save transformation optional for script pages (T34858)
87 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
88 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
89 $popts = clone $pstParams->getParserOptions();
90 $popts->setPreSaveTransform( false );
91 }
92
93 $text = $content->getText();
94 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
95 $text,
96 $pstParams->getPage(),
97 $pstParams->getUser(),
98 $pstParams->getParserOptions()
99 );
100
101 $class = $this->getContentClass();
102 return new $class( $pst );
103 }
104
108 protected function fillParserOutput(
109 Content $content,
110 ContentParseParams $cpoParams,
111 ParserOutput &$output
112 ) {
113 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
115 '@phan-var CssContent $content';
116 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
117 // parse just to get links etc into the database, HTML is replaced below.
118 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
119 ->parse(
120 $content->getText(),
121 $cpoParams->getPage(),
122 WikiPage::makeParserOptionsFromTitleAndModel(
123 $cpoParams->getPage(),
124 $content->getModel(),
125 'canonical'
126 ),
127 true,
128 true,
129 $cpoParams->getRevId()
130 );
131 }
132
133 if ( $cpoParams->getGenerateHtml() ) {
134 // Return CSS wrapped in a <pre> tag.
135 $html = Html::element(
136 'pre',
137 [ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
138 "\n" . $content->getText() . "\n"
139 ) . "\n";
140 } else {
141 $html = null;
142 }
143
144 $output->clearWrapperDivClass();
145 $output->setRawText( $html );
146 // Suppress the TOC (T307691)
147 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
148 $output->setSections( [] );
149 }
150}
152class_alias( CssContentHandler::class, 'CssContentHandler' );
const CONTENT_MODEL_CSS
Definition Defines.php:224
const CONTENT_FORMAT_CSS
For CSS pages.
Definition Defines.php:242
const PROTO_RELATIVE
Definition Defines.php:206
Content handler for code content such as CSS, JavaScript, JSON, etc.
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.
__construct( $modelId=CONTENT_MODEL_CSS)
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.Unless $generateHtml...
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:56
A class containing constants representing the names of configuration variables.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
ParserOutput is a rendering of a Content object or a message.
setOutputFlag(string $name, bool $val=true)
Provides a uniform interface to various boolean flags stored in the ParserOutput.
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)
Represents a title within MediaWiki.
Definition Title.php:78
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2130
Base representation for an editable wiki page.
Definition WikiPage.php:83
Base interface for representing page content.
Definition Content.php:39
getModel()
Returns the ID of the content model used by this Content object.
element(SerializerNode $parent, SerializerNode $node, $contents)