MediaWiki master
EditConstraintRunner.php
Go to the documentation of this file.
1<?php
22
24use Psr\Log\LoggerInterface;
25use Wikimedia\Assert\Assert;
26
37
38 private LoggerInterface $logger;
39
45 private $constraints = [];
46
52 private $failedConstraint = false;
53
57 public function __construct() {
58 // TODO allow passing an array here as the starting constraints?
59 // TODO consider injecting this?
60 $this->logger = LoggerFactory::getInstance( 'EditConstraintRunner' );
61 }
62
73 public function addConstraint( IEditConstraint $constraint ) {
74 $this->constraints[] = $constraint;
75 }
76
85 public function checkConstraints(): bool {
86 foreach ( $this->constraints as $constraint ) {
87 $result = $constraint->checkConstraint();
88 if ( $result !== IEditConstraint::CONSTRAINT_PASSED ) {
89 // Use `info` instead of `debug` for the one constraint that failed
90 $this->logger->info(
91 'Checked {name}, got result: {result}',
92 [
93 'name' => $this->getConstraintName( $constraint ),
94 'result' => $result
95 ]
96 );
97
98 $this->failedConstraint = $constraint;
99 return false;
100 }
101
102 // Pass, log at `debug` level
103 $this->logger->debug(
104 'Checked {name}, got result: {result}',
105 [
106 'name' => $this->getConstraintName( $constraint ),
107 'result' => $result
108 ]
109 );
110 }
111 return true;
112 }
113
118 private function getConstraintName( IEditConstraint $constraint ): string {
119 // Used for debug logging
120 $fullClassName = explode( '\\', get_class( $constraint ) );
121 $constraintName = end( $fullClassName );
122 if ( $constraint instanceof PageSizeConstraint ) {
123 // TODO "When you need to do this instanceof, it's a code smell"
124 // Convert IEditConstraint to abstract class with a getName method
125 // once the initial migration to edit constraints is complete
126 // PageSizeConstraint is used twice, make sure they can be told apart
127 $constraintName .= ' ' . $constraint->getType();
128 }
129 return $constraintName;
130 }
131
138 Assert::precondition(
139 $this->failedConstraint !== false,
140 'getFailedConstraint called with no failed constraint'
141 );
142 return $this->failedConstraint;
143 }
144
145}
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.