Merge "SpecialBlock [Vue]: add NamespacesField and PagesField components"
[mediawiki.git] / includes / content / JavaScriptContent.php
blob21dce3d17c2d00ef46325b47c05dd66063c735f8
1 <?php
2 /**
3 * Content for JavaScript pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @since 1.21
22 * @file
23 * @ingroup Content
25 * @author Daniel Kinzler
28 namespace MediaWiki\Content;
30 use MediaWiki\Title\Title;
32 /**
33 * Content for JavaScript pages.
35 * @newable
36 * @ingroup Content
38 class JavaScriptContent extends TextContent {
40 /**
41 * @var Title|null|false
43 private $redirectTarget = false;
45 /**
46 * @stable to call
47 * @param string $text JavaScript code.
48 * @param string $modelId the content model name
50 public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
51 parent::__construct( $text, $modelId );
54 /**
55 * If this page is a redirect, return the content
56 * if it should redirect to $target instead
58 * @param Title $target
59 * @return JavaScriptContent
61 public function updateRedirect( Title $target ) {
62 if ( !$this->isRedirect() ) {
63 return $this;
66 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
67 return $this->getContentHandler()->makeRedirectContent( $target );
70 /**
71 * @return Title|null
73 public function getRedirectTarget() {
74 if ( $this->redirectTarget !== false ) {
75 return $this->redirectTarget;
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;
98 return $this->redirectTarget;
102 /** @deprecated class alias since 1.43 */
103 class_alias( JavaScriptContent::class, 'JavaScriptContent' );