MediaWiki master
JavaScriptContent.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
24
34
38 private $redirectTarget = false;
39
45 public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
46 parent::__construct( $text, $modelId );
47 }
48
56 public function updateRedirect( Title $target ) {
57 if ( !$this->isRedirect() ) {
58 return $this;
59 }
60
61 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
62 return $this->getContentHandler()->makeRedirectContent( $target );
63 }
64
68 public function getRedirectTarget() {
69 if ( $this->redirectTarget !== false ) {
70 return $this->redirectTarget;
71 }
72 $this->redirectTarget = null;
73 $text = $this->getText();
74 if ( str_starts_with( $text, '/* #REDIRECT */' ) ) {
75 // Compatiblity with pages created by MW 1.41 and earlier:
76 // Older redirects use an over-escaped \u0026 instead of a literal ampersand (T107289)
77 $text = str_replace( '\u0026', '&', $text );
78 // Extract the title from the url
79 if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) {
80 $title = Title::newFromText( urldecode( $matches[1] ) );
81 if ( $title ) {
82 // Have a title, check that the current content equals what
83 // the redirect content should be
84 $expected = $this->getContentHandler()->makeRedirectContent( $title );
85 '@phan-var JavaScriptContent $expected';
86 if ( $expected->getText() === $text ) {
87 $this->redirectTarget = $title;
88 }
89 }
90 }
91 }
92
93 return $this->redirectTarget;
94 }
95
96}
98class_alias( JavaScriptContent::class, 'JavaScriptContent' );
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:250
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:78