Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SelfRedirectConstraint
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 getLegacyStatus
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
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\EditPage\Constraint;
22
23use Content;
24use MediaWiki\Linker\LinkTarget;
25use StatusValue;
26
27/**
28 * Verify the page does not redirect to itself unless
29 *   - the user is okay with a self redirect, or
30 *   - the page already redirected to itself before the edit
31 *
32 * @since 1.36
33 * @internal
34 */
35class SelfRedirectConstraint implements IEditConstraint {
36
37    /** @var bool */
38    private $allowSelfRedirect;
39
40    /** @var Content */
41    private $newContent;
42
43    /** @var Content */
44    private $originalContent;
45
46    /** @var LinkTarget */
47    private $title;
48
49    /** @var string|null */
50    private $result;
51
52    /**
53     * @param bool $allowSelfRedirect
54     * @param Content $newContent
55     * @param Content $originalContent
56     * @param LinkTarget $title
57     */
58    public function __construct(
59        bool $allowSelfRedirect,
60        Content $newContent,
61        Content $originalContent,
62        LinkTarget $title
63    ) {
64        $this->allowSelfRedirect = $allowSelfRedirect;
65        $this->newContent = $newContent;
66        $this->originalContent = $originalContent;
67        $this->title = $title;
68    }
69
70    public function checkConstraint(): string {
71        if ( !$this->allowSelfRedirect
72            && $this->newContent->isRedirect()
73            && $this->newContent->getRedirectTarget()->equals( $this->title )
74        ) {
75            // T29683 If the page already redirects to itself, don't warn.
76            $currentTarget = $this->originalContent->getRedirectTarget();
77            if ( !$currentTarget || !$currentTarget->equals( $this->title ) ) {
78                $this->result = self::CONSTRAINT_FAILED;
79                return self::CONSTRAINT_FAILED;
80            }
81        }
82        $this->result = self::CONSTRAINT_PASSED;
83        return self::CONSTRAINT_PASSED;
84    }
85
86    public function getLegacyStatus(): StatusValue {
87        $statusValue = StatusValue::newGood();
88        if ( $this->result === self::CONSTRAINT_FAILED ) {
89            $statusValue->fatal( 'selfredirect' );
90            $statusValue->value = self::AS_SELF_REDIRECT;
91        }
92        return $statusValue;
93    }
94
95}