| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Page; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use ChangeTags; |
| 7 |
use DeletePageJob; |
| 8 |
use Exception; |
| 9 |
use JobQueueGroup; |
| 10 |
use LogicException; |
| 11 |
use ManualLogEntry; |
| 12 |
use MediaWiki\Cache\BacklinkCacheFactory; |
| 13 |
use MediaWiki\CommentStore\CommentStore; |
| 14 |
use MediaWiki\Config\ServiceOptions; |
| 15 |
use MediaWiki\Content\Content; |
| 16 |
use MediaWiki\Deferred\DeferrableUpdate; |
| 17 |
use MediaWiki\Deferred\DeferredUpdates; |
| 18 |
use MediaWiki\Deferred\LinksUpdate\LinksDeletionUpdate; |
| 19 |
use MediaWiki\Deferred\LinksUpdate\LinksUpdate; |
| 20 |
use MediaWiki\Deferred\SearchUpdate; |
| 21 |
use MediaWiki\Deferred\SiteStatsUpdate; |
| 22 |
use MediaWiki\HookContainer\HookContainer; |
| 23 |
use MediaWiki\HookContainer\HookRunner; |
| 24 |
use MediaWiki\Language\RawMessage; |
| 25 |
use MediaWiki\Logger\LoggerFactory; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Message\Message; |
| 28 |
use MediaWiki\Permissions\Authority; |
| 29 |
use MediaWiki\Permissions\PermissionStatus; |
| 30 |
use MediaWiki\ResourceLoader\WikiModule; |
| 31 |
use MediaWiki\Revision\RevisionRecord; |
| 32 |
use MediaWiki\Revision\RevisionStore; |
| 33 |
use MediaWiki\Revision\SlotRecord; |
| 34 |
use MediaWiki\Status\Status; |
| 35 |
use MediaWiki\Title\NamespaceInfo; |
| 36 |
use MediaWiki\User\UserFactory; |
| 37 |
use StatusValue; |
| 38 |
use Wikimedia\IPUtils; |
| 39 |
use Wikimedia\Message\ITextFormatter; |
| 40 |
use Wikimedia\Message\MessageValue; |
| 41 |
use Wikimedia\ObjectCache\BagOStuff; |
| 42 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 43 |
use Wikimedia\Rdbms\LBFactory; |
| 44 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 45 |
use WikiPage; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class DeletePage { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const CONSTRUCTOR_OPTIONS = [ |
| 57 |
MainConfigNames::DeleteRevisionsBatchSize, |
| 58 |
MainConfigNames::DeleteRevisionsLimit, |
| 59 |
]; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public const PAGE_BASE = 'base'; |
| 65 |
public const PAGE_TALK = 'talk'; |
| 66 |
|
| 67 |
|
| 68 |
private $isDeletePageUnitTest = false; |
| 69 |
|
| 70 |
private $suppress = false; |
| 71 |
|
| 72 |
private $tags = []; |
| 73 |
|
| 74 |
private $logSubtype = 'delete'; |
| 75 |
|
| 76 |
private $forceImmediate = false; |
| 77 |
|
| 78 |
private $associatedTalk; |
| 79 |
|
| 80 |
|
| 81 |
private $legacyHookErrors = ''; |
| 82 |
|
| 83 |
private $mergeLegacyHookErrors = true; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private $successfulDeletionsIDs; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private $wasScheduled; |
| 95 |
|
| 96 |
private $attemptedDeletion = false; |
| 97 |
|
| 98 |
private HookRunner $hookRunner; |
| 99 |
private RevisionStore $revisionStore; |
| 100 |
private LBFactory $lbFactory; |
| 101 |
private JobQueueGroup $jobQueueGroup; |
| 102 |
private CommentStore $commentStore; |
| 103 |
private ServiceOptions $options; |
| 104 |
private BagOStuff $recentDeletesCache; |
| 105 |
private string $localWikiID; |
| 106 |
private string $webRequestID; |
| 107 |
private WikiPageFactory $wikiPageFactory; |
| 108 |
private UserFactory $userFactory; |
| 109 |
private BacklinkCacheFactory $backlinkCacheFactory; |
| 110 |
private NamespaceInfo $namespaceInfo; |
| 111 |
private ITextFormatter $contLangMsgTextFormatter; |
| 112 |
private RedirectStore $redirectStore; |
| 113 |
private WikiPage $page; |
| 114 |
private Authority $deleter; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function __construct( |
| 120 |
HookContainer $hookContainer, |
| 121 |
RevisionStore $revisionStore, |
| 122 |
LBFactory $lbFactory, |
| 123 |
JobQueueGroup $jobQueueGroup, |
| 124 |
CommentStore $commentStore, |
| 125 |
ServiceOptions $serviceOptions, |
| 126 |
BagOStuff $recentDeletesCache, |
| 127 |
string $localWikiID, |
| 128 |
string $webRequestID, |
| 129 |
WikiPageFactory $wikiPageFactory, |
| 130 |
UserFactory $userFactory, |
| 131 |
BacklinkCacheFactory $backlinkCacheFactory, |
| 132 |
NamespaceInfo $namespaceInfo, |
| 133 |
ITextFormatter $contLangMsgTextFormatter, |
| 134 |
RedirectStore $redirectStore, |
| 135 |
ProperPageIdentity $page, |
| 136 |
Authority $deleter |
| 137 |
) { |
| 138 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 139 |
$this->revisionStore = $revisionStore; |
| 140 |
$this->lbFactory = $lbFactory; |
| 141 |
$this->jobQueueGroup = $jobQueueGroup; |
| 142 |
$this->commentStore = $commentStore; |
| 143 |
$serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 144 |
$this->options = $serviceOptions; |
| 145 |
$this->recentDeletesCache = $recentDeletesCache; |
| 146 |
$this->localWikiID = $localWikiID; |
| 147 |
$this->webRequestID = $webRequestID; |
| 148 |
$this->wikiPageFactory = $wikiPageFactory; |
| 149 |
$this->userFactory = $userFactory; |
| 150 |
$this->backlinkCacheFactory = $backlinkCacheFactory; |
| 151 |
$this->namespaceInfo = $namespaceInfo; |
| 152 |
$this->contLangMsgTextFormatter = $contLangMsgTextFormatter; |
| 153 |
|
| 154 |
$this->page = $wikiPageFactory->newFromTitle( $page ); |
| 155 |
$this->deleter = $deleter; |
| 156 |
$this->redirectStore = $redirectStore; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getLegacyHookErrors() { |
| 164 |
return $this->legacyHookErrors; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function keepLegacyHookErrorsSeparate(): self { |
| 172 |
$this->mergeLegacyHookErrors = false; |
| 173 |
return $this; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function setSuppress( bool $suppress ): self { |
| 184 |
$this->suppress = $suppress; |
| 185 |
return $this; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function setTags( array $tags ): self { |
| 195 |
$this->tags = $tags; |
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function setLogSubtype( string $logSubtype ): self { |
| 206 |
$this->logSubtype = $logSubtype; |
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function forceImmediate( bool $forceImmediate ): self { |
| 217 |
$this->forceImmediate = $forceImmediate; |
| 218 |
return $this; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function canProbablyDeleteAssociatedTalk(): StatusValue { |
| 228 |
if ( $this->namespaceInfo->isTalk( $this->page->getNamespace() ) ) { |
| 229 |
return StatusValue::newFatal( 'delete-error-associated-alreadytalk' ); |
| 230 |
} |
| 231 |
|
| 232 |
$talkPage = $this->wikiPageFactory->newFromLinkTarget( |
| 233 |
$this->namespaceInfo->getTalkPage( $this->page->getTitle() ) |
| 234 |
); |
| 235 |
if ( !$talkPage->exists() ) { |
| 236 |
return StatusValue::newFatal( 'delete-error-associated-doesnotexist' ); |
| 237 |
} |
| 238 |
return StatusValue::newGood(); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function setDeleteAssociatedTalk( bool $delete ): self { |
| 253 |
if ( !$delete ) { |
| 254 |
$this->associatedTalk = null; |
| 255 |
return $this; |
| 256 |
} |
| 257 |
|
| 258 |
if ( $this->namespaceInfo->isTalk( $this->page->getNamespace() ) ) { |
| 259 |
throw new BadMethodCallException( "Cannot delete associated talk page of a talk page! ($this->page)" ); |
| 260 |
} |
| 261 |
|
| 262 |
$this->associatedTalk = $this->wikiPageFactory->newFromLinkTarget( |
| 263 |
$this->namespaceInfo->getTalkPage( $this->page->getTitle() ) |
| 264 |
); |
| 265 |
return $this; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public function setIsDeletePageUnitTest( bool $test ): void { |
| 274 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 275 |
throw new LogicException( __METHOD__ . ' can only be used in tests!' ); |
| 276 |
} |
| 277 |
$this->isDeletePageUnitTest = $test; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function setDeletionAttempted(): self { |
| 286 |
$this->attemptedDeletion = true; |
| 287 |
$this->successfulDeletionsIDs = [ self::PAGE_BASE => null ]; |
| 288 |
$this->wasScheduled = [ self::PAGE_BASE => null ]; |
| 289 |
if ( $this->associatedTalk ) { |
| 290 |
$this->successfulDeletionsIDs[self::PAGE_TALK] = null; |
| 291 |
$this->wasScheduled[self::PAGE_TALK] = null; |
| 292 |
} |
| 293 |
return $this; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private function assertDeletionAttempted(): void { |
| 301 |
if ( !$this->attemptedDeletion ) { |
| 302 |
throw new BadMethodCallException( 'No deletion was attempted' ); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public function getSuccessfulDeletionsIDs(): array { |
| 311 |
$this->assertDeletionAttempted(); |
| 312 |
return $this->successfulDeletionsIDs; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function deletionsWereScheduled(): array { |
| 320 |
$this->assertDeletionAttempted(); |
| 321 |
return $this->wasScheduled; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function deleteIfAllowed( string $reason ): StatusValue { |
| 331 |
$this->setDeletionAttempted(); |
| 332 |
$status = $this->authorizeDeletion(); |
| 333 |
if ( !$status->isGood() ) { |
| 334 |
return $status; |
| 335 |
} |
| 336 |
|
| 337 |
return $this->deleteUnsafe( $reason ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
private function authorizeDeletion(): PermissionStatus { |
| 344 |
$status = PermissionStatus::newEmpty(); |
| 345 |
$this->deleter->authorizeWrite( 'delete', $this->page, $status ); |
| 346 |
if ( $this->associatedTalk ) { |
| 347 |
$this->deleter->authorizeWrite( 'delete', $this->associatedTalk, $status ); |
| 348 |
} |
| 349 |
if ( !$this->deleter->isAllowed( 'bigdelete' ) && $this->isBigDeletion() ) { |
| 350 |
$status->fatal( |
| 351 |
'delete-toomanyrevisions', |
| 352 |
Message::numParam( $this->options->get( MainConfigNames::DeleteRevisionsLimit ) ) |
| 353 |
); |
| 354 |
} |
| 355 |
if ( $this->tags ) { |
| 356 |
$status->merge( ChangeTags::canAddTagsAccompanyingChange( $this->tags, $this->deleter ) ); |
| 357 |
} |
| 358 |
return $status; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
private function isBigDeletion(): bool { |
| 365 |
$revLimit = $this->options->get( MainConfigNames::DeleteRevisionsLimit ); |
| 366 |
if ( !$revLimit ) { |
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 371 |
$revCount = $this->revisionStore->countRevisionsByPageId( $dbr, $this->page->getId() ); |
| 372 |
if ( $this->associatedTalk ) { |
| 373 |
$revCount += $this->revisionStore->countRevisionsByPageId( $dbr, $this->associatedTalk->getId() ); |
| 374 |
} |
| 375 |
|
| 376 |
return $revCount > $revLimit; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public function isBatchedDelete( int $safetyMargin = 0 ): bool { |
| 392 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 393 |
$revCount = $this->revisionStore->countRevisionsByPageId( $dbr, $this->page->getId() ); |
| 394 |
$revCount += $safetyMargin; |
| 395 |
|
| 396 |
if ( $revCount >= $this->options->get( MainConfigNames::DeleteRevisionsBatchSize ) ) { |
| 397 |
return true; |
| 398 |
} elseif ( !$this->associatedTalk ) { |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
$talkRevCount = $this->revisionStore->countRevisionsByPageId( $dbr, $this->associatedTalk->getId() ); |
| 403 |
$talkRevCount += $safetyMargin; |
| 404 |
|
| 405 |
return $talkRevCount >= $this->options->get( MainConfigNames::DeleteRevisionsBatchSize ); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function deleteUnsafe( string $reason ): Status { |
| 419 |
$this->setDeletionAttempted(); |
| 420 |
$origReason = $reason; |
| 421 |
$hookStatus = $this->runPreDeleteHooks( $this->page, $reason ); |
| 422 |
if ( !$hookStatus->isGood() ) { |
| 423 |
return $hookStatus; |
| 424 |
} |
| 425 |
if ( $this->associatedTalk ) { |
| 426 |
$talkReason = $this->contLangMsgTextFormatter->format( |
| 427 |
MessageValue::new( 'delete-talk-summary-prefix' )->plaintextParams( $origReason ) |
| 428 |
); |
| 429 |
$talkHookStatus = $this->runPreDeleteHooks( $this->associatedTalk, $talkReason ); |
| 430 |
if ( !$talkHookStatus->isGood() ) { |
| 431 |
return $talkHookStatus; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
$status = $this->deleteInternal( $this->page, self::PAGE_BASE, $reason ); |
| 436 |
if ( !$this->associatedTalk || !$status->isGood() ) { |
| 437 |
return $status; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
$status->merge( $this->deleteInternal( $this->associatedTalk, self::PAGE_TALK, $talkReason ) ); |
| 444 |
return $status; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
private function runPreDeleteHooks( WikiPage $page, string &$reason ): Status { |
| 453 |
$status = Status::newGood(); |
| 454 |
|
| 455 |
$legacyDeleter = $this->userFactory->newFromAuthority( $this->deleter ); |
| 456 |
if ( !$this->hookRunner->onArticleDelete( |
| 457 |
$page, $legacyDeleter, $reason, $this->legacyHookErrors, $status, $this->suppress ) |
| 458 |
) { |
| 459 |
if ( $this->mergeLegacyHookErrors && $this->legacyHookErrors !== '' ) { |
| 460 |
if ( is_string( $this->legacyHookErrors ) ) { |
| 461 |
$this->legacyHookErrors = [ $this->legacyHookErrors ]; |
| 462 |
} |
| 463 |
foreach ( $this->legacyHookErrors as $legacyError ) { |
| 464 |
$status->fatal( new RawMessage( $legacyError ) ); |
| 465 |
} |
| 466 |
} |
| 467 |
if ( $status->isOK() ) { |
| 468 |
|
| 469 |
$status->fatal( 'delete-hook-aborted' ); |
| 470 |
} |
| 471 |
return $status; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
$status = Status::newGood(); |
| 476 |
$hookRes = $this->hookRunner->onPageDelete( $page, $this->deleter, $reason, $status, $this->suppress ); |
| 477 |
if ( !$hookRes && !$status->isGood() ) { |
| 478 |
|
| 479 |
return $status; |
| 480 |
} |
| 481 |
return Status::newGood(); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public function deleteInternal( |
| 499 |
WikiPage $page, |
| 500 |
string $pageRole, |
| 501 |
string $reason, |
| 502 |
?string $webRequestId = null |
| 503 |
): Status { |
| 504 |
$title = $page->getTitle(); |
| 505 |
$status = Status::newGood(); |
| 506 |
|
| 507 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 508 |
$dbw->startAtomic( __METHOD__ ); |
| 509 |
|
| 510 |
$page->loadPageData( IDBAccessObject::READ_LATEST ); |
| 511 |
$id = $page->getId(); |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
$lockedLatest = $page->lockAndGetLatest(); |
| 517 |
if ( $id === 0 || $page->getLatest() !== $lockedLatest ) { |
| 518 |
$dbw->endAtomic( __METHOD__ ); |
| 519 |
|
| 520 |
$status->error( 'cannotdelete', wfEscapeWikiText( $title->getPrefixedText() ) ); |
| 521 |
return $status; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
$revisionRecord = $page->getRevisionRecord(); |
| 531 |
if ( !$revisionRecord ) { |
| 532 |
throw new LogicException( "No revisions for $page?" ); |
| 533 |
} |
| 534 |
try { |
| 535 |
$content = $page->getContent( RevisionRecord::RAW ); |
| 536 |
} catch ( TimeoutException $e ) { |
| 537 |
throw $e; |
| 538 |
} catch ( Exception $ex ) { |
| 539 |
wfLogWarning( __METHOD__ . ': failed to load content during deletion! ' |
| 540 |
. $ex->getMessage() ); |
| 541 |
|
| 542 |
$content = null; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
$explictTrxLogged = false; |
| 548 |
while ( true ) { |
| 549 |
$done = $this->archiveRevisions( $page, $id ); |
| 550 |
if ( $done || !$this->forceImmediate ) { |
| 551 |
break; |
| 552 |
} |
| 553 |
$dbw->endAtomic( __METHOD__ ); |
| 554 |
if ( $dbw->explicitTrxActive() ) { |
| 555 |
|
| 556 |
if ( !$explictTrxLogged ) { |
| 557 |
$explictTrxLogged = true; |
| 558 |
LoggerFactory::getInstance( 'wfDebug' )->debug( |
| 559 |
'explicit transaction active in ' . __METHOD__ . ' while deleting {title}', [ |
| 560 |
'title' => $title->getText(), |
| 561 |
] ); |
| 562 |
} |
| 563 |
continue; |
| 564 |
} |
| 565 |
if ( $dbw->trxLevel() ) { |
| 566 |
$dbw->commit( __METHOD__ ); |
| 567 |
} |
| 568 |
$this->lbFactory->waitForReplication(); |
| 569 |
$dbw->startAtomic( __METHOD__ ); |
| 570 |
} |
| 571 |
|
| 572 |
if ( !$done ) { |
| 573 |
$dbw->endAtomic( __METHOD__ ); |
| 574 |
|
| 575 |
$jobParams = [ |
| 576 |
'namespace' => $title->getNamespace(), |
| 577 |
'title' => $title->getDBkey(), |
| 578 |
'wikiPageId' => $id, |
| 579 |
'requestId' => $webRequestId ?? $this->webRequestID, |
| 580 |
'reason' => $reason, |
| 581 |
'suppress' => $this->suppress, |
| 582 |
'userId' => $this->deleter->getUser()->getId(), |
| 583 |
'tags' => json_encode( $this->tags ), |
| 584 |
'logsubtype' => $this->logSubtype, |
| 585 |
'pageRole' => $pageRole, |
| 586 |
]; |
| 587 |
|
| 588 |
$job = new DeletePageJob( $jobParams ); |
| 589 |
$this->jobQueueGroup->push( $job ); |
| 590 |
$this->wasScheduled[$pageRole] = true; |
| 591 |
return $status; |
| 592 |
} |
| 593 |
$this->wasScheduled[$pageRole] = false; |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
$archivedRevisionCount = $dbw->newSelectQueryBuilder() |
| 603 |
->select( '*' ) |
| 604 |
->from( 'archive' ) |
| 605 |
->where( [ |
| 606 |
'ar_namespace' => $title->getNamespace(), |
| 607 |
'ar_title' => $title->getDBkey(), |
| 608 |
'ar_page_id' => $id |
| 609 |
] ) |
| 610 |
->caller( __METHOD__ )->fetchRowCount(); |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
$page->getRedirectTarget(); |
| 616 |
|
| 617 |
|
| 618 |
$logTitle = clone $title; |
| 619 |
$wikiPageBeforeDelete = clone $page; |
| 620 |
|
| 621 |
|
| 622 |
$dbw->newDeleteQueryBuilder() |
| 623 |
->deleteFrom( 'page' ) |
| 624 |
->where( [ 'page_id' => $id ] ) |
| 625 |
->caller( __METHOD__ )->execute(); |
| 626 |
|
| 627 |
|
| 628 |
$logtype = $this->suppress ? 'suppress' : 'delete'; |
| 629 |
|
| 630 |
$logEntry = new ManualLogEntry( $logtype, $this->logSubtype ); |
| 631 |
$logEntry->setPerformer( $this->deleter->getUser() ); |
| 632 |
$logEntry->setTarget( $logTitle ); |
| 633 |
$logEntry->setComment( $reason ); |
| 634 |
$logEntry->addTags( $this->tags ); |
| 635 |
if ( !$this->isDeletePageUnitTest ) { |
| 636 |
|
| 637 |
$logid = $logEntry->insert(); |
| 638 |
|
| 639 |
$dbw->onTransactionPreCommitOrIdle( |
| 640 |
static function () use ( $logEntry, $logid ) { |
| 641 |
|
| 642 |
$logEntry->publish( $logid ); |
| 643 |
}, |
| 644 |
__METHOD__ |
| 645 |
); |
| 646 |
} else { |
| 647 |
$logid = 42; |
| 648 |
} |
| 649 |
|
| 650 |
$dbw->endAtomic( __METHOD__ ); |
| 651 |
|
| 652 |
$this->doDeleteUpdates( $page, $revisionRecord ); |
| 653 |
|
| 654 |
$legacyDeleter = $this->userFactory->newFromAuthority( $this->deleter ); |
| 655 |
$this->hookRunner->onArticleDeleteComplete( |
| 656 |
$wikiPageBeforeDelete, |
| 657 |
$legacyDeleter, |
| 658 |
$reason, |
| 659 |
$id, |
| 660 |
$content, |
| 661 |
$logEntry, |
| 662 |
$archivedRevisionCount |
| 663 |
); |
| 664 |
$this->hookRunner->onPageDeleteComplete( |
| 665 |
$wikiPageBeforeDelete, |
| 666 |
$this->deleter, |
| 667 |
$reason, |
| 668 |
$id, |
| 669 |
$revisionRecord, |
| 670 |
$logEntry, |
| 671 |
$archivedRevisionCount |
| 672 |
); |
| 673 |
$this->successfulDeletionsIDs[$pageRole] = $logid; |
| 674 |
|
| 675 |
|
| 676 |
$this->redirectStore->clearCache( $page ); |
| 677 |
|
| 678 |
|
| 679 |
$key = $this->recentDeletesCache->makeKey( 'page-recent-delete', md5( $logTitle->getPrefixedText() ) ); |
| 680 |
$this->recentDeletesCache->set( $key, 1, BagOStuff::TTL_DAY ); |
| 681 |
|
| 682 |
return $status; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
private function archiveRevisions( WikiPage $page, int $id ): bool { |
| 693 |
|
| 694 |
$namespace = $page->getTitle()->getNamespace(); |
| 695 |
$dbKey = $page->getTitle()->getDBkey(); |
| 696 |
|
| 697 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 698 |
|
| 699 |
$revQuery = $this->revisionStore->getQueryInfo(); |
| 700 |
$bitfield = false; |
| 701 |
|
| 702 |
|
| 703 |
if ( $this->suppress ) { |
| 704 |
$bitfield = RevisionRecord::SUPPRESSED_ALL; |
| 705 |
$revQuery['fields'] = array_diff( $revQuery['fields'], [ 'rev_deleted' ] ); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
$lockQuery = $revQuery; |
| 720 |
$lockQuery['tables'] = array_intersect( |
| 721 |
$revQuery['tables'], |
| 722 |
[ 'revision', 'revision_comment_temp' ] |
| 723 |
); |
| 724 |
unset( $lockQuery['fields'] ); |
| 725 |
$dbw->newSelectQueryBuilder() |
| 726 |
->queryInfo( $lockQuery ) |
| 727 |
->where( [ 'rev_page' => $id ] ) |
| 728 |
->forUpdate() |
| 729 |
->caller( __METHOD__ ) |
| 730 |
->acquireRowLocks(); |
| 731 |
|
| 732 |
$deleteBatchSize = $this->options->get( MainConfigNames::DeleteRevisionsBatchSize ); |
| 733 |
|
| 734 |
|
| 735 |
$res = $dbw->newSelectQueryBuilder() |
| 736 |
->queryInfo( $revQuery ) |
| 737 |
->where( [ 'rev_page' => $id ] ) |
| 738 |
->orderBy( [ 'rev_timestamp', 'rev_id' ] ) |
| 739 |
->limit( $deleteBatchSize + 1 ) |
| 740 |
->caller( __METHOD__ ) |
| 741 |
->fetchResultSet(); |
| 742 |
|
| 743 |
|
| 744 |
$rowsInsert = []; |
| 745 |
$revids = []; |
| 746 |
|
| 747 |
|
| 748 |
$ipRevIds = []; |
| 749 |
|
| 750 |
$done = true; |
| 751 |
foreach ( $res as $row ) { |
| 752 |
if ( count( $revids ) >= $deleteBatchSize ) { |
| 753 |
$done = false; |
| 754 |
break; |
| 755 |
} |
| 756 |
|
| 757 |
$comment = $this->commentStore->getComment( 'rev_comment', $row ); |
| 758 |
$rowInsert = [ |
| 759 |
'ar_namespace' => $namespace, |
| 760 |
'ar_title' => $dbKey, |
| 761 |
'ar_actor' => $row->rev_actor, |
| 762 |
'ar_timestamp' => $row->rev_timestamp, |
| 763 |
'ar_minor_edit' => $row->rev_minor_edit, |
| 764 |
'ar_rev_id' => $row->rev_id, |
| 765 |
'ar_parent_id' => $row->rev_parent_id, |
| 766 |
'ar_len' => $row->rev_len, |
| 767 |
'ar_page_id' => $id, |
| 768 |
'ar_deleted' => $this->suppress ? $bitfield : $row->rev_deleted, |
| 769 |
'ar_sha1' => $row->rev_sha1, |
| 770 |
] + $this->commentStore->insert( $dbw, 'ar_comment', $comment ); |
| 771 |
|
| 772 |
$rowsInsert[] = $rowInsert; |
| 773 |
$revids[] = $row->rev_id; |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
if ( (int)$row->rev_user === 0 && IPUtils::isValid( $row->rev_user_text ) ) { |
| 778 |
$ipRevIds[] = $row->rev_id; |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
if ( count( $revids ) > 0 ) { |
| 783 |
|
| 784 |
$dbw->newInsertQueryBuilder() |
| 785 |
->insertInto( 'archive' ) |
| 786 |
->rows( $rowsInsert ) |
| 787 |
->caller( __METHOD__ )->execute(); |
| 788 |
|
| 789 |
$dbw->newDeleteQueryBuilder() |
| 790 |
->deleteFrom( 'revision' ) |
| 791 |
->where( [ 'rev_id' => $revids ] ) |
| 792 |
->caller( __METHOD__ )->execute(); |
| 793 |
|
| 794 |
if ( count( $ipRevIds ) > 0 ) { |
| 795 |
$dbw->newDeleteQueryBuilder() |
| 796 |
->deleteFrom( 'ip_changes' ) |
| 797 |
->where( [ 'ipc_rev_id' => $ipRevIds ] ) |
| 798 |
->caller( __METHOD__ )->execute(); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
return $done; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
private function doDeleteUpdates( WikiPage $page, RevisionRecord $revRecord ): void { |
| 814 |
try { |
| 815 |
$countable = $page->isCountable(); |
| 816 |
} catch ( TimeoutException $e ) { |
| 817 |
throw $e; |
| 818 |
} catch ( Exception $ex ) { |
| 819 |
|
| 820 |
|
| 821 |
$countable = false; |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
DeferredUpdates::addUpdate( SiteStatsUpdate::factory( |
| 826 |
[ 'edits' => 1, 'articles' => $countable ? -1 : 0, 'pages' => -1 ] |
| 827 |
) ); |
| 828 |
|
| 829 |
|
| 830 |
$updates = $this->getDeletionUpdates( $page, $revRecord ); |
| 831 |
foreach ( $updates as $update ) { |
| 832 |
DeferredUpdates::addUpdate( $update ); |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
LinksUpdate::queueRecursiveJobsForTable( |
| 837 |
$page->getTitle(), |
| 838 |
'templatelinks', |
| 839 |
'delete-page', |
| 840 |
$this->deleter->getUser()->getName(), |
| 841 |
$this->backlinkCacheFactory->getBacklinkCache( $page->getTitle() ) |
| 842 |
); |
| 843 |
|
| 844 |
if ( $page->getTitle()->getNamespace() === NS_FILE ) { |
| 845 |
LinksUpdate::queueRecursiveJobsForTable( |
| 846 |
$page->getTitle(), |
| 847 |
'imagelinks', |
| 848 |
'delete-page', |
| 849 |
$this->deleter->getUser()->getName(), |
| 850 |
$this->backlinkCacheFactory->getBacklinkCache( $page->getTitle() ) |
| 851 |
); |
| 852 |
} |
| 853 |
|
| 854 |
if ( !$this->isDeletePageUnitTest ) { |
| 855 |
|
| 856 |
|
| 857 |
WikiPage::onArticleDelete( $page->getTitle() ); |
| 858 |
} |
| 859 |
|
| 860 |
WikiModule::invalidateModuleCache( |
| 861 |
$page->getTitle(), |
| 862 |
$revRecord, |
| 863 |
null, |
| 864 |
$this->localWikiID |
| 865 |
); |
| 866 |
|
| 867 |
|
| 868 |
$page->loadFromRow( false, IDBAccessObject::READ_LATEST ); |
| 869 |
|
| 870 |
|
| 871 |
DeferredUpdates::addUpdate( new SearchUpdate( $page->getId(), $page->getTitle() ) ); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
private function getDeletionUpdates( WikiPage $page, RevisionRecord $rev ): array { |
| 884 |
if ( $this->isDeletePageUnitTest ) { |
| 885 |
|
| 886 |
return []; |
| 887 |
} |
| 888 |
$slotContent = array_map( static function ( SlotRecord $slot ) { |
| 889 |
return $slot->getContent(); |
| 890 |
}, $rev->getSlots()->getSlots() ); |
| 891 |
|
| 892 |
$allUpdates = [ new LinksDeletionUpdate( $page ) ]; |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
$content = null; |
| 899 |
foreach ( $slotContent as $role => $content ) { |
| 900 |
$handler = $content->getContentHandler(); |
| 901 |
|
| 902 |
$updates = $handler->getDeletionUpdates( |
| 903 |
$page->getTitle(), |
| 904 |
$role |
| 905 |
); |
| 906 |
|
| 907 |
$allUpdates = array_merge( $allUpdates, $updates ); |
| 908 |
} |
| 909 |
|
| 910 |
$this->hookRunner->onPageDeletionDataUpdates( |
| 911 |
$page->getTitle(), $rev, $allUpdates ); |
| 912 |
|
| 913 |
|
| 914 |
$this->hookRunner->onWikiPageDeletionUpdates( $page, $content, $allUpdates ); |
| 915 |
return $allUpdates; |
| 916 |
} |
| 917 |
} |