MediaWiki  master
CssContentHandler.php
Go to the documentation of this file.
1 <?php
27 use Wikimedia\Minify\CSSMin;
28 
36 
40  public function __construct( $modelId = CONTENT_MODEL_CSS ) {
41  parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
42  }
43 
44  protected function getContentClass() {
45  return CssContent::class;
46  }
47 
48  public function supportsRedirects() {
49  return true;
50  }
51 
59  public function makeRedirectContent( Title $destination, $text = '' ) {
60  // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
61  $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
62  $class = $this->getContentClass();
63  return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
64  }
65 
66  public function preSaveTransform(
68  PreSaveTransformParams $pstParams
69  ): Content {
70  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
71  $content,
72  $pstParams
73  );
74 
75  if ( $shouldCallDeprecatedMethod ) {
76  return $this->callDeprecatedContentPST(
77  $content,
78  $pstParams
79  );
80  }
81 
82  '@phan-var CssContent $content';
83 
84  // @todo Make pre-save transformation optional for script pages (T34858)
85  $services = MediaWikiServices::getInstance();
86  if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
87  // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
88  $popts = clone $pstParams->getParserOptions();
89  $popts->setPreSaveTransform( false );
90  }
91 
92  $text = $content->getText();
93  $pst = $services->getParser()->preSaveTransform(
94  $text,
95  $pstParams->getPage(),
96  $pstParams->getUser(),
97  $pstParams->getParserOptions()
98  );
99 
100  $class = $this->getContentClass();
101  return new $class( $pst );
102  }
103 
107  protected function fillParserOutput(
109  ContentParseParams $cpoParams,
110  ParserOutput &$output
111  ) {
112  global $wgTextModelsToParse;
113  '@phan-var CssContent $content';
114  if ( in_array( $content->getModel(), $wgTextModelsToParse ) ) {
115  // parse just to get links etc into the database, HTML is replaced below.
116  $output = MediaWikiServices::getInstance()->getParser()
117  ->parse(
118  $content->getText(),
119  $cpoParams->getPage(),
120  $cpoParams->getParserOptions(),
121  true,
122  true,
123  $cpoParams->getRevId()
124  );
125  }
126 
127  if ( $cpoParams->getGenerateHtml() ) {
128  // Return CSS wrapped in a <pre> tag.
129  $html = Html::element(
130  'pre',
131  [ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
132  "\n" . $content->getText() . "\n"
133  ) . "\n";
134  } else {
135  $html = '';
136  }
137 
138  $output->clearWrapperDivClass();
139  $output->setText( $html );
140  }
141 }
CssContentHandler\preSaveTransform
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
Definition: CssContentHandler.php:66
MediaWiki\Content\Renderer\ContentParseParams\getRevId
getRevId()
Definition: ContentParseParams.php:48
MediaWiki\Content\Renderer\ContentParseParams\getPage
getPage()
Definition: ContentParseParams.php:40
ParserOutput
Definition: ParserOutput.php:35
CssContentHandler\getContentClass
getContentClass()
Definition: CssContentHandler.php:44
MediaWiki\Content\Renderer\ContentParseParams\getGenerateHtml
getGenerateHtml()
Definition: ContentParseParams.php:64
CssContentHandler\makeRedirectContent
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid CSS.
Definition: CssContentHandler.php:59
CssContentHandler\__construct
__construct( $modelId=CONTENT_MODEL_CSS)
Definition: CssContentHandler.php:40
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:202
CodeContentHandler
Content handler for code content such as CSS, JavaScript, JSON, etc.
Definition: CodeContentHandler.php:33
CONTENT_FORMAT_CSS
const CONTENT_FORMAT_CSS
For CSS pages.
Definition: Defines.php:228
MediaWiki\Content\Transform\PreSaveTransformParams\getUser
getUser()
ContentHandler\shouldCallDeprecatedContentTransformMethod
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
Definition: ContentHandler.php:1791
MediaWiki\Content\Renderer\ContentParseParams
Definition: ContentParseParams.php:11
MediaWiki\Content\Transform\PreSaveTransformParams
Definition: PreSaveTransformParams.php:12
PROTO_RELATIVE
const PROTO_RELATIVE
Definition: Defines.php:194
MediaWiki\Content\Renderer\ContentParseParams\getParserOptions
getParserOptions()
Definition: ContentParseParams.php:56
CssContentHandler
Content handler for CSS pages.
Definition: CssContentHandler.php:35
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:810
ParserOutput\clearWrapperDivClass
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
Definition: ParserOutput.php:546
$content
$content
Definition: router.php:76
CssContentHandler\supportsRedirects
supportsRedirects()
Returns true if this content model supports redirects.
Definition: CssContentHandler.php:48
Title\getFullURL
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition: Title.php:2204
Content
Base interface for content objects.
Definition: Content.php:35
Title
Represents a title within MediaWiki.
Definition: Title.php:47
MediaWiki\Content\Transform\PreSaveTransformParams\getParserOptions
getParserOptions()
$wgTextModelsToParse
$wgTextModelsToParse
Determines which types of text are parsed as wikitext.
Definition: DefaultSettings.php:2482
ContentHandler\callDeprecatedContentPST
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Definition: ContentHandler.php:1814
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:210
MediaWiki\Content\Transform\PreSaveTransformParams\getPage
getPage()
CssContentHandler\fillParserOutput
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.Unless $generateHtml...
Definition: CssContentHandler.php:107