MediaWiki  master
PageSizeConstraint.php
Go to the documentation of this file.
1 <?php
22 
23 use InvalidArgumentException;
24 use StatusValue;
25 
37 
42  public const BEFORE_MERGE = 'check-before-edit-merge';
43  public const AFTER_MERGE = 'check-after-edit-merge';
44 
46  private $contentSize;
47 
49  private $maxSize;
50 
52  private $errorCode;
53 
55  private $type;
56 
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 
104  public function getType(): string {
105  return $this->type;
106  }
107 
108 }
MediaWiki\EditPage\Constraint\PageSizeConstraint\$type
string $type
Definition: PageSizeConstraint.php:55
MediaWiki\EditPage\Constraint\PageSizeConstraint\getType
getType()
Get the type, so that the two different uses of this constraint can be told apart in debug logs.
Definition: PageSizeConstraint.php:104
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
MediaWiki\EditPage\Constraint\PageSizeConstraint
Verify the page isn't larger than the maximum.
Definition: PageSizeConstraint.php:36
MediaWiki\EditPage\Constraint\PageSizeConstraint\AFTER_MERGE
const AFTER_MERGE
Definition: PageSizeConstraint.php:43
MediaWiki\EditPage\Constraint\PageSizeConstraint\getLegacyStatus
getLegacyStatus()
Get the legacy status for failure (or success)
Definition: PageSizeConstraint.php:87
MediaWiki\EditPage\Constraint\PageSizeConstraint\checkConstraint
checkConstraint()
Definition: PageSizeConstraint.php:81
MediaWiki\EditPage\IEditObject\AS_CONTENT_TOO_BIG
const AS_CONTENT_TOO_BIG
Status: Content too big (> $wgMaxArticleSize)
Definition: IEditObject.php:47
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MediaWiki\EditPage\Constraint\PageSizeConstraint\$contentSize
int $contentSize
Definition: PageSizeConstraint.php:46
MediaWiki\EditPage\Constraint\PageSizeConstraint\$maxSize
int $maxSize
Definition: PageSizeConstraint.php:49
MediaWiki\EditPage\IEditObject\AS_MAX_ARTICLE_SIZE_EXCEEDED
const AS_MAX_ARTICLE_SIZE_EXCEEDED
Status: article is too big (> $wgMaxArticleSize), after merging in the new section.
Definition: IEditObject.php:83
MediaWiki\EditPage\Constraint\PageSizeConstraint\BEFORE_MERGE
const BEFORE_MERGE
Same constraint is used for two different errors, use these to specify which one should be used.
Definition: PageSizeConstraint.php:42
MediaWiki\EditPage\Constraint
Definition: AccidentalRecreationConstraint.php:21
MediaWiki\EditPage\Constraint\PageSizeConstraint\$errorCode
int $errorCode
Definition: PageSizeConstraint.php:52
MediaWiki\EditPage\Constraint\PageSizeConstraint\__construct
__construct(int $maxSize, int $contentSize, string $type)
Definition: PageSizeConstraint.php:62
MediaWiki\EditPage\Constraint\IEditConstraint
Interface for all constraints that can prevent edits.
Definition: IEditConstraint.php:33