Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.96% covered (warning)
65.96%
31 / 47
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CssContentHandler
67.39% covered (warning)
67.39%
31 / 46
50.00% covered (danger)
50.00%
3 / 6
11.81
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContentClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 supportsRedirects
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeRedirectContent
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 preSaveTransform
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 fillParserOutput
96.30% covered (success)
96.30%
26 / 27
0.00% covered (danger)
0.00%
0 / 1
3
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Content;
22
23use MediaWiki\Content\Renderer\ContentParseParams;
24use MediaWiki\Content\Transform\PreSaveTransformParams;
25use MediaWiki\Html\Html;
26use MediaWiki\MainConfigNames;
27use MediaWiki\MediaWikiServices;
28use MediaWiki\Page\WikiPage;
29use MediaWiki\Parser\ParserOutput;
30use MediaWiki\Parser\ParserOutputFlags;
31use MediaWiki\Title\Title;
32use Wikimedia\Minify\CSSMin;
33
34/**
35 * Content handler for CSS pages.
36 *
37 * @since 1.21
38 * @ingroup Content
39 */
40class CssContentHandler extends CodeContentHandler {
41
42    /**
43     * @param string $modelId
44     */
45    public function __construct( $modelId = CONTENT_MODEL_CSS ) {
46        parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
47    }
48
49    /**
50     * @return class-string<CssContent>
51     */
52    protected function getContentClass() {
53        return CssContent::class;
54    }
55
56    public function supportsRedirects() {
57        return true;
58    }
59
60    /**
61     * Create a redirect that is also valid CSS
62     *
63     * @param Title $destination
64     * @param string $text ignored
65     *
66     * @return CssContent
67     */
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)
83        $services = MediaWikiServices::getInstance();
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
102    /**
103     * @inheritDoc
104     */
105    protected function fillParserOutput(
106        Content $content,
107        ContentParseParams $cpoParams,
108        ParserOutput &$output
109    ) {
110        $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
111            ->get( MainConfigNames::TextModelsToParse );
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}
148/** @deprecated class alias since 1.43 */
149class_alias( CssContentHandler::class, 'CssContentHandler' );