MediaWiki master
UserRateLimitConstraint.php
Go to the documentation of this file.
1<?php
22
25use StatusValue;
26
35
36 private RateLimitSubject $subject;
37 private string $oldContentModel;
38 private string $newContentModel;
39 private RateLimiter $limiter;
40
41 private string $result;
42
43 public function __construct(
44 RateLimiter $limiter,
45 RateLimitSubject $subject,
46 string $oldContentModel,
47 string $newContentModel
48 ) {
49 $this->limiter = $limiter;
50 $this->subject = $subject;
51 $this->oldContentModel = $oldContentModel;
52 $this->newContentModel = $newContentModel;
53 }
54
55 private function limit( string $action, int $inc = 1 ) {
56 return $this->limiter->limit( $this->subject, $action, $inc );
57 }
58
59 public function checkConstraint(): string {
60 // Need to check for rate limits on `editcontentmodel` if it is changing
61 $contentModelChange = ( $this->newContentModel !== $this->oldContentModel );
62
63 // TODO inject and use a ThrottleStore once available, see T261744
64 // Checking if the user is rate limited increments the counts, so we cannot perform
65 // the check again when getting the status; thus, store the result
66 if ( $this->limit( 'edit' )
67 || $this->limit( 'linkpurge', 0 ) // only counted after the fact
68 || ( $contentModelChange && $this->limit( 'editcontentmodel' ) )
69 ) {
70 $this->result = self::CONSTRAINT_FAILED;
71 } else {
72 $this->result = self::CONSTRAINT_PASSED;
73 }
74
75 return $this->result;
76 }
77
78 public function getLegacyStatus(): StatusValue {
79 $statusValue = StatusValue::newGood();
80
81 if ( $this->result === self::CONSTRAINT_FAILED ) {
82 $statusValue->fatal( 'actionthrottledtext' );
83 $statusValue->value = self::AS_RATE_LIMITED;
84 }
85
86 return $statusValue;
87 }
88
89}
getLegacyStatus()
Get the legacy status for failure (or success)
__construct(RateLimiter $limiter, RateLimitSubject $subject, string $oldContentModel, string $newContentModel)
Represents the subject that rate limits are applied to.
Provides rate limiting for a set of actions based on several counter buckets.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
Interface for all constraints that can prevent edits.