Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
BrokenRedirectConstraint
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
 getLegacyStatus
100.00% covered (success)
100.00%
6 / 6
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;
26use Wikimedia\Message\MessageValue;
27
28/**
29 * Verify the page does not redirect to an unknown page unless
30 *  - the user is okay with a broken redirect, or
31 *  - the page already redirected to an unknown page before the edit
32 *
33 * @since 1.44
34 * @internal
35 */
36class BrokenRedirectConstraint implements IEditConstraint {
37
38    private bool $allowBrokenRedirects;
39    private Content $newContent;
40    private Content $originalContent;
41    private LinkTarget $title;
42    private string $submitButtonLabel;
43    private string $result;
44
45    /**
46     * @param bool $allowBrokenRedirects
47     * @param Content $newContent
48     * @param Content $originalContent
49     * @param LinkTarget $title
50     */
51    public function __construct(
52        bool $allowBrokenRedirects,
53        Content $newContent,
54        Content $originalContent,
55        LinkTarget $title,
56        string $submitButtonLabel
57    ) {
58        $this->allowBrokenRedirects = $allowBrokenRedirects;
59        $this->newContent = $newContent;
60        $this->originalContent = $originalContent;
61        $this->title = $title;
62        $this->submitButtonLabel = $submitButtonLabel;
63    }
64
65    public function checkConstraint(): string {
66        if ( !$this->allowBrokenRedirects ) {
67            $newRedirectTarget = $this->newContent->getRedirectTarget();
68
69            if ( $newRedirectTarget !== null && !$newRedirectTarget->isKnown() &&
70                !$newRedirectTarget->equals( $this->title ) ) {
71                $currentTarget = $this->originalContent->getRedirectTarget();
72
73                // fail if there was no previous content or the previous content contained
74                // a redirect to a known page
75                if ( !$currentTarget || $currentTarget->isKnown() ) {
76                    $this->result = self::CONSTRAINT_FAILED;
77
78                    return self::CONSTRAINT_FAILED;
79                }
80            }
81
82        }
83        $this->result = self::CONSTRAINT_PASSED;
84
85        return self::CONSTRAINT_PASSED;
86    }
87
88    public function getLegacyStatus(): StatusValue {
89        $statusValue = StatusValue::newGood();
90
91        if ( $this->result === self::CONSTRAINT_FAILED ) {
92            $statusValue->fatal( MessageValue::new( 'edit-constraint-brokenredirect',
93                [ MessageValue::new( $this->submitButtonLabel ) ] ) );
94            $statusValue->value = self::AS_BROKEN_REDIRECT;
95        }
96
97        return $statusValue;
98    }
99
100}