MediaWiki  master
CssContentHandler.php
Go to the documentation of this file.
1 <?php
30 use Wikimedia\Minify\CSSMin;
31 
39 
43  public function __construct( $modelId = CONTENT_MODEL_CSS ) {
44  parent::__construct( $modelId, [ CONTENT_FORMAT_CSS ] );
45  }
46 
50  protected function getContentClass() {
51  return CssContent::class;
52  }
53 
54  public function supportsRedirects() {
55  return true;
56  }
57 
65  public function makeRedirectContent( Title $destination, $text = '' ) {
66  // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
67  $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
68  $class = $this->getContentClass();
69  return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
70  }
71 
72  public function preSaveTransform(
74  PreSaveTransformParams $pstParams
75  ): Content {
76  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
77  $content,
78  $pstParams
79  );
80 
81  if ( $shouldCallDeprecatedMethod ) {
82  return $this->callDeprecatedContentPST(
83  $content,
84  $pstParams
85  );
86  }
87 
88  '@phan-var CssContent $content';
89 
90  // @todo Make pre-save transformation optional for script pages (T34858)
91  $services = MediaWikiServices::getInstance();
92  if ( !$services->getUserOptionsLookup()->getBoolOption( $pstParams->getUser(), 'pst-cssjs' ) ) {
93  // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
94  $popts = clone $pstParams->getParserOptions();
95  $popts->setPreSaveTransform( false );
96  }
97 
98  $text = $content->getText();
99  $pst = $services->getParserFactory()->getInstance()->preSaveTransform(
100  $text,
101  $pstParams->getPage(),
102  $pstParams->getUser(),
103  $pstParams->getParserOptions()
104  );
105 
106  $class = $this->getContentClass();
107  return new $class( $pst );
108  }
109 
113  protected function fillParserOutput(
115  ContentParseParams $cpoParams,
116  ParserOutput &$output
117  ) {
118  $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()
119  ->get( MainConfigNames::TextModelsToParse );
120  '@phan-var CssContent $content';
121  if ( in_array( $content->getModel(), $textModelsToParse ) ) {
122  // parse just to get links etc into the database, HTML is replaced below.
123  $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
124  ->parse(
125  $content->getText(),
126  $cpoParams->getPage(),
128  $cpoParams->getPage(),
129  $content->getModel(),
130  'canonical'
131  ),
132  true,
133  true,
134  $cpoParams->getRevId()
135  );
136  }
137 
138  if ( $cpoParams->getGenerateHtml() ) {
139  // Return CSS wrapped in a <pre> tag.
140  $html = Html::element(
141  'pre',
142  [ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
143  "\n" . $content->getText() . "\n"
144  ) . "\n";
145  } else {
146  $html = null;
147  }
148 
149  $output->clearWrapperDivClass();
150  $output->setText( $html );
151  }
152 }
const CONTENT_MODEL_CSS
Definition: Defines.php:211
const CONTENT_FORMAT_CSS
For CSS pages.
Definition: Defines.php:229
const PROTO_RELATIVE
Definition: Defines.php:193
Content handler for code content such as CSS, JavaScript, JSON, etc.
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Content handler for CSS pages.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.Unless $generateHtml...
__construct( $modelId=CONTENT_MODEL_CSS)
supportsRedirects()
Returns true if this content model supports redirects.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
makeRedirectContent(Title $destination, $text='')
Create a redirect that is also valid CSS.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:76
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition: Title.php:2135
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setText( $text)
Set the text of the ParserOutput.
static makeParserOptionsFromTitleAndModel(PageReference $pageRef, string $contentModel, $context)
Create canonical parser options for a given title and content model.
Definition: WikiPage.php:1904
Base interface for representing page content.
Definition: Content.php:39
$content
Definition: router.php:76