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