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 MediaWiki\Content\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    private bool $allowSelfRedirect;
38    private Content $newContent;
39    private Content $originalContent;
40    private LinkTarget $title;
41    private string $result;
42
43    /**
44     * @param bool $allowSelfRedirect
45     * @param Content $newContent
46     * @param Content $originalContent
47     * @param LinkTarget $title
48     */
49    public function __construct(
50        bool $allowSelfRedirect,
51        Content $newContent,
52        Content $originalContent,
53        LinkTarget $title
54    ) {
55        $this->allowSelfRedirect = $allowSelfRedirect;
56        $this->newContent = $newContent;
57        $this->originalContent = $originalContent;
58        $this->title = $title;
59    }
60
61    public function checkConstraint(): string {
62        if ( !$this->allowSelfRedirect
63            && $this->newContent->isRedirect()
64            && $this->newContent->getRedirectTarget()->equals( $this->title )
65        ) {
66            // T29683 If the page already redirects to itself, don't warn.
67            $currentTarget = $this->originalContent->getRedirectTarget();
68            if ( !$currentTarget || !$currentTarget->equals( $this->title ) ) {
69                $this->result = self::CONSTRAINT_FAILED;
70                return self::CONSTRAINT_FAILED;
71            }
72        }
73        $this->result = self::CONSTRAINT_PASSED;
74        return self::CONSTRAINT_PASSED;
75    }
76
77    public function getLegacyStatus(): StatusValue {
78        $statusValue = StatusValue::newGood();
79        if ( $this->result === self::CONSTRAINT_FAILED ) {
80            $statusValue->fatal( 'selfredirect' );
81            $statusValue->value = self::AS_SELF_REDIRECT;
82        }
83        return $statusValue;
84    }
85
86}