MediaWiki  master
CssContent.php
Go to the documentation of this file.
1 <?php
29 
36 class CssContent extends TextContent {
37 
41  private $redirectTarget = false;
42 
48  public function __construct( $text, $modelId = CONTENT_MODEL_CSS ) {
49  parent::__construct( $text, $modelId );
50  }
51 
64  public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
65  // @todo Make pre-save transformation optional for script pages (T34858)
66 
67  if ( !$user->getBoolOption( 'pst-cssjs' ) ) {
68  // Allow bot users to disable the pre-save transform for CSS/JS (T236828).
69  $popts = clone $popts;
70  $popts->setPreSaveTransform( false );
71  }
72 
73  $text = $this->getText();
74  $pst = MediaWikiServices::getInstance()->getParser()
75  ->preSaveTransform( $text, $title, $user, $popts );
76 
77  return new static( $pst );
78  }
79 
83  protected function getHtml() {
84  return Html::element( 'pre',
85  [ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
86  "\n" . $this->getText() . "\n"
87  ) . "\n";
88  }
89 
94  public function updateRedirect( Title $target ) {
95  if ( !$this->isRedirect() ) {
96  return $this;
97  }
98 
99  return $this->getContentHandler()->makeRedirectContent( $target );
100  }
101 
105  public function getRedirectTarget() {
106  if ( $this->redirectTarget !== false ) {
107  return $this->redirectTarget;
108  }
109  $this->redirectTarget = null;
110  $text = $this->getText();
111  if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
112  // Extract the title from the url
113  if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) {
114  $title = Title::newFromText( urldecode( $matches[1] ) );
115  if ( $title ) {
116  // Have a title, check that the current content equals what
117  // the redirect content should be
118  if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
119  $this->redirectTarget = $title;
120  }
121  }
122  }
123  }
124 
125  return $this->redirectTarget;
126  }
127 
128 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:44
CssContent\updateRedirect
updateRedirect(Title $target)
Definition: CssContent.php:94
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:361
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:379
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
CssContent
Content object for CSS pages.
Definition: CssContent.php:36
AbstractContent\equals
equals(Content $that=null)
Decides whether two Content objects are equal.
Definition: AbstractContent.php:220
TextContent\getText
getText()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:153
CssContent\__construct
__construct( $text, $modelId=CONTENT_MODEL_CSS)
Stable to call.
Definition: CssContent.php:48
AbstractContent\getContentHandler
getContentHandler()
Definition: AbstractContent.php:95
$matches
$matches
Definition: NoLocalSettings.php:24
CssContent\preSaveTransform
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Returns a Content object with pre-save transformations applied using Parser::preSaveTransform().
Definition: CssContent.php:64
$title
$title
Definition: testCompression.php:38
CssContent\$redirectTarget
bool Title null $redirectTarget
Definition: CssContent.php:41
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:39
Title
Represents a title within MediaWiki.
Definition: Title.php:46
CssContent\getHtml
getHtml()
Definition: CssContent.php:83
CssContent\getRedirectTarget
getRedirectTarget()
Definition: CssContent.php:105
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
User\getBoolOption
getBoolOption( $oname)
Get the user's current setting for a given option, as a boolean value.
Definition: User.php:2604
ParserOptions\setPreSaveTransform
setPreSaveTransform( $x)
Transform wiki markup when saving the page?
Definition: ParserOptions.php:634
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:220
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:63