Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.52% covered (danger)
6.52%
3 / 46
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CssContentHandler
6.52% covered (danger)
6.52%
3 / 46
16.67% covered (danger)
16.67%
1 / 6
75.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContentClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Content handler for CSS pages.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Content
22 */
23
24use MediaWiki\Content\Renderer\ContentParseParams;
25use MediaWiki\Content\Transform\PreSaveTransformParams;
26use MediaWiki\Html\Html;
27use MediaWiki\MainConfigNames;
28use MediaWiki\MediaWikiServices;
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     * @return CssContent
66     */
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
100    /**
101     * @inheritDoc
102     */
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}