Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
65.96% |
31 / 47 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
CssContentHandler | |
67.39% |
31 / 46 |
|
50.00% |
3 / 6 |
11.81 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getContentClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supportsRedirects | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
makeRedirectContent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
preSaveTransform | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
fillParserOutput | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
3 |
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 | |
24 | namespace MediaWiki\Content; |
25 | |
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; |
36 | |
37 | /** |
38 | * Content handler for CSS pages. |
39 | * |
40 | * @since 1.21 |
41 | * @ingroup Content |
42 | */ |
43 | class CssContentHandler extends CodeContentHandler { |
44 | |
45 | /** |
46 | * @param string $modelId |
47 | */ |
48 | public function __construct( $modelId = CONTENT_MODEL_CSS ) { |
49 | parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] ); |
50 | } |
51 | |
52 | /** |
53 | * @return class-string<CssContent> |
54 | */ |
55 | protected function getContentClass() { |
56 | return CssContent::class; |
57 | } |
58 | |
59 | public function supportsRedirects() { |
60 | return true; |
61 | } |
62 | |
63 | /** |
64 | * Create a redirect that is also valid CSS |
65 | * |
66 | * @param Title $destination |
67 | * @param string $text ignored |
68 | * |
69 | * @return CssContent |
70 | */ |
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(); |
75 | |
76 | return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' ); |
77 | } |
78 | |
79 | public function preSaveTransform( |
80 | Content $content, |
81 | PreSaveTransformParams $pstParams |
82 | ): Content { |
83 | '@phan-var CssContent $content'; |
84 | |
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 ); |
91 | } |
92 | |
93 | $text = $content->getText(); |
94 | $pst = $services->getParserFactory()->getInstance()->preSaveTransform( |
95 | $text, |
96 | $pstParams->getPage(), |
97 | $pstParams->getUser(), |
98 | $pstParams->getParserOptions() |
99 | ); |
100 | |
101 | $class = $this->getContentClass(); |
102 | return new $class( $pst ); |
103 | } |
104 | |
105 | /** |
106 | * @inheritDoc |
107 | */ |
108 | protected function fillParserOutput( |
109 | Content $content, |
110 | ContentParseParams $cpoParams, |
111 | ParserOutput &$output |
112 | ) { |
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' |
126 | ), |
127 | true, |
128 | true, |
129 | $cpoParams->getRevId() |
130 | ); |
131 | } |
132 | |
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; |
142 | } |
143 | |
144 | $output->clearWrapperDivClass(); |
145 | $output->setRawText( $html ); |
146 | // Suppress the TOC (T307691) |
147 | $output->setOutputFlag( ParserOutputFlags::NO_TOC ); |
148 | $output->setSections( [] ); |
149 | } |
150 | } |
151 | /** @deprecated class alias since 1.43 */ |
152 | class_alias( CssContentHandler::class, 'CssContentHandler' ); |