Debian Codegraph @

includes/editpage/Constraint/PageSizeConstraint.php

raw symbols SCIP
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
21 namespace MediaWiki\EditPage\Constraint;
22
23 use InvalidArgumentException;
24 use 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 */
36 class 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 private int $contentSize;
46 private int $maxSize;
47 private int $errorCode;
48 private string $type;
49
50 /**
51 * @param int $maxSize In kibibytes, from $wgMaxArticleSize
52 * @param int $contentSize
53 * @param string $type
54 */
55 public function __construct(
56 int $maxSize,
57 int $contentSize,
58 string $type
59 ) {
60 $this->maxSize = $maxSize * 1024; // Convert from kibibytes
61 $this->contentSize = $contentSize;
62
63 if ( $type === self::BEFORE_MERGE ) {
64 $this->errorCode = self::AS_CONTENT_TOO_BIG;
65 } elseif ( $type === self::AFTER_MERGE ) {
66 $this->errorCode = self::AS_MAX_ARTICLE_SIZE_EXCEEDED;
67 } else {
68 throw new InvalidArgumentException( "Invalid type: $type" );
69 }
70
71 $this->type = $type;
72 }
73
74 public function checkConstraint(): string {
75 return $this->contentSize > $this->maxSize ?
76 self::CONSTRAINT_FAILED :
77 self::CONSTRAINT_PASSED;
78 }
79
80 public function getLegacyStatus(): StatusValue {
81 $statusValue = StatusValue::newGood();
82 if ( $this->contentSize > $this->maxSize ) {
83 // Either self::AS_CONTENT_TOO_BIG, if it was too big before merging,
84 // or self::AS_MAX_ARTICLE_SIZE_EXCEEDED, if it was too big after merging
85 $statusValue->setResult( false, $this->errorCode );
86 }
87 return $statusValue;
88 }
89
90 /**
91 * Get the type, so that the two different uses of this constraint can be told
92 * apart in debug logs.
93 * @internal
94 * @codeCoverageIgnore
95 * @return string
96 */
97 public function getType(): string {
98 return $this->type;
99 }
100
101 }