MediaWiki master
UserRequirementsConditionChecker.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\User;
8
9use InvalidArgumentException;
10use LogicException;
11use MediaWiki\Config\ServiceOptions;
12use MediaWiki\Context\IContextSource;
13use MediaWiki\HookContainer\HookContainer;
14use MediaWiki\HookContainer\HookRunner;
15use MediaWiki\MainConfigNames;
16use MediaWiki\WikiMap\WikiMap;
17
22
28 public const VALID_OPS = [ '&', '|', '^', '!' ];
29
31 public const CONSTRUCTOR_OPTIONS = [
33 ];
34
35 private HookRunner $hookRunner;
36
37 public function __construct(
38 private readonly ServiceOptions $options,
39 HookContainer $hookContainer,
40 private readonly UserFactory $userFactory,
41 private readonly IContextSource $context,
42 private readonly UserRequirementsConditionValidator $userRequirementsConditionValidator,
44 private readonly array $evaluators = [],
45 ) {
46 $this->hookRunner = new HookRunner( $hookContainer );
47 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
48 }
49
63 protected function checkCondition( array $cond, UserIdentity $user ): ?bool {
64 $isPerformingRequest = $this->context->getUser()->isSafeToLoad() &&
65 $user->equals( $this->context->getUser() );
66
67 $conditionType = $cond[0];
68 $args = array_slice( $cond, 1 );
69
70 foreach ( $this->evaluators as $evaluator ) {
71 $result = $evaluator->checkCondition( $conditionType, $args, $user, $isPerformingRequest );
72 if ( $result !== null ) {
73 return $result;
74 }
75 }
76
77 $result = null;
78 $type = $cond[0];
79 $args = array_slice( $cond, 1 );
80 $this->hookRunner->onUserRequirementsCondition( $conditionType, $args, $user, $isPerformingRequest, $result );
81
82 $isCurrentWiki = ( $user->getWikiId() === false ) || WikiMap::isCurrentWikiId( $user->getWikiId() );
83 if ( $isPerformingRequest && $isCurrentWiki ) {
84 // The legacy hook is run only if the tested user is the one performing
85 // the request (like for autopromote), and the user is from the local wiki.
86 // If any of these conditions is not met, we cannot invoke the hook,
87 // as it may produce incorrect results.
88 $userObject = $this->userFactory->newFromUserIdentity( $user );
89 $this->hookRunner->onAutopromoteCondition( $conditionType, $args, $userObject, $result );
90 }
91
92 if ( $result === null ) {
93 throw new InvalidArgumentException(
94 "Unrecognized condition $type in UserRequirementsCondition!"
95 );
96 }
97
98 return (bool)$result;
99 }
100
126 public function recursivelyCheckCondition( $cond, UserIdentity $user, bool $usePrivateConditions = true ): ?bool {
127 if ( !$this->userRequirementsConditionValidator->isValid( $cond ) ) {
128 return false;
129 }
130
131 $skippedConditions = [];
132 if ( !$usePrivateConditions ) {
133 $skippedConditions = $this->options->get( MainConfigNames::UserRequirementsPrivateConditions );
134 $skippedConditions = array_fill_keys( $skippedConditions, true );
135 }
136
137 return $this->recursivelyCheckConditionInternal( $cond, $user, $skippedConditions );
138 }
139
151 private function recursivelyCheckConditionInternal( $cond, UserIdentity $user, array $skippedConditions ): ?bool {
152 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], self::VALID_OPS ) ) {
153 // Recursive condition
154
155 // AND (all conditions pass)
156 if ( $cond[0] === '&' ) {
157 $hasNulls = false;
158 foreach ( array_slice( $cond, 1 ) as $subcond ) {
159 $result = $this->recursivelyCheckConditionInternal( $subcond, $user, $skippedConditions );
160 if ( $result === false ) {
161 return false;
162 }
163 $hasNulls = $hasNulls || $result === null;
164 }
165
166 return $hasNulls ? null : true;
167 }
168
169 // OR (at least one condition passes)
170 if ( $cond[0] === '|' ) {
171 $hasNulls = false;
172 foreach ( array_slice( $cond, 1 ) as $subcond ) {
173 $result = $this->recursivelyCheckConditionInternal( $subcond, $user, $skippedConditions );
174 if ( $result === true ) {
175 return true;
176 }
177 $hasNulls = $hasNulls || $result === null;
178 }
179
180 return $hasNulls ? null : false;
181 }
182
183 // XOR (exactly one condition passes)
184 if ( $cond[0] === '^' ) {
185 $result1 = $this->recursivelyCheckConditionInternal( $cond[1], $user, $skippedConditions );
186 $result2 = $this->recursivelyCheckConditionInternal( $cond[2], $user, $skippedConditions );
187 if ( $result1 === null || $result2 === null ) {
188 return null;
189 }
190 return $result1 xor $result2;
191 }
192
193 // NOT (no conditions pass)
194 if ( $cond[0] === '!' ) {
195 $hasNulls = false;
196 foreach ( array_slice( $cond, 1 ) as $subcond ) {
197 $result = $this->recursivelyCheckConditionInternal( $subcond, $user, $skippedConditions );
198 if ( $result === true ) {
199 return false;
200 }
201 $hasNulls = $hasNulls || $result === null;
202 }
203
204 return $hasNulls ? null : true;
205 }
206 }
207 // If we got here, the array presumably does not contain other conditions;
208 // it's not recursive. Pass it off to checkCondition.
209 if ( !is_array( $cond ) ) {
210 $cond = [ $cond ];
211 }
212
213 // Ensure the condition makes sense at all
214 if ( count( $cond ) < 1 ) {
215 return false;
216 }
217
218 if ( isset( $skippedConditions[$cond[0]] ) ) {
219 return null;
220 }
221
222 return $this->checkCondition( $cond, $user );
223 }
224
231 public function extractPrivateConditions( $cond ): array {
232 $allPrivateConditions = $this->options->get( MainConfigNames::UserRequirementsPrivateConditions );
233 $allConditionsUsed = $this->extractConditions( $cond );
234 $privateConditionsUsed = array_intersect( $allPrivateConditions, $allConditionsUsed );
235 return array_values( $privateConditionsUsed );
236 }
237
247 public function extractConditions( $cond ): array {
248 $result = $this->extractConditionsInternal( $cond );
249 return array_values( array_unique( $result ) );
250 }
251
258 private function extractConditionsInternal( $cond ): array {
259 if ( $cond === [] ) {
260 return [];
261 }
262
263 $result = [];
264 if ( is_array( $cond ) ) {
265 $op = $cond[0];
266 if ( in_array( $op, self::VALID_OPS ) ) {
267 foreach ( array_slice( $cond, 1 ) as $subcond ) {
268 $result = array_merge(
269 $result, $this->extractConditionsInternal( $subcond ) );
270 }
271 } else {
272 $result[] = $op;
273 }
274 } else {
275 $result[] = $cond;
276 }
277 return $result;
278 }
279}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
A class for passing options to services.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
A class containing constants representing the names of configuration variables.
const UserRequirementsPrivateConditions
Name constant for the UserRequirementsPrivateConditions setting, for use with Config\get().
Create User objects.
__construct(private readonly ServiceOptions $options, HookContainer $hookContainer, private readonly UserFactory $userFactory, private readonly IContextSource $context, private readonly UserRequirementsConditionValidator $userRequirementsConditionValidator, private readonly array UserRequirementsConditionEvaluatorBase[] $evaluators=[],)
const VALID_OPS
Logical operators recognized in $wgAutopromote.
extractPrivateConditions( $cond)
Goes through a condition passed as the input and extracts all private conditions that are used within...
extractConditions( $cond)
Goes through a condition passed as the input and extracts all simple conditions that are used within ...
checkCondition(array $cond, UserIdentity $user)
As recursivelyCheckCondition, but not recursive.
recursivelyCheckCondition( $cond, UserIdentity $user, bool $usePrivateConditions=true)
Recursively check a condition.
A base class for classes that evaluate atomic user requirements conditions (i.e., conditions checked ...
Interface for objects which can provide a MediaWiki context on request.
getWikiId()
Get the ID of the wiki this page belongs to.
Interface for objects representing user identity.