7namespace MediaWiki\User;
9use InvalidArgumentException;
11use MediaWiki\Config\ServiceOptions;
12use MediaWiki\Context\IContextSource;
13use MediaWiki\HookContainer\HookContainer;
14use MediaWiki\HookContainer\HookRunner;
15use MediaWiki\MainConfigNames;
16use MediaWiki\WikiMap\WikiMap;
44 private readonly array $evaluators = [],
46 $this->hookRunner =
new HookRunner( $hookContainer );
47 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
64 $isPerformingRequest = $this->context->getUser()->isSafeToLoad() &&
65 $user->equals( $this->context->getUser() );
67 $conditionType = $cond[0];
68 $args = array_slice( $cond, 1 );
70 foreach ( $this->evaluators as $evaluator ) {
71 $result = $evaluator->checkCondition( $conditionType, $args, $user, $isPerformingRequest );
72 if ( $result !==
null ) {
79 $args = array_slice( $cond, 1 );
80 $this->hookRunner->onUserRequirementsCondition( $conditionType, $args, $user, $isPerformingRequest, $result );
82 $isCurrentWiki = ( $user->
getWikiId() ===
false ) || WikiMap::isCurrentWikiId( $user->
getWikiId() );
83 if ( $isPerformingRequest && $isCurrentWiki ) {
88 $userObject = $this->userFactory->newFromUserIdentity( $user );
89 $this->hookRunner->onAutopromoteCondition( $conditionType, $args, $userObject, $result );
92 if ( $result ===
null ) {
93 throw new InvalidArgumentException(
94 "Unrecognized condition $type in UserRequirementsCondition!"
127 if ( !$this->userRequirementsConditionValidator->isValid( $cond ) ) {
131 $skippedConditions = [];
132 if ( !$usePrivateConditions ) {
134 $skippedConditions = array_fill_keys( $skippedConditions,
true );
137 return $this->recursivelyCheckConditionInternal( $cond, $user, $skippedConditions );
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 ) ) {
156 if ( $cond[0] ===
'&' ) {
158 foreach ( array_slice( $cond, 1 ) as $subcond ) {
159 $result = $this->recursivelyCheckConditionInternal( $subcond, $user, $skippedConditions );
160 if ( $result ===
false ) {
163 $hasNulls = $hasNulls || $result ===
null;
166 return $hasNulls ? null :
true;
170 if ( $cond[0] ===
'|' ) {
172 foreach ( array_slice( $cond, 1 ) as $subcond ) {
173 $result = $this->recursivelyCheckConditionInternal( $subcond, $user, $skippedConditions );
174 if ( $result ===
true ) {
177 $hasNulls = $hasNulls || $result ===
null;
180 return $hasNulls ? null :
false;
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 ) {
190 return $result1 xor $result2;
194 if ( $cond[0] ===
'!' ) {
196 foreach ( array_slice( $cond, 1 ) as $subcond ) {
197 $result = $this->recursivelyCheckConditionInternal( $subcond, $user, $skippedConditions );
198 if ( $result ===
true ) {
201 $hasNulls = $hasNulls || $result ===
null;
204 return $hasNulls ? null :
true;
209 if ( !is_array( $cond ) ) {
214 if ( count( $cond ) < 1 ) {
218 if ( isset( $skippedConditions[$cond[0]] ) ) {
222 return $this->checkCondition( $cond, $user );
232 $allPrivateConditions = $this->options->get(
MainConfigNames::UserRequirementsPrivateConditions );
234 $privateConditionsUsed = array_intersect( $allPrivateConditions, $allConditionsUsed );
235 return array_values( $privateConditionsUsed );
248 $result = $this->extractConditionsInternal( $cond );
249 return array_values( array_unique( $result ) );
258 private function extractConditionsInternal( $cond ): array {
259 if ( $cond === [] ) {
264 if ( is_array( $cond ) ) {
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 ) );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const UserRequirementsPrivateConditions
Name constant for the UserRequirementsPrivateConditions setting, for use with Config\get().
Interface for objects which can provide a MediaWiki context on request.