MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
32use Wikimedia\Minify\CSSMin;
33
41
45 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
46 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
47 }
48
52 protected function getContentClass() {
53 return CssContent::class;
54 }
55
56 public function supportsRedirects() {
57 return true;
58 }
59
67 public function makeRedirectContent( Title $destination, $text = '' ) {
68 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
69 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
70 $class = $this->getContentClass();
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 $services = MediaWikiServices::getInstance();
82 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
83 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
84 $popts = clone $pstParams->getParserOptions();
85 $popts->setPreSaveTransform( false );
86 }
87
88 $text = $content->getText();
89 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
90 $text,
91 $pstParams->getPage(),
92 $pstParams->getUser(),
93 $pstParams->getParserOptions()
94 );
95
96 $class = $this->getContentClass();
97 return new $class( $pst );
98 }
99
103 protected function fillParserOutput(
104 Content $content,
105 ContentParseParams $cpoParams,
106 ParserOutput &$output
107 ) {
108 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
109 ->get( MainConfigNames::TextModelsToParse );
110 '@phan-var CssContent $content';
111 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
112 // parse just to get links etc into the database, HTML is replaced below.
113 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
114 ->parse(
115 $content->getText(),
116 $cpoParams->getPage(),
117 WikiPage::makeParserOptionsFromTitleAndModel(
118 $cpoParams->getPage(),
119 $content->getModel(),
120 'canonical'
121 ),
122 true,
123 true,
124 $cpoParams->getRevId()
125 );
126 }
127
128 if ( $cpoParams->getGenerateHtml() ) {
129 // Return CSS wrapped in a <pre> tag.
130 $html = Html::element(
131 'pre',
132 [ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
133 "\n" . $content->getText() . "\n"
134 ) . "\n";
135 } else {
136 $html = null;
137 }
138
139 $output->clearWrapperDivClass();
140 $output->setRawText( $html );
141 // Suppress the TOC (T307691)
142 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
143 $output->setSections( [] );
144 }
145}
const CONTENT_MODEL_CSS
Definition Defines.php:222
const CONTENT_FORMAT_CSS
For CSS pages.
Definition Defines.php:240
const PROTO_RELATIVE
Definition Defines.php:204
Content handler for code content such as CSS, JavaScript, JSON, etc.
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:56
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
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:2133
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.