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
59 public function updateRedirect( Title $target ) {
60 if ( !$this->isRedirect() ) {
61 return $this;
62 }
63
64 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
65 return $this->getContentHandler()->makeRedirectContent( $target );
66 }
67
71 public function getRedirectTarget() {
72 if ( $this->redirectTarget !== false ) {
73 return $this->redirectTarget;
74 }
75 $this->redirectTarget = null;
76 $text = $this->getText();
77 if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
78 // Compatiblity with pages created by MW 1.41 and earlier:
79 // Older redirects use an over-escaped \u0026 instead of a literal ampersand (T107289)
80 $text = str_replace( '\u0026', '&', $text );
81 // Extract the title from the url
82 if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) {
83 $title = Title::newFromText( urldecode( $matches[1] ) );
84 if ( $title ) {
85 // Have a title, check that the current content equals what
86 // the redirect content should be
87 $expected = $this->getContentHandler()->makeRedirectContent( $title );
88 '@phan-var JavaScriptContent $expected';
89 if ( $expected->getText() === $text ) {
90 $this->redirectTarget = $title;
91 }
92 }
93 }
94 }
95
96 return $this->redirectTarget;
97 }
98
99}
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:221
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.
Represents a title within MediaWiki.
Definition Title.php:78
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.