Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
10 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CssContent
66.67% covered (warning)
66.67%
10 / 15
0.00% covered (danger)
0.00%
0 / 3
12.00
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
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getRedirectTarget
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
6.03
1<?php
2/**
3 * Content object for CSS 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 object for CSS pages.
32 *
33 * @newable
34 * @ingroup Content
35 */
36class CssContent extends TextContent {
37
38    /**
39     * @var Title|null|false
40     */
41    private $redirectTarget = false;
42
43    /**
44     * @stable to call
45     * @param string $text CSS code.
46     * @param string $modelId the content content model
47     */
48    public function __construct( $text, $modelId = CONTENT_MODEL_CSS ) {
49        parent::__construct( $text, $modelId );
50    }
51
52    /**
53     * @param Title $target
54     * @return CssContent
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            // Extract the title from the url
76            if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) {
77                $title = Title::newFromText( urldecode( $matches[1] ) );
78                if ( $title ) {
79                    // Have a title, check that the current content equals what
80                    // the redirect content should be
81                    if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
82                        $this->redirectTarget = $title;
83                    }
84                }
85            }
86        }
87
88        return $this->redirectTarget;
89    }
90
91}