MediaWiki REL1_39
CssContentHandler.php
Go to the documentation of this file.
1<?php
28use Wikimedia\Minify\CSSMin;
29
37
41 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
42 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
43 }
44
45 protected function getContentClass() {
46 return CssContent::class;
47 }
48
49 public function supportsRedirects() {
50 return true;
51 }
52
60 public function makeRedirectContent( Title $destination, $text = '' ) {
61 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
62 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
63 $class = $this->getContentClass();
64 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
65 }
66
67 public function preSaveTransform(
69 PreSaveTransformParams $pstParams
70 ): Content {
71 $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
73 $pstParams
74 );
75
76 if ( $shouldCallDeprecatedMethod ) {
77 return $this->callDeprecatedContentPST(
79 $pstParams
80 );
81 }
82
83 '@phan-var CssContent $content';
84
85 // @todo Make pre-save transformation optional for script pages (T34858)
86 $services = MediaWikiServices::getInstance();
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(
110 ContentParseParams $cpoParams,
111 ParserOutput &$output
112 ) {
113 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
114 ->get( MainConfigNames::TextModelsToParse );
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->setText( $html );
146 }
147}
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.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
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.
Represents a title within MediaWiki.
Definition Title.php:49
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2180
Base interface for content objects.
Definition Content.php:35
$content
Definition router.php:76