Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
15 / 17
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
JavaScriptContent
88.24% covered (warning)
88.24%
15 / 17
33.33% covered (danger)
33.33%
1 / 3
9.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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 * Content for JavaScript pages.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
28use MediaWiki\Title\Title;
29
30/**
31 * Content for JavaScript pages.
32 *
33 * @newable
34 * @ingroup Content
35 */
36class JavaScriptContent extends TextContent {
37
38    /**
39     * @var Title|null|false
40     */
41    private $redirectTarget = false;
42
43    /**
44     * @stable to call
45     * @param string $text JavaScript code.
46     * @param string $modelId the content model name
47     */
48    public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
49        parent::__construct( $text, $modelId );
50    }
51
52    /**
53     * If this page is a redirect, return the content
54     * if it should redirect to $target instead
55     *
56     * @param Title $target
57     * @return JavaScriptContent
58     */
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
68    /**
69     * @return Title|null
70     */
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}