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