MediaWiki master
JavaScriptContent.php
Go to the documentation of this file.
1<?php
28namespace MediaWiki\Content;
29
31
39
43 private $redirectTarget = false;
44
50 public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
51 parent::__construct( $text, $modelId );
52 }
53
61 public function updateRedirect( Title $target ) {
62 if ( !$this->isRedirect() ) {
63 return $this;
64 }
65
66 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
67 return $this->getContentHandler()->makeRedirectContent( $target );
68 }
69
73 public function getRedirectTarget() {
74 if ( $this->redirectTarget !== false ) {
75 return $this->redirectTarget;
76 }
77 $this->redirectTarget = null;
78 $text = $this->getText();
79 if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
80 // Compatiblity with pages created by MW 1.41 and earlier:
81 // Older redirects use an over-escaped \u0026 instead of a literal ampersand (T107289)
82 $text = str_replace( '\u0026', '&', $text );
83 // Extract the title from the url
84 if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) {
85 $title = Title::newFromText( urldecode( $matches[1] ) );
86 if ( $title ) {
87 // Have a title, check that the current content equals what
88 // the redirect content should be
89 $expected = $this->getContentHandler()->makeRedirectContent( $title );
90 '@phan-var JavaScriptContent $expected';
91 if ( $expected->getText() === $text ) {
92 $this->redirectTarget = $title;
93 }
94 }
95 }
96 }
97
98 return $this->redirectTarget;
99 }
100
101}
103class_alias( JavaScriptContent::class, 'JavaScriptContent' );
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:222
Content for JavaScript pages.
__construct( $text, $modelId=CONTENT_MODEL_JAVASCRIPT)
updateRedirect(Title $target)
If this page is a redirect, return the content if it should redirect to $target instead.
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.
Represents a title within MediaWiki.
Definition Title.php:79