| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\User; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use JobQueueGroup; |
| 25 |
use LogicException; |
| 26 |
use ManualLogEntry; |
| 27 |
use MediaWiki\Config\ServiceOptions; |
| 28 |
use MediaWiki\Deferred\DeferredUpdates; |
| 29 |
use MediaWiki\HookContainer\HookContainer; |
| 30 |
use MediaWiki\HookContainer\HookRunner; |
| 31 |
use MediaWiki\MainConfigNames; |
| 32 |
use MediaWiki\Parser\Sanitizer; |
| 33 |
use MediaWiki\Permissions\Authority; |
| 34 |
use MediaWiki\Permissions\GroupPermissionsLookup; |
| 35 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 36 |
use MediaWiki\WikiMap\WikiMap; |
| 37 |
use Psr\Log\LoggerInterface; |
| 38 |
use UserGroupExpiryJob; |
| 39 |
use Wikimedia\Assert\Assert; |
| 40 |
use Wikimedia\IPUtils; |
| 41 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 42 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 43 |
use Wikimedia\Rdbms\ILBFactory; |
| 44 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 45 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 46 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class UserGroupManager { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public const CONSTRUCTOR_OPTIONS = [ |
| 58 |
MainConfigNames::AddGroups, |
| 59 |
MainConfigNames::AutoConfirmAge, |
| 60 |
MainConfigNames::AutoConfirmCount, |
| 61 |
MainConfigNames::Autopromote, |
| 62 |
MainConfigNames::AutopromoteOnce, |
| 63 |
MainConfigNames::AutopromoteOnceLogInRC, |
| 64 |
MainConfigNames::EmailAuthentication, |
| 65 |
MainConfigNames::ImplicitGroups, |
| 66 |
MainConfigNames::GroupInheritsPermissions, |
| 67 |
MainConfigNames::GroupPermissions, |
| 68 |
MainConfigNames::GroupsAddToSelf, |
| 69 |
MainConfigNames::GroupsRemoveFromSelf, |
| 70 |
MainConfigNames::RevokePermissions, |
| 71 |
MainConfigNames::RemoveGroups, |
| 72 |
MainConfigNames::PrivilegedGroups, |
| 73 |
]; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public const VALID_OPS = [ '&', '|', '^', '!' ]; |
| 81 |
|
| 82 |
private ServiceOptions $options; |
| 83 |
private IConnectionProvider $dbProvider; |
| 84 |
private HookContainer $hookContainer; |
| 85 |
private HookRunner $hookRunner; |
| 86 |
private ReadOnlyMode $readOnlyMode; |
| 87 |
private UserEditTracker $userEditTracker; |
| 88 |
private GroupPermissionsLookup $groupPermissionsLookup; |
| 89 |
private JobQueueGroup $jobQueueGroup; |
| 90 |
private LoggerInterface $logger; |
| 91 |
private TempUserConfig $tempUserConfig; |
| 92 |
|
| 93 |
|
| 94 |
private $clearCacheCallbacks; |
| 95 |
|
| 96 |
|
| 97 |
private $wikiId; |
| 98 |
|
| 99 |
|
| 100 |
private const CACHE_IMPLICIT = 'implicit'; |
| 101 |
|
| 102 |
|
| 103 |
private const CACHE_EFFECTIVE = 'effective'; |
| 104 |
|
| 105 |
|
| 106 |
private const CACHE_MEMBERSHIP = 'membership'; |
| 107 |
|
| 108 |
|
| 109 |
private const CACHE_FORMER = 'former'; |
| 110 |
|
| 111 |
|
| 112 |
private const CACHE_PRIVILEGED = 'privileged'; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private $userGroupCache = []; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private $queryFlagsUsedForCaching = []; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private $recursionMap = []; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function __construct( |
| 163 |
ServiceOptions $options, |
| 164 |
ReadOnlyMode $readOnlyMode, |
| 165 |
ILBFactory $lbFactory, |
| 166 |
HookContainer $hookContainer, |
| 167 |
UserEditTracker $userEditTracker, |
| 168 |
GroupPermissionsLookup $groupPermissionsLookup, |
| 169 |
JobQueueGroup $jobQueueGroup, |
| 170 |
LoggerInterface $logger, |
| 171 |
TempUserConfig $tempUserConfig, |
| 172 |
array $clearCacheCallbacks = [], |
| 173 |
$wikiId = UserIdentity::LOCAL |
| 174 |
) { |
| 175 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 176 |
$this->options = $options; |
| 177 |
$this->dbProvider = $lbFactory; |
| 178 |
$this->hookContainer = $hookContainer; |
| 179 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 180 |
$this->userEditTracker = $userEditTracker; |
| 181 |
$this->groupPermissionsLookup = $groupPermissionsLookup; |
| 182 |
$this->jobQueueGroup = $jobQueueGroup; |
| 183 |
$this->logger = $logger; |
| 184 |
$this->tempUserConfig = $tempUserConfig; |
| 185 |
$this->readOnlyMode = $readOnlyMode; |
| 186 |
$this->clearCacheCallbacks = $clearCacheCallbacks; |
| 187 |
$this->wikiId = $wikiId; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function listAllGroups(): array { |
| 197 |
return array_values( array_unique( |
| 198 |
array_diff( |
| 199 |
array_merge( |
| 200 |
array_keys( $this->options->get( MainConfigNames::GroupPermissions ) ), |
| 201 |
array_keys( $this->options->get( MainConfigNames::RevokePermissions ) ), |
| 202 |
array_keys( $this->options->get( MainConfigNames::GroupInheritsPermissions ) ) |
| 203 |
), |
| 204 |
$this->listAllImplicitGroups() |
| 205 |
) |
| 206 |
) ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function listAllImplicitGroups(): array { |
| 214 |
return $this->options->get( MainConfigNames::ImplicitGroups ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function newGroupMembershipFromRow( \stdClass $row ): UserGroupMembership { |
| 227 |
return new UserGroupMembership( |
| 228 |
(int)$row->ug_user, |
| 229 |
$row->ug_group, |
| 230 |
$row->ug_expiry === null ? null : wfTimestamp( |
| 231 |
TS_MW, |
| 232 |
$row->ug_expiry |
| 233 |
) |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function loadGroupMembershipsFromArray( |
| 245 |
UserIdentity $user, |
| 246 |
array $userGroups, |
| 247 |
int $queryFlags = IDBAccessObject::READ_NORMAL |
| 248 |
) { |
| 249 |
$user->assertWiki( $this->wikiId ); |
| 250 |
$membershipGroups = []; |
| 251 |
reset( $userGroups ); |
| 252 |
foreach ( $userGroups as $row ) { |
| 253 |
$ugm = $this->newGroupMembershipFromRow( $row ); |
| 254 |
$membershipGroups[ $ugm->getGroup() ] = $ugm; |
| 255 |
} |
| 256 |
$this->setCache( |
| 257 |
$this->getCacheKey( $user ), |
| 258 |
self::CACHE_MEMBERSHIP, |
| 259 |
$membershipGroups, |
| 260 |
$queryFlags |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function getUserImplicitGroups( |
| 276 |
UserIdentity $user, |
| 277 |
int $queryFlags = IDBAccessObject::READ_NORMAL, |
| 278 |
bool $recache = false |
| 279 |
): array { |
| 280 |
$user->assertWiki( $this->wikiId ); |
| 281 |
$userKey = $this->getCacheKey( $user ); |
| 282 |
if ( $recache || |
| 283 |
!isset( $this->userGroupCache[$userKey][self::CACHE_IMPLICIT] ) || |
| 284 |
!$this->canUseCachedValues( $user, self::CACHE_IMPLICIT, $queryFlags ) |
| 285 |
) { |
| 286 |
$groups = [ '*' ]; |
| 287 |
if ( $this->tempUserConfig->isTempName( $user->getName() ) ) { |
| 288 |
$groups[] = 'temp'; |
| 289 |
} elseif ( $user->isRegistered() ) { |
| 290 |
$groups[] = 'user'; |
| 291 |
$groups = array_unique( array_merge( |
| 292 |
$groups, |
| 293 |
$this->getUserAutopromoteGroups( $user ) |
| 294 |
) ); |
| 295 |
} |
| 296 |
$this->setCache( $userKey, self::CACHE_IMPLICIT, $groups, $queryFlags ); |
| 297 |
if ( $recache ) { |
| 298 |
|
| 299 |
|
| 300 |
$this->clearUserCacheForKind( $user, self::CACHE_EFFECTIVE ); |
| 301 |
} |
| 302 |
} |
| 303 |
return $this->userGroupCache[$userKey][self::CACHE_IMPLICIT]; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public function getUserEffectiveGroups( |
| 318 |
UserIdentity $user, |
| 319 |
int $queryFlags = IDBAccessObject::READ_NORMAL, |
| 320 |
bool $recache = false |
| 321 |
): array { |
| 322 |
$user->assertWiki( $this->wikiId ); |
| 323 |
$userKey = $this->getCacheKey( $user ); |
| 324 |
|
| 325 |
|
| 326 |
if ( $recache || |
| 327 |
!$this->canUseCachedValues( $user, self::CACHE_EFFECTIVE, $queryFlags ) || |
| 328 |
!isset( $this->userGroupCache[$userKey][self::CACHE_EFFECTIVE] ) |
| 329 |
) { |
| 330 |
$groups = array_unique( array_merge( |
| 331 |
$this->getUserGroups( $user, $queryFlags ), |
| 332 |
$this->getUserImplicitGroups( $user, $queryFlags, $recache ) |
| 333 |
) ); |
| 334 |
|
| 335 |
|
| 336 |
if ( $this->hookContainer->isRegistered( 'UserEffectiveGroups' ) ) { |
| 337 |
$userObj = User::newFromIdentity( $user ); |
| 338 |
$userObj->load(); |
| 339 |
|
| 340 |
$this->hookRunner->onUserEffectiveGroups( $userObj, $groups ); |
| 341 |
} |
| 342 |
|
| 343 |
$effectiveGroups = array_values( array_unique( $groups ) ); |
| 344 |
$this->setCache( $userKey, self::CACHE_EFFECTIVE, $effectiveGroups, $queryFlags ); |
| 345 |
} |
| 346 |
return $this->userGroupCache[$userKey][self::CACHE_EFFECTIVE]; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function getUserFormerGroups( |
| 362 |
UserIdentity $user, |
| 363 |
int $queryFlags = IDBAccessObject::READ_NORMAL |
| 364 |
): array { |
| 365 |
$user->assertWiki( $this->wikiId ); |
| 366 |
$userKey = $this->getCacheKey( $user ); |
| 367 |
|
| 368 |
if ( $this->canUseCachedValues( $user, self::CACHE_FORMER, $queryFlags ) && |
| 369 |
isset( $this->userGroupCache[$userKey][self::CACHE_FORMER] ) |
| 370 |
) { |
| 371 |
return $this->userGroupCache[$userKey][self::CACHE_FORMER]; |
| 372 |
} |
| 373 |
|
| 374 |
if ( !$user->isRegistered() ) { |
| 375 |
|
| 376 |
return []; |
| 377 |
} |
| 378 |
|
| 379 |
$res = $this->getDBConnectionRefForQueryFlags( $queryFlags )->newSelectQueryBuilder() |
| 380 |
->select( 'ufg_group' ) |
| 381 |
->from( 'user_former_groups' ) |
| 382 |
->where( [ 'ufg_user' => $user->getId( $this->wikiId ) ] ) |
| 383 |
->caller( __METHOD__ ) |
| 384 |
->fetchResultSet(); |
| 385 |
$formerGroups = []; |
| 386 |
foreach ( $res as $row ) { |
| 387 |
$formerGroups[] = $row->ufg_group; |
| 388 |
} |
| 389 |
$this->setCache( $userKey, self::CACHE_FORMER, $formerGroups, $queryFlags ); |
| 390 |
|
| 391 |
return $this->userGroupCache[$userKey][self::CACHE_FORMER]; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public function getUserAutopromoteGroups( UserIdentity $user ): array { |
| 403 |
$user->assertWiki( $this->wikiId ); |
| 404 |
$promote = []; |
| 405 |
|
| 406 |
$userObj = User::newFromIdentity( $user ); |
| 407 |
if ( $userObj->isTemp() ) { |
| 408 |
return []; |
| 409 |
} |
| 410 |
foreach ( $this->options->get( MainConfigNames::Autopromote ) as $group => $cond ) { |
| 411 |
if ( $this->recCheckCondition( $cond, $userObj ) ) { |
| 412 |
$promote[] = $group; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
$this->hookRunner->onGetAutoPromoteGroups( $userObj, $promote ); |
| 417 |
return $promote; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function getUserAutopromoteOnceGroups( |
| 433 |
UserIdentity $user, |
| 434 |
string $event |
| 435 |
): array { |
| 436 |
$user->assertWiki( $this->wikiId ); |
| 437 |
$autopromoteOnce = $this->options->get( MainConfigNames::AutopromoteOnce ); |
| 438 |
$promote = []; |
| 439 |
|
| 440 |
if ( isset( $autopromoteOnce[$event] ) && count( $autopromoteOnce[$event] ) ) { |
| 441 |
|
| 442 |
$userObj = User::newFromIdentity( $user ); |
| 443 |
if ( $userObj->isTemp() ) { |
| 444 |
return []; |
| 445 |
} |
| 446 |
$currentGroups = $this->getUserGroups( $user ); |
| 447 |
$formerGroups = $this->getUserFormerGroups( $user ); |
| 448 |
foreach ( $autopromoteOnce[$event] as $group => $cond ) { |
| 449 |
|
| 450 |
if ( in_array( $group, $currentGroups ) ) { |
| 451 |
continue; |
| 452 |
} |
| 453 |
|
| 454 |
if ( in_array( $group, $formerGroups ) ) { |
| 455 |
continue; |
| 456 |
} |
| 457 |
|
| 458 |
if ( $this->recCheckCondition( $cond, $userObj ) ) { |
| 459 |
$promote[] = $group; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
return $promote; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
public function getUserPrivilegedGroups( |
| 484 |
UserIdentity $user, |
| 485 |
int $queryFlags = IDBAccessObject::READ_NORMAL, |
| 486 |
bool $recache = false |
| 487 |
): array { |
| 488 |
$userKey = $this->getCacheKey( $user ); |
| 489 |
|
| 490 |
if ( !$recache && |
| 491 |
$this->canUseCachedValues( $user, self::CACHE_PRIVILEGED, $queryFlags ) && |
| 492 |
isset( $this->userGroupCache[$userKey][self::CACHE_PRIVILEGED] ) |
| 493 |
) { |
| 494 |
return $this->userGroupCache[$userKey][self::CACHE_PRIVILEGED]; |
| 495 |
} |
| 496 |
|
| 497 |
if ( !$user->isRegistered() ) { |
| 498 |
return []; |
| 499 |
} |
| 500 |
|
| 501 |
$groups = array_intersect( |
| 502 |
$this->getUserEffectiveGroups( $user, $queryFlags, $recache ), |
| 503 |
$this->options->get( MainConfigNames::PrivilegedGroups ) |
| 504 |
); |
| 505 |
|
| 506 |
$this->hookRunner->onUserPrivilegedGroups( $user, $groups ); |
| 507 |
|
| 508 |
$this->setCache( |
| 509 |
$this->getCacheKey( $user ), |
| 510 |
self::CACHE_PRIVILEGED, |
| 511 |
array_values( array_unique( $groups ) ), |
| 512 |
$queryFlags |
| 513 |
); |
| 514 |
|
| 515 |
return $this->userGroupCache[$userKey][self::CACHE_PRIVILEGED]; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
private function recCheckCondition( $cond, User $user ): bool { |
| 539 |
if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], self::VALID_OPS ) ) { |
| 540 |
|
| 541 |
if ( $cond[0] == '&' ) { |
| 542 |
foreach ( array_slice( $cond, 1 ) as $subcond ) { |
| 543 |
if ( !$this->recCheckCondition( $subcond, $user ) ) { |
| 544 |
return false; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
return true; |
| 549 |
} elseif ( $cond[0] == '|' ) { |
| 550 |
foreach ( array_slice( $cond, 1 ) as $subcond ) { |
| 551 |
if ( $this->recCheckCondition( $subcond, $user ) ) { |
| 552 |
return true; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
return false; |
| 557 |
} elseif ( $cond[0] == '^' ) { |
| 558 |
if ( count( $cond ) > 3 ) { |
| 559 |
$this->logger->warning( |
| 560 |
'recCheckCondition() given XOR ("^") condition on three or more conditions.' . |
| 561 |
' Check your $wgAutopromote and $wgAutopromoteOnce settings.' |
| 562 |
); |
| 563 |
} |
| 564 |
return $this->recCheckCondition( $cond[1], $user ) |
| 565 |
xor $this->recCheckCondition( $cond[2], $user ); |
| 566 |
} elseif ( $cond[0] == '!' ) { |
| 567 |
foreach ( array_slice( $cond, 1 ) as $subcond ) { |
| 568 |
if ( $this->recCheckCondition( $subcond, $user ) ) { |
| 569 |
return false; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
return true; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
if ( !is_array( $cond ) ) { |
| 579 |
$cond = [ $cond ]; |
| 580 |
} |
| 581 |
|
| 582 |
return $this->checkCondition( $cond, $user ); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
private function checkCondition( array $cond, User $user ): bool { |
| 597 |
if ( count( $cond ) < 1 ) { |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
switch ( $cond[0] ) { |
| 602 |
case APCOND_EMAILCONFIRMED: |
| 603 |
if ( Sanitizer::validateEmail( $user->getEmail() ) ) { |
| 604 |
if ( $this->options->get( MainConfigNames::EmailAuthentication ) ) { |
| 605 |
return (bool)$user->getEmailAuthenticationTimestamp(); |
| 606 |
} else { |
| 607 |
return true; |
| 608 |
} |
| 609 |
} |
| 610 |
return false; |
| 611 |
case APCOND_EDITCOUNT: |
| 612 |
$reqEditCount = $cond[1] ?? $this->options->get( MainConfigNames::AutoConfirmCount ); |
| 613 |
|
| 614 |
|
| 615 |
if ( $reqEditCount <= 0 ) { |
| 616 |
return true; |
| 617 |
} |
| 618 |
return (int)$this->userEditTracker->getUserEditCount( $user ) >= $reqEditCount; |
| 619 |
case APCOND_AGE: |
| 620 |
$reqAge = $cond[1] ?? $this->options->get( MainConfigNames::AutoConfirmAge ); |
| 621 |
$age = time() - (int)wfTimestampOrNull( TS_UNIX, $user->getRegistration() ); |
| 622 |
return $age >= $reqAge; |
| 623 |
case APCOND_AGE_FROM_EDIT: |
| 624 |
$age = time() - (int)wfTimestampOrNull( |
| 625 |
TS_UNIX, $this->userEditTracker->getFirstEditTimestamp( $user ) ); |
| 626 |
return $age >= $cond[1]; |
| 627 |
case APCOND_INGROUPS: |
| 628 |
$groups = array_slice( $cond, 1 ); |
| 629 |
return count( array_intersect( $groups, $this->getUserGroups( $user ) ) ) == count( $groups ); |
| 630 |
case APCOND_ISIP: |
| 631 |
return $cond[1] == $user->getRequest()->getIP(); |
| 632 |
case APCOND_IPINRANGE: |
| 633 |
return IPUtils::isInRange( $user->getRequest()->getIP(), $cond[1] ); |
| 634 |
case APCOND_BLOCKED: |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
$userKey = $this->getCacheKey( $user ); |
| 639 |
if ( $this->recursionMap[$userKey] ?? false ) { |
| 640 |
throw new LogicException( |
| 641 |
"Unexpected recursion! APCOND_BLOCKED is being checked during" . |
| 642 |
" an existing APCOND_BLOCKED check for \"{$user->getName()}\"!" |
| 643 |
); |
| 644 |
} |
| 645 |
$this->recursionMap[$userKey] = true; |
| 646 |
|
| 647 |
|
| 648 |
$block = $user->getBlock( IDBAccessObject::READ_LATEST, true ); |
| 649 |
$this->recursionMap[$userKey] = false; |
| 650 |
|
| 651 |
return $block && $block->isSitewide(); |
| 652 |
case APCOND_ISBOT: |
| 653 |
return in_array( 'bot', $this->groupPermissionsLookup |
| 654 |
->getGroupPermissions( $this->getUserGroups( $user ) ) ); |
| 655 |
default: |
| 656 |
$result = null; |
| 657 |
$this->hookRunner->onAutopromoteCondition( $cond[0], |
| 658 |
|
| 659 |
array_slice( $cond, 1 ), $user, $result ); |
| 660 |
if ( $result === null ) { |
| 661 |
throw new InvalidArgumentException( |
| 662 |
"Unrecognized condition {$cond[0]} for autopromotion!" |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
return (bool)$result; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
public function addUserToAutopromoteOnceGroups( |
| 686 |
UserIdentity $user, |
| 687 |
string $event |
| 688 |
): array { |
| 689 |
$user->assertWiki( $this->wikiId ); |
| 690 |
Assert::precondition( |
| 691 |
!$this->wikiId || WikiMap::isCurrentWikiDbDomain( $this->wikiId ), |
| 692 |
__METHOD__ . " is not supported for foreign wikis: {$this->wikiId} used" |
| 693 |
); |
| 694 |
|
| 695 |
if ( |
| 696 |
$this->readOnlyMode->isReadOnly( $this->wikiId ) || |
| 697 |
!$user->isRegistered() || |
| 698 |
$this->tempUserConfig->isTempName( $user->getName() ) |
| 699 |
) { |
| 700 |
return []; |
| 701 |
} |
| 702 |
|
| 703 |
$toPromote = $this->getUserAutopromoteOnceGroups( $user, $event ); |
| 704 |
if ( $toPromote === [] ) { |
| 705 |
return []; |
| 706 |
} |
| 707 |
|
| 708 |
$userObj = User::newFromIdentity( $user ); |
| 709 |
if ( !$userObj->checkAndSetTouched() ) { |
| 710 |
return []; |
| 711 |
} |
| 712 |
|
| 713 |
$oldGroups = $this->getUserGroups( $user ); |
| 714 |
$oldUGMs = $this->getUserGroupMemberships( $user ); |
| 715 |
$this->addUserToMultipleGroups( $user, $toPromote ); |
| 716 |
$newGroups = array_merge( $oldGroups, $toPromote ); |
| 717 |
$newUGMs = $this->getUserGroupMemberships( $user ); |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
$this->hookRunner->onUserGroupsChanged( |
| 722 |
$userObj, |
| 723 |
$toPromote, [], |
| 724 |
false, |
| 725 |
false, |
| 726 |
$oldUGMs, |
| 727 |
$newUGMs |
| 728 |
); |
| 729 |
|
| 730 |
$logEntry = new ManualLogEntry( 'rights', 'autopromote' ); |
| 731 |
$logEntry->setPerformer( $user ); |
| 732 |
$logEntry->setTarget( $userObj->getUserPage() ); |
| 733 |
$logEntry->setParameters( [ |
| 734 |
'4::oldgroups' => $oldGroups, |
| 735 |
'5::newgroups' => $newGroups, |
| 736 |
] ); |
| 737 |
$logid = $logEntry->insert(); |
| 738 |
if ( $this->options->get( MainConfigNames::AutopromoteOnceLogInRC ) ) { |
| 739 |
$logEntry->publish( $logid ); |
| 740 |
} |
| 741 |
|
| 742 |
return $toPromote; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
public function getUserGroups( |
| 754 |
UserIdentity $user, |
| 755 |
int $queryFlags = IDBAccessObject::READ_NORMAL |
| 756 |
): array { |
| 757 |
return array_keys( $this->getUserGroupMemberships( $user, $queryFlags ) ); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public function getUserGroupMemberships( |
| 769 |
UserIdentity $user, |
| 770 |
int $queryFlags = IDBAccessObject::READ_NORMAL |
| 771 |
): array { |
| 772 |
$user->assertWiki( $this->wikiId ); |
| 773 |
$userKey = $this->getCacheKey( $user ); |
| 774 |
|
| 775 |
if ( $this->canUseCachedValues( $user, self::CACHE_MEMBERSHIP, $queryFlags ) && |
| 776 |
isset( $this->userGroupCache[$userKey][self::CACHE_MEMBERSHIP] ) |
| 777 |
) { |
| 778 |
|
| 779 |
return $this->userGroupCache[$userKey][self::CACHE_MEMBERSHIP]; |
| 780 |
} |
| 781 |
|
| 782 |
if ( !$user->isRegistered() ) { |
| 783 |
|
| 784 |
return []; |
| 785 |
} |
| 786 |
|
| 787 |
$queryBuilder = $this->newQueryBuilder( $this->getDBConnectionRefForQueryFlags( $queryFlags ) ); |
| 788 |
$res = $queryBuilder |
| 789 |
->where( [ 'ug_user' => $user->getId( $this->wikiId ) ] ) |
| 790 |
->caller( __METHOD__ ) |
| 791 |
->fetchResultSet(); |
| 792 |
|
| 793 |
$ugms = []; |
| 794 |
foreach ( $res as $row ) { |
| 795 |
$ugm = $this->newGroupMembershipFromRow( $row ); |
| 796 |
if ( !$ugm->isExpired() ) { |
| 797 |
$ugms[$ugm->getGroup()] = $ugm; |
| 798 |
} |
| 799 |
} |
| 800 |
ksort( $ugms ); |
| 801 |
|
| 802 |
$this->setCache( $userKey, self::CACHE_MEMBERSHIP, $ugms, $queryFlags ); |
| 803 |
|
| 804 |
return $ugms; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
public function addUserToGroup( |
| 823 |
UserIdentity $user, |
| 824 |
string $group, |
| 825 |
?string $expiry = null, |
| 826 |
bool $allowUpdate = false |
| 827 |
): bool { |
| 828 |
$user->assertWiki( $this->wikiId ); |
| 829 |
if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) { |
| 830 |
return false; |
| 831 |
} |
| 832 |
|
| 833 |
$isTemp = $this->tempUserConfig->isTempName( $user->getName() ); |
| 834 |
if ( !$user->isRegistered() ) { |
| 835 |
throw new InvalidArgumentException( |
| 836 |
'UserGroupManager::addUserToGroup() needs a positive user ID. ' . |
| 837 |
'Perhaps addUserToGroup() was called before the user was added to the database.' |
| 838 |
); |
| 839 |
} |
| 840 |
if ( $isTemp ) { |
| 841 |
throw new InvalidArgumentException( |
| 842 |
'UserGroupManager::addUserToGroup() cannot be called on a temporary user.' |
| 843 |
); |
| 844 |
} |
| 845 |
|
| 846 |
if ( $expiry ) { |
| 847 |
$expiry = wfTimestamp( TS_MW, $expiry ); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
if ( $this->hookContainer->isRegistered( 'UserAddGroup' ) ) { |
| 853 |
$userObj = User::newFromIdentity( $user ); |
| 854 |
$userObj->load(); |
| 855 |
if ( !$this->hookRunner->onUserAddGroup( $userObj, $group, $expiry ) ) { |
| 856 |
return false; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
$oldUgms = $this->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST ); |
| 861 |
$dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId ); |
| 862 |
|
| 863 |
$dbw->startAtomic( __METHOD__ ); |
| 864 |
$dbw->newInsertQueryBuilder() |
| 865 |
->insertInto( 'user_groups' ) |
| 866 |
->ignore() |
| 867 |
->row( [ |
| 868 |
'ug_user' => $user->getId( $this->wikiId ), |
| 869 |
'ug_group' => $group, |
| 870 |
'ug_expiry' => $expiry ? $dbw->timestamp( $expiry ) : null, |
| 871 |
] ) |
| 872 |
->caller( __METHOD__ )->execute(); |
| 873 |
|
| 874 |
$affected = $dbw->affectedRows(); |
| 875 |
if ( !$affected ) { |
| 876 |
|
| 877 |
|
| 878 |
$conds = [ |
| 879 |
'ug_user' => $user->getId( $this->wikiId ), |
| 880 |
'ug_group' => $group |
| 881 |
]; |
| 882 |
if ( $allowUpdate ) { |
| 883 |
|
| 884 |
$conds[] = $expiry |
| 885 |
? $dbw->expr( 'ug_expiry', '=', null ) |
| 886 |
->or( 'ug_expiry', '!=', $dbw->timestamp( $expiry ) ) |
| 887 |
: $dbw->expr( 'ug_expiry', '!=', null ); |
| 888 |
} else { |
| 889 |
|
| 890 |
$conds[] = $dbw->expr( 'ug_expiry', '<', $dbw->timestamp() ); |
| 891 |
} |
| 892 |
$dbw->newUpdateQueryBuilder() |
| 893 |
->update( 'user_groups' ) |
| 894 |
->set( [ 'ug_expiry' => $expiry ? $dbw->timestamp( $expiry ) : null ] ) |
| 895 |
->where( $conds ) |
| 896 |
->caller( __METHOD__ )->execute(); |
| 897 |
$affected = $dbw->affectedRows(); |
| 898 |
} |
| 899 |
$dbw->endAtomic( __METHOD__ ); |
| 900 |
|
| 901 |
|
| 902 |
DeferredUpdates::addCallableUpdate( function ( $fname ) { |
| 903 |
$dbr = $this->dbProvider->getReplicaDatabase( $this->wikiId ); |
| 904 |
$hasExpiredRow = (bool)$dbr->newSelectQueryBuilder() |
| 905 |
->select( '1' ) |
| 906 |
->from( 'user_groups' ) |
| 907 |
->where( [ $dbr->expr( 'ug_expiry', '<', $dbr->timestamp() ) ] ) |
| 908 |
->caller( $fname ) |
| 909 |
->fetchField(); |
| 910 |
if ( $hasExpiredRow ) { |
| 911 |
$this->jobQueueGroup->push( new UserGroupExpiryJob( [] ) ); |
| 912 |
} |
| 913 |
} ); |
| 914 |
|
| 915 |
if ( $affected > 0 ) { |
| 916 |
$oldUgms[$group] = new UserGroupMembership( $user->getId( $this->wikiId ), $group, $expiry ); |
| 917 |
if ( !$oldUgms[$group]->isExpired() ) { |
| 918 |
$this->setCache( |
| 919 |
$this->getCacheKey( $user ), |
| 920 |
self::CACHE_MEMBERSHIP, |
| 921 |
$oldUgms, |
| 922 |
IDBAccessObject::READ_LATEST |
| 923 |
); |
| 924 |
$this->clearUserCacheForKind( $user, self::CACHE_EFFECTIVE ); |
| 925 |
} |
| 926 |
foreach ( $this->clearCacheCallbacks as $callback ) { |
| 927 |
$callback( $user ); |
| 928 |
} |
| 929 |
return true; |
| 930 |
} |
| 931 |
return false; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
public function addUserToMultipleGroups( |
| 948 |
UserIdentity $user, |
| 949 |
array $groups, |
| 950 |
?string $expiry = null, |
| 951 |
bool $allowUpdate = false |
| 952 |
) { |
| 953 |
foreach ( $groups as $group ) { |
| 954 |
$this->addUserToGroup( $user, $group, $expiry, $allowUpdate ); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
public function removeUserFromGroup( UserIdentity $user, string $group ): bool { |
| 967 |
$user->assertWiki( $this->wikiId ); |
| 968 |
|
| 969 |
|
| 970 |
if ( $this->hookContainer->isRegistered( 'UserRemoveGroup' ) ) { |
| 971 |
$userObj = User::newFromIdentity( $user ); |
| 972 |
$userObj->load(); |
| 973 |
if ( !$this->hookRunner->onUserRemoveGroup( $userObj, $group ) ) { |
| 974 |
return false; |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) { |
| 979 |
return false; |
| 980 |
} |
| 981 |
|
| 982 |
if ( !$user->isRegistered() ) { |
| 983 |
throw new InvalidArgumentException( |
| 984 |
'UserGroupManager::removeUserFromGroup() needs a positive user ID. ' . |
| 985 |
'Perhaps removeUserFromGroup() was called before the user was added to the database.' |
| 986 |
); |
| 987 |
} |
| 988 |
|
| 989 |
$oldUgms = $this->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST ); |
| 990 |
$oldFormerGroups = $this->getUserFormerGroups( $user, IDBAccessObject::READ_LATEST ); |
| 991 |
$dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId ); |
| 992 |
$dbw->newDeleteQueryBuilder() |
| 993 |
->deleteFrom( 'user_groups' ) |
| 994 |
->where( [ 'ug_user' => $user->getId( $this->wikiId ), 'ug_group' => $group ] ) |
| 995 |
->caller( __METHOD__ )->execute(); |
| 996 |
|
| 997 |
if ( !$dbw->affectedRows() ) { |
| 998 |
return false; |
| 999 |
} |
| 1000 |
|
| 1001 |
$dbw->newInsertQueryBuilder() |
| 1002 |
->insertInto( 'user_former_groups' ) |
| 1003 |
->ignore() |
| 1004 |
->row( [ 'ufg_user' => $user->getId( $this->wikiId ), 'ufg_group' => $group ] ) |
| 1005 |
->caller( __METHOD__ )->execute(); |
| 1006 |
|
| 1007 |
unset( $oldUgms[$group] ); |
| 1008 |
$userKey = $this->getCacheKey( $user ); |
| 1009 |
$this->setCache( $userKey, self::CACHE_MEMBERSHIP, $oldUgms, IDBAccessObject::READ_LATEST ); |
| 1010 |
$oldFormerGroups[] = $group; |
| 1011 |
$this->setCache( $userKey, self::CACHE_FORMER, $oldFormerGroups, IDBAccessObject::READ_LATEST ); |
| 1012 |
$this->clearUserCacheForKind( $user, self::CACHE_EFFECTIVE ); |
| 1013 |
foreach ( $this->clearCacheCallbacks as $callback ) { |
| 1014 |
$callback( $user ); |
| 1015 |
} |
| 1016 |
return true; |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
public function newQueryBuilder( IReadableDatabase $db ): SelectQueryBuilder { |
| 1027 |
return $db->newSelectQueryBuilder() |
| 1028 |
->select( [ |
| 1029 |
'ug_user', |
| 1030 |
'ug_group', |
| 1031 |
'ug_expiry', |
| 1032 |
] ) |
| 1033 |
->from( 'user_groups' ); |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
public function purgeExpired() { |
| 1044 |
if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) { |
| 1045 |
return false; |
| 1046 |
} |
| 1047 |
|
| 1048 |
$ticket = $this->dbProvider->getEmptyTransactionTicket( __METHOD__ ); |
| 1049 |
$dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId ); |
| 1050 |
|
| 1051 |
$lockKey = "{$dbw->getDomainID()}:UserGroupManager:purge"; |
| 1052 |
$scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 0 ); |
| 1053 |
if ( !$scopedLock ) { |
| 1054 |
return false; |
| 1055 |
} |
| 1056 |
|
| 1057 |
$now = time(); |
| 1058 |
$purgedRows = 0; |
| 1059 |
do { |
| 1060 |
$dbw->startAtomic( __METHOD__ ); |
| 1061 |
$res = $this->newQueryBuilder( $dbw ) |
| 1062 |
->where( [ $dbw->expr( 'ug_expiry', '<', $dbw->timestamp( $now ) ) ] ) |
| 1063 |
->forUpdate() |
| 1064 |
->limit( 100 ) |
| 1065 |
->caller( __METHOD__ ) |
| 1066 |
->fetchResultSet(); |
| 1067 |
|
| 1068 |
if ( $res->numRows() > 0 ) { |
| 1069 |
$insertData = []; |
| 1070 |
$deleteCond = []; |
| 1071 |
foreach ( $res as $row ) { |
| 1072 |
$insertData[] = [ 'ufg_user' => $row->ug_user, 'ufg_group' => $row->ug_group ]; |
| 1073 |
$deleteCond[] = $dbw |
| 1074 |
->expr( 'ug_user', '=', $row->ug_user ) |
| 1075 |
->and( 'ug_group', '=', $row->ug_group ); |
| 1076 |
} |
| 1077 |
|
| 1078 |
$dbw->newDeleteQueryBuilder() |
| 1079 |
->deleteFrom( 'user_groups' ) |
| 1080 |
->where( $dbw->orExpr( $deleteCond ) ) |
| 1081 |
->caller( __METHOD__ )->execute(); |
| 1082 |
|
| 1083 |
$dbw->newInsertQueryBuilder() |
| 1084 |
->insertInto( 'user_former_groups' ) |
| 1085 |
->ignore() |
| 1086 |
->rows( $insertData ) |
| 1087 |
->caller( __METHOD__ )->execute(); |
| 1088 |
|
| 1089 |
$purgedRows += $res->numRows(); |
| 1090 |
} |
| 1091 |
|
| 1092 |
$dbw->endAtomic( __METHOD__ ); |
| 1093 |
|
| 1094 |
$this->dbProvider->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 1095 |
} while ( $res->numRows() > 0 ); |
| 1096 |
return $purgedRows; |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
private function expandChangeableGroupConfig( array $config, string $group ): array { |
| 1105 |
if ( empty( $config[$group] ) ) { |
| 1106 |
return []; |
| 1107 |
} elseif ( $config[$group] === true ) { |
| 1108 |
|
| 1109 |
return $this->listAllGroups(); |
| 1110 |
} elseif ( is_array( $config[$group] ) ) { |
| 1111 |
return $config[$group]; |
| 1112 |
} |
| 1113 |
return []; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
public function getGroupsChangeableByGroup( string $group ): array { |
| 1128 |
return [ |
| 1129 |
'add' => $this->expandChangeableGroupConfig( |
| 1130 |
$this->options->get( MainConfigNames::AddGroups ), $group |
| 1131 |
), |
| 1132 |
'remove' => $this->expandChangeableGroupConfig( |
| 1133 |
$this->options->get( MainConfigNames::RemoveGroups ), $group |
| 1134 |
), |
| 1135 |
'add-self' => $this->expandChangeableGroupConfig( |
| 1136 |
$this->options->get( MainConfigNames::GroupsAddToSelf ), $group |
| 1137 |
), |
| 1138 |
'remove-self' => $this->expandChangeableGroupConfig( |
| 1139 |
$this->options->get( MainConfigNames::GroupsRemoveFromSelf ), $group |
| 1140 |
), |
| 1141 |
]; |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
public function getGroupsChangeableBy( Authority $authority ): array { |
| 1158 |
if ( $authority->isAllowed( 'userrights' ) ) { |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
$all = array_merge( $this->listAllGroups() ); |
| 1164 |
return [ |
| 1165 |
'add' => $all, |
| 1166 |
'remove' => $all, |
| 1167 |
'add-self' => [], |
| 1168 |
'remove-self' => [] |
| 1169 |
]; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
$groups = [ |
| 1174 |
'add' => [], |
| 1175 |
'remove' => [], |
| 1176 |
'add-self' => [], |
| 1177 |
'remove-self' => [] |
| 1178 |
]; |
| 1179 |
$actorGroups = $this->getUserEffectiveGroups( $authority->getUser() ); |
| 1180 |
|
| 1181 |
foreach ( $actorGroups as $actorGroup ) { |
| 1182 |
$groups = array_merge_recursive( |
| 1183 |
$groups, $this->getGroupsChangeableByGroup( $actorGroup ) |
| 1184 |
); |
| 1185 |
$groups['add'] = array_unique( $groups['add'] ); |
| 1186 |
$groups['remove'] = array_unique( $groups['remove'] ); |
| 1187 |
$groups['add-self'] = array_unique( $groups['add-self'] ); |
| 1188 |
$groups['remove-self'] = array_unique( $groups['remove-self'] ); |
| 1189 |
} |
| 1190 |
return $groups; |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
public function clearCache( UserIdentity $user ) { |
| 1199 |
$user->assertWiki( $this->wikiId ); |
| 1200 |
$userKey = $this->getCacheKey( $user ); |
| 1201 |
unset( $this->userGroupCache[$userKey] ); |
| 1202 |
unset( $this->queryFlagsUsedForCaching[$userKey] ); |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
private function setCache( |
| 1214 |
string $userKey, |
| 1215 |
string $cacheKind, |
| 1216 |
array $groupValue, |
| 1217 |
int $queryFlags |
| 1218 |
) { |
| 1219 |
$this->userGroupCache[$userKey][$cacheKind] = $groupValue; |
| 1220 |
$this->queryFlagsUsedForCaching[$userKey][$cacheKind] = $queryFlags; |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
private function clearUserCacheForKind( UserIdentity $user, string $cacheKind ) { |
| 1230 |
$userKey = $this->getCacheKey( $user ); |
| 1231 |
unset( $this->userGroupCache[$userKey][$cacheKind] ); |
| 1232 |
unset( $this->queryFlagsUsedForCaching[$userKey][$cacheKind] ); |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
private function getDBConnectionRefForQueryFlags( int $recency ): IReadableDatabase { |
| 1240 |
if ( ( IDBAccessObject::READ_LATEST & $recency ) == IDBAccessObject::READ_LATEST ) { |
| 1241 |
return $this->dbProvider->getPrimaryDatabase( $this->wikiId ); |
| 1242 |
} |
| 1243 |
return $this->dbProvider->getReplicaDatabase( $this->wikiId ); |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
private function getCacheKey( UserIdentity $user ): string { |
| 1252 |
return $user->isRegistered() ? "u:{$user->getId( $this->wikiId )}" : "anon:{$user->getName()}"; |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
private function canUseCachedValues( |
| 1263 |
UserIdentity $user, |
| 1264 |
string $cacheKind, |
| 1265 |
int $queryFlags |
| 1266 |
): bool { |
| 1267 |
if ( !$user->isRegistered() ) { |
| 1268 |
|
| 1269 |
|
| 1270 |
return true; |
| 1271 |
} |
| 1272 |
if ( $queryFlags >= IDBAccessObject::READ_LOCKING ) { |
| 1273 |
return false; |
| 1274 |
} |
| 1275 |
$userKey = $this->getCacheKey( $user ); |
| 1276 |
$queryFlagsUsed = $this->queryFlagsUsedForCaching[$userKey][$cacheKind] ?? IDBAccessObject::READ_NONE; |
| 1277 |
return $queryFlagsUsed >= $queryFlags; |
| 1278 |
} |
| 1279 |
} |