| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
s |
| 24 |
m |
| 25 |
o |
| 26 |
|
| 27 |
|
| 28 |
namespace MediaWiki\Cache; |
| 29 |
|
| 30 |
use Iterator; |
| 31 |
use LogicException; |
| 32 |
use MediaWiki\Config\ServiceOptions; |
| 33 |
use MediaWiki\HookContainer\HookContainer; |
| 34 |
use MediaWiki\HookContainer\HookRunner; |
| 35 |
use MediaWiki\Linker\LinksMigration; |
| 36 |
use MediaWiki\MainConfigNames; |
| 37 |
use MediaWiki\Page\PageIdentity; |
| 38 |
use MediaWiki\Page\PageIdentityValue; |
| 39 |
use MediaWiki\Page\PageReference; |
| 40 |
use MediaWiki\Title\Title; |
| 41 |
use MediaWiki\Title\TitleValue; |
| 42 |
use Psr\Log\LoggerInterface; |
| 43 |
use RuntimeException; |
| 44 |
use stdClass; |
| 45 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 46 |
use Wikimedia\Rdbms\Database; |
| 47 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 48 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 49 |
use Wikimedia\Rdbms\IResultWrapper; |
| 50 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
class BacklinkCache { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public const CONSTRUCTOR_OPTIONS = [ |
| 68 |
MainConfigNames::UpdateRowsPerJob, |
| 69 |
]; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private $partitionCache = []; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private $fullResultCache = []; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private $hasLinksCache = []; |
| 99 |
|
| 100 |
|
| 101 |
private $wanCache; |
| 102 |
|
| 103 |
|
| 104 |
private $hookRunner; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private $page; |
| 111 |
|
| 112 |
private const CACHE_EXPIRY = 3600; |
| 113 |
private IConnectionProvider $dbProvider; |
| 114 |
private ServiceOptions $options; |
| 115 |
private LinksMigration $linksMigration; |
| 116 |
private LoggerInterface $logger; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function __construct( |
| 130 |
ServiceOptions $options, |
| 131 |
LinksMigration $linksMigration, |
| 132 |
WANObjectCache $wanCache, |
| 133 |
HookContainer $hookContainer, |
| 134 |
IConnectionProvider $dbProvider, |
| 135 |
LoggerInterface $logger, |
| 136 |
PageReference $page |
| 137 |
) { |
| 138 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 139 |
$this->options = $options; |
| 140 |
$this->linksMigration = $linksMigration; |
| 141 |
$this->wanCache = $wanCache; |
| 142 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 143 |
$this->dbProvider = $dbProvider; |
| 144 |
$this->logger = $logger; |
| 145 |
$this->page = $page; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function getPage(): PageReference { |
| 153 |
return $this->page; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private function getDB() { |
| 162 |
return $this->dbProvider->getReplicaDatabase(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function getLinkPages( |
| 175 |
string $table, $startId = false, $endId = false, $max = INF |
| 176 |
): Iterator { |
| 177 |
$i = 0; |
| 178 |
foreach ( $this->queryLinks( $table, $startId, $endId, $max ) as $row ) { |
| 179 |
yield PageIdentityValue::localIdentity( |
| 180 |
$row->page_id, $row->page_namespace, $row->page_title ); |
| 181 |
|
| 182 |
|
| 183 |
if ( is_finite( $max ) && ++$i >= $max ) { |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
private function queryLinks( $table, $startId, $endId, $max, $select = 'all' ) { |
| 201 |
if ( !$startId && !$endId && isset( $this->fullResultCache[$table] ) ) { |
| 202 |
$this->logger->debug( __METHOD__ . ': got results from cache' ); |
| 203 |
return $this->fullResultCache[$table]; |
| 204 |
} |
| 205 |
|
| 206 |
$this->logger->debug( __METHOD__ . ': got results from DB' ); |
| 207 |
$queryBuilder = $this->initQueryBuilderForTable( $table, $select ); |
| 208 |
$fromField = $this->getPrefix( $table ) . '_from'; |
| 209 |
|
| 210 |
|
| 211 |
if ( $startId ) { |
| 212 |
$queryBuilder->where( |
| 213 |
$this->getDB()->expr( $fromField, '>=', $startId ) |
| 214 |
); |
| 215 |
} |
| 216 |
if ( $endId ) { |
| 217 |
$queryBuilder->where( |
| 218 |
$this->getDB()->expr( $fromField, '<=', $endId ) |
| 219 |
); |
| 220 |
} |
| 221 |
$queryBuilder->orderBy( $fromField ); |
| 222 |
if ( is_finite( $max ) && $max > 0 ) { |
| 223 |
$queryBuilder->limit( $max ); |
| 224 |
} |
| 225 |
|
| 226 |
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 227 |
|
| 228 |
if ( $select === 'all' && !$startId && !$endId && $res->numRows() < $max ) { |
| 229 |
|
| 230 |
$this->fullResultCache[$table] = $res; |
| 231 |
} else { |
| 232 |
$this->logger->debug( __METHOD__ . ": results from DB were uncacheable" ); |
| 233 |
} |
| 234 |
|
| 235 |
return $res; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private function getPrefix( $table ) { |
| 244 |
static $prefixes = [ |
| 245 |
'pagelinks' => 'pl', |
| 246 |
'imagelinks' => 'il', |
| 247 |
'categorylinks' => 'cl', |
| 248 |
'templatelinks' => 'tl', |
| 249 |
'redirect' => 'rd', |
| 250 |
]; |
| 251 |
|
| 252 |
if ( isset( $prefixes[$table] ) ) { |
| 253 |
return $prefixes[$table]; |
| 254 |
} else { |
| 255 |
$prefix = null; |
| 256 |
$this->hookRunner->onBacklinkCacheGetPrefix( $table, $prefix ); |
| 257 |
if ( $prefix ) { |
| 258 |
return $prefix; |
| 259 |
} else { |
| 260 |
throw new LogicException( "Invalid table \"$table\" in " . __CLASS__ ); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private function initQueryBuilderForTable( string $table, string $select ): SelectQueryBuilder { |
| 274 |
$prefix = $this->getPrefix( $table ); |
| 275 |
$queryBuilder = $this->getDB()->newSelectQueryBuilder(); |
| 276 |
$joinPageTable = $select !== 'ids'; |
| 277 |
|
| 278 |
if ( $select === 'ids' ) { |
| 279 |
$queryBuilder->select( [ 'page_id' => $prefix . '_from' ] ); |
| 280 |
} else { |
| 281 |
$queryBuilder->select( [ 'page_namespace', 'page_title', 'page_id' ] ); |
| 282 |
} |
| 283 |
$queryBuilder->from( $table ); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
$knownTable = true; |
| 294 |
|
| 295 |
switch ( $table ) { |
| 296 |
case 'pagelinks': |
| 297 |
case 'templatelinks': |
| 298 |
$queryBuilder->where( |
| 299 |
$this->linksMigration->getLinksConditions( $table, TitleValue::newFromPage( $this->page ) ) |
| 300 |
); |
| 301 |
break; |
| 302 |
case 'redirect': |
| 303 |
$queryBuilder->where( [ |
| 304 |
"{$prefix}_namespace" => $this->page->getNamespace(), |
| 305 |
"{$prefix}_title" => $this->page->getDBkey(), |
| 306 |
"{$prefix}_interwiki" => [ '', null ], |
| 307 |
] ); |
| 308 |
break; |
| 309 |
case 'imagelinks': |
| 310 |
case 'categorylinks': |
| 311 |
$queryBuilder->where( [ |
| 312 |
"{$prefix}_to" => $this->page->getDBkey(), |
| 313 |
] ); |
| 314 |
break; |
| 315 |
default: |
| 316 |
$knownTable = false; |
| 317 |
$conds = null; |
| 318 |
$this->hookRunner->onBacklinkCacheGetConditions( $table, |
| 319 |
Title::newFromPageReference( $this->page ), |
| 320 |
$conds |
| 321 |
); |
| 322 |
if ( !$conds ) { |
| 323 |
throw new LogicException( "Invalid table \"$table\" in " . __CLASS__ ); |
| 324 |
} |
| 325 |
if ( $joinPageTable ) { |
| 326 |
$queryBuilder->table( 'page' ); |
| 327 |
} else { |
| 328 |
|
| 329 |
$conds = array_filter( (array)$conds, static function ( $clause ) { |
| 330 |
return !preg_match( '/(\b|=)page_id(\b|=)/', (string)$clause ); |
| 331 |
} ); |
| 332 |
} |
| 333 |
$queryBuilder->where( $conds ); |
| 334 |
break; |
| 335 |
} |
| 336 |
|
| 337 |
if ( $knownTable && $joinPageTable ) { |
| 338 |
$queryBuilder->join( 'page', null, "page_id={$prefix}_from" ); |
| 339 |
} |
| 340 |
if ( $joinPageTable ) { |
| 341 |
$queryBuilder->straightJoinOption(); |
| 342 |
} |
| 343 |
|
| 344 |
return $queryBuilder; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function hasLinks( $table ) { |
| 355 |
if ( isset( $this->hasLinksCache[$table] ) ) { |
| 356 |
return $this->hasLinksCache[$table]; |
| 357 |
} |
| 358 |
if ( isset( $this->partitionCache[$table] ) ) { |
| 359 |
$entry = reset( $this->partitionCache[$table] ); |
| 360 |
return (bool)$entry['numRows']; |
| 361 |
} |
| 362 |
if ( isset( $this->fullResultCache[$table] ) ) { |
| 363 |
return (bool)$this->fullResultCache[$table]->numRows(); |
| 364 |
} |
| 365 |
$hasLinks = (bool)$this->queryLinks( $table, false, false, 1 )->numRows(); |
| 366 |
$this->hasLinksCache[$table] = $hasLinks; |
| 367 |
return $hasLinks; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function getNumLinks( $table ) { |
| 376 |
if ( isset( $this->partitionCache[$table] ) ) { |
| 377 |
$entry = reset( $this->partitionCache[$table] ); |
| 378 |
return $entry['numRows']; |
| 379 |
} |
| 380 |
|
| 381 |
if ( isset( $this->fullResultCache[$table] ) ) { |
| 382 |
return $this->fullResultCache[$table]->numRows(); |
| 383 |
} |
| 384 |
|
| 385 |
return $this->wanCache->getWithSetCallback( |
| 386 |
$this->wanCache->makeKey( |
| 387 |
'numbacklinks', |
| 388 |
CacheKeyHelper::getKeyForPage( $this->page ), |
| 389 |
$table |
| 390 |
), |
| 391 |
self::CACHE_EXPIRY, |
| 392 |
function ( $oldValue, &$ttl, array &$setOpts ) use ( $table ) { |
| 393 |
$setOpts += Database::getCacheSetOptions( $this->getDB() ); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
$batchSize = $this->options->get( MainConfigNames::UpdateRowsPerJob ); |
| 398 |
$this->partition( $table, $batchSize ); |
| 399 |
return $this->partitionCache[$table][$batchSize]['numRows']; |
| 400 |
} |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public function partition( $table, $batchSize ) { |
| 414 |
if ( isset( $this->partitionCache[$table][$batchSize] ) ) { |
| 415 |
$this->logger->debug( __METHOD__ . ": got from partition cache" ); |
| 416 |
|
| 417 |
return $this->partitionCache[$table][$batchSize]['batches']; |
| 418 |
} |
| 419 |
|
| 420 |
$this->partitionCache[$table][$batchSize] = false; |
| 421 |
$cacheEntry =& $this->partitionCache[$table][$batchSize]; |
| 422 |
|
| 423 |
if ( isset( $this->fullResultCache[$table] ) ) { |
| 424 |
$res = $this->fullResultCache[$table]; |
| 425 |
$numRows = $res->numRows(); |
| 426 |
$batches = $this->partitionResult( $res, $numRows, $batchSize ); |
| 427 |
$this->openBatchEnds( $batches ); |
| 428 |
$cacheEntry = [ 'numRows' => $numRows, 'batches' => $batches ]; |
| 429 |
$this->logger->debug( __METHOD__ . ": got from full result cache" ); |
| 430 |
|
| 431 |
return $cacheEntry['batches']; |
| 432 |
} |
| 433 |
|
| 434 |
$cacheEntry = $this->wanCache->getWithSetCallback( |
| 435 |
$this->wanCache->makeKey( |
| 436 |
'backlinks', |
| 437 |
CacheKeyHelper::getKeyForPage( $this->page ), |
| 438 |
$table, |
| 439 |
$batchSize |
| 440 |
), |
| 441 |
self::CACHE_EXPIRY, |
| 442 |
function ( $oldValue, &$ttl, array &$setOpts ) use ( $table, $batchSize ) { |
| 443 |
$setOpts += Database::getCacheSetOptions( $this->getDB() ); |
| 444 |
|
| 445 |
$value = [ 'numRows' => 0, 'batches' => [] ]; |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
$selectSize = max( $batchSize, 200_000 - ( 200_000 % $batchSize ) ); |
| 450 |
$start = false; |
| 451 |
do { |
| 452 |
$res = $this->queryLinks( $table, $start, false, $selectSize, 'ids' ); |
| 453 |
$numRows = $res->numRows(); |
| 454 |
$batches = $this->partitionResult( $res, $numRows, $batchSize ); |
| 455 |
|
| 456 |
$value['numRows'] += $numRows; |
| 457 |
$value['batches'] = array_merge( $value['batches'], $batches ); |
| 458 |
if ( count( $batches ) ) { |
| 459 |
|
| 460 |
$start = end( $batches )[1] + 1; |
| 461 |
} |
| 462 |
} while ( $numRows >= $selectSize ); |
| 463 |
|
| 464 |
$this->openBatchEnds( $value['batches'] ); |
| 465 |
|
| 466 |
return $value; |
| 467 |
} |
| 468 |
); |
| 469 |
|
| 470 |
return $cacheEntry['batches']; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
private function openBatchEnds( array &$batches ) { |
| 481 |
if ( !count( $batches ) ) { |
| 482 |
$batches = [ [ false, false ] ]; |
| 483 |
} else { |
| 484 |
$batches[0][0] = false; |
| 485 |
$batches[ array_key_last( $batches ) ][1] = false; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
private function partitionResult( $res, $numRows, $batchSize ) { |
| 497 |
$numBatches = ceil( $numRows / $batchSize ); |
| 498 |
$batches = []; |
| 499 |
for ( $i = 0; $i < $numBatches; $i++ ) { |
| 500 |
$rowNum = $i * $batchSize; |
| 501 |
$res->seek( $rowNum ); |
| 502 |
$row = $res->fetchObject(); |
| 503 |
$start = (int)$row->page_id; |
| 504 |
|
| 505 |
$rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 ); |
| 506 |
$res->seek( $rowNum ); |
| 507 |
$row = $res->fetchObject(); |
| 508 |
$end = (int)$row->page_id; |
| 509 |
|
| 510 |
|
| 511 |
if ( $start && $end && $start > $end ) { |
| 512 |
throw new RuntimeException( __METHOD__ . ': Internal error: query result out of order' ); |
| 513 |
} |
| 514 |
|
| 515 |
$batches[] = [ $start, $end ]; |
| 516 |
} |
| 517 |
|
| 518 |
return $batches; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
public function getCascadeProtectedLinkPages(): Iterator { |
| 528 |
foreach ( $this->getCascadeProtectedLinksInternal() as $row ) { |
| 529 |
yield PageIdentityValue::localIdentity( |
| 530 |
$row->page_id, $row->page_namespace, $row->page_title ); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
private function getCascadeProtectedLinksInternal(): array { |
| 540 |
$dbr = $this->getDB(); |
| 541 |
|
| 542 |
|
| 543 |
$resSets = []; |
| 544 |
$linkConds = $this->linksMigration->getLinksConditions( |
| 545 |
'templatelinks', TitleValue::newFromPage( $this->page ) |
| 546 |
); |
| 547 |
$resSets[] = $dbr->newSelectQueryBuilder() |
| 548 |
->select( [ 'page_namespace', 'page_title', 'page_id' ] ) |
| 549 |
->from( 'templatelinks' ) |
| 550 |
->join( 'page_restrictions', null, 'tl_from = pr_page' ) |
| 551 |
->join( 'page', null, 'page_id = tl_from' ) |
| 552 |
->where( $linkConds ) |
| 553 |
->andWhere( [ 'pr_cascade' => 1 ] ) |
| 554 |
->distinct() |
| 555 |
->caller( __METHOD__ )->fetchResultSet(); |
| 556 |
if ( $this->page->getNamespace() === NS_FILE ) { |
| 557 |
$resSets[] = $dbr->newSelectQueryBuilder() |
| 558 |
->select( [ 'page_namespace', 'page_title', 'page_id' ] ) |
| 559 |
->from( 'imagelinks' ) |
| 560 |
->join( 'page_restrictions', null, 'il_from = pr_page' ) |
| 561 |
->join( 'page', null, 'page_id = il_from' ) |
| 562 |
->where( [ |
| 563 |
'il_to' => $this->page->getDBkey(), |
| 564 |
'pr_cascade' => 1, |
| 565 |
] ) |
| 566 |
->distinct() |
| 567 |
->caller( __METHOD__ )->fetchResultSet(); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
$mergedRes = []; |
| 572 |
foreach ( $resSets as $res ) { |
| 573 |
foreach ( $res as $row ) { |
| 574 |
|
| 575 |
$mergedRes[$row->page_id] = $row; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
return array_values( $mergedRes ); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
class_alias( BacklinkCache::class, 'BacklinkCache' ); |