Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ImageRedirectConstraint
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getLegacyStatus
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
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 MediaWiki\Permissions\Authority;
26use StatusValue;
27
28/**
29 * Verify user permissions:
30 *    If creating a redirect in the file namespace, must have upload rights
31 *
32 * @since 1.36
33 * @internal
34 * @author DannyS712
35 */
36class ImageRedirectConstraint implements IEditConstraint {
37
38    /** @var Content */
39    private $newContent;
40
41    /** @var LinkTarget */
42    private $title;
43
44    /** @var Authority */
45    private $performer;
46
47    /** @var string|null */
48    private $result;
49
50    /**
51     * @param Content $newContent
52     * @param LinkTarget $title
53     * @param Authority $performer
54     */
55    public function __construct(
56        Content $newContent,
57        LinkTarget $title,
58        Authority $performer
59    ) {
60        $this->newContent = $newContent;
61        $this->title = $title;
62        $this->performer = $performer;
63    }
64
65    public function checkConstraint(): string {
66        // Check isn't simple enough to just repeat when getting the status
67        if ( $this->title->getNamespace() === NS_FILE &&
68            $this->newContent->isRedirect() &&
69            !$this->performer->isAllowed( 'upload' )
70        ) {
71            $this->result = self::CONSTRAINT_FAILED;
72            return self::CONSTRAINT_FAILED;
73        }
74
75        $this->result = self::CONSTRAINT_PASSED;
76        return self::CONSTRAINT_PASSED;
77    }
78
79    public function getLegacyStatus(): StatusValue {
80        $statusValue = StatusValue::newGood();
81
82        if ( $this->result === self::CONSTRAINT_FAILED ) {
83            $errorCode = $this->performer->getUser()->isRegistered() ?
84                self::AS_IMAGE_REDIRECT_LOGGED :
85                self::AS_IMAGE_REDIRECT_ANON;
86            $statusValue->setResult( false, $errorCode );
87        }
88
89        return $statusValue;
90    }
91
92}