| 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\Deferred\LinksUpdate; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use Job; |
| 27 |
use MediaWiki\Cache\BacklinkCache; |
| 28 |
use MediaWiki\Deferred\AutoCommitUpdate; |
| 29 |
use MediaWiki\Deferred\DataUpdate; |
| 30 |
use MediaWiki\Deferred\DeferredUpdates; |
| 31 |
use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 32 |
use MediaWiki\Logger\LoggerFactory; |
| 33 |
use MediaWiki\MainConfigNames; |
| 34 |
use MediaWiki\MediaWikiServices; |
| 35 |
use MediaWiki\Page\PageIdentity; |
| 36 |
use MediaWiki\Page\PageReference; |
| 37 |
use MediaWiki\Page\PageReferenceValue; |
| 38 |
use MediaWiki\Parser\ParserOutput; |
| 39 |
use MediaWiki\Revision\RevisionRecord; |
| 40 |
use MediaWiki\Title\Title; |
| 41 |
use MediaWiki\User\UserIdentity; |
| 42 |
use RefreshLinksJob; |
| 43 |
use RuntimeException; |
| 44 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 45 |
use Wikimedia\Rdbms\IDatabase; |
| 46 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 47 |
use Wikimedia\ScopedCallback; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class LinksUpdate extends DataUpdate { |
| 57 |
use ProtectedHookAccessorTrait; |
| 58 |
|
| 59 |
|
| 60 |
protected $mId; |
| 61 |
|
| 62 |
|
| 63 |
protected $mTitle; |
| 64 |
|
| 65 |
|
| 66 |
protected $mParserOutput; |
| 67 |
|
| 68 |
|
| 69 |
protected $mRecursive; |
| 70 |
|
| 71 |
|
| 72 |
protected $mMaybeRedirectChanged; |
| 73 |
|
| 74 |
|
| 75 |
private $mRevisionRecord; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private $user; |
| 81 |
|
| 82 |
|
| 83 |
private $db; |
| 84 |
|
| 85 |
|
| 86 |
private $tableFactory; |
| 87 |
|
| 88 |
private IConnectionProvider $connectionProvider; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function __construct( |
| 98 |
PageIdentity $page, |
| 99 |
ParserOutput $parserOutput, |
| 100 |
$recursive = true, |
| 101 |
$maybeRedirectChanged = true |
| 102 |
) { |
| 103 |
parent::__construct(); |
| 104 |
|
| 105 |
$this->mTitle = Title::newFromPageIdentity( $page ); |
| 106 |
$this->mParserOutput = $parserOutput; |
| 107 |
$this->mRecursive = $recursive; |
| 108 |
$this->mMaybeRedirectChanged = $maybeRedirectChanged; |
| 109 |
|
| 110 |
$services = MediaWikiServices::getInstance(); |
| 111 |
$config = $services->getMainConfig(); |
| 112 |
$this->tableFactory = new LinksTableGroup( |
| 113 |
$services->getObjectFactory(), |
| 114 |
$services->getDBLoadBalancerFactory(), |
| 115 |
$services->getCollationFactory(), |
| 116 |
$page, |
| 117 |
$services->getLinkTargetLookup(), |
| 118 |
$config->get( MainConfigNames::UpdateRowsPerQuery ), |
| 119 |
$config->get( MainConfigNames::TempCategoryCollations ) |
| 120 |
); |
| 121 |
|
| 122 |
$this->tableFactory->setParserOutput( $parserOutput ); |
| 123 |
$this->connectionProvider = $services->getDBLoadBalancerFactory(); |
| 124 |
} |
| 125 |
|
| 126 |
public function setTransactionTicket( $ticket ) { |
| 127 |
parent::setTransactionTicket( $ticket ); |
| 128 |
$this->tableFactory->setTransactionTicket( $ticket ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function setMoveDetails( PageReference $oldPage ) { |
| 138 |
$this->tableFactory->setMoveDetails( $oldPage ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function doUpdate() { |
| 147 |
if ( !$this->mId ) { |
| 148 |
|
| 149 |
$this->mId = $this->mTitle->getArticleID( IDBAccessObject::READ_LATEST ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( !$this->mId ) { |
| 153 |
|
| 154 |
$logger = LoggerFactory::getInstance( 'SecondaryDataUpdate' ); |
| 155 |
$logger->warning( |
| 156 |
'LinksUpdate: The Title object yields no ID. Perhaps the page was deleted?', |
| 157 |
[ |
| 158 |
'page_title' => $this->mTitle->getPrefixedDBkey(), |
| 159 |
'cause_action' => $this->getCauseAction(), |
| 160 |
'cause_agent' => $this->getCauseAgent() |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
|
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
foreach ( $this->tableFactory->getAll() as $table ) { |
| 173 |
$table->beforeLock(); |
| 174 |
} |
| 175 |
|
| 176 |
if ( $this->ticket ) { |
| 177 |
|
| 178 |
|
| 179 |
$scopedLock = self::acquirePageLock( $this->getDB(), $this->mId ); |
| 180 |
if ( !$scopedLock ) { |
| 181 |
throw new RuntimeException( "Could not acquire lock for page ID '{$this->mId}'." ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$this->getHookRunner()->onLinksUpdate( $this ); |
| 186 |
$this->doIncrementalUpdate(); |
| 187 |
|
| 188 |
|
| 189 |
ScopedCallback::consume( $scopedLock ); |
| 190 |
|
| 191 |
DeferredUpdates::addUpdate( new AutoCommitUpdate( |
| 192 |
$this->getDB(), |
| 193 |
__METHOD__, |
| 194 |
function () { |
| 195 |
$this->getHookRunner()->onLinksUpdateComplete( $this, $this->ticket ); |
| 196 |
} |
| 197 |
) ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public static function acquirePageLock( IDatabase $dbw, $pageId, $why = 'atomicity' ) { |
| 210 |
$key = "{$dbw->getDomainID()}:LinksUpdate:$why:pageid:$pageId"; |
| 211 |
$scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__, 15 ); |
| 212 |
if ( !$scopedLock ) { |
| 213 |
$logger = LoggerFactory::getInstance( 'SecondaryDataUpdate' ); |
| 214 |
$logger->info( "Could not acquire lock '{key}' for page ID '{page_id}'.", [ |
| 215 |
'key' => $key, |
| 216 |
'page_id' => $pageId, |
| 217 |
] ); |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
return $scopedLock; |
| 222 |
} |
| 223 |
|
| 224 |
protected function doIncrementalUpdate() { |
| 225 |
foreach ( $this->tableFactory->getAll() as $table ) { |
| 226 |
$table->update(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if ( $this->mRecursive ) { |
| 232 |
$this->queueRecursiveJobs(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
$this->updateLinksTimestamp(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
protected function queueRecursiveJobs() { |
| 246 |
$services = MediaWikiServices::getInstance(); |
| 247 |
$backlinkCache = $services->getBacklinkCacheFactory() |
| 248 |
->getBacklinkCache( $this->mTitle ); |
| 249 |
$action = $this->getCauseAction(); |
| 250 |
$agent = $this->getCauseAgent(); |
| 251 |
|
| 252 |
self::queueRecursiveJobsForTable( |
| 253 |
$this->mTitle, 'templatelinks', $action, $agent, $backlinkCache |
| 254 |
); |
| 255 |
if ( $this->mMaybeRedirectChanged && $this->mTitle->getNamespace() === NS_FILE ) { |
| 256 |
|
| 257 |
self::queueRecursiveJobsForTable( |
| 258 |
$this->mTitle, 'imagelinks', $action, $agent, $backlinkCache |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
$jobs = []; |
| 268 |
foreach ( $backlinkCache->getCascadeProtectedLinkPages() as $page ) { |
| 269 |
$jobs[] = RefreshLinksJob::newPrioritized( |
| 270 |
$page, |
| 271 |
[ |
| 272 |
'causeAction' => $action, |
| 273 |
'causeAgent' => $agent |
| 274 |
] |
| 275 |
); |
| 276 |
} |
| 277 |
$services->getJobQueueGroup()->push( $jobs ); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public static function queueRecursiveJobsForTable( |
| 290 |
PageIdentity $page, $table, $action = 'LinksUpdate', $userName = 'unknown', ?BacklinkCache $backlinkCache = null |
| 291 |
) { |
| 292 |
$title = Title::newFromPageIdentity( $page ); |
| 293 |
if ( !$backlinkCache ) { |
| 294 |
wfDeprecatedMsg( __METHOD__ . " needs a BacklinkCache object, null passed", '1.37' ); |
| 295 |
$backlinkCache = MediaWikiServices::getInstance()->getBacklinkCacheFactory() |
| 296 |
->getBacklinkCache( $title ); |
| 297 |
} |
| 298 |
if ( $backlinkCache->hasLinks( $table ) ) { |
| 299 |
$job = new RefreshLinksJob( |
| 300 |
$title, |
| 301 |
[ |
| 302 |
'table' => $table, |
| 303 |
'recursive' => true, |
| 304 |
] + Job::newRootJobParams( |
| 305 |
"refreshlinks:{$table}:{$title->getPrefixedText()}" |
| 306 |
) + [ 'causeAction' => $action, 'causeAgent' => $userName ] |
| 307 |
); |
| 308 |
|
| 309 |
MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function setStrictTestMode( $mode = true ) { |
| 320 |
$this->tableFactory->setStrictTestMode( $mode ); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function getTitle() { |
| 328 |
return $this->mTitle; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function getPageId() { |
| 338 |
if ( $this->mId ) { |
| 339 |
return $this->mId; |
| 340 |
} else { |
| 341 |
return $this->mTitle->getArticleID(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function getParserOutput() { |
| 351 |
return $this->mParserOutput; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function getImages() { |
| 359 |
return $this->getParserOutput()->getImages(); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function setRevisionRecord( RevisionRecord $revisionRecord ) { |
| 369 |
$this->mRevisionRecord = $revisionRecord; |
| 370 |
$this->tableFactory->setRevision( $revisionRecord ); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function getRevisionRecord() { |
| 378 |
return $this->mRevisionRecord; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public function setTriggeringUser( UserIdentity $user ) { |
| 388 |
$this->user = $user; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public function getTriggeringUser(): ?UserIdentity { |
| 398 |
return $this->user; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
protected function getPageLinksTable(): PageLinksTable { |
| 405 |
|
| 406 |
return $this->tableFactory->get( 'pagelinks' ); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
protected function getExternalLinksTable(): ExternalLinksTable { |
| 413 |
|
| 414 |
return $this->tableFactory->get( 'externallinks' ); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
protected function getPagePropsTable(): PagePropsTable { |
| 421 |
|
| 422 |
return $this->tableFactory->get( 'page_props' ); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function getAddedLinks() { |
| 433 |
wfDeprecated( __METHOD__, '1.43' ); |
| 434 |
return $this->getPageLinksTable()->getTitleArray( LinksTable::INSERTED ); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public function getRemovedLinks() { |
| 445 |
wfDeprecated( __METHOD__, '1.43' ); |
| 446 |
return $this->getPageLinksTable()->getTitleArray( LinksTable::DELETED ); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public function getAddedExternalLinks() { |
| 456 |
return $this->getExternalLinksTable()->getStringArray( LinksTable::INSERTED ); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public function getRemovedExternalLinks() { |
| 466 |
return $this->getExternalLinksTable()->getStringArray( LinksTable::DELETED ); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public function getAddedProperties() { |
| 476 |
return $this->getPagePropsTable()->getAssocArray( LinksTable::INSERTED ); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public function getRemovedProperties() { |
| 486 |
return $this->getPagePropsTable()->getAssocArray( LinksTable::DELETED ); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public function getPageReferenceIterator( $tableName, $setType ) { |
| 505 |
$table = $this->tableFactory->get( $tableName ); |
| 506 |
if ( $table instanceof TitleLinksTable ) { |
| 507 |
return $table->getPageReferenceIterator( $setType ); |
| 508 |
} else { |
| 509 |
throw new InvalidArgumentException( |
| 510 |
__METHOD__ . ": $tableName does not have a list of titles" ); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public function getPageReferenceArray( $tableName, $setType ) { |
| 524 |
return iterator_to_array( $this->getPageReferenceIterator( $tableName, $setType ) ); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
protected function updateLinksTimestamp() { |
| 531 |
if ( $this->mId ) { |
| 532 |
|
| 533 |
$timestamp = $this->mParserOutput->getCacheTime(); |
| 534 |
$this->getDB()->newUpdateQueryBuilder() |
| 535 |
->update( 'page' ) |
| 536 |
->set( [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ] ) |
| 537 |
->where( [ 'page_id' => $this->mId ] ) |
| 538 |
->caller( __METHOD__ )->execute(); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
protected function getDB() { |
| 546 |
if ( !$this->db ) { |
| 547 |
$this->db = $this->connectionProvider->getPrimaryDatabase(); |
| 548 |
} |
| 549 |
|
| 550 |
return $this->db; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public function isRecursive() { |
| 560 |
return $this->mRecursive; |
| 561 |
} |
| 562 |
} |