| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Deferred\LinksUpdate; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Linker\LinkTargetLookup; |
| 7 |
use MediaWiki\Page\PageIdentity; |
| 8 |
use MediaWiki\Page\PageReference; |
| 9 |
use MediaWiki\Parser\ParserOutput; |
| 10 |
use MediaWiki\Revision\RevisionRecord; |
| 11 |
use Wikimedia\Rdbms\IDatabase; |
| 12 |
use Wikimedia\Rdbms\IResultWrapper; |
| 13 |
use Wikimedia\Rdbms\LBFactory; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
abstract class LinksTable { |
| 43 |
|
| 44 |
public const INSERTED = 1; |
| 45 |
|
| 46 |
|
| 47 |
public const DELETED = 2; |
| 48 |
|
| 49 |
|
| 50 |
public const CHANGED = 3; |
| 51 |
|
| 52 |
|
| 53 |
public const OLD = 4; |
| 54 |
|
| 55 |
|
| 56 |
public const NEW = 5; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected $rowsToDelete = []; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
protected $rowsToInsert = []; |
| 74 |
|
| 75 |
|
| 76 |
protected $insertedLinks = []; |
| 77 |
|
| 78 |
|
| 79 |
protected $deletedLinks = []; |
| 80 |
|
| 81 |
|
| 82 |
private $lbFactory; |
| 83 |
|
| 84 |
|
| 85 |
protected $linkTargetLookup; |
| 86 |
|
| 87 |
|
| 88 |
private $db; |
| 89 |
|
| 90 |
|
| 91 |
private $sourcePage; |
| 92 |
|
| 93 |
|
| 94 |
private $movedPage; |
| 95 |
|
| 96 |
|
| 97 |
private $batchSize; |
| 98 |
|
| 99 |
|
| 100 |
private $ticket; |
| 101 |
|
| 102 |
|
| 103 |
private $revision; |
| 104 |
|
| 105 |
|
| 106 |
protected $strictTestMode; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
final public function injectBaseDependencies( |
| 119 |
LBFactory $lbFactory, |
| 120 |
LinkTargetLookup $linkTargetLookup, |
| 121 |
PageIdentity $sourcePage, |
| 122 |
$batchSize |
| 123 |
) { |
| 124 |
$this->lbFactory = $lbFactory; |
| 125 |
$this->db = $this->lbFactory->getPrimaryDatabase(); |
| 126 |
$this->sourcePage = $sourcePage; |
| 127 |
$this->batchSize = $batchSize; |
| 128 |
$this->linkTargetLookup = $linkTargetLookup; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function setTransactionTicket( $ticket ) { |
| 137 |
$this->ticket = $ticket; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function setRevision( RevisionRecord $revision ) { |
| 146 |
$this->revision = $revision; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function setMoveDetails( PageReference $movedPage ) { |
| 156 |
$this->movedPage = $movedPage; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
abstract public function setParserOutput( ParserOutput $parserOutput ); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
abstract protected function getTableName(); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
abstract protected function getFromField(); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
abstract protected function getExistingFields(); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
abstract protected function getNewLinkIDs(); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
abstract protected function getExistingLinkIDs(); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
abstract protected function isExisting( $linkId ); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
abstract protected function isInNewSet( $linkId ); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
abstract protected function insertLink( $linkId ); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
abstract protected function deleteLink( $linkId ); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
protected function needForcedLinkRefresh() { |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
protected function getDB(): IDatabase { |
| 262 |
return $this->db; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
protected function getLBFactory(): LBFactory { |
| 269 |
return $this->lbFactory; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
protected function getSourcePageId(): int { |
| 278 |
return $this->sourcePage->getId(); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
protected function getSourcePage(): PageIdentity { |
| 288 |
return $this->sourcePage; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
protected function isMove() { |
| 297 |
return $this->movedPage !== null; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
protected function isCrossNamespaceMove() { |
| 306 |
return $this->movedPage !== null |
| 307 |
&& $this->sourcePage->getNamespace() !== $this->movedPage->getNamespace(); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
protected function getMovedPage(): PageReference { |
| 317 |
return $this->movedPage; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
protected function getBatchSize(): int { |
| 326 |
return $this->batchSize; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
protected function getTransactionTicket() { |
| 335 |
return $this->ticket; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
protected function getRevision(): ?RevisionRecord { |
| 345 |
return $this->revision; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
protected function getFromConds() { |
| 355 |
return [ $this->getFromField() => $this->getSourcePageId() ]; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
protected function fetchExistingRows(): IResultWrapper { |
| 365 |
return $this->getDB()->newSelectQueryBuilder() |
| 366 |
->select( $this->getExistingFields() ) |
| 367 |
->from( $this->getTableName() ) |
| 368 |
->where( $this->getFromConds() ) |
| 369 |
->caller( __METHOD__ ) |
| 370 |
->fetchResultSet(); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
final public function update() { |
| 377 |
$this->startUpdate(); |
| 378 |
$force = $this->needForcedLinkRefresh(); |
| 379 |
foreach ( $this->getNewLinkIDs() as $link ) { |
| 380 |
if ( $force || !$this->isExisting( $link ) ) { |
| 381 |
$this->insertLink( $link ); |
| 382 |
$this->insertedLinks[] = $link; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
foreach ( $this->getExistingLinkIDs() as $link ) { |
| 387 |
if ( $force || !$this->isInNewSet( $link ) ) { |
| 388 |
$this->deleteLink( $link ); |
| 389 |
$this->deletedLinks[] = $link; |
| 390 |
} |
| 391 |
} |
| 392 |
$this->doWrites(); |
| 393 |
$this->finishUpdate(); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
protected function insertRow( $row ) { |
| 403 |
$row += $this->getFromConds(); |
| 404 |
$this->rowsToInsert[] = $row; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
protected function deleteRow( $conds ) { |
| 418 |
|
| 419 |
$conds = $this->getFromConds() + $conds; |
| 420 |
$this->rowsToDelete[] = $conds; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public function beforeLock() { |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
protected function startUpdate() { |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
protected function finishUpdate() { |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
protected function doWrites() { |
| 454 |
$db = $this->getDB(); |
| 455 |
$table = $this->getTableName(); |
| 456 |
$domainId = $db->getDomainID(); |
| 457 |
$batchSize = $this->getBatchSize(); |
| 458 |
$ticket = $this->getTransactionTicket(); |
| 459 |
|
| 460 |
$deleteBatches = array_chunk( $this->rowsToDelete, $batchSize ); |
| 461 |
foreach ( $deleteBatches as $chunk ) { |
| 462 |
$db->newDeleteQueryBuilder() |
| 463 |
->deleteFrom( $table ) |
| 464 |
->where( $db->factorConds( $chunk ) ) |
| 465 |
->caller( __METHOD__ )->execute(); |
| 466 |
if ( count( $deleteBatches ) > 1 ) { |
| 467 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
$insertBatches = array_chunk( $this->rowsToInsert, $batchSize ); |
| 472 |
foreach ( $insertBatches as $insertBatch ) { |
| 473 |
$db->newInsertQueryBuilder() |
| 474 |
->options( $this->getInsertOptions() ) |
| 475 |
->insertInto( $table ) |
| 476 |
->rows( $insertBatch ) |
| 477 |
->caller( __METHOD__ )->execute(); |
| 478 |
if ( count( $insertBatches ) > 1 ) { |
| 479 |
$this->lbFactory->commitAndWaitForReplication( __METHOD__, $ticket ); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public function setStrictTestMode( $mode = true ) { |
| 491 |
$this->strictTestMode = $mode; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
protected function getInsertOptions() { |
| 500 |
if ( $this->strictTestMode ) { |
| 501 |
return []; |
| 502 |
} else { |
| 503 |
return [ 'IGNORE' ]; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
protected function getLinkIDs( $setType ) { |
| 517 |
switch ( $setType ) { |
| 518 |
case self::INSERTED: |
| 519 |
return $this->insertedLinks; |
| 520 |
|
| 521 |
case self::DELETED: |
| 522 |
return $this->deletedLinks; |
| 523 |
|
| 524 |
case self::CHANGED: |
| 525 |
return array_merge( $this->insertedLinks, $this->deletedLinks ); |
| 526 |
|
| 527 |
case self::OLD: |
| 528 |
return $this->getExistingLinkIDs(); |
| 529 |
|
| 530 |
case self::NEW: |
| 531 |
return $this->getNewLinkIDs(); |
| 532 |
|
| 533 |
default: |
| 534 |
throw new InvalidArgumentException( __METHOD__ . ": Unknown link type" ); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
protected function linksTargetNormalizationStage(): int { |
| 543 |
return SCHEMA_COMPAT_OLD; |
| 544 |
} |
| 545 |
} |