Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / CssContentHandler.php
blob661f8c980c61679a8c45f216ac2c9dec9270450d
1 <?php
2 /**
3 * Content handler for CSS pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Content
24 namespace MediaWiki\Content;
26 use MediaWiki\Content\Renderer\ContentParseParams;
27 use MediaWiki\Content\Transform\PreSaveTransformParams;
28 use MediaWiki\Html\Html;
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\MediaWikiServices;
31 use MediaWiki\Parser\ParserOutput;
32 use MediaWiki\Parser\ParserOutputFlags;
33 use MediaWiki\Title\Title;
34 use Wikimedia\Minify\CSSMin;
35 use WikiPage;
37 /**
38 * Content handler for CSS pages.
40 * @since 1.21
41 * @ingroup Content
43 class CssContentHandler extends CodeContentHandler {
45 /**
46 * @param string $modelId
48 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
49 parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
52 /**
53 * @return class-string<CssContent>
55 protected function getContentClass() {
56 return CssContent::class;
59 public function supportsRedirects() {
60 return true;
63 /**
64 * Create a redirect that is also valid CSS
66 * @param Title $destination
67 * @param string $text ignored
69 * @return CssContent
71 public function makeRedirectContent( Title $destination, $text = '' ) {
72 // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
73 $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
74 $class = $this->getContentClass();
76 return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
79 public function preSaveTransform(
80 Content $content,
81 PreSaveTransformParams $pstParams
82 ): Content {
83 '@phan-var CssContent $content';
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 );
93 $text = $content->getText();
94 $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
95 $text,
96 $pstParams->getPage(),
97 $pstParams->getUser(),
98 $pstParams->getParserOptions()
101 $class = $this->getContentClass();
102 return new $class( $pst );
106 * @inheritDoc
108 protected function fillParserOutput(
109 Content $content,
110 ContentParseParams $cpoParams,
111 ParserOutput &$output
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'
127 true,
128 true,
129 $cpoParams->getRevId()
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;
144 $output->clearWrapperDivClass();
145 $output->setRawText( $html );
146 // Suppress the TOC (T307691)
147 $output->setOutputFlag( ParserOutputFlags::NO_TOC );
148 $output->setSections( [] );
151 /** @deprecated class alias since 1.43 */
152 class_alias( CssContentHandler::class, 'CssContentHandler' );