| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Permissions; |
| 4 |
|
| 5 |
use MediaWiki\Cache\CacheKeyHelper; |
| 6 |
use MediaWiki\Cache\LinkCache; |
| 7 |
use MediaWiki\CommentStore\CommentStore; |
| 8 |
use MediaWiki\Config\ServiceOptions; |
| 9 |
use MediaWiki\HookContainer\HookContainer; |
| 10 |
use MediaWiki\HookContainer\HookRunner; |
| 11 |
use MediaWiki\Linker\LinksMigration; |
| 12 |
use MediaWiki\MainConfigNames; |
| 13 |
use MediaWiki\Page\PageIdentity; |
| 14 |
use MediaWiki\Page\PageIdentityValue; |
| 15 |
use MediaWiki\Page\PageStore; |
| 16 |
use MediaWiki\Title\Title; |
| 17 |
use MediaWiki\Title\TitleValue; |
| 18 |
use stdClass; |
| 19 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 20 |
use Wikimedia\Rdbms\Database; |
| 21 |
use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 22 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 23 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 24 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class RestrictionStore { |
| 30 |
|
| 31 |
|
| 32 |
public const CONSTRUCTOR_OPTIONS = [ |
| 33 |
MainConfigNames::NamespaceProtection, |
| 34 |
MainConfigNames::RestrictionLevels, |
| 35 |
MainConfigNames::RestrictionTypes, |
| 36 |
MainConfigNames::SemiprotectedRestrictionLevels, |
| 37 |
]; |
| 38 |
|
| 39 |
private ServiceOptions $options; |
| 40 |
private WANObjectCache $wanCache; |
| 41 |
private ILoadBalancer $loadBalancer; |
| 42 |
private LinkCache $linkCache; |
| 43 |
private LinksMigration $linksMigration; |
| 44 |
private CommentStore $commentStore; |
| 45 |
private HookContainer $hookContainer; |
| 46 |
private HookRunner $hookRunner; |
| 47 |
private PageStore $pageStore; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private $cache = []; |
| 60 |
|
| 61 |
public function __construct( |
| 62 |
ServiceOptions $options, |
| 63 |
WANObjectCache $wanCache, |
| 64 |
ILoadBalancer $loadBalancer, |
| 65 |
LinkCache $linkCache, |
| 66 |
LinksMigration $linksMigration, |
| 67 |
CommentStore $commentStore, |
| 68 |
HookContainer $hookContainer, |
| 69 |
PageStore $pageStore |
| 70 |
) { |
| 71 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 72 |
$this->options = $options; |
| 73 |
$this->wanCache = $wanCache; |
| 74 |
$this->loadBalancer = $loadBalancer; |
| 75 |
$this->linkCache = $linkCache; |
| 76 |
$this->linksMigration = $linksMigration; |
| 77 |
$this->commentStore = $commentStore; |
| 78 |
$this->hookContainer = $hookContainer; |
| 79 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 80 |
$this->pageStore = $pageStore; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function getRestrictions( PageIdentity $page, string $action ): array { |
| 95 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
$restrictionTypes = $this->listApplicableRestrictionTypes( $page ); |
| 102 |
if ( !in_array( $action, $restrictionTypes ) ) { |
| 103 |
return []; |
| 104 |
} |
| 105 |
|
| 106 |
$restrictions = $this->getAllRestrictions( $page ); |
| 107 |
return $restrictions[$action] ?? []; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function getAllRestrictions( PageIdentity $page ): array { |
| 118 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 119 |
|
| 120 |
if ( !$this->areRestrictionsLoaded( $page ) ) { |
| 121 |
$this->loadRestrictions( $page ); |
| 122 |
} |
| 123 |
return $this->cache[CacheKeyHelper::getKeyForPage( $page )]['restrictions'] ?? []; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function getRestrictionExpiry( PageIdentity $page, string $action ): ?string { |
| 135 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 136 |
|
| 137 |
if ( !$this->areRestrictionsLoaded( $page ) ) { |
| 138 |
$this->loadRestrictions( $page ); |
| 139 |
} |
| 140 |
return $this->cache[CacheKeyHelper::getKeyForPage( $page )]['expiry'][$action] ?? null; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function getCreateProtection( PageIdentity $page ): ?array { |
| 158 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 159 |
|
| 160 |
$protection = $this->getCreateProtectionInternal( $page ); |
| 161 |
|
| 162 |
if ( $protection ) { |
| 163 |
if ( $protection['permission'] == 'sysop' ) { |
| 164 |
$protection['permission'] = 'editprotected'; |
| 165 |
} |
| 166 |
if ( $protection['permission'] == 'autoconfirmed' ) { |
| 167 |
$protection['permission'] = 'editsemiprotected'; |
| 168 |
} |
| 169 |
} |
| 170 |
return $protection; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function deleteCreateProtection( PageIdentity $page ): void { |
| 180 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 181 |
|
| 182 |
$dbw = $this->loadBalancer->getConnection( DB_PRIMARY ); |
| 183 |
$dbw->newDeleteQueryBuilder() |
| 184 |
->deleteFrom( 'protected_titles' ) |
| 185 |
->where( [ 'pt_namespace' => $page->getNamespace(), 'pt_title' => $page->getDBkey() ] ) |
| 186 |
->caller( __METHOD__ )->execute(); |
| 187 |
$this->cache[CacheKeyHelper::getKeyForPage( $page )]['create_protection'] = null; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function isSemiProtected( PageIdentity $page, string $action = 'edit' ): bool { |
| 199 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 200 |
|
| 201 |
$restrictions = $this->getRestrictions( $page, $action ); |
| 202 |
$semi = $this->options->get( MainConfigNames::SemiprotectedRestrictionLevels ); |
| 203 |
if ( !$restrictions || !$semi ) { |
| 204 |
|
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
foreach ( array_keys( $semi, 'editsemiprotected' ) as $key ) { |
| 210 |
$semi[$key] = 'autoconfirmed'; |
| 211 |
} |
| 212 |
foreach ( array_keys( $restrictions, 'editsemiprotected' ) as $key ) { |
| 213 |
$restrictions[$key] = 'autoconfirmed'; |
| 214 |
} |
| 215 |
|
| 216 |
return !array_diff( $restrictions, $semi ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function isProtected( PageIdentity $page, string $action = '' ): bool { |
| 227 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 228 |
|
| 229 |
|
| 230 |
if ( $page->getNamespace() === NS_SPECIAL ) { |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
$applicableTypes = $this->listApplicableRestrictionTypes( $page ); |
| 236 |
|
| 237 |
if ( $action === '' ) { |
| 238 |
foreach ( $applicableTypes as $type ) { |
| 239 |
if ( $this->isProtected( $page, $type ) ) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
} |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
if ( !in_array( $action, $applicableTypes ) ) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
return (bool)array_diff( |
| 251 |
array_intersect( |
| 252 |
$this->getRestrictions( $page, $action ), |
| 253 |
$this->options->get( MainConfigNames::RestrictionLevels ) |
| 254 |
), |
| 255 |
[ '' ] |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public function isCascadeProtected( PageIdentity $page ): bool { |
| 266 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 267 |
|
| 268 |
return $this->getCascadeProtectionSourcesInternal( $page )[0] !== []; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function listApplicableRestrictionTypes( PageIdentity $page ): array { |
| 278 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 279 |
|
| 280 |
if ( !$page->canExist() ) { |
| 281 |
return []; |
| 282 |
} |
| 283 |
|
| 284 |
$types = $this->listAllRestrictionTypes( $page->exists() ); |
| 285 |
|
| 286 |
if ( $page->getNamespace() !== NS_FILE ) { |
| 287 |
|
| 288 |
$types = array_values( array_diff( $types, [ 'upload' ] ) ); |
| 289 |
} |
| 290 |
|
| 291 |
if ( $this->hookContainer->isRegistered( 'TitleGetRestrictionTypes' ) ) { |
| 292 |
$this->hookRunner->onTitleGetRestrictionTypes( |
| 293 |
Title::newFromPageIdentity( $page ), $types ); |
| 294 |
} |
| 295 |
|
| 296 |
return $types; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public function listAllRestrictionTypes( bool $exists = true ): array { |
| 307 |
$types = $this->options->get( MainConfigNames::RestrictionTypes ); |
| 308 |
if ( $exists ) { |
| 309 |
|
| 310 |
return array_values( array_diff( $types, [ 'create' ] ) ); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
return array_values( array_intersect( $types, [ 'create' ] ) ); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public function loadRestrictions( |
| 326 |
PageIdentity $page, int $flags = IDBAccessObject::READ_NORMAL |
| 327 |
): void { |
| 328 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 329 |
|
| 330 |
if ( !$page->canExist() ) { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
$readLatest = DBAccessObjectUtils::hasFlags( $flags, IDBAccessObject::READ_LATEST ); |
| 335 |
|
| 336 |
if ( $this->areRestrictionsLoaded( $page ) && !$readLatest ) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
$cacheEntry = &$this->cache[CacheKeyHelper::getKeyForPage( $page )]; |
| 341 |
|
| 342 |
$cacheEntry['restrictions'] = []; |
| 343 |
|
| 344 |
|
| 345 |
if ( $readLatest ) { |
| 346 |
$page = $this->pageStore->getPageByReference( $page, $flags ) ?? $page; |
| 347 |
} |
| 348 |
$id = $page->getId(); |
| 349 |
if ( $id ) { |
| 350 |
$fname = __METHOD__; |
| 351 |
$loadRestrictionsFromDb = static function ( IReadableDatabase $dbr ) use ( $fname, $id ) { |
| 352 |
return iterator_to_array( |
| 353 |
$dbr->newSelectQueryBuilder() |
| 354 |
->select( [ 'pr_type', 'pr_expiry', 'pr_level', 'pr_cascade' ] ) |
| 355 |
->from( 'page_restrictions' ) |
| 356 |
->where( [ 'pr_page' => $id ] ) |
| 357 |
->caller( $fname )->fetchResultSet() |
| 358 |
); |
| 359 |
}; |
| 360 |
|
| 361 |
if ( $readLatest ) { |
| 362 |
$dbr = $this->loadBalancer->getConnection( DB_PRIMARY ); |
| 363 |
$rows = $loadRestrictionsFromDb( $dbr ); |
| 364 |
} else { |
| 365 |
$this->pageStore->getPageForLink( TitleValue::newFromPage( $page ) )->getId(); |
| 366 |
$latestRev = $this->linkCache->getGoodLinkFieldObj( $page, 'revision' ); |
| 367 |
if ( !$latestRev ) { |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
$rows = []; |
| 372 |
} else { |
| 373 |
$rows = $this->wanCache->getWithSetCallback( |
| 374 |
|
| 375 |
$this->wanCache->makeKey( 'page-restrictions', 'v1', $id, $latestRev ), |
| 376 |
$this->wanCache::TTL_DAY, |
| 377 |
function ( $curValue, &$ttl, array &$setOpts ) use ( $loadRestrictionsFromDb ) { |
| 378 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 379 |
$setOpts += Database::getCacheSetOptions( $dbr ); |
| 380 |
if ( $this->loadBalancer->hasOrMadeRecentPrimaryChanges() ) { |
| 381 |
|
| 382 |
$ttl = WANObjectCache::TTL_UNCACHEABLE; |
| 383 |
} |
| 384 |
|
| 385 |
return $loadRestrictionsFromDb( $dbr ); |
| 386 |
} |
| 387 |
); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
$this->loadRestrictionsFromRows( $page, $rows ); |
| 392 |
} else { |
| 393 |
$titleProtection = $this->getCreateProtectionInternal( $page ); |
| 394 |
|
| 395 |
if ( $titleProtection ) { |
| 396 |
$now = wfTimestampNow(); |
| 397 |
$expiry = $titleProtection['expiry']; |
| 398 |
|
| 399 |
if ( !$expiry || $expiry > $now ) { |
| 400 |
|
| 401 |
$cacheEntry['expiry']['create'] = $expiry ?: null; |
| 402 |
$cacheEntry['restrictions']['create'] = |
| 403 |
explode( ',', trim( $titleProtection['permission'] ) ); |
| 404 |
} else { |
| 405 |
|
| 406 |
$cacheEntry['create_protection'] = null; |
| 407 |
} |
| 408 |
} else { |
| 409 |
$cacheEntry['expiry']['create'] = 'infinity'; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public function loadRestrictionsFromRows( |
| 422 |
PageIdentity $page, array $rows |
| 423 |
): void { |
| 424 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 425 |
|
| 426 |
$cacheEntry = &$this->cache[CacheKeyHelper::getKeyForPage( $page )]; |
| 427 |
|
| 428 |
$restrictionTypes = $this->listApplicableRestrictionTypes( $page ); |
| 429 |
|
| 430 |
foreach ( $restrictionTypes as $type ) { |
| 431 |
$cacheEntry['restrictions'][$type] = []; |
| 432 |
$cacheEntry['expiry'][$type] = 'infinity'; |
| 433 |
} |
| 434 |
|
| 435 |
$cacheEntry['cascade'] = false; |
| 436 |
|
| 437 |
if ( !$rows ) { |
| 438 |
return; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
$now = wfTimestampNow(); |
| 443 |
|
| 444 |
|
| 445 |
foreach ( $rows as $row ) { |
| 446 |
|
| 447 |
if ( !in_array( $row->pr_type, $restrictionTypes ) ) { |
| 448 |
continue; |
| 449 |
} |
| 450 |
|
| 451 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 452 |
$expiry = $dbr->decodeExpiry( $row->pr_expiry ); |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
if ( !$expiry || $expiry > $now ) { |
| 458 |
$cacheEntry['expiry'][$row->pr_type] = $expiry ?: null; |
| 459 |
$cacheEntry['restrictions'][$row->pr_type] |
| 460 |
= explode( ',', trim( $row->pr_level ) ); |
| 461 |
if ( $row->pr_cascade ) { |
| 462 |
$cacheEntry['cascade'] = true; |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
private function getCreateProtectionInternal( PageIdentity $page ): ?array { |
| 479 |
|
| 480 |
if ( !$page->canExist() ) { |
| 481 |
return null; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
if ( $page->exists() ) { |
| 486 |
return null; |
| 487 |
} |
| 488 |
|
| 489 |
$cacheEntry = &$this->cache[CacheKeyHelper::getKeyForPage( $page )]; |
| 490 |
|
| 491 |
if ( !$cacheEntry || !array_key_exists( 'create_protection', $cacheEntry ) ) { |
| 492 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 493 |
$commentQuery = $this->commentStore->getJoin( 'pt_reason' ); |
| 494 |
$row = $dbr->newSelectQueryBuilder() |
| 495 |
->select( [ 'pt_user', 'pt_expiry', 'pt_create_perm' ] ) |
| 496 |
->from( 'protected_titles' ) |
| 497 |
->where( [ 'pt_namespace' => $page->getNamespace(), 'pt_title' => $page->getDBkey() ] ) |
| 498 |
->queryInfo( $commentQuery ) |
| 499 |
->caller( __METHOD__ ) |
| 500 |
->fetchRow(); |
| 501 |
|
| 502 |
if ( $row ) { |
| 503 |
$cacheEntry['create_protection'] = [ |
| 504 |
'user' => $row->pt_user, |
| 505 |
'expiry' => $dbr->decodeExpiry( $row->pt_expiry ), |
| 506 |
'permission' => $row->pt_create_perm, |
| 507 |
'reason' => $this->commentStore->getComment( 'pt_reason', $row )->text, |
| 508 |
]; |
| 509 |
} else { |
| 510 |
$cacheEntry['create_protection'] = null; |
| 511 |
} |
| 512 |
|
| 513 |
} |
| 514 |
|
| 515 |
return $cacheEntry['create_protection']; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public function getCascadeProtectionSources( PageIdentity $page ): array { |
| 531 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 532 |
|
| 533 |
return $this->getCascadeProtectionSourcesInternal( $page ); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
private function getCascadeProtectionSourcesInternal( |
| 543 |
PageIdentity $page |
| 544 |
): array { |
| 545 |
if ( !$page->canExist() ) { |
| 546 |
return [ [], [], [], [] ]; |
| 547 |
} |
| 548 |
|
| 549 |
$cacheEntry = &$this->cache[CacheKeyHelper::getKeyForPage( $page )]; |
| 550 |
|
| 551 |
if ( isset( $cacheEntry['cascade_sources'] ) ) { |
| 552 |
return $cacheEntry['cascade_sources']; |
| 553 |
} |
| 554 |
|
| 555 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 556 |
|
| 557 |
$baseQuery = $dbr->newSelectQueryBuilder() |
| 558 |
->select( [ |
| 559 |
'pr_expiry', |
| 560 |
'pr_page', |
| 561 |
'page_namespace', |
| 562 |
'page_title', |
| 563 |
'pr_type', |
| 564 |
'pr_level' |
| 565 |
] ) |
| 566 |
->from( 'page_restrictions' ) |
| 567 |
->join( 'page', null, 'page_id=pr_page' ) |
| 568 |
->where( [ 'pr_cascade' => 1 ] ); |
| 569 |
|
| 570 |
$imageQuery = clone $baseQuery; |
| 571 |
$imageQuery->join( 'imagelinks', null, 'il_from=pr_page' ) |
| 572 |
->fields( [ |
| 573 |
'type' => $dbr->addQuotes( 'il' ), |
| 574 |
] ) |
| 575 |
->andWhere( [ 'il_to' => $page->getDBkey() ] ); |
| 576 |
|
| 577 |
$templateQuery = clone $baseQuery; |
| 578 |
$templateQuery->join( 'templatelinks', null, 'tl_from=pr_page' ) |
| 579 |
->fields( [ |
| 580 |
'type' => $dbr->addQuotes( 'tl' ), |
| 581 |
] ) |
| 582 |
->andWhere( |
| 583 |
$this->linksMigration->getLinksConditions( 'templatelinks', TitleValue::newFromPage( $page ) ) |
| 584 |
); |
| 585 |
|
| 586 |
if ( $page->getNamespace() === NS_FILE ) { |
| 587 |
$unionQuery = $dbr->newUnionQueryBuilder() |
| 588 |
->add( $imageQuery ) |
| 589 |
->add( $templateQuery ) |
| 590 |
->all(); |
| 591 |
|
| 592 |
$res = $unionQuery->caller( __METHOD__ )->fetchResultSet(); |
| 593 |
} else { |
| 594 |
$res = $templateQuery->caller( __METHOD__ )->fetchResultSet(); |
| 595 |
} |
| 596 |
|
| 597 |
$tlSources = []; |
| 598 |
$ilSources = []; |
| 599 |
$pageRestrictions = []; |
| 600 |
$now = wfTimestampNow(); |
| 601 |
foreach ( $res as $row ) { |
| 602 |
$expiry = $dbr->decodeExpiry( $row->pr_expiry ); |
| 603 |
if ( $expiry > $now ) { |
| 604 |
if ( $row->type === 'il' ) { |
| 605 |
$ilSources[$row->pr_page] = new PageIdentityValue( $row->pr_page, |
| 606 |
$row->page_namespace, $row->page_title, PageIdentity::LOCAL ); |
| 607 |
} elseif ( $row->type === 'tl' ) { |
| 608 |
$tlSources[$row->pr_page] = new PageIdentityValue( $row->pr_page, |
| 609 |
$row->page_namespace, $row->page_title, PageIdentity::LOCAL ); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
if ( !isset( $pageRestrictions[$row->pr_type] ) ) { |
| 616 |
$pageRestrictions[$row->pr_type] = []; |
| 617 |
} |
| 618 |
|
| 619 |
if ( !in_array( $row->pr_level, $pageRestrictions[$row->pr_type] ) ) { |
| 620 |
$pageRestrictions[$row->pr_type][] = $row->pr_level; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
$sources = array_replace( $tlSources, $ilSources ); |
| 626 |
|
| 627 |
$cacheEntry['cascade_sources'] = [ $sources, $pageRestrictions, $tlSources, $ilSources ]; |
| 628 |
|
| 629 |
return $cacheEntry['cascade_sources']; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
public function areRestrictionsLoaded( PageIdentity $page ): bool { |
| 638 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 639 |
|
| 640 |
return isset( $this->cache[CacheKeyHelper::getKeyForPage( $page )]['restrictions'] ); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public function areCascadeProtectionSourcesLoaded( PageIdentity $page ): bool { |
| 650 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 651 |
|
| 652 |
return isset( $this->cache[CacheKeyHelper::getKeyForPage( $page )]['cascade_sources'] ); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
public function areRestrictionsCascading( PageIdentity $page ): bool { |
| 662 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 663 |
|
| 664 |
if ( !$this->areRestrictionsLoaded( $page ) ) { |
| 665 |
$this->loadRestrictions( $page ); |
| 666 |
} |
| 667 |
return $this->cache[CacheKeyHelper::getKeyForPage( $page )]['cascade'] ?? false; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
public function flushRestrictions( PageIdentity $page ): void { |
| 678 |
$page->assertWiki( PageIdentity::LOCAL ); |
| 679 |
|
| 680 |
unset( $this->cache[CacheKeyHelper::getKeyForPage( $page )] ); |
| 681 |
} |
| 682 |
|
| 683 |
} |