MediaWiki  master
EditConstraintRunner.php
Go to the documentation of this file.
1 <?php
22 
24 use Psr\Log\LoggerInterface;
25 use Wikimedia\Assert\Assert;
26 
37 
39  private $logger;
40 
46  private $constraints = [];
47 
53  private $failedConstraint = false;
54 
58  public function __construct() {
59  // TODO allow passing an array here as the starting constraints?
60  // TODO consider injecting this?
61  $this->logger = LoggerFactory::getInstance( 'EditConstraintRunner' );
62  }
63 
74  public function addConstraint( IEditConstraint $constraint ) {
75  $this->constraints[] = $constraint;
76  }
77 
86  public function checkConstraints() : bool {
87  foreach ( $this->constraints as $constraint ) {
88  $result = $constraint->checkConstraint();
89  if ( $result !== IEditConstraint::CONSTRAINT_PASSED ) {
90  // Use `info` instead of `debug` for the one constraint that failed
91  $this->logger->info(
92  'Checked {name}, got result: {result}',
93  [
94  'name' => $this->getConstraintName( $constraint ),
95  'result' => $result
96  ]
97  );
98 
99  $this->failedConstraint = $constraint;
100  return false;
101  }
102 
103  // Pass, log at `debug` level
104  $this->logger->debug(
105  'Checked {name}, got result: {result}',
106  [
107  'name' => $this->getConstraintName( $constraint ),
108  'result' => $result
109  ]
110  );
111  }
112  return true;
113  }
114 
119  private function getConstraintName( IEditConstraint $constraint ) : string {
120  // Used for debug logging
121  $fullClassName = explode( '\\', get_class( $constraint ) );
122  $constraintName = end( $fullClassName );
123  if ( $constraint instanceof PageSizeConstraint ) {
124  // TODO "When you need to do this instanceof, it's a code smell"
125  // Convert IEditConstraint to abstract class with a getName method
126  // once the initial migration to edit constraints is complete
127  // PageSizeConstraint is used twice, make sure they can be told apart
128  $constraintName .= ' ' . $constraint->getType();
129  }
130  return $constraintName;
131  }
132 
138  public function getFailedConstraint() : IEditConstraint {
139  Assert::precondition(
140  $this->failedConstraint !== false,
141  'getFailedConstraint called with no failed constraint'
142  );
144  }
145 
146 }
MediaWiki\EditPage\Constraint\EditConstraintRunner
Back end to process the edit constraints.
Definition: EditConstraintRunner.php:36
MediaWiki\EditPage\Constraint\EditConstraintRunner\checkConstraints
checkConstraints()
Run constraint checks.
Definition: EditConstraintRunner.php:86
MediaWiki\Logger\LoggerFactory\getInstance
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Definition: LoggerFactory.php:92
MediaWiki\EditPage\Constraint\EditConstraintRunner\__construct
__construct()
Create a new runner.
Definition: EditConstraintRunner.php:58
MediaWiki\EditPage\Constraint\PageSizeConstraint
Verify the page isn't larger than the maximum.
Definition: PageSizeConstraint.php:36
MediaWiki\EditPage\Constraint\EditConstraintRunner\getFailedConstraint
getFailedConstraint()
Get the constraint that failed.
Definition: EditConstraintRunner.php:138
MediaWiki\EditPage\Constraint\EditConstraintRunner\$failedConstraint
IEditConstraint bool $failedConstraint
The constraint that failed, so that its status can be fetched, or false if none failed.
Definition: EditConstraintRunner.php:53
MediaWiki\EditPage\Constraint\EditConstraintRunner\addConstraint
addConstraint(IEditConstraint $constraint)
Add a constraint to check.
Definition: EditConstraintRunner.php:74
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
MediaWiki\EditPage\Constraint\EditConstraintRunner\getConstraintName
getConstraintName(IEditConstraint $constraint)
Definition: EditConstraintRunner.php:119
MediaWiki\EditPage\Constraint\EditConstraintRunner\$logger
LoggerInterface $logger
Definition: EditConstraintRunner.php:39
MediaWiki\EditPage\Constraint
Definition: AccidentalRecreationConstraint.php:21
MediaWiki\EditPage\Constraint\EditConstraintRunner\$constraints
IEditConstraint[] $constraints
Constraints to check.
Definition: EditConstraintRunner.php:46
MediaWiki\EditPage\Constraint\IEditConstraint
Interface for all constraints that can prevent edits.
Definition: IEditConstraint.php:33