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