| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class CssContentHandler extends CodeContentHandler { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( $modelId = CONTENT_MODEL_CSS ) { |
| 49 |
parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected function getContentClass() { |
| 56 |
return CssContent::class; |
| 57 |
} |
| 58 |
|
| 59 |
public function supportsRedirects() { |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function makeRedirectContent( Title $destination, $text = '' ) { |
| 72 |
|
| 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 |
|
| 86 |
$services = MediaWikiServices::getInstance(); |
| 87 |
if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) { |
| 88 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 147 |
$output->setOutputFlag( ParserOutputFlags::NO_TOC ); |
| 148 |
$output->setSections( [] ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
class_alias( CssContentHandler::class, 'CssContentHandler' ); |