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
71 public function addConstraint( IEditConstraint $constraint ) {
72 $this->constraints[] = $constraint;
73 }
74
81 public function checkConstraints(): bool {
82 foreach ( $this->constraints as $constraint ) {
83 $result = $constraint->checkConstraint();
84 if ( $result !== IEditConstraint::CONSTRAINT_PASSED ) {
85 // Use `info` instead of `debug` for the one constraint that failed
86 $this->logger->info(
87 'Checked {name}, got result: {result}',
88 [
89 'name' => $this->getConstraintName( $constraint ),
90 'result' => $result
91 ]
92 );
93
94 $this->failedConstraint = $constraint;
95 return false;
96 }
97
98 // Pass, log at `debug` level
99 $this->logger->debug(
100 'Checked {name}, got result: {result}',
101 [
102 'name' => $this->getConstraintName( $constraint ),
103 'result' => $result
104 ]
105 );
106 }
107 return true;
108 }
109
110 private function getConstraintName( IEditConstraint $constraint ): string {
111 // Used for debug logging
112 $fullClassName = explode( '\\', get_class( $constraint ) );
113 $constraintName = end( $fullClassName );
114 if ( $constraint instanceof PageSizeConstraint ) {
115 // TODO "When you need to do this instanceof, it's a code smell"
116 // Convert IEditConstraint to abstract class with a getName method
117 // once the initial migration to edit constraints is complete
118 // PageSizeConstraint is used twice, make sure they can be told apart
119 $constraintName .= ' ' . $constraint->getType();
120 }
121 return $constraintName;
122 }
123
128 Assert::precondition(
129 $this->failedConstraint !== false,
130 'getFailedConstraint called with no failed constraint'
131 );
132 return $this->failedConstraint;
133 }
134
135}
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.