MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Content;
25
27use Content;
36use Wikimedia\Minify\CSSMin;
37use WikiPage;
38
46
50 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
51 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
52 }
53
57 protected function getContentClass() {
58 return CssContent::class;
59 }
60
61 public function supportsRedirects() {
62 return true;
63 }
64
73 public function makeRedirectContent( Title $destination, $text = '' ) {
74 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
75 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
76 $class = $this->getContentClass();
77
78 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
79 }
80
81 public function preSaveTransform(
82 Content $content,
83 PreSaveTransformParams $pstParams
84 ): Content {
85 '@phan-var CssContent $content';
86
87 // @todo Make pre-save transformation optional for script pages (T34858)
89 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
90 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
91 $popts = clone $pstParams->getParserOptions();
92 $popts->setPreSaveTransform( false );
93 }
94
95 $text = $content->getText();
96 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
97 $text,
98 $pstParams->getPage(),
99 $pstParams->getUser(),
100 $pstParams->getParserOptions()
101 );
102
103 $class = $this->getContentClass();
104 return new $class( $pst );
105 }
106
110 protected function fillParserOutput(
111 Content $content,
112 ContentParseParams $cpoParams,
113 ParserOutput &$output
114 ) {
115 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
117 '@phan-var CssContent $content';
118 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
119 // parse just to get links etc into the database, HTML is replaced below.
120 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
121 ->parse(
122 $content->getText(),
123 $cpoParams->getPage(),
124 WikiPage::makeParserOptionsFromTitleAndModel(
125 $cpoParams->getPage(),
126 $content->getModel(),
127 'canonical'
128 ),
129 true,
130 true,
131 $cpoParams->getRevId()
132 );
133 }
134
135 if ( $cpoParams->getGenerateHtml() ) {
136 // Return CSS wrapped in a <pre> tag.
137 $html = Html::element(
138 'pre',
139 [ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
140 "\n" . $content->getText() . "\n"
141 ) . "\n";
142 } else {
143 $html = null;
144 }
145
146 $output->clearWrapperDivClass();
147 $output->setRawText( $html );
148 // Suppress the TOC (T307691)
149 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
150 $output->setSections( [] );
151 }
152}
154class_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:79
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2134
Base representation for an editable wiki page.
Definition WikiPage.php:81
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.
element(SerializerNode $parent, SerializerNode $node, $contents)