Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
JavaScriptContent
94.12% covered (success)
94.12%
16 / 17
66.67% covered (warning)
66.67%
2 / 3
9.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateRedirect
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getRedirectTarget
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
6.02
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Content;
22
23use MediaWiki\Title\Title;
24
25/**
26 * Content for JavaScript pages.
27 *
28 * @newable
29 * @since 1.21
30 * @ingroup Content
31 * @author Daniel Kinzler
32 */
33class JavaScriptContent extends TextContent {
34
35    /**
36     * @var Title|null|false
37     */
38    private $redirectTarget = false;
39
40    /**
41     * @stable to call
42     * @param string $text JavaScript code.
43     * @param string $modelId the content model name
44     */
45    public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
46        parent::__construct( $text, $modelId );
47    }
48
49    /**
50     * If this page is a redirect, return the content
51     * if it should redirect to $target instead
52     *
53     * @param Title $target
54     * @return JavaScriptContent
55     */
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
65    /**
66     * @return Title|null
67     */
68    public function getRedirectTarget() {
69        if ( $this->redirectTarget !== false ) {
70            return $this->redirectTarget;
71        }
72        $this->redirectTarget = null;
73        $text = $this->getText();
74        if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
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}
97/** @deprecated class alias since 1.43 */
98class_alias( JavaScriptContent::class, 'JavaScriptContent' );