MediaWiki REL1_40
CssContentHandler.php
Go to the documentation of this file.
1<?php
30use Wikimedia\Minify\CSSMin;
31
39
43 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
44 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
45 }
46
47 protected function getContentClass() {
48 return CssContent::class;
49 }
50
51 public function supportsRedirects() {
52 return true;
53 }
54
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 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
67 }
68
69 public function preSaveTransform(
71 PreSaveTransformParams $pstParams
72 ): Content {
73 $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
75 $pstParams
76 );
77
78 if ( $shouldCallDeprecatedMethod ) {
79 return $this->callDeprecatedContentPST(
81 $pstParams
82 );
83 }
84
85 '@phan-var CssContent $content';
86
87 // @todo Make pre-save transformation optional for script pages (T34858)
88 $services = MediaWikiServices::getInstance();
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(
112 ContentParseParams $cpoParams,
113 ParserOutput &$output
114 ) {
115 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
116 ->get( MainConfigNames::TextModelsToParse );
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->setText( $html );
148 }
149}
const CONTENT_MODEL_CSS
Definition Defines.php:213
const CONTENT_FORMAT_CSS
For CSS pages.
Definition Defines.php:231
const PROTO_RELATIVE
Definition Defines.php:195
Content handler for code content such as CSS, JavaScript, JSON, etc.
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Content handler for CSS pages.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.Unless $generateHtml...
__construct( $modelId=CONTENT_MODEL_CSS)
supportsRedirects()
Returns true if this content model supports redirects.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
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:55
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2213
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setText( $text)
Set the text of the ParserOutput.
Base interface for representing page content.
Definition Content.php:37
$content
Definition router.php:76