| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Block; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\CommentStore\CommentStore; |
| 27 |
use MediaWiki\Config\ServiceOptions; |
| 28 |
use MediaWiki\Deferred\AutoCommitUpdate; |
| 29 |
use MediaWiki\Deferred\DeferredUpdates; |
| 30 |
use MediaWiki\HookContainer\HookContainer; |
| 31 |
use MediaWiki\HookContainer\HookRunner; |
| 32 |
use MediaWiki\MainConfigNames; |
| 33 |
use MediaWiki\Session\SessionManager; |
| 34 |
use MediaWiki\User\ActorStoreFactory; |
| 35 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 36 |
use MediaWiki\User\UserFactory; |
| 37 |
use MediaWiki\User\UserIdentity; |
| 38 |
use MediaWiki\User\UserIdentityValue; |
| 39 |
use Psr\Log\LoggerInterface; |
| 40 |
use RuntimeException; |
| 41 |
use stdClass; |
| 42 |
use Wikimedia\IPUtils; |
| 43 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 44 |
use Wikimedia\Rdbms\IDatabase; |
| 45 |
use Wikimedia\Rdbms\IExpression; |
| 46 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 47 |
use Wikimedia\Rdbms\IResultWrapper; |
| 48 |
use Wikimedia\Rdbms\LikeValue; |
| 49 |
use Wikimedia\Rdbms\RawSQLExpression; |
| 50 |
use Wikimedia\Rdbms\RawSQLValue; |
| 51 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 52 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 53 |
use function array_key_exists; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class DatabaseBlockStore { |
| 61 |
|
| 62 |
public const SCHEMA_IPBLOCKS = 'ipblocks'; |
| 63 |
|
| 64 |
public const SCHEMA_BLOCK = 'block'; |
| 65 |
|
| 66 |
public const SCHEMA_CURRENT = 'current'; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public const CONSTRUCTOR_OPTIONS = [ |
| 72 |
MainConfigNames::AutoblockExpiry, |
| 73 |
MainConfigNames::BlockCIDRLimit, |
| 74 |
MainConfigNames::BlockDisablesLogin, |
| 75 |
MainConfigNames::PutIPinRC, |
| 76 |
MainConfigNames::UpdateRowsPerQuery, |
| 77 |
]; |
| 78 |
|
| 79 |
|
| 80 |
private $wikiId; |
| 81 |
|
| 82 |
private ServiceOptions $options; |
| 83 |
private LoggerInterface $logger; |
| 84 |
private ActorStoreFactory $actorStoreFactory; |
| 85 |
private BlockRestrictionStore $blockRestrictionStore; |
| 86 |
private CommentStore $commentStore; |
| 87 |
private HookRunner $hookRunner; |
| 88 |
private IConnectionProvider $dbProvider; |
| 89 |
private ReadOnlyMode $readOnlyMode; |
| 90 |
private UserFactory $userFactory; |
| 91 |
private TempUserConfig $tempUserConfig; |
| 92 |
private BlockUtils $blockUtils; |
| 93 |
private AutoblockExemptionList $autoblockExemptionList; |
| 94 |
|
| 95 |
public function __construct( |
| 96 |
ServiceOptions $options, |
| 97 |
LoggerInterface $logger, |
| 98 |
ActorStoreFactory $actorStoreFactory, |
| 99 |
BlockRestrictionStore $blockRestrictionStore, |
| 100 |
CommentStore $commentStore, |
| 101 |
HookContainer $hookContainer, |
| 102 |
IConnectionProvider $dbProvider, |
| 103 |
ReadOnlyMode $readOnlyMode, |
| 104 |
UserFactory $userFactory, |
| 105 |
TempUserConfig $tempUserConfig, |
| 106 |
BlockUtils $blockUtils, |
| 107 |
AutoblockExemptionList $autoblockExemptionList, |
| 108 |
$wikiId = DatabaseBlock::LOCAL |
| 109 |
) { |
| 110 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 111 |
|
| 112 |
$this->wikiId = $wikiId; |
| 113 |
|
| 114 |
$this->options = $options; |
| 115 |
$this->logger = $logger; |
| 116 |
$this->actorStoreFactory = $actorStoreFactory; |
| 117 |
$this->blockRestrictionStore = $blockRestrictionStore; |
| 118 |
$this->commentStore = $commentStore; |
| 119 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 120 |
$this->dbProvider = $dbProvider; |
| 121 |
$this->readOnlyMode = $readOnlyMode; |
| 122 |
$this->userFactory = $userFactory; |
| 123 |
$this->tempUserConfig = $tempUserConfig; |
| 124 |
$this->blockUtils = $blockUtils; |
| 125 |
$this->autoblockExemptionList = $autoblockExemptionList; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function getReadStage() { |
| 136 |
wfDeprecated( __METHOD__, '1.43' ); |
| 137 |
return SCHEMA_COMPAT_NEW; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getWriteStage() { |
| 148 |
wfDeprecated( __METHOD__, '1.43' ); |
| 149 |
return SCHEMA_COMPAT_NEW; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function newFromID( $id ) { |
| 164 |
$dbr = $this->getReplicaDB(); |
| 165 |
$blockQuery = $this->getQueryInfo(); |
| 166 |
$res = $dbr->newSelectQueryBuilder() |
| 167 |
->queryInfo( $blockQuery ) |
| 168 |
->where( [ 'bl_id' => $id ] ) |
| 169 |
->caller( __METHOD__ ) |
| 170 |
->fetchRow(); |
| 171 |
if ( $res ) { |
| 172 |
return $this->newFromRow( $dbr, $res ); |
| 173 |
} else { |
| 174 |
return null; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function getQueryInfo( $schema = self::SCHEMA_BLOCK ) { |
| 204 |
$commentQuery = $this->commentStore->getJoin( 'bl_reason' ); |
| 205 |
if ( $schema === self::SCHEMA_IPBLOCKS ) { |
| 206 |
return [ |
| 207 |
'tables' => [ |
| 208 |
'block', |
| 209 |
'block_by_actor' => 'actor', |
| 210 |
] + $commentQuery['tables'], |
| 211 |
'fields' => [ |
| 212 |
'ipb_id' => 'bl_id', |
| 213 |
'ipb_address' => 'COALESCE(bt_address, bt_user_text)', |
| 214 |
'ipb_timestamp' => 'bl_timestamp', |
| 215 |
'ipb_auto' => 'bt_auto', |
| 216 |
'ipb_anon_only' => 'bl_anon_only', |
| 217 |
'ipb_create_account' => 'bl_create_account', |
| 218 |
'ipb_enable_autoblock' => 'bl_enable_autoblock', |
| 219 |
'ipb_expiry' => 'bl_expiry', |
| 220 |
'ipb_deleted' => 'bl_deleted', |
| 221 |
'ipb_block_email' => 'bl_block_email', |
| 222 |
'ipb_allow_usertalk' => 'bl_allow_usertalk', |
| 223 |
'ipb_parent_block_id' => 'bl_parent_block_id', |
| 224 |
'ipb_sitewide' => 'bl_sitewide', |
| 225 |
'ipb_by_actor' => 'bl_by_actor', |
| 226 |
'ipb_by' => 'block_by_actor.actor_user', |
| 227 |
'ipb_by_text' => 'block_by_actor.actor_name', |
| 228 |
'ipb_reason_text' => $commentQuery['fields']['bl_reason_text'], |
| 229 |
'ipb_reason_data' => $commentQuery['fields']['bl_reason_data'], |
| 230 |
'ipb_reason_cid' => $commentQuery['fields']['bl_reason_cid'], |
| 231 |
], |
| 232 |
'joins' => [ |
| 233 |
'block_by_actor' => [ 'JOIN', 'actor_id=bl_by_actor' ], |
| 234 |
] + $commentQuery['joins'], |
| 235 |
]; |
| 236 |
} elseif ( $schema === self::SCHEMA_BLOCK ) { |
| 237 |
return [ |
| 238 |
'tables' => [ |
| 239 |
'block', |
| 240 |
'block_target', |
| 241 |
'block_by_actor' => 'actor', |
| 242 |
] + $commentQuery['tables'], |
| 243 |
'fields' => [ |
| 244 |
'bl_id', |
| 245 |
'bt_address', |
| 246 |
'bt_user', |
| 247 |
'bt_user_text', |
| 248 |
'bl_timestamp', |
| 249 |
'bt_auto', |
| 250 |
'bl_anon_only', |
| 251 |
'bl_create_account', |
| 252 |
'bl_enable_autoblock', |
| 253 |
'bl_expiry', |
| 254 |
'bl_deleted', |
| 255 |
'bl_block_email', |
| 256 |
'bl_allow_usertalk', |
| 257 |
'bl_parent_block_id', |
| 258 |
'bl_sitewide', |
| 259 |
'bl_by_actor', |
| 260 |
'bl_by' => 'block_by_actor.actor_user', |
| 261 |
'bl_by_text' => 'block_by_actor.actor_name', |
| 262 |
] + $commentQuery['fields'], |
| 263 |
'joins' => [ |
| 264 |
'block_target' => [ 'JOIN', 'bt_id=bl_target' ], |
| 265 |
'block_by_actor' => [ 'JOIN', 'actor_id=bl_by_actor' ], |
| 266 |
] + $commentQuery['joins'], |
| 267 |
]; |
| 268 |
} |
| 269 |
throw new InvalidArgumentException( |
| 270 |
'$schema must be SCHEMA_IPBLOCKS or SCHEMA_BLOCK' ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
private function newLoad( |
| 285 |
$specificTarget, |
| 286 |
$specificType, |
| 287 |
$fromPrimary, |
| 288 |
$vagueTarget = null |
| 289 |
) { |
| 290 |
if ( $fromPrimary ) { |
| 291 |
$db = $this->getPrimaryDB(); |
| 292 |
} else { |
| 293 |
$db = $this->getReplicaDB(); |
| 294 |
} |
| 295 |
|
| 296 |
$userIds = []; |
| 297 |
$userNames = []; |
| 298 |
$addresses = []; |
| 299 |
$ranges = []; |
| 300 |
if ( $specificType === Block::TYPE_USER ) { |
| 301 |
if ( $specificTarget instanceof UserIdentity ) { |
| 302 |
$userId = $specificTarget->getId( $this->wikiId ); |
| 303 |
if ( $userId ) { |
| 304 |
$userIds[] = $specificTarget->getId( $this->wikiId ); |
| 305 |
} else { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
} |
| 310 |
} else { |
| 311 |
$userNames[] = (string)$specificTarget; |
| 312 |
} |
| 313 |
} elseif ( in_array( $specificType, [ Block::TYPE_IP, Block::TYPE_RANGE ], true ) ) { |
| 314 |
$addresses[] = (string)$specificTarget; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if ( $vagueTarget != '' ) { |
| 320 |
[ $target, $type ] = $this->blockUtils->parseBlockTarget( $vagueTarget ); |
| 321 |
switch ( $type ) { |
| 322 |
case Block::TYPE_USER: |
| 323 |
|
| 324 |
|
| 325 |
$vagueUser = $target; |
| 326 |
if ( $vagueUser->getId( $this->wikiId ) ) { |
| 327 |
$userIds[] = $vagueUser->getId( $this->wikiId ); |
| 328 |
} else { |
| 329 |
$userNames[] = $vagueUser->getName(); |
| 330 |
} |
| 331 |
break; |
| 332 |
|
| 333 |
case Block::TYPE_IP: |
| 334 |
$ranges[] = [ IPUtils::toHex( $target ), null ]; |
| 335 |
break; |
| 336 |
|
| 337 |
case Block::TYPE_RANGE: |
| 338 |
$ranges[] = IPUtils::parseRange( $target ); |
| 339 |
break; |
| 340 |
|
| 341 |
default: |
| 342 |
$this->logger->debug( "Ignoring invalid vague target" ); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
$orConds = []; |
| 347 |
if ( $userIds ) { |
| 348 |
|
| 349 |
$orConds[] = $db->expr( 'bt_user', '=', array_unique( $userIds ) ); |
| 350 |
} |
| 351 |
if ( $userNames ) { |
| 352 |
|
| 353 |
$orConds[] = $db->expr( 'bt_ip_hex', '=', null ) |
| 354 |
|
| 355 |
->and( 'bt_user_text', '=', array_unique( $userNames ) ); |
| 356 |
} |
| 357 |
if ( $addresses ) { |
| 358 |
|
| 359 |
$orConds[] = $db->expr( 'bt_address', '=', array_unique( $addresses ) ); |
| 360 |
} |
| 361 |
foreach ( $ranges as $range ) { |
| 362 |
$orConds[] = new RawSQLExpression( $this->getRangeCond( $range[0], $range[1] ) ); |
| 363 |
} |
| 364 |
if ( !$orConds ) { |
| 365 |
return []; |
| 366 |
} |
| 367 |
|
| 368 |
$blockQuery = $this->getQueryInfo(); |
| 369 |
$res = $db->newSelectQueryBuilder() |
| 370 |
->queryInfo( $blockQuery ) |
| 371 |
->where( $db->orExpr( $orConds ) ) |
| 372 |
->caller( __METHOD__ ) |
| 373 |
->fetchResultSet(); |
| 374 |
|
| 375 |
$blocks = []; |
| 376 |
$blockIds = []; |
| 377 |
$autoBlocks = []; |
| 378 |
foreach ( $res as $row ) { |
| 379 |
$block = $this->newFromRow( $db, $row ); |
| 380 |
|
| 381 |
|
| 382 |
if ( $block->isExpired() ) { |
| 383 |
continue; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
if ( |
| 388 |
$specificType == Block::TYPE_USER && $specificTarget && |
| 389 |
!$block->isHardblock() && |
| 390 |
!$this->tempUserConfig->isTempName( $specificTarget ) |
| 391 |
) { |
| 392 |
continue; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
if ( $block->getType() === Block::TYPE_AUTO ) { |
| 397 |
$autoBlocks[] = $block; |
| 398 |
} else { |
| 399 |
$blocks[] = $block; |
| 400 |
$blockIds[] = $block->getId( $this->wikiId ); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
foreach ( $autoBlocks as $block ) { |
| 406 |
if ( !in_array( $block->getParentBlockId(), $blockIds ) ) { |
| 407 |
$blocks[] = $block; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
return $blocks; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private function chooseMostSpecificBlock( array $blocks ) { |
| 423 |
if ( count( $blocks ) === 1 ) { |
| 424 |
return $blocks[0]; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
$bestBlock = null; |
| 430 |
|
| 431 |
|
| 432 |
$bestBlockScore = 100; |
| 433 |
foreach ( $blocks as $block ) { |
| 434 |
if ( $block->getType() == Block::TYPE_RANGE ) { |
| 435 |
|
| 436 |
|
| 437 |
$target = $block->getTargetName(); |
| 438 |
$max = IPUtils::isIPv6( $target ) ? 128 : 32; |
| 439 |
[ , $bits ] = IPUtils::parseCIDR( $target ); |
| 440 |
$size = $max - $bits; |
| 441 |
|
| 442 |
|
| 443 |
$score = Block::TYPE_RANGE - 1 + ( $size / $max ); |
| 444 |
|
| 445 |
} else { |
| 446 |
$score = $block->getType(); |
| 447 |
} |
| 448 |
|
| 449 |
if ( $score < $bestBlockScore ) { |
| 450 |
$bestBlockScore = $score; |
| 451 |
$bestBlock = $block; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
return $bestBlock; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
public function getRangeCond( $start, $end, $schema = self::SCHEMA_BLOCK ) { |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
$chunk = $this->getIpFragment( $start ); |
| 480 |
$dbr = $this->getReplicaDB(); |
| 481 |
$end ??= $start; |
| 482 |
|
| 483 |
if ( $schema === self::SCHEMA_CURRENT ) { |
| 484 |
$schema = self::SCHEMA_BLOCK; |
| 485 |
} |
| 486 |
|
| 487 |
if ( $schema === self::SCHEMA_IPBLOCKS ) { |
| 488 |
return $dbr->makeList( |
| 489 |
[ |
| 490 |
$dbr->expr( 'ipb_range_start', IExpression::LIKE, |
| 491 |
new LikeValue( $chunk, $dbr->anyString() ) ), |
| 492 |
$dbr->expr( 'ipb_range_start', '<=', $start ), |
| 493 |
$dbr->expr( 'ipb_range_end', '>=', $end ), |
| 494 |
], |
| 495 |
LIST_AND |
| 496 |
); |
| 497 |
} elseif ( $schema === self::SCHEMA_BLOCK ) { |
| 498 |
$expr = $dbr->expr( |
| 499 |
'bt_range_start', |
| 500 |
IExpression::LIKE, |
| 501 |
new LikeValue( $chunk, $dbr->anyString() ) |
| 502 |
) |
| 503 |
->and( 'bt_range_start', '<=', $start ) |
| 504 |
->and( 'bt_range_end', '>=', $end ); |
| 505 |
if ( $start === $end ) { |
| 506 |
|
| 507 |
$expr = $dbr->orExpr( [ |
| 508 |
$dbr->expr( 'bt_ip_hex', '=', $start ) |
| 509 |
->and( 'bt_range_start', '=', null ), |
| 510 |
$expr |
| 511 |
] ); |
| 512 |
} |
| 513 |
return $expr->toSql( $dbr ); |
| 514 |
} else { |
| 515 |
throw new InvalidArgumentException( |
| 516 |
'$schema must be SCHEMA_IPBLOCKS or SCHEMA_BLOCK' ); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
private function getIpFragment( $hex ) { |
| 528 |
$blockCIDRLimit = $this->options->get( MainConfigNames::BlockCIDRLimit ); |
| 529 |
if ( str_starts_with( $hex, 'v6-' ) ) { |
| 530 |
return 'v6-' . substr( substr( $hex, 3 ), 0, (int)floor( $blockCIDRLimit['IPv6'] / 4 ) ); |
| 531 |
} else { |
| 532 |
return substr( $hex, 0, (int)floor( $blockCIDRLimit['IPv4'] / 4 ) ); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public function newFromRow( IReadableDatabase $db, $row ) { |
| 545 |
if ( isset( $row->ipb_id ) ) { |
| 546 |
return new DatabaseBlock( [ |
| 547 |
'address' => $row->ipb_address, |
| 548 |
'wiki' => $this->wikiId, |
| 549 |
'timestamp' => $row->ipb_timestamp, |
| 550 |
'auto' => (bool)$row->ipb_auto, |
| 551 |
'hideName' => (bool)$row->ipb_deleted, |
| 552 |
'id' => (int)$row->ipb_id, |
| 553 |
|
| 554 |
|
| 555 |
'parentBlockId' => $row->ipb_parent_block_id |
| 556 |
? (int)$row->ipb_parent_block_id : null, |
| 557 |
'by' => $this->actorStoreFactory |
| 558 |
->getActorStore( $this->wikiId ) |
| 559 |
->newActorFromRowFields( $row->ipb_by, $row->ipb_by_text, $row->ipb_by_actor ), |
| 560 |
'decodedExpiry' => $db->decodeExpiry( $row->ipb_expiry ), |
| 561 |
'reason' => $this->commentStore |
| 562 |
|
| 563 |
->getCommentLegacy( $db, 'ipb_reason', $row ), |
| 564 |
'anonOnly' => $row->ipb_anon_only, |
| 565 |
'enableAutoblock' => (bool)$row->ipb_enable_autoblock, |
| 566 |
'sitewide' => (bool)$row->ipb_sitewide, |
| 567 |
'createAccount' => (bool)$row->ipb_create_account, |
| 568 |
'blockEmail' => (bool)$row->ipb_block_email, |
| 569 |
'allowUsertalk' => (bool)$row->ipb_allow_usertalk |
| 570 |
] ); |
| 571 |
} else { |
| 572 |
$address = $row->bt_address |
| 573 |
?? new UserIdentityValue( $row->bt_user, $row->bt_user_text, $this->wikiId ); |
| 574 |
return new DatabaseBlock( [ |
| 575 |
'address' => $address, |
| 576 |
'wiki' => $this->wikiId, |
| 577 |
'timestamp' => $row->bl_timestamp, |
| 578 |
'auto' => (bool)$row->bt_auto, |
| 579 |
'hideName' => (bool)$row->bl_deleted, |
| 580 |
'id' => (int)$row->bl_id, |
| 581 |
|
| 582 |
|
| 583 |
'parentBlockId' => $row->bl_parent_block_id |
| 584 |
? (int)$row->bl_parent_block_id : null, |
| 585 |
'by' => $this->actorStoreFactory |
| 586 |
->getActorStore( $this->wikiId ) |
| 587 |
->newActorFromRowFields( $row->bl_by, $row->bl_by_text, $row->bl_by_actor ), |
| 588 |
'decodedExpiry' => $db->decodeExpiry( $row->bl_expiry ), |
| 589 |
'reason' => $this->commentStore->getComment( 'bl_reason', $row ), |
| 590 |
'anonOnly' => $row->bl_anon_only, |
| 591 |
'enableAutoblock' => (bool)$row->bl_enable_autoblock, |
| 592 |
'sitewide' => (bool)$row->bl_sitewide, |
| 593 |
'createAccount' => (bool)$row->bl_create_account, |
| 594 |
'blockEmail' => (bool)$row->bl_block_email, |
| 595 |
'allowUsertalk' => (bool)$row->bl_allow_usertalk |
| 596 |
] ); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public function newFromTarget( |
| 624 |
$specificTarget, |
| 625 |
$vagueTarget = null, |
| 626 |
$fromPrimary = false |
| 627 |
) { |
| 628 |
$blocks = $this->newListFromTarget( $specificTarget, $vagueTarget, $fromPrimary ); |
| 629 |
return $this->chooseMostSpecificBlock( $blocks ); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
public function newListFromTarget( |
| 642 |
$specificTarget, |
| 643 |
$vagueTarget = null, |
| 644 |
$fromPrimary = false |
| 645 |
) { |
| 646 |
[ $target, $type ] = $this->blockUtils->parseBlockTarget( $specificTarget ); |
| 647 |
if ( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ) { |
| 648 |
$block = $this->newFromID( $target ); |
| 649 |
return $block ? [ $block ] : []; |
| 650 |
} elseif ( $target === null && $vagueTarget == '' ) { |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
return []; |
| 655 |
} elseif ( in_array( |
| 656 |
$type, |
| 657 |
[ Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ] ) |
| 658 |
) { |
| 659 |
return $this->newLoad( $target, $type, $fromPrimary, $vagueTarget ); |
| 660 |
} |
| 661 |
return []; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
public function newListFromIPs( array $addresses, $applySoftBlocks, $fromPrimary = false ) { |
| 675 |
if ( $addresses === [] ) { |
| 676 |
return []; |
| 677 |
} |
| 678 |
|
| 679 |
$conds = []; |
| 680 |
foreach ( array_unique( $addresses ) as $ipaddr ) { |
| 681 |
$conds[] = $this->getRangeCond( IPUtils::toHex( $ipaddr ), null ); |
| 682 |
} |
| 683 |
|
| 684 |
if ( $conds === [] ) { |
| 685 |
return []; |
| 686 |
} |
| 687 |
|
| 688 |
if ( $fromPrimary ) { |
| 689 |
$db = $this->getPrimaryDB(); |
| 690 |
} else { |
| 691 |
$db = $this->getReplicaDB(); |
| 692 |
} |
| 693 |
$conds = $db->makeList( $conds, LIST_OR ); |
| 694 |
if ( !$applySoftBlocks ) { |
| 695 |
$conds = [ $conds, 'bl_anon_only' => 0 ]; |
| 696 |
} |
| 697 |
$blockQuery = $this->getQueryInfo(); |
| 698 |
$rows = $db->newSelectQueryBuilder() |
| 699 |
->queryInfo( $blockQuery ) |
| 700 |
->fields( [ 'bt_range_start', 'bt_range_end' ] ) |
| 701 |
->where( $conds ) |
| 702 |
->caller( __METHOD__ ) |
| 703 |
->fetchResultSet(); |
| 704 |
|
| 705 |
$blocks = []; |
| 706 |
foreach ( $rows as $row ) { |
| 707 |
$block = $this->newFromRow( $db, $row ); |
| 708 |
if ( !$block->isExpired() ) { |
| 709 |
$blocks[] = $block; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
return $blocks; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
public function newListFromConds( $conds, $fromPrimary = false, $includeExpired = false ) { |
| 728 |
$db = $fromPrimary ? $this->getPrimaryDB() : $this->getReplicaDB(); |
| 729 |
$conds = self::mapActorAlias( $conds ); |
| 730 |
if ( !$includeExpired ) { |
| 731 |
$conds[] = $db->expr( 'bl_expiry', '>=', $db->timestamp() ); |
| 732 |
} |
| 733 |
$res = $db->newSelectQueryBuilder() |
| 734 |
->queryInfo( $this->getQueryInfo() ) |
| 735 |
->conds( $conds ) |
| 736 |
->caller( __METHOD__ ) |
| 737 |
->fetchResultSet(); |
| 738 |
$blocks = []; |
| 739 |
foreach ( $res as $row ) { |
| 740 |
$blocks[] = $this->newFromRow( $db, $row ); |
| 741 |
} |
| 742 |
return $blocks; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
public function purgeExpiredBlocks() { |
| 757 |
if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) { |
| 758 |
return; |
| 759 |
} |
| 760 |
|
| 761 |
$dbw = $this->getPrimaryDB(); |
| 762 |
|
| 763 |
DeferredUpdates::addUpdate( new AutoCommitUpdate( |
| 764 |
$dbw, |
| 765 |
__METHOD__, |
| 766 |
function ( IDatabase $dbw, $fname ) { |
| 767 |
$limit = $this->options->get( MainConfigNames::UpdateRowsPerQuery ); |
| 768 |
$res = $dbw->newSelectQueryBuilder() |
| 769 |
->select( [ 'bl_id', 'bl_target' ] ) |
| 770 |
->from( 'block' ) |
| 771 |
->where( $dbw->expr( 'bl_expiry', '<', $dbw->timestamp() ) ) |
| 772 |
|
| 773 |
->limit( $limit ) |
| 774 |
->caller( $fname )->fetchResultSet(); |
| 775 |
$this->deleteBlockRows( $res ); |
| 776 |
} |
| 777 |
) ); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
public function deleteBlocksMatchingConds( array $conds, $limit = null ) { |
| 791 |
$dbw = $this->getPrimaryDB(); |
| 792 |
$conds = self::mapActorAlias( $conds ); |
| 793 |
$qb = $dbw->newSelectQueryBuilder() |
| 794 |
->select( [ 'bl_id', 'bl_target' ] ) |
| 795 |
->from( 'block' ) |
| 796 |
|
| 797 |
->join( 'block_target', null, 'bt_id=bl_target' ) |
| 798 |
->where( $conds ) |
| 799 |
->caller( __METHOD__ ); |
| 800 |
if ( self::hasActorAlias( $conds ) ) { |
| 801 |
$qb->join( 'actor', 'ipblocks_actor', 'actor_id=bl_by_actor' ); |
| 802 |
} |
| 803 |
if ( $limit !== null ) { |
| 804 |
$qb->limit( $limit ); |
| 805 |
} |
| 806 |
$res = $qb->fetchResultSet(); |
| 807 |
return $this->deleteBlockRows( $res ); |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
private static function mapActorAlias( $conds ) { |
| 817 |
return self::mapConds( |
| 818 |
[ |
| 819 |
'bl_by' => 'ipblocks_actor.actor_user', |
| 820 |
], |
| 821 |
$conds |
| 822 |
); |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
private static function hasActorAlias( $conds ) { |
| 830 |
return array_key_exists( 'ipblocks_actor.actor_user', $conds ) |
| 831 |
|| array_key_exists( 'ipblocks_actor.actor_name', $conds ); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
private static function mapConds( $map, $conds ) { |
| 842 |
$newConds = []; |
| 843 |
foreach ( $conds as $field => $value ) { |
| 844 |
if ( isset( $map[$field] ) ) { |
| 845 |
$newConds[$map[$field]] = $value; |
| 846 |
} else { |
| 847 |
$newConds[$field] = $value; |
| 848 |
} |
| 849 |
} |
| 850 |
return $newConds; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
private function deleteBlockRows( $rows ) { |
| 861 |
$ids = []; |
| 862 |
$deltasByTarget = []; |
| 863 |
foreach ( $rows as $row ) { |
| 864 |
$ids[] = (int)$row->bl_id; |
| 865 |
$target = (int)$row->bl_target; |
| 866 |
if ( !isset( $deltasByTarget[$target] ) ) { |
| 867 |
$deltasByTarget[$target] = 0; |
| 868 |
} |
| 869 |
$deltasByTarget[$target]++; |
| 870 |
} |
| 871 |
if ( !$ids ) { |
| 872 |
return 0; |
| 873 |
} |
| 874 |
$dbw = $this->getPrimaryDB(); |
| 875 |
$dbw->startAtomic( __METHOD__ ); |
| 876 |
|
| 877 |
$maxTargetCount = max( $deltasByTarget ); |
| 878 |
for ( $delta = 1; $delta <= $maxTargetCount; $delta++ ) { |
| 879 |
$targetsWithThisDelta = array_keys( $deltasByTarget, $delta, true ); |
| 880 |
$this->releaseTargets( $dbw, $targetsWithThisDelta, $delta ); |
| 881 |
} |
| 882 |
|
| 883 |
$dbw->newDeleteQueryBuilder() |
| 884 |
->deleteFrom( 'block' ) |
| 885 |
->where( [ 'bl_id' => $ids ] ) |
| 886 |
->caller( __METHOD__ )->execute(); |
| 887 |
$numDeleted = $dbw->affectedRows(); |
| 888 |
$dbw->endAtomic( __METHOD__ ); |
| 889 |
$this->blockRestrictionStore->deleteByBlockId( $ids ); |
| 890 |
return $numDeleted; |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
private function releaseTargets( IDatabase $dbw, $targetIds, int $delta = 1 ) { |
| 902 |
$dbw->newUpdateQueryBuilder() |
| 903 |
->update( 'block_target' ) |
| 904 |
->set( [ 'bt_count' => new RawSQLValue( "bt_count-$delta" ) ] ) |
| 905 |
->where( [ 'bt_id' => $targetIds ] ) |
| 906 |
->caller( __METHOD__ ) |
| 907 |
->execute(); |
| 908 |
$dbw->newDeleteQueryBuilder() |
| 909 |
->deleteFrom( 'block_target' ) |
| 910 |
->where( [ |
| 911 |
'bt_count<1', |
| 912 |
'bt_id' => $targetIds |
| 913 |
] ) |
| 914 |
->caller( __METHOD__ ) |
| 915 |
->execute(); |
| 916 |
} |
| 917 |
|
| 918 |
private function getReplicaDB(): IReadableDatabase { |
| 919 |
return $this->dbProvider->getReplicaDatabase( $this->wikiId ); |
| 920 |
} |
| 921 |
|
| 922 |
private function getPrimaryDB(): IDatabase { |
| 923 |
return $this->dbProvider->getPrimaryDatabase( $this->wikiId ); |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
public function insertBlock( |
| 938 |
DatabaseBlock $block, |
| 939 |
$expectedTargetCount = 0 |
| 940 |
) { |
| 941 |
$block->assertWiki( $this->wikiId ); |
| 942 |
|
| 943 |
$blocker = $block->getBlocker(); |
| 944 |
if ( !$blocker || $blocker->getName() === '' ) { |
| 945 |
throw new InvalidArgumentException( 'Cannot insert a block without a blocker set' ); |
| 946 |
} |
| 947 |
|
| 948 |
if ( $expectedTargetCount instanceof IDatabase ) { |
| 949 |
throw new InvalidArgumentException( |
| 950 |
'Old method signature: Passing a custom database connection to ' |
| 951 |
. 'DatabaseBlockStore::insertBlock is no longer supported' |
| 952 |
); |
| 953 |
} |
| 954 |
|
| 955 |
$this->logger->debug( 'Inserting block; timestamp ' . $block->getTimestamp() ); |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
$this->purgeExpiredBlocks(); |
| 960 |
|
| 961 |
$dbw = $this->getPrimaryDB(); |
| 962 |
$dbw->startAtomic( __METHOD__ ); |
| 963 |
$success = $this->attemptInsert( $block, $dbw, $expectedTargetCount ); |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
if ( !$success ) { |
| 968 |
if ( $this->purgeExpiredConflicts( $block, $dbw ) ) { |
| 969 |
$success = $this->attemptInsert( $block, $dbw, $expectedTargetCount ); |
| 970 |
} |
| 971 |
} |
| 972 |
$dbw->endAtomic( __METHOD__ ); |
| 973 |
|
| 974 |
if ( $success ) { |
| 975 |
$autoBlockIds = $this->doRetroactiveAutoblock( $block ); |
| 976 |
|
| 977 |
if ( $this->options->get( MainConfigNames::BlockDisablesLogin ) ) { |
| 978 |
$targetUserIdentity = $block->getTargetUserIdentity(); |
| 979 |
if ( $targetUserIdentity ) { |
| 980 |
$targetUser = $this->userFactory->newFromUserIdentity( $targetUserIdentity ); |
| 981 |
SessionManager::singleton()->invalidateSessionsForUser( $targetUser ); |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
return [ 'id' => $block->getId( $this->wikiId ), 'autoIds' => $autoBlockIds ]; |
| 986 |
} |
| 987 |
|
| 988 |
return false; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
private function attemptInsert( |
| 1001 |
DatabaseBlock $block, |
| 1002 |
IDatabase $dbw, |
| 1003 |
$expectedTargetCount |
| 1004 |
) { |
| 1005 |
$targetId = $this->acquireTarget( $block, $dbw, $expectedTargetCount ); |
| 1006 |
if ( !$targetId ) { |
| 1007 |
return false; |
| 1008 |
} |
| 1009 |
$row = $this->getArrayForBlockUpdate( $block, $dbw ); |
| 1010 |
$row['bl_target'] = $targetId; |
| 1011 |
$dbw->newInsertQueryBuilder() |
| 1012 |
->insertInto( 'block' ) |
| 1013 |
->row( $row ) |
| 1014 |
->caller( __METHOD__ )->execute(); |
| 1015 |
if ( !$dbw->affectedRows() ) { |
| 1016 |
return false; |
| 1017 |
} |
| 1018 |
$id = $dbw->insertId(); |
| 1019 |
|
| 1020 |
if ( !$id ) { |
| 1021 |
throw new RuntimeException( 'block insert ID is falsey' ); |
| 1022 |
} |
| 1023 |
$block->setId( $id ); |
| 1024 |
$restrictions = $block->getRawRestrictions(); |
| 1025 |
if ( $restrictions ) { |
| 1026 |
$this->blockRestrictionStore->insert( $restrictions ); |
| 1027 |
} |
| 1028 |
|
| 1029 |
return true; |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
private function purgeExpiredConflicts( |
| 1040 |
DatabaseBlock $block, |
| 1041 |
IDatabase $dbw |
| 1042 |
) { |
| 1043 |
$targetConds = $this->getTargetConds( $block ); |
| 1044 |
$res = $dbw->newSelectQueryBuilder() |
| 1045 |
->select( [ 'bl_id', 'bl_target' ] ) |
| 1046 |
->from( 'block' ) |
| 1047 |
->join( 'block_target', null, [ 'bt_id=bl_target' ] ) |
| 1048 |
->where( $targetConds ) |
| 1049 |
->andWhere( $dbw->expr( 'bl_expiry', '<', $dbw->timestamp() ) ) |
| 1050 |
->caller( __METHOD__ )->fetchResultSet(); |
| 1051 |
return (bool)$this->deleteBlockRows( $res ); |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
private function getTargetConds( DatabaseBlock $block ) { |
| 1061 |
if ( $block->getType() === Block::TYPE_USER ) { |
| 1062 |
return [ |
| 1063 |
'bt_user' => $block->getTargetUserIdentity()->getId( $this->wikiId ) |
| 1064 |
]; |
| 1065 |
} else { |
| 1066 |
return [ 'bt_address' => $block->getTargetName() ]; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
private function acquireTarget( |
| 1085 |
DatabaseBlock $block, |
| 1086 |
IDatabase $dbw, |
| 1087 |
$expectedTargetCount |
| 1088 |
) { |
| 1089 |
$isUser = $block->getType() === Block::TYPE_USER; |
| 1090 |
$isRange = $block->getType() === Block::TYPE_RANGE; |
| 1091 |
$isAuto = $block->getType() === Block::TYPE_AUTO; |
| 1092 |
$isSingle = !$isUser && !$isRange; |
| 1093 |
$targetAddress = $isUser ? null : $block->getTargetName(); |
| 1094 |
$targetUserName = $isUser ? $block->getTargetName() : null; |
| 1095 |
$targetUserId = $isUser |
| 1096 |
? $block->getTargetUserIdentity()->getId( $this->wikiId ) : null; |
| 1097 |
|
| 1098 |
|
| 1099 |
if ( $isUser ) { |
| 1100 |
$targetConds = [ 'bt_user' => $targetUserId ]; |
| 1101 |
$targetLockKey = $dbw->getDomainID() . ':block:u:' . $targetUserId; |
| 1102 |
} else { |
| 1103 |
$targetConds = [ |
| 1104 |
'bt_address' => $targetAddress, |
| 1105 |
'bt_auto' => $isAuto, |
| 1106 |
]; |
| 1107 |
$targetLockKey = $dbw->getDomainID() . ':block:' . |
| 1108 |
( $isAuto ? 'a' : 'i' ) . ':' . $targetAddress; |
| 1109 |
} |
| 1110 |
$condsWithCount = $targetConds; |
| 1111 |
if ( $expectedTargetCount !== null ) { |
| 1112 |
$condsWithCount['bt_count'] = $expectedTargetCount; |
| 1113 |
} |
| 1114 |
|
| 1115 |
$dbw->lock( $targetLockKey, __METHOD__ ); |
| 1116 |
$func = __METHOD__; |
| 1117 |
$dbw->onTransactionCommitOrIdle( |
| 1118 |
static function () use ( $dbw, $targetLockKey, $func ) { |
| 1119 |
$dbw->unlock( $targetLockKey, "$func.closure" ); |
| 1120 |
}, |
| 1121 |
__METHOD__ |
| 1122 |
); |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
$dbw->newUpdateQueryBuilder() |
| 1130 |
->update( 'block_target' ) |
| 1131 |
->set( [ 'bt_count' => new RawSQLValue( 'bt_count+1' ) ] ) |
| 1132 |
->where( $condsWithCount ) |
| 1133 |
->caller( __METHOD__ )->execute(); |
| 1134 |
$numUpdatedRows = $dbw->affectedRows(); |
| 1135 |
|
| 1136 |
|
| 1137 |
$ids = $dbw->newSelectQueryBuilder() |
| 1138 |
->select( 'bt_id' ) |
| 1139 |
->from( 'block_target' ) |
| 1140 |
->where( $targetConds ) |
| 1141 |
->forUpdate() |
| 1142 |
->caller( __METHOD__ ) |
| 1143 |
->fetchFieldValues(); |
| 1144 |
if ( count( $ids ) > 1 ) { |
| 1145 |
throw new RuntimeException( "Duplicate block_target rows detected: " . |
| 1146 |
implode( ',', $ids ) ); |
| 1147 |
} |
| 1148 |
$id = $ids[0] ?? false; |
| 1149 |
|
| 1150 |
if ( $id === false ) { |
| 1151 |
if ( $numUpdatedRows ) { |
| 1152 |
throw new RuntimeException( |
| 1153 |
'block_target row unexpectedly missing after we locked it' ); |
| 1154 |
} |
| 1155 |
if ( $expectedTargetCount !== 0 && $expectedTargetCount !== null ) { |
| 1156 |
|
| 1157 |
return null; |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
$targetRow = [ |
| 1162 |
'bt_address' => $targetAddress, |
| 1163 |
'bt_user' => $targetUserId, |
| 1164 |
'bt_user_text' => $targetUserName, |
| 1165 |
'bt_auto' => $isAuto, |
| 1166 |
'bt_range_start' => $isRange ? $block->getRangeStart() : null, |
| 1167 |
'bt_range_end' => $isRange ? $block->getRangeEnd() : null, |
| 1168 |
'bt_ip_hex' => $isSingle || $isRange ? $block->getRangeStart() : null, |
| 1169 |
'bt_count' => 1 |
| 1170 |
]; |
| 1171 |
$dbw->newInsertQueryBuilder() |
| 1172 |
->insertInto( 'block_target' ) |
| 1173 |
->row( $targetRow ) |
| 1174 |
->caller( __METHOD__ )->execute(); |
| 1175 |
$id = $dbw->insertId(); |
| 1176 |
if ( !$id ) { |
| 1177 |
throw new RuntimeException( |
| 1178 |
'block_target insert ID is falsey despite unconditional insert' ); |
| 1179 |
} |
| 1180 |
} elseif ( !$numUpdatedRows ) { |
| 1181 |
|
| 1182 |
return null; |
| 1183 |
} |
| 1184 |
|
| 1185 |
return (int)$id; |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
public function updateBlock( DatabaseBlock $block ) { |
| 1197 |
$this->logger->debug( 'Updating block; timestamp ' . $block->getTimestamp() ); |
| 1198 |
|
| 1199 |
$block->assertWiki( $this->wikiId ); |
| 1200 |
|
| 1201 |
$blockId = $block->getId( $this->wikiId ); |
| 1202 |
if ( !$blockId ) { |
| 1203 |
throw new InvalidArgumentException( |
| 1204 |
__METHOD__ . ' requires that a block id be set' |
| 1205 |
); |
| 1206 |
} |
| 1207 |
|
| 1208 |
$dbw = $this->getPrimaryDB(); |
| 1209 |
|
| 1210 |
$dbw->startAtomic( __METHOD__ ); |
| 1211 |
|
| 1212 |
$row = $this->getArrayForBlockUpdate( $block, $dbw ); |
| 1213 |
$dbw->newUpdateQueryBuilder() |
| 1214 |
->update( 'block' ) |
| 1215 |
->set( $row ) |
| 1216 |
->where( [ 'bl_id' => $blockId ] ) |
| 1217 |
->caller( __METHOD__ )->execute(); |
| 1218 |
|
| 1219 |
|
| 1220 |
$result = true; |
| 1221 |
$restrictions = $block->getRawRestrictions(); |
| 1222 |
if ( $restrictions !== null ) { |
| 1223 |
|
| 1224 |
if ( $restrictions === [] ) { |
| 1225 |
$result = $this->blockRestrictionStore->deleteByBlockId( $blockId ); |
| 1226 |
} else { |
| 1227 |
$result = $this->blockRestrictionStore->update( $restrictions ); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
if ( $block->isAutoblocking() ) { |
| 1232 |
|
| 1233 |
$dbw->newUpdateQueryBuilder() |
| 1234 |
->update( 'block' ) |
| 1235 |
->set( $this->getArrayForAutoblockUpdate( $block ) ) |
| 1236 |
->where( [ 'bl_parent_block_id' => $blockId ] ) |
| 1237 |
->caller( __METHOD__ )->execute(); |
| 1238 |
|
| 1239 |
|
| 1240 |
if ( $restrictions !== null ) { |
| 1241 |
$this->blockRestrictionStore->updateByParentBlockId( |
| 1242 |
$blockId, |
| 1243 |
$restrictions |
| 1244 |
); |
| 1245 |
} |
| 1246 |
} else { |
| 1247 |
|
| 1248 |
$this->deleteBlocksMatchingConds( [ 'bl_parent_block_id' => $blockId ] ); |
| 1249 |
} |
| 1250 |
|
| 1251 |
$dbw->endAtomic( __METHOD__ ); |
| 1252 |
|
| 1253 |
if ( $result ) { |
| 1254 |
$autoBlockIds = $this->doRetroactiveAutoblock( $block ); |
| 1255 |
return [ 'id' => $blockId, 'autoIds' => $autoBlockIds ]; |
| 1256 |
} |
| 1257 |
|
| 1258 |
return false; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
public function updateTarget( DatabaseBlock $block, $newTarget ) { |
| 1275 |
$dbw = $this->getPrimaryDB(); |
| 1276 |
$blockId = $block->getId( $this->wikiId ); |
| 1277 |
if ( !$blockId ) { |
| 1278 |
throw new InvalidArgumentException( |
| 1279 |
__METHOD__ . " requires that a block id be set\n" |
| 1280 |
); |
| 1281 |
} |
| 1282 |
|
| 1283 |
$oldTargetConds = $this->getTargetConds( $block ); |
| 1284 |
$block->setTarget( $newTarget ); |
| 1285 |
|
| 1286 |
$dbw->startAtomic( __METHOD__ ); |
| 1287 |
$targetId = $this->acquireTarget( $block, $dbw, null ); |
| 1288 |
if ( !$targetId ) { |
| 1289 |
|
| 1290 |
$dbw->endAtomic( __METHOD__ ); |
| 1291 |
return false; |
| 1292 |
} |
| 1293 |
$oldTargetId = $dbw->newSelectQueryBuilder() |
| 1294 |
->select( 'bt_id' ) |
| 1295 |
->from( 'block_target' ) |
| 1296 |
->where( $oldTargetConds ) |
| 1297 |
->caller( __METHOD__ )->fetchField(); |
| 1298 |
$this->releaseTargets( $dbw, [ $oldTargetId ] ); |
| 1299 |
|
| 1300 |
$dbw->newUpdateQueryBuilder() |
| 1301 |
->update( 'block' ) |
| 1302 |
->set( [ 'bl_target' => $targetId ] ) |
| 1303 |
->where( [ 'bl_id' => $blockId ] ) |
| 1304 |
->caller( __METHOD__ ) |
| 1305 |
->execute(); |
| 1306 |
$affected = $dbw->affectedRows(); |
| 1307 |
$dbw->endAtomic( __METHOD__ ); |
| 1308 |
return (bool)$affected; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
public function deleteBlock( DatabaseBlock $block ): bool { |
| 1318 |
if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) { |
| 1319 |
return false; |
| 1320 |
} |
| 1321 |
|
| 1322 |
$block->assertWiki( $this->wikiId ); |
| 1323 |
|
| 1324 |
$blockId = $block->getId( $this->wikiId ); |
| 1325 |
|
| 1326 |
if ( !$blockId ) { |
| 1327 |
throw new InvalidArgumentException( |
| 1328 |
__METHOD__ . ' requires that a block id be set' |
| 1329 |
); |
| 1330 |
} |
| 1331 |
$dbw = $this->getPrimaryDB(); |
| 1332 |
$dbw->startAtomic( __METHOD__ ); |
| 1333 |
$res = $dbw->newSelectQueryBuilder() |
| 1334 |
->select( [ 'bl_id', 'bl_target' ] ) |
| 1335 |
->from( 'block' ) |
| 1336 |
->where( |
| 1337 |
$dbw->orExpr( [ |
| 1338 |
'bl_parent_block_id' => $blockId, |
| 1339 |
'bl_id' => $blockId, |
| 1340 |
] ) |
| 1341 |
) |
| 1342 |
->caller( __METHOD__ )->fetchResultSet(); |
| 1343 |
$this->deleteBlockRows( $res ); |
| 1344 |
$affected = $res->numRows(); |
| 1345 |
$dbw->endAtomic( __METHOD__ ); |
| 1346 |
|
| 1347 |
return $affected > 0; |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
private function getArrayForBlockUpdate( |
| 1359 |
DatabaseBlock $block, |
| 1360 |
IDatabase $dbw |
| 1361 |
): array { |
| 1362 |
$expiry = $dbw->encodeExpiry( $block->getExpiry() ); |
| 1363 |
|
| 1364 |
$blocker = $block->getBlocker(); |
| 1365 |
if ( !$blocker ) { |
| 1366 |
throw new RuntimeException( __METHOD__ . ': this block does not have a blocker' ); |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
$blockerActor = $this->actorStoreFactory |
| 1371 |
->getActorStore( $dbw->getDomainID() ) |
| 1372 |
->acquireActorId( $blocker, $dbw ); |
| 1373 |
|
| 1374 |
$blockArray = [ |
| 1375 |
'bl_by_actor' => $blockerActor, |
| 1376 |
'bl_timestamp' => $dbw->timestamp( $block->getTimestamp() ), |
| 1377 |
'bl_anon_only' => !$block->isHardblock(), |
| 1378 |
'bl_create_account' => $block->isCreateAccountBlocked(), |
| 1379 |
'bl_enable_autoblock' => $block->isAutoblocking(), |
| 1380 |
'bl_expiry' => $expiry, |
| 1381 |
'bl_deleted' => intval( $block->getHideName() ), |
| 1382 |
'bl_block_email' => $block->isEmailBlocked(), |
| 1383 |
'bl_allow_usertalk' => $block->isUsertalkEditAllowed(), |
| 1384 |
'bl_parent_block_id' => $block->getParentBlockId(), |
| 1385 |
'bl_sitewide' => $block->isSitewide(), |
| 1386 |
]; |
| 1387 |
$commentArray = $this->commentStore->insert( |
| 1388 |
$dbw, |
| 1389 |
'bl_reason', |
| 1390 |
$block->getReasonComment() |
| 1391 |
); |
| 1392 |
|
| 1393 |
$combinedArray = $blockArray + $commentArray; |
| 1394 |
return $combinedArray; |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
private function getArrayForAutoblockUpdate( DatabaseBlock $block ): array { |
| 1404 |
$blocker = $block->getBlocker(); |
| 1405 |
if ( !$blocker ) { |
| 1406 |
throw new RuntimeException( __METHOD__ . ': this block does not have a blocker' ); |
| 1407 |
} |
| 1408 |
$dbw = $this->getPrimaryDB(); |
| 1409 |
$blockerActor = $this->actorStoreFactory |
| 1410 |
->getActorNormalization( $this->wikiId ) |
| 1411 |
->acquireActorId( $blocker, $dbw ); |
| 1412 |
|
| 1413 |
$blockArray = [ |
| 1414 |
'bl_by_actor' => $blockerActor, |
| 1415 |
'bl_create_account' => $block->isCreateAccountBlocked(), |
| 1416 |
'bl_deleted' => (int)$block->getHideName(), |
| 1417 |
'bl_allow_usertalk' => $block->isUsertalkEditAllowed(), |
| 1418 |
'bl_sitewide' => $block->isSitewide(), |
| 1419 |
]; |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
if ( $block->getExpiry() !== 'infinity' ) { |
| 1425 |
$blockArray['bl_expiry'] = new RawSQLValue( $dbw->conditional( |
| 1426 |
$dbw->expr( 'bl_expiry', '>', $dbw->timestamp( $block->getExpiry() ) ), |
| 1427 |
$dbw->addQuotes( $dbw->timestamp( $block->getExpiry() ) ), |
| 1428 |
'bl_expiry' |
| 1429 |
) ); |
| 1430 |
} |
| 1431 |
|
| 1432 |
$commentArray = $this->commentStore->insert( |
| 1433 |
$dbw, |
| 1434 |
'bl_reason', |
| 1435 |
$this->getAutoblockReason( $block ) |
| 1436 |
); |
| 1437 |
|
| 1438 |
$combinedArray = $blockArray + $commentArray; |
| 1439 |
return $combinedArray; |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
private function doRetroactiveAutoblock( DatabaseBlock $block ): array { |
| 1450 |
$autoBlockIds = []; |
| 1451 |
|
| 1452 |
if ( $block->isAutoblocking() && $block->getType() == AbstractBlock::TYPE_USER ) { |
| 1453 |
$this->logger->debug( |
| 1454 |
'Doing retroactive autoblocks for ' . $block->getTargetName() |
| 1455 |
); |
| 1456 |
|
| 1457 |
$hookAutoBlocked = []; |
| 1458 |
$continue = $this->hookRunner->onPerformRetroactiveAutoblock( |
| 1459 |
$block, |
| 1460 |
$hookAutoBlocked |
| 1461 |
); |
| 1462 |
|
| 1463 |
if ( $continue ) { |
| 1464 |
$coreAutoBlocked = $this->performRetroactiveAutoblock( $block ); |
| 1465 |
$autoBlockIds = array_merge( $hookAutoBlocked, $coreAutoBlocked ); |
| 1466 |
} else { |
| 1467 |
$autoBlockIds = $hookAutoBlocked; |
| 1468 |
} |
| 1469 |
} |
| 1470 |
return $autoBlockIds; |
| 1471 |
} |
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
private function performRetroactiveAutoblock( DatabaseBlock $block ): array { |
| 1481 |
if ( !$this->options->get( MainConfigNames::PutIPinRC ) ) { |
| 1482 |
|
| 1483 |
return []; |
| 1484 |
} |
| 1485 |
|
| 1486 |
$type = $block->getType(); |
| 1487 |
if ( $type !== AbstractBlock::TYPE_USER ) { |
| 1488 |
|
| 1489 |
return []; |
| 1490 |
} |
| 1491 |
|
| 1492 |
$dbr = $this->getReplicaDB(); |
| 1493 |
|
| 1494 |
$targetUser = $block->getTargetUserIdentity(); |
| 1495 |
$actor = $targetUser ? $this->actorStoreFactory |
| 1496 |
->getActorNormalization( $this->wikiId ) |
| 1497 |
->findActorId( $targetUser, $dbr ) : null; |
| 1498 |
|
| 1499 |
if ( !$actor ) { |
| 1500 |
$this->logger->debug( 'No actor found to retroactively autoblock' ); |
| 1501 |
return []; |
| 1502 |
} |
| 1503 |
|
| 1504 |
$rcIp = $dbr->newSelectQueryBuilder() |
| 1505 |
->select( 'rc_ip' ) |
| 1506 |
->from( 'recentchanges' ) |
| 1507 |
->where( [ 'rc_actor' => $actor ] ) |
| 1508 |
->orderBy( 'rc_timestamp', SelectQueryBuilder::SORT_DESC ) |
| 1509 |
->caller( __METHOD__ )->fetchField(); |
| 1510 |
|
| 1511 |
if ( !$rcIp ) { |
| 1512 |
$this->logger->debug( 'No IP found to retroactively autoblock' ); |
| 1513 |
return []; |
| 1514 |
} |
| 1515 |
|
| 1516 |
$id = $this->doAutoblock( $block, $rcIp ); |
| 1517 |
if ( !$id ) { |
| 1518 |
return []; |
| 1519 |
} |
| 1520 |
return [ $id ]; |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
public function doAutoblock( DatabaseBlock $parentBlock, $autoblockIP ) { |
| 1532 |
|
| 1533 |
if ( !$parentBlock->isAutoblocking() ) { |
| 1534 |
return false; |
| 1535 |
} |
| 1536 |
$parentBlock->assertWiki( $this->wikiId ); |
| 1537 |
|
| 1538 |
[ $target, $type ] = $this->blockUtils->parseBlockTarget( $autoblockIP ); |
| 1539 |
if ( $type != Block::TYPE_IP ) { |
| 1540 |
$this->logger->debug( "Autoblock not supported for ip ranges." ); |
| 1541 |
return false; |
| 1542 |
} |
| 1543 |
$target = (string)$target; |
| 1544 |
|
| 1545 |
|
| 1546 |
if ( $this->autoblockExemptionList->isExempt( $target ) ) { |
| 1547 |
return false; |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
if ( !$this->hookRunner->onAbortAutoblock( $target, $parentBlock ) ) { |
| 1552 |
$this->logger->debug( "Autoblock aborted by hook." ); |
| 1553 |
return false; |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
$blocks = $this->newLoad( $target, Block::TYPE_IP, false ); |
| 1560 |
if ( $blocks ) { |
| 1561 |
foreach ( $blocks as $ipblock ) { |
| 1562 |
|
| 1563 |
|
| 1564 |
if ( $ipblock->getType() === Block::TYPE_AUTO |
| 1565 |
&& $parentBlock->getExpiry() > $ipblock->getExpiry() |
| 1566 |
) { |
| 1567 |
|
| 1568 |
|
| 1569 |
$this->updateTimestamp( $ipblock ); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
return false; |
| 1573 |
} |
| 1574 |
$blocker = $parentBlock->getBlocker(); |
| 1575 |
if ( !$blocker ) { |
| 1576 |
throw new RuntimeException( __METHOD__ . ': this block does not have a blocker' ); |
| 1577 |
} |
| 1578 |
|
| 1579 |
$timestamp = wfTimestampNow(); |
| 1580 |
$expiry = $this->getAutoblockExpiry( $timestamp, $parentBlock->getExpiry() ); |
| 1581 |
$autoblock = new DatabaseBlock( [ |
| 1582 |
'wiki' => $this->wikiId, |
| 1583 |
'address' => UserIdentityValue::newAnonymous( $target, $this->wikiId ), |
| 1584 |
'by' => $blocker, |
| 1585 |
'reason' => $this->getAutoblockReason( $parentBlock ), |
| 1586 |
'decodedTimestamp' => $timestamp, |
| 1587 |
'auto' => true, |
| 1588 |
'createAccount' => $parentBlock->isCreateAccountBlocked(), |
| 1589 |
|
| 1590 |
'hideName' => $parentBlock->getHideName(), |
| 1591 |
'allowUsertalk' => $parentBlock->isUsertalkEditAllowed(), |
| 1592 |
'parentBlockId' => $parentBlock->getId( $this->wikiId ), |
| 1593 |
'sitewide' => $parentBlock->isSitewide(), |
| 1594 |
'restrictions' => $parentBlock->getRestrictions(), |
| 1595 |
'decodedExpiry' => $expiry, |
| 1596 |
] ); |
| 1597 |
|
| 1598 |
$this->logger->debug( "Autoblocking {$parentBlock->getTargetName()}@" . $target ); |
| 1599 |
|
| 1600 |
$status = $this->insertBlock( $autoblock ); |
| 1601 |
return $status |
| 1602 |
? $status['id'] |
| 1603 |
: false; |
| 1604 |
} |
| 1605 |
|
| 1606 |
private function getAutoblockReason( DatabaseBlock $parentBlock ) { |
| 1607 |
return wfMessage( |
| 1608 |
'autoblocker', |
| 1609 |
$parentBlock->getTargetName(), |
| 1610 |
$parentBlock->getReasonComment()->text |
| 1611 |
)->inContentLanguage()->plain(); |
| 1612 |
} |
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
public function updateTimestamp( DatabaseBlock $block ) { |
| 1621 |
$block->assertWiki( $this->wikiId ); |
| 1622 |
if ( $block->getType() !== Block::TYPE_AUTO ) { |
| 1623 |
return; |
| 1624 |
} |
| 1625 |
$now = wfTimestamp(); |
| 1626 |
$block->setTimestamp( $now ); |
| 1627 |
|
| 1628 |
|
| 1629 |
$block->setExpiry( $this->getAutoblockExpiry( $now ) ); |
| 1630 |
|
| 1631 |
$dbw = $this->getPrimaryDB(); |
| 1632 |
$dbw->newUpdateQueryBuilder() |
| 1633 |
->update( 'block' ) |
| 1634 |
->set( |
| 1635 |
[ |
| 1636 |
'bl_timestamp' => $dbw->timestamp( $block->getTimestamp() ), |
| 1637 |
'bl_expiry' => $dbw->timestamp( $block->getExpiry() ), |
| 1638 |
] |
| 1639 |
) |
| 1640 |
->where( [ 'bl_id' => $block->getId( $this->wikiId ) ] ) |
| 1641 |
->caller( __METHOD__ )->execute(); |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
public function getAutoblockExpiry( $timestamp, ?string $parentExpiry = null ) { |
| 1656 |
$maxDuration = $this->options->get( MainConfigNames::AutoblockExpiry ); |
| 1657 |
$expiry = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $timestamp ) + $maxDuration ); |
| 1658 |
if ( $parentExpiry !== null && $parentExpiry !== 'infinity' ) { |
| 1659 |
$expiry = min( $parentExpiry, $expiry ); |
| 1660 |
} |
| 1661 |
return $expiry; |
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
} |