Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PageSizeConstraint
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 checkConstraint
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getLegacyStatus
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getType
n/a
0 / 0
n/a
0 / 0
1
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 InvalidArgumentException;
24use StatusValue;
25
26/**
27 * Verify the page isn't larger than the maximum
28 *
29 * This is used for both checking the size //before// merging the edit, and checking the size
30 * //after// applying the edit, and the result codes they return are different.
31 *
32 * @since 1.36
33 * @internal
34 * @author DannyS712
35 */
36class PageSizeConstraint implements IEditConstraint {
37
38    /**
39     * Same constraint is used for two different errors, use these
40     * to specify which one should be used
41     */
42    public const BEFORE_MERGE = 'check-before-edit-merge';
43    public const AFTER_MERGE = 'check-after-edit-merge';
44
45    /** @var int */
46    private $contentSize;
47
48    /** @var int */
49    private $maxSize;
50
51    /** @var int */
52    private $errorCode;
53
54    /** @var string */
55    private $type;
56
57    /**
58     * @param int $maxSize In kibibytes, from $wgMaxArticleSize
59     * @param int $contentSize
60     * @param string $type
61     */
62    public function __construct(
63        int $maxSize,
64        int $contentSize,
65        string $type
66    ) {
67        $this->maxSize = $maxSize * 1024; // Convert from kibibytes
68        $this->contentSize = $contentSize;
69
70        if ( $type === self::BEFORE_MERGE ) {
71            $this->errorCode = self::AS_CONTENT_TOO_BIG;
72        } elseif ( $type === self::AFTER_MERGE ) {
73            $this->errorCode = self::AS_MAX_ARTICLE_SIZE_EXCEEDED;
74        } else {
75            throw new InvalidArgumentException( "Invalid type: $type" );
76        }
77
78        $this->type = $type;
79    }
80
81    public function checkConstraint(): string {
82        return $this->contentSize > $this->maxSize ?
83            self::CONSTRAINT_FAILED :
84            self::CONSTRAINT_PASSED;
85    }
86
87    public function getLegacyStatus(): StatusValue {
88        $statusValue = StatusValue::newGood();
89        if ( $this->contentSize > $this->maxSize ) {
90            // Either self::AS_CONTENT_TOO_BIG, if it was too big before merging,
91            // or self::AS_MAX_ARTICLE_SIZE_EXCEEDED, if it was too big after merging
92            $statusValue->setResult( false, $this->errorCode );
93        }
94        return $statusValue;
95    }
96
97    /**
98     * Get the type, so that the two different uses of this constraint can be told
99     * apart in debug logs.
100     * @internal
101     * @codeCoverageIgnore
102     * @return string
103     */
104    public function getType(): string {
105        return $this->type;
106    }
107
108}