| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\EditPage\Constraint; |
| 22 |
|
| 23 |
use MediaWiki\Permissions\RateLimiter; |
| 24 |
use MediaWiki\Permissions\RateLimitSubject; |
| 25 |
use StatusValue; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class UserRateLimitConstraint implements IEditConstraint { |
| 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 |
|
| 61 |
$contentModelChange = ( $this->newContentModel !== $this->oldContentModel ); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if ( $this->limit( 'edit' ) |
| 67 |
|| $this->limit( 'linkpurge', 0 ) |
| 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 |
} |