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