MediaWiki  master
JavaScriptContent.php
Go to the documentation of this file.
1 <?php
29 
36 
40  private $redirectTarget = false;
41 
46  public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
47  parent::__construct( $text, $modelId );
48  }
49 
60  public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
61  // @todo Make pre-save transformation optional for script pages
62  // See T34858
63 
64  $text = $this->getText();
65  $pst = MediaWikiServices::getInstance()->getParser()
66  ->preSaveTransform( $text, $title, $user, $popts );
67 
68  return new static( $pst );
69  }
70 
74  protected function getHtml() {
75  return Html::element( 'pre',
76  [ 'class' => 'mw-code mw-js', 'dir' => 'ltr' ],
77  "\n" . $this->getText() . "\n"
78  ) . "\n";
79  }
80 
88  public function updateRedirect( Title $target ) {
89  if ( !$this->isRedirect() ) {
90  return $this;
91  }
92 
93  return $this->getContentHandler()->makeRedirectContent( $target );
94  }
95 
99  public function getRedirectTarget() {
100  if ( $this->redirectTarget !== false ) {
101  return $this->redirectTarget;
102  }
103  $this->redirectTarget = null;
104  $text = $this->getText();
105  if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
106  // Extract the title from the url
107  preg_match( '/title=(.*?)\\\\u0026action=raw/', $text, $matches );
108  if ( isset( $matches[1] ) ) {
109  $title = Title::newFromText( urldecode( $matches[1] ) );
110  if ( $title ) {
111  // Have a title, check that the current content equals what
112  // the redirect content should be
113  if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
114  $this->redirectTarget = $title;
115  }
116  }
117  }
118  }
119 
120  return $this->redirectTarget;
121  }
122 
123 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
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:317
AbstractContent\isRedirect
isRedirect()
Definition: AbstractContent.php:356
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:129
JavaScriptContent\updateRedirect
updateRedirect(Title $target)
If this page is a redirect, return the content if it should redirect to $target instead.
Definition: JavaScriptContent.php:88
JavaScriptContent\preSaveTransform
preSaveTransform(Title $title, User $user, ParserOptions $popts)
Returns a Content object with pre-save transformations applied using Parser::preSaveTransform().
Definition: JavaScriptContent.php:60
AbstractContent\equals
equals(Content $that=null)
Decides whether two Content objects are equal.
Definition: AbstractContent.php:202
JavaScriptContent\__construct
__construct( $text, $modelId=CONTENT_MODEL_JAVASCRIPT)
Definition: JavaScriptContent.php:46
TextContent\getText
getText()
Returns the text represented by this Content object, as a string.
Definition: TextContent.php:136
JavaScriptContent\getRedirectTarget
getRedirectTarget()
Definition: JavaScriptContent.php:99
AbstractContent\getContentHandler
getContentHandler()
Definition: AbstractContent.php:88
$matches
$matches
Definition: NoLocalSettings.php:24
$title
$title
Definition: testCompression.php:36
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:35
JavaScriptContent\$redirectTarget
bool Title null $redirectTarget
Definition: JavaScriptContent.php:40
JavaScriptContent\getHtml
getHtml()
Definition: JavaScriptContent.php:74
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
Title
Represents a title within MediaWiki.
Definition: Title.php:42
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:216
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51