MediaWiki master
EditConstraintRunner.php
Go to the documentation of this file.
1<?php
22
24use Psr\Log\LoggerInterface;
25use 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
139 Assert::precondition(
140 $this->failedConstraint !== false,
141 'getFailedConstraint called with no failed constraint'
142 );
143 return $this->failedConstraint;
144 }
145
146}
Back end to process the edit constraints.
addConstraint(IEditConstraint $constraint)
Add a constraint to check.
Verify the page isn't larger than the maximum.
Create PSR-3 logger objects.
Interface for all constraints that can prevent edits.