MediaWiki master
CssContentHandler.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
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
68 public function makeRedirectContent( Title $destination, $text = '' ) {
69 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
70 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
71 $class = $this->getContentClass();
72
73 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
74 }
75
76 public function preSaveTransform(
77 Content $content,
78 PreSaveTransformParams $pstParams
79 ): Content {
80 '@phan-var CssContent $content';
81
82 // @todo Make pre-save transformation optional for script pages (T34858)
84 if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
85 // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
86 $popts = clone $pstParams->getParserOptions();
87 $popts->setPreSaveTransform( false );
88 }
89
90 $text = $content->getText();
91 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
92 $text,
93 $pstParams->getPage(),
94 $pstParams->getUser(),
95 $pstParams->getParserOptions()
96 );
97
98 $class = $this->getContentClass();
99 return new $class( $pst );
100 }
101
105 protected function fillParserOutput(
106 Content $content,
107 ContentParseParams $cpoParams,
108 ParserOutput &$output
109 ) {
110 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
112 '@phan-var CssContent $content';
113 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
114 // parse just to get links etc into the database, HTML is replaced below.
115 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
116 ->parse(
117 $content->getText(),
118 $cpoParams->getPage(),
119 WikiPage::makeParserOptionsFromTitleAndModel(
120 $cpoParams->getPage(),
121 $content->getModel(),
122 'canonical'
123 ),
124 true,
125 true,
126 $cpoParams->getRevId()
127 );
128 }
129
130 if ( $cpoParams->getGenerateHtml() ) {
131 // Return CSS wrapped in a <pre> tag.
132 $html = Html::element(
133 'pre',
134 [ 'class' => [ 'mw-code', 'mw-css' ], 'dir' => 'ltr' ],
135 "\n" . $content->getText() . "\n"
136 ) . "\n";
137 } else {
138 $html = null;
139 }
140
141 $output->clearWrapperDivClass();
142 $output->setRawText( $html );
143 // Suppress the TOC (T307691)
144 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
145 $output->setSections( [] );
146 }
147}
149class_alias( CssContentHandler::class, 'CssContentHandler' );
const CONTENT_MODEL_CSS
Definition Defines.php:251
const CONTENT_FORMAT_CSS
For CSS pages.
Definition Defines.php:269
const PROTO_RELATIVE
Definition Defines.php:233
Content handler for pages with source code as content (e.g.
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:57
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.
Base representation for an editable wiki page.
Definition WikiPage.php:93
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:2127
Content objects represent page content, e.g.
Definition Content.php:42
getModel()
Get the content model ID.
element(SerializerNode $parent, SerializerNode $node, $contents)