| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\Revision; |
| 27 |
|
| 28 |
use InvalidArgumentException; |
| 29 |
use LogicException; |
| 30 |
use MediaWiki\CommentStore\CommentStore; |
| 31 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 32 |
use MediaWiki\Content\Content; |
| 33 |
use MediaWiki\Content\FallbackContent; |
| 34 |
use MediaWiki\Content\IContentHandlerFactory; |
| 35 |
use MediaWiki\DAO\WikiAwareEntity; |
| 36 |
use MediaWiki\HookContainer\HookContainer; |
| 37 |
use MediaWiki\HookContainer\HookRunner; |
| 38 |
use MediaWiki\Linker\LinkTarget; |
| 39 |
use MediaWiki\MainConfigNames; |
| 40 |
use MediaWiki\MediaWikiServices; |
| 41 |
use MediaWiki\Page\LegacyArticleIdAccess; |
| 42 |
use MediaWiki\Page\PageIdentity; |
| 43 |
use MediaWiki\Page\PageIdentityValue; |
| 44 |
use MediaWiki\Page\PageStore; |
| 45 |
use MediaWiki\Permissions\Authority; |
| 46 |
use MediaWiki\Storage\BadBlobException; |
| 47 |
use MediaWiki\Storage\BlobAccessException; |
| 48 |
use MediaWiki\Storage\BlobStore; |
| 49 |
use MediaWiki\Storage\NameTableAccessException; |
| 50 |
use MediaWiki\Storage\NameTableStore; |
| 51 |
use MediaWiki\Storage\RevisionSlotsUpdate; |
| 52 |
use MediaWiki\Storage\SqlBlobStore; |
| 53 |
use MediaWiki\Title\Title; |
| 54 |
use MediaWiki\Title\TitleFactory; |
| 55 |
use MediaWiki\User\ActorStore; |
| 56 |
use MediaWiki\User\UserIdentity; |
| 57 |
use MediaWiki\Utils\MWTimestamp; |
| 58 |
use MWException; |
| 59 |
use MWUnknownContentModelException; |
| 60 |
use Psr\Log\LoggerAwareInterface; |
| 61 |
use Psr\Log\LoggerInterface; |
| 62 |
use Psr\Log\NullLogger; |
| 63 |
use RecentChange; |
| 64 |
use RuntimeException; |
| 65 |
use StatusValue; |
| 66 |
use stdClass; |
| 67 |
use Traversable; |
| 68 |
use Wikimedia\Assert\Assert; |
| 69 |
use Wikimedia\IPUtils; |
| 70 |
use Wikimedia\ObjectCache\BagOStuff; |
| 71 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 72 |
use Wikimedia\Rdbms\Database; |
| 73 |
use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 74 |
use Wikimedia\Rdbms\IDatabase; |
| 75 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 76 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 77 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 78 |
use Wikimedia\Rdbms\IResultWrapper; |
| 79 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 80 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class RevisionStore implements RevisionFactory, RevisionLookup, LoggerAwareInterface { |
| 92 |
|
| 93 |
use LegacyArticleIdAccess; |
| 94 |
|
| 95 |
public const ROW_CACHE_KEY = 'revision-row-1.29'; |
| 96 |
|
| 97 |
public const ORDER_OLDEST_TO_NEWEST = 'ASC'; |
| 98 |
public const ORDER_NEWEST_TO_OLDEST = 'DESC'; |
| 99 |
|
| 100 |
|
| 101 |
public const INCLUDE_OLD = 'include_old'; |
| 102 |
public const INCLUDE_NEW = 'include_new'; |
| 103 |
public const INCLUDE_BOTH = 'include_both'; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private $blobStore; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private $wikiId; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private $loadBalancer; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private $cache; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private $localCache; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private $commentStore; |
| 134 |
|
| 135 |
|
| 136 |
private $actorStore; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private $logger; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private $contentModelStore; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private $slotRoleStore; |
| 152 |
|
| 153 |
|
| 154 |
private $slotRoleRegistry; |
| 155 |
|
| 156 |
|
| 157 |
private $contentHandlerFactory; |
| 158 |
|
| 159 |
|
| 160 |
private $hookRunner; |
| 161 |
|
| 162 |
|
| 163 |
private $pageStore; |
| 164 |
|
| 165 |
|
| 166 |
private $titleFactory; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function __construct( |
| 193 |
ILoadBalancer $loadBalancer, |
| 194 |
SqlBlobStore $blobStore, |
| 195 |
WANObjectCache $cache, |
| 196 |
BagOStuff $localCache, |
| 197 |
CommentStore $commentStore, |
| 198 |
NameTableStore $contentModelStore, |
| 199 |
NameTableStore $slotRoleStore, |
| 200 |
SlotRoleRegistry $slotRoleRegistry, |
| 201 |
ActorStore $actorStore, |
| 202 |
IContentHandlerFactory $contentHandlerFactory, |
| 203 |
PageStore $pageStore, |
| 204 |
TitleFactory $titleFactory, |
| 205 |
HookContainer $hookContainer, |
| 206 |
$wikiId = WikiAwareEntity::LOCAL |
| 207 |
) { |
| 208 |
Assert::parameterType( [ 'string', 'false' ], $wikiId, '$wikiId' ); |
| 209 |
|
| 210 |
$this->loadBalancer = $loadBalancer; |
| 211 |
$this->blobStore = $blobStore; |
| 212 |
$this->cache = $cache; |
| 213 |
$this->localCache = $localCache; |
| 214 |
$this->commentStore = $commentStore; |
| 215 |
$this->contentModelStore = $contentModelStore; |
| 216 |
$this->slotRoleStore = $slotRoleStore; |
| 217 |
$this->slotRoleRegistry = $slotRoleRegistry; |
| 218 |
$this->actorStore = $actorStore; |
| 219 |
$this->wikiId = $wikiId; |
| 220 |
$this->logger = new NullLogger(); |
| 221 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 222 |
$this->pageStore = $pageStore; |
| 223 |
$this->titleFactory = $titleFactory; |
| 224 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 225 |
} |
| 226 |
|
| 227 |
public function setLogger( LoggerInterface $logger ) { |
| 228 |
$this->logger = $logger; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function isReadOnly() { |
| 235 |
return $this->blobStore->isReadOnly(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function getWikiId() { |
| 244 |
return $this->wikiId; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
private function getDBConnectionRefForQueryFlags( $queryFlags ) { |
| 253 |
if ( ( $queryFlags & IDBAccessObject::READ_LATEST ) == IDBAccessObject::READ_LATEST ) { |
| 254 |
return $this->getPrimaryConnection(); |
| 255 |
} else { |
| 256 |
return $this->getReplicaConnection(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
private function getReplicaConnection( $groups = [] ) { |
| 265 |
|
| 266 |
return $this->loadBalancer->getConnection( DB_REPLICA, $groups, $this->wikiId ); |
| 267 |
} |
| 268 |
|
| 269 |
private function getPrimaryConnection(): IDatabase { |
| 270 |
|
| 271 |
return $this->loadBalancer->getConnection( DB_PRIMARY, [], $this->wikiId ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public function getTitle( $pageId, $revId, $queryFlags = IDBAccessObject::READ_NORMAL ) { |
| 291 |
|
| 292 |
if ( $this->wikiId !== WikiAwareEntity::LOCAL ) { |
| 293 |
wfDeprecatedMsg( 'Using a Title object to refer to a page on another site.', '1.36' ); |
| 294 |
} |
| 295 |
|
| 296 |
$page = $this->getPage( $pageId, $revId, $queryFlags ); |
| 297 |
return $this->titleFactory->newFromPageIdentity( $page ); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
private function getPage( ?int $pageId, ?int $revId, int $queryFlags = IDBAccessObject::READ_NORMAL ) { |
| 311 |
if ( !$pageId && !$revId ) { |
| 312 |
throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
if ( DBAccessObjectUtils::hasFlags( $queryFlags, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) { |
| 318 |
$queryFlags = IDBAccessObject::READ_NORMAL; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
if ( $pageId !== null && $pageId > 0 ) { |
| 323 |
$page = $this->pageStore->getPageById( $pageId, $queryFlags ); |
| 324 |
if ( $page ) { |
| 325 |
return $this->wrapPage( $page ); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
if ( $revId !== null && $revId > 0 ) { |
| 331 |
$pageQuery = $this->pageStore->newSelectQueryBuilder( $queryFlags ) |
| 332 |
->join( 'revision', null, 'page_id=rev_page' ) |
| 333 |
->conds( [ 'rev_id' => $revId ] ) |
| 334 |
->caller( __METHOD__ ); |
| 335 |
|
| 336 |
$page = $pageQuery->fetchPageRecord(); |
| 337 |
if ( $page ) { |
| 338 |
return $this->wrapPage( $page ); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
if ( $queryFlags === IDBAccessObject::READ_NORMAL ) { |
| 344 |
$title = $this->getPage( $pageId, $revId, IDBAccessObject::READ_LATEST ); |
| 345 |
if ( $title ) { |
| 346 |
$this->logger->info( |
| 347 |
__METHOD__ . ' fell back to READ_LATEST and got a Title.', |
| 348 |
[ 'exception' => new RuntimeException() ] |
| 349 |
); |
| 350 |
return $title; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
throw new RevisionAccessException( |
| 355 |
'Could not determine title for page ID {page_id} and revision ID {rev_id}', |
| 356 |
[ |
| 357 |
'page_id' => $pageId, |
| 358 |
'rev_id' => $revId, |
| 359 |
] |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
private function wrapPage( PageIdentity $page ): PageIdentity { |
| 369 |
if ( $this->wikiId === WikiAwareEntity::LOCAL ) { |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
return $this->titleFactory->newFromPageIdentity( $page ); |
| 377 |
} else { |
| 378 |
return $page; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
private function failOnNull( $value, $name ) { |
| 390 |
if ( $value === null ) { |
| 391 |
throw new IncompleteRevisionException( |
| 392 |
"$name must not be " . var_export( $value, true ) . "!" |
| 393 |
); |
| 394 |
} |
| 395 |
|
| 396 |
return $value; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
private function failOnEmpty( $value, $name ) { |
| 407 |
if ( $value === null || $value === 0 || $value === '' ) { |
| 408 |
throw new IncompleteRevisionException( |
| 409 |
"$name must not be " . var_export( $value, true ) . "!" |
| 410 |
); |
| 411 |
} |
| 412 |
|
| 413 |
return $value; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public function insertRevisionOn( RevisionRecord $rev, IDatabase $dbw ) { |
| 431 |
|
| 432 |
$this->checkDatabaseDomain( $dbw ); |
| 433 |
|
| 434 |
$slotRoles = $rev->getSlotRoles(); |
| 435 |
|
| 436 |
|
| 437 |
if ( !in_array( SlotRecord::MAIN, $slotRoles ) ) { |
| 438 |
throw new IncompleteRevisionException( |
| 439 |
'main slot must be provided' |
| 440 |
); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
$this->failOnNull( $rev->getSize(), 'size field' ); |
| 445 |
$this->failOnEmpty( $rev->getSha1(), 'sha1 field' ); |
| 446 |
$this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' ); |
| 447 |
$comment = $this->failOnNull( $rev->getComment( RevisionRecord::RAW ), 'comment' ); |
| 448 |
$user = $this->failOnNull( $rev->getUser( RevisionRecord::RAW ), 'user' ); |
| 449 |
$this->failOnNull( $user->getId(), 'user field' ); |
| 450 |
$this->failOnEmpty( $user->getName(), 'user_text field' ); |
| 451 |
|
| 452 |
if ( !$rev->isReadyForInsertion() ) { |
| 453 |
|
| 454 |
|
| 455 |
throw new IncompleteRevisionException( 'Revision is incomplete' ); |
| 456 |
} |
| 457 |
|
| 458 |
if ( $slotRoles == [ SlotRecord::MAIN ] ) { |
| 459 |
|
| 460 |
|
| 461 |
$mainSlot = $rev->getSlot( SlotRecord::MAIN, RevisionRecord::RAW ); |
| 462 |
Assert::precondition( |
| 463 |
$mainSlot->getSize() === $rev->getSize(), |
| 464 |
'The revisions\'s size must match the main slot\'s size (see T239717)' |
| 465 |
); |
| 466 |
Assert::precondition( |
| 467 |
$mainSlot->getSha1() === $rev->getSha1(), |
| 468 |
'The revisions\'s SHA1 hash must match the main slot\'s SHA1 hash (see T239717)' |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
$pageId = $this->failOnEmpty( $rev->getPageId( $this->wikiId ), 'rev_page field' ); |
| 473 |
|
| 474 |
$parentId = $rev->getParentId() ?? $this->getPreviousRevisionId( $dbw, $rev ); |
| 475 |
|
| 476 |
|
| 477 |
$rev = $dbw->doAtomicSection( |
| 478 |
__METHOD__, |
| 479 |
function ( IDatabase $dbw, $fname ) use ( |
| 480 |
$rev, |
| 481 |
$user, |
| 482 |
$comment, |
| 483 |
$pageId, |
| 484 |
$parentId |
| 485 |
) { |
| 486 |
return $this->insertRevisionInternal( |
| 487 |
$rev, |
| 488 |
$dbw, |
| 489 |
$user, |
| 490 |
$comment, |
| 491 |
$rev->getPage(), |
| 492 |
$pageId, |
| 493 |
$parentId |
| 494 |
); |
| 495 |
} |
| 496 |
); |
| 497 |
|
| 498 |
Assert::postcondition( $rev->getId( $this->wikiId ) > 0, 'revision must have an ID' ); |
| 499 |
Assert::postcondition( $rev->getPageId( $this->wikiId ) > 0, 'revision must have a page ID' ); |
| 500 |
Assert::postcondition( |
| 501 |
$rev->getComment( RevisionRecord::RAW ) !== null, |
| 502 |
'revision must have a comment' |
| 503 |
); |
| 504 |
Assert::postcondition( |
| 505 |
$rev->getUser( RevisionRecord::RAW ) !== null, |
| 506 |
'revision must have a user' |
| 507 |
); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
$rev->getSlot( SlotRecord::MAIN, RevisionRecord::RAW ); |
| 514 |
|
| 515 |
foreach ( $slotRoles as $role ) { |
| 516 |
$slot = $rev->getSlot( $role, RevisionRecord::RAW ); |
| 517 |
Assert::postcondition( |
| 518 |
$slot->getContent() !== null, |
| 519 |
$role . ' slot must have content' |
| 520 |
); |
| 521 |
Assert::postcondition( |
| 522 |
$slot->hasRevision(), |
| 523 |
$role . ' slot must have a revision associated' |
| 524 |
); |
| 525 |
} |
| 526 |
|
| 527 |
$this->hookRunner->onRevisionRecordInserted( $rev ); |
| 528 |
|
| 529 |
return $rev; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public function updateSlotsOn( |
| 545 |
RevisionRecord $revision, |
| 546 |
RevisionSlotsUpdate $revisionSlotsUpdate, |
| 547 |
IDatabase $dbw |
| 548 |
): array { |
| 549 |
$this->checkDatabaseDomain( $dbw ); |
| 550 |
|
| 551 |
|
| 552 |
foreach ( $revisionSlotsUpdate->getModifiedRoles() as $role ) { |
| 553 |
Assert::precondition( |
| 554 |
$this->slotRoleRegistry->getRoleHandler( $role )->isDerived(), |
| 555 |
'Trying to modify a slot that is not derived' |
| 556 |
); |
| 557 |
} |
| 558 |
foreach ( $revisionSlotsUpdate->getRemovedRoles() as $role ) { |
| 559 |
$isDerived = $this->slotRoleRegistry->getRoleHandler( $role )->isDerived(); |
| 560 |
Assert::precondition( |
| 561 |
$isDerived, |
| 562 |
'Trying to remove a slot that is not derived' |
| 563 |
); |
| 564 |
throw new LogicException( 'Removing derived slots is not yet implemented. See T277394.' ); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
$slotRecords = $dbw->doAtomicSection( |
| 569 |
__METHOD__, |
| 570 |
function ( IDatabase $dbw, $fname ) use ( |
| 571 |
$revision, |
| 572 |
$revisionSlotsUpdate |
| 573 |
) { |
| 574 |
return $this->updateSlotsInternal( |
| 575 |
$revision, |
| 576 |
$revisionSlotsUpdate, |
| 577 |
$dbw |
| 578 |
); |
| 579 |
} |
| 580 |
); |
| 581 |
|
| 582 |
foreach ( $slotRecords as $role => $slot ) { |
| 583 |
Assert::postcondition( |
| 584 |
$slot->getContent() !== null, |
| 585 |
$role . ' slot must have content' |
| 586 |
); |
| 587 |
Assert::postcondition( |
| 588 |
$slot->hasRevision(), |
| 589 |
$role . ' slot must have a revision associated' |
| 590 |
); |
| 591 |
} |
| 592 |
|
| 593 |
return $slotRecords; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
private function updateSlotsInternal( |
| 603 |
RevisionRecord $revision, |
| 604 |
RevisionSlotsUpdate $revisionSlotsUpdate, |
| 605 |
IDatabase $dbw |
| 606 |
): array { |
| 607 |
$page = $revision->getPage(); |
| 608 |
$revId = $revision->getId( $this->wikiId ); |
| 609 |
$blobHints = [ |
| 610 |
BlobStore::PAGE_HINT => $page->getId( $this->wikiId ), |
| 611 |
BlobStore::REVISION_HINT => $revId, |
| 612 |
BlobStore::PARENT_HINT => $revision->getParentId( $this->wikiId ), |
| 613 |
]; |
| 614 |
|
| 615 |
$newSlots = []; |
| 616 |
foreach ( $revisionSlotsUpdate->getModifiedRoles() as $role ) { |
| 617 |
$slot = $revisionSlotsUpdate->getModifiedSlot( $role ); |
| 618 |
$newSlots[$role] = $this->insertSlotOn( $dbw, $revId, $slot, $page, $blobHints ); |
| 619 |
} |
| 620 |
|
| 621 |
return $newSlots; |
| 622 |
} |
| 623 |
|
| 624 |
private function insertRevisionInternal( |
| 625 |
RevisionRecord $rev, |
| 626 |
IDatabase $dbw, |
| 627 |
UserIdentity $user, |
| 628 |
CommentStoreComment $comment, |
| 629 |
PageIdentity $page, |
| 630 |
$pageId, |
| 631 |
$parentId |
| 632 |
) { |
| 633 |
$slotRoles = $rev->getSlotRoles(); |
| 634 |
|
| 635 |
$revisionRow = $this->insertRevisionRowOn( |
| 636 |
$dbw, |
| 637 |
$rev, |
| 638 |
$parentId |
| 639 |
); |
| 640 |
|
| 641 |
$revisionId = $revisionRow['rev_id']; |
| 642 |
|
| 643 |
$blobHints = [ |
| 644 |
BlobStore::PAGE_HINT => $pageId, |
| 645 |
BlobStore::REVISION_HINT => $revisionId, |
| 646 |
BlobStore::PARENT_HINT => $parentId, |
| 647 |
]; |
| 648 |
|
| 649 |
$newSlots = []; |
| 650 |
foreach ( $slotRoles as $role ) { |
| 651 |
$slot = $rev->getSlot( $role, RevisionRecord::RAW ); |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
if ( $slot->hasRevision() && $slot->hasContentId() ) { |
| 661 |
|
| 662 |
Assert::parameter( |
| 663 |
$slot->getRevision() === $revisionId, |
| 664 |
'slot role ' . $slot->getRole(), |
| 665 |
'Existing slot should belong to revision ' |
| 666 |
. $revisionId . ', but belongs to revision ' . $slot->getRevision() . '!' |
| 667 |
); |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
$newSlots[$role] = $slot; |
| 673 |
} else { |
| 674 |
$newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $page, $blobHints ); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
$this->insertIpChangesRow( $dbw, $user, $rev, $revisionId ); |
| 679 |
|
| 680 |
$rev = new RevisionStoreRecord( |
| 681 |
$page, |
| 682 |
$user, |
| 683 |
$comment, |
| 684 |
(object)$revisionRow, |
| 685 |
new RevisionSlots( $newSlots ), |
| 686 |
$this->wikiId |
| 687 |
); |
| 688 |
|
| 689 |
return $rev; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
private function insertSlotOn( |
| 701 |
IDatabase $dbw, |
| 702 |
$revisionId, |
| 703 |
SlotRecord $protoSlot, |
| 704 |
PageIdentity $page, |
| 705 |
array $blobHints = [] |
| 706 |
) { |
| 707 |
if ( $protoSlot->hasAddress() ) { |
| 708 |
$blobAddress = $protoSlot->getAddress(); |
| 709 |
} else { |
| 710 |
$blobAddress = $this->storeContentBlob( $protoSlot, $page, $blobHints ); |
| 711 |
} |
| 712 |
|
| 713 |
if ( $protoSlot->hasContentId() ) { |
| 714 |
$contentId = $protoSlot->getContentId(); |
| 715 |
} else { |
| 716 |
$contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress ); |
| 717 |
} |
| 718 |
|
| 719 |
$this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId ); |
| 720 |
|
| 721 |
return SlotRecord::newSaved( |
| 722 |
$revisionId, |
| 723 |
$contentId, |
| 724 |
$blobAddress, |
| 725 |
$protoSlot |
| 726 |
); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
private function insertIpChangesRow( |
| 737 |
IDatabase $dbw, |
| 738 |
UserIdentity $user, |
| 739 |
RevisionRecord $rev, |
| 740 |
$revisionId |
| 741 |
) { |
| 742 |
if ( !$user->isRegistered() && IPUtils::isValid( $user->getName() ) ) { |
| 743 |
$dbw->newInsertQueryBuilder() |
| 744 |
->insertInto( 'ip_changes' ) |
| 745 |
->row( [ |
| 746 |
'ipc_rev_id' => $revisionId, |
| 747 |
'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ), |
| 748 |
'ipc_hex' => IPUtils::toHex( $user->getName() ), |
| 749 |
] ) |
| 750 |
->caller( __METHOD__ )->execute(); |
| 751 |
|
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
private function insertRevisionRowOn( |
| 766 |
IDatabase $dbw, |
| 767 |
RevisionRecord $rev, |
| 768 |
$parentId |
| 769 |
) { |
| 770 |
$revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $parentId ); |
| 771 |
|
| 772 |
$revisionRow += $this->commentStore->insert( |
| 773 |
$dbw, |
| 774 |
'rev_comment', |
| 775 |
$rev->getComment( RevisionRecord::RAW ) |
| 776 |
); |
| 777 |
|
| 778 |
$dbw->newInsertQueryBuilder() |
| 779 |
->insertInto( 'revision' ) |
| 780 |
->row( $revisionRow ) |
| 781 |
->caller( __METHOD__ )->execute(); |
| 782 |
|
| 783 |
if ( !isset( $revisionRow['rev_id'] ) ) { |
| 784 |
|
| 785 |
$revisionRow['rev_id'] = intval( $dbw->insertId() ); |
| 786 |
|
| 787 |
if ( $dbw->getType() === 'mysql' ) { |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
$maxRevId = intval( $dbw->newSelectQueryBuilder() |
| 793 |
->select( 'MAX(ar_rev_id)' ) |
| 794 |
->from( 'archive' ) |
| 795 |
->caller( __METHOD__ ) |
| 796 |
->fetchField() ); |
| 797 |
$table = 'archive'; |
| 798 |
$maxRevId2 = intval( $dbw->newSelectQueryBuilder() |
| 799 |
->select( 'MAX(slot_revision_id)' ) |
| 800 |
->from( 'slots' ) |
| 801 |
->caller( __METHOD__ ) |
| 802 |
->fetchField() ); |
| 803 |
if ( $maxRevId2 >= $maxRevId ) { |
| 804 |
$maxRevId = $maxRevId2; |
| 805 |
$table = 'slots'; |
| 806 |
} |
| 807 |
|
| 808 |
if ( $maxRevId >= $revisionRow['rev_id'] ) { |
| 809 |
$this->logger->debug( |
| 810 |
'__METHOD__: Inserted revision {revid} but {table} has revisions up to {maxrevid}.' |
| 811 |
. ' Trying to fix it.', |
| 812 |
[ |
| 813 |
'revid' => $revisionRow['rev_id'], |
| 814 |
'table' => $table, |
| 815 |
'maxrevid' => $maxRevId, |
| 816 |
] |
| 817 |
); |
| 818 |
|
| 819 |
if ( !$dbw->lock( 'fix-for-T202032', __METHOD__ ) ) { |
| 820 |
throw new MWException( 'Failed to get database lock for T202032' ); |
| 821 |
} |
| 822 |
$fname = __METHOD__; |
| 823 |
$dbw->onTransactionResolution( |
| 824 |
static function ( $trigger, IDatabase $dbw ) use ( $fname ) { |
| 825 |
$dbw->unlock( 'fix-for-T202032', $fname ); |
| 826 |
}, |
| 827 |
__METHOD__ |
| 828 |
); |
| 829 |
|
| 830 |
$dbw->newDeleteQueryBuilder() |
| 831 |
->deleteFrom( 'revision' ) |
| 832 |
->where( [ 'rev_id' => $revisionRow['rev_id'] ] ) |
| 833 |
->caller( __METHOD__ )->execute(); |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
$row1 = $dbw->query( |
| 845 |
$dbw->selectSQLText( 'archive', [ 'v' => "MAX(ar_rev_id)" ], '', __METHOD__ ) . ' FOR UPDATE', |
| 846 |
__METHOD__ |
| 847 |
)->fetchObject(); |
| 848 |
|
| 849 |
$row2 = $dbw->query( |
| 850 |
$dbw->selectSQLText( 'slots', [ 'v' => "MAX(slot_revision_id)" ], '', __METHOD__ ) |
| 851 |
. ' FOR UPDATE', |
| 852 |
__METHOD__ |
| 853 |
)->fetchObject(); |
| 854 |
|
| 855 |
$maxRevId = max( |
| 856 |
$maxRevId, |
| 857 |
$row1 ? intval( $row1->v ) : 0, |
| 858 |
$row2 ? intval( $row2->v ) : 0 |
| 859 |
); |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
$revisionRow['rev_id'] = $maxRevId + 1; |
| 865 |
$dbw->newInsertQueryBuilder() |
| 866 |
->insertInto( 'revision' ) |
| 867 |
->row( $revisionRow ) |
| 868 |
->caller( __METHOD__ )->execute(); |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
return $revisionRow; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
private function getBaseRevisionRow( |
| 884 |
IDatabase $dbw, |
| 885 |
RevisionRecord $rev, |
| 886 |
$parentId |
| 887 |
) { |
| 888 |
|
| 889 |
$revisionRow = [ |
| 890 |
'rev_page' => $rev->getPageId( $this->wikiId ), |
| 891 |
'rev_parent_id' => $parentId, |
| 892 |
'rev_actor' => $this->actorStore->acquireActorId( |
| 893 |
$rev->getUser( RevisionRecord::RAW ), |
| 894 |
$dbw |
| 895 |
), |
| 896 |
'rev_minor_edit' => $rev->isMinor() ? 1 : 0, |
| 897 |
'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ), |
| 898 |
'rev_deleted' => $rev->getVisibility(), |
| 899 |
'rev_len' => $rev->getSize(), |
| 900 |
'rev_sha1' => $rev->getSha1(), |
| 901 |
]; |
| 902 |
|
| 903 |
if ( $rev->getId( $this->wikiId ) !== null ) { |
| 904 |
|
| 905 |
$revisionRow['rev_id'] = $rev->getId( $this->wikiId ); |
| 906 |
} |
| 907 |
|
| 908 |
return $revisionRow; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
private function storeContentBlob( |
| 920 |
SlotRecord $slot, |
| 921 |
PageIdentity $page, |
| 922 |
array $blobHints = [] |
| 923 |
) { |
| 924 |
$content = $slot->getContent(); |
| 925 |
$format = $content->getDefaultFormat(); |
| 926 |
$model = $content->getModel(); |
| 927 |
|
| 928 |
$this->checkContent( $content, $page, $slot->getRole() ); |
| 929 |
|
| 930 |
return $this->blobStore->storeBlob( |
| 931 |
$content->serialize( $format ), |
| 932 |
|
| 933 |
|
| 934 |
array_merge( |
| 935 |
$blobHints, |
| 936 |
[ |
| 937 |
BlobStore::DESIGNATION_HINT => 'page-content', |
| 938 |
BlobStore::ROLE_HINT => $slot->getRole(), |
| 939 |
BlobStore::SHA1_HINT => $slot->getSha1(), |
| 940 |
BlobStore::MODEL_HINT => $model, |
| 941 |
BlobStore::FORMAT_HINT => $format, |
| 942 |
] |
| 943 |
) |
| 944 |
); |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
private function insertSlotRowOn( SlotRecord $slot, IDatabase $dbw, $revisionId, $contentId ) { |
| 954 |
$dbw->newInsertQueryBuilder() |
| 955 |
->insertInto( 'slots' ) |
| 956 |
->row( [ |
| 957 |
'slot_revision_id' => $revisionId, |
| 958 |
'slot_role_id' => $this->slotRoleStore->acquireId( $slot->getRole() ), |
| 959 |
'slot_content_id' => $contentId, |
| 960 |
|
| 961 |
|
| 962 |
'slot_origin' => $slot->hasOrigin() ? $slot->getOrigin() : $revisionId, |
| 963 |
] ) |
| 964 |
->caller( __METHOD__ )->execute(); |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
private function insertContentRowOn( SlotRecord $slot, IDatabase $dbw, $blobAddress ) { |
| 974 |
$dbw->newInsertQueryBuilder() |
| 975 |
->insertInto( 'content' ) |
| 976 |
->row( [ |
| 977 |
'content_size' => $slot->getSize(), |
| 978 |
'content_sha1' => $slot->getSha1(), |
| 979 |
'content_model' => $this->contentModelStore->acquireId( $slot->getModel() ), |
| 980 |
'content_address' => $blobAddress, |
| 981 |
] ) |
| 982 |
->caller( __METHOD__ )->execute(); |
| 983 |
return intval( $dbw->insertId() ); |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
private function checkContent( Content $content, PageIdentity $page, string $role ) { |
| 997 |
|
| 998 |
|
| 999 |
$model = $content->getModel(); |
| 1000 |
$format = $content->getDefaultFormat(); |
| 1001 |
$handler = $content->getContentHandler(); |
| 1002 |
|
| 1003 |
if ( !$handler->isSupportedFormat( $format ) ) { |
| 1004 |
throw new MWException( |
| 1005 |
"Can't use format $format with content model $model on $page role $role" |
| 1006 |
); |
| 1007 |
} |
| 1008 |
|
| 1009 |
if ( !$content->isValid() ) { |
| 1010 |
throw new MWException( |
| 1011 |
"New content for $page role $role is not valid! Content model is $model" |
| 1012 |
); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
public function newNullRevision( |
| 1042 |
IDatabase $dbw, |
| 1043 |
PageIdentity $page, |
| 1044 |
CommentStoreComment $comment, |
| 1045 |
$minor, |
| 1046 |
UserIdentity $user |
| 1047 |
) { |
| 1048 |
$this->checkDatabaseDomain( $dbw ); |
| 1049 |
|
| 1050 |
$pageId = $this->getArticleId( $page ); |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
$pageLatest = $dbw->newSelectQueryBuilder() |
| 1056 |
->select( 'page_latest' ) |
| 1057 |
->forUpdate() |
| 1058 |
->from( 'page' ) |
| 1059 |
->where( [ 'page_id' => $pageId ] ) |
| 1060 |
->caller( __METHOD__ )->fetchField(); |
| 1061 |
|
| 1062 |
if ( !$pageLatest ) { |
| 1063 |
$msg = 'T235589: Failed to select table row during null revision creation' . |
| 1064 |
" Page id '$pageId' does not exist."; |
| 1065 |
$this->logger->error( |
| 1066 |
$msg, |
| 1067 |
[ 'exception' => new RuntimeException( $msg ) ] |
| 1068 |
); |
| 1069 |
|
| 1070 |
return null; |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
$oldRevision = $this->loadRevisionFromConds( |
| 1075 |
$dbw, |
| 1076 |
[ 'rev_id' => intval( $pageLatest ) ], |
| 1077 |
IDBAccessObject::READ_LATEST, |
| 1078 |
$page |
| 1079 |
); |
| 1080 |
|
| 1081 |
if ( !$oldRevision ) { |
| 1082 |
$msg = "Failed to load latest revision ID $pageLatest of page ID $pageId."; |
| 1083 |
$this->logger->error( |
| 1084 |
$msg, |
| 1085 |
[ 'exception' => new RuntimeException( $msg ) ] |
| 1086 |
); |
| 1087 |
return null; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
$timestamp = MWTimestamp::now( TS_MW ); |
| 1092 |
$newRevision = MutableRevisionRecord::newFromParentRevision( $oldRevision ); |
| 1093 |
|
| 1094 |
$newRevision->setComment( $comment ); |
| 1095 |
$newRevision->setUser( $user ); |
| 1096 |
$newRevision->setTimestamp( $timestamp ); |
| 1097 |
$newRevision->setMinorEdit( $minor ); |
| 1098 |
|
| 1099 |
return $newRevision; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
public function getRcIdIfUnpatrolled( RevisionRecord $rev ) { |
| 1112 |
$rc = $this->getRecentChange( $rev ); |
| 1113 |
if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_UNPATROLLED ) { |
| 1114 |
return $rc->getAttribute( 'rc_id' ); |
| 1115 |
} else { |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
public function getRecentChange( RevisionRecord $rev, $flags = 0 ) { |
| 1134 |
if ( ( $flags & IDBAccessObject::READ_LATEST ) == IDBAccessObject::READ_LATEST ) { |
| 1135 |
$dbType = DB_PRIMARY; |
| 1136 |
} else { |
| 1137 |
$dbType = DB_REPLICA; |
| 1138 |
} |
| 1139 |
|
| 1140 |
$rc = RecentChange::newFromConds( |
| 1141 |
[ |
| 1142 |
'rc_this_oldid' => $rev->getId( $this->wikiId ), |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
'rc_type' => [ RC_EDIT, RC_NEW, RC_LOG ], |
| 1148 |
], |
| 1149 |
__METHOD__, |
| 1150 |
$dbType |
| 1151 |
); |
| 1152 |
|
| 1153 |
|
| 1154 |
return $rc; |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
private function loadSlotContent( |
| 1177 |
SlotRecord $slot, |
| 1178 |
?string $blobData = null, |
| 1179 |
?string $blobFlags = null, |
| 1180 |
?string $blobFormat = null, |
| 1181 |
int $queryFlags = 0 |
| 1182 |
) { |
| 1183 |
if ( $blobData !== null ) { |
| 1184 |
$blobAddress = $slot->hasAddress() ? $slot->getAddress() : null; |
| 1185 |
|
| 1186 |
if ( $blobFlags === null ) { |
| 1187 |
|
| 1188 |
$data = $blobData; |
| 1189 |
} else { |
| 1190 |
try { |
| 1191 |
$data = $this->blobStore->expandBlob( $blobData, $blobFlags, $blobAddress ); |
| 1192 |
} catch ( BadBlobException $e ) { |
| 1193 |
throw new BadRevisionException( $e->getMessage(), [], 0, $e ); |
| 1194 |
} |
| 1195 |
|
| 1196 |
if ( $data === false ) { |
| 1197 |
throw new RevisionAccessException( |
| 1198 |
'Failed to expand blob data using flags {flags} (key: {cache_key})', |
| 1199 |
[ |
| 1200 |
'flags' => $blobFlags, |
| 1201 |
'cache_key' => $blobAddress, |
| 1202 |
] |
| 1203 |
); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
} else { |
| 1208 |
$address = $slot->getAddress(); |
| 1209 |
try { |
| 1210 |
$data = $this->blobStore->getBlob( $address, $queryFlags ); |
| 1211 |
} catch ( BadBlobException $e ) { |
| 1212 |
throw new BadRevisionException( $e->getMessage(), [], 0, $e ); |
| 1213 |
} catch ( BlobAccessException $e ) { |
| 1214 |
throw new RevisionAccessException( |
| 1215 |
'Failed to load data blob from {address} for revision {revision}. ' |
| 1216 |
. 'If this problem persist, use the findBadBlobs maintenance script ' |
| 1217 |
. 'to investigate the issue and mark bad blobs.', |
| 1218 |
[ 'address' => $e->getMessage(), 'revision' => $slot->getRevision() ], |
| 1219 |
0, |
| 1220 |
$e |
| 1221 |
); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
$model = $slot->getModel(); |
| 1226 |
|
| 1227 |
|
| 1228 |
if ( !$this->contentHandlerFactory->isDefinedModel( $model ) ) { |
| 1229 |
$this->logger->warning( |
| 1230 |
"Undefined content model '$model', falling back to FallbackContent", |
| 1231 |
[ |
| 1232 |
'content_address' => $slot->getAddress(), |
| 1233 |
'rev_id' => $slot->getRevision(), |
| 1234 |
'role_name' => $slot->getRole(), |
| 1235 |
'model_name' => $model, |
| 1236 |
'exception' => new RuntimeException() |
| 1237 |
] |
| 1238 |
); |
| 1239 |
|
| 1240 |
return new FallbackContent( $data, $model ); |
| 1241 |
} |
| 1242 |
|
| 1243 |
return $this->contentHandlerFactory |
| 1244 |
->getContentHandler( $model ) |
| 1245 |
->unserializeContent( $data, $blobFormat ); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
public function getRevisionById( $id, $flags = 0, ?PageIdentity $page = null ) { |
| 1266 |
return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags, $page ); |
| 1267 |
} |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
public function getRevisionByTitle( $page, $revId = 0, $flags = 0 ) { |
| 1286 |
$conds = [ |
| 1287 |
'page_namespace' => $page->getNamespace(), |
| 1288 |
'page_title' => $page->getDBkey() |
| 1289 |
]; |
| 1290 |
|
| 1291 |
if ( $page instanceof LinkTarget ) { |
| 1292 |
|
| 1293 |
$page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) : null; |
| 1294 |
} |
| 1295 |
|
| 1296 |
if ( $revId ) { |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
$conds['rev_id'] = $revId; |
| 1303 |
return $this->newRevisionFromConds( $conds, $flags, $page ); |
| 1304 |
} else { |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 1311 |
$conds[] = 'rev_id=page_latest'; |
| 1312 |
return $this->loadRevisionFromConds( $db, $conds, $flags, $page ); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) { |
| 1333 |
$conds = [ 'page_id' => $pageId ]; |
| 1334 |
if ( $revId ) { |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
$conds['rev_id'] = $revId; |
| 1341 |
return $this->newRevisionFromConds( $conds, $flags ); |
| 1342 |
} else { |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 1349 |
|
| 1350 |
$conds[] = 'rev_id=page_latest'; |
| 1351 |
|
| 1352 |
return $this->loadRevisionFromConds( $db, $conds, $flags ); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
public function getRevisionByTimestamp( |
| 1372 |
$page, |
| 1373 |
string $timestamp, |
| 1374 |
int $flags = IDBAccessObject::READ_NORMAL |
| 1375 |
): ?RevisionRecord { |
| 1376 |
if ( $page instanceof LinkTarget ) { |
| 1377 |
|
| 1378 |
$page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) : null; |
| 1379 |
} |
| 1380 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 1381 |
return $this->newRevisionFromConds( |
| 1382 |
[ |
| 1383 |
'rev_timestamp' => $db->timestamp( $timestamp ), |
| 1384 |
'page_namespace' => $page->getNamespace(), |
| 1385 |
'page_title' => $page->getDBkey() |
| 1386 |
], |
| 1387 |
$flags, |
| 1388 |
$page |
| 1389 |
); |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
private function loadSlotRecords( $revId, $queryFlags, PageIdentity $page ) { |
| 1400 |
|
| 1401 |
if ( $page->getNamespace() !== NS_TEMPLATE ) { |
| 1402 |
$res = $this->loadSlotRecordsFromDb( $revId, $queryFlags, $page ); |
| 1403 |
return $this->constructSlotRecords( $revId, $res, $queryFlags, $page ); |
| 1404 |
} |
| 1405 |
|
| 1406 |
$ttl = MediaWikiServices::getInstance() |
| 1407 |
->getMainConfig() |
| 1408 |
->get( MainConfigNames::RevisionSlotsCacheExpiry ); |
| 1409 |
|
| 1410 |
|
| 1411 |
$res = $this->localCache->getWithSetCallback( |
| 1412 |
$this->localCache->makeKey( |
| 1413 |
'revision-slots', |
| 1414 |
$page->getWikiId(), |
| 1415 |
$page->getId( $page->getWikiId() ), |
| 1416 |
$revId |
| 1417 |
), |
| 1418 |
$ttl['local'] ?? $this->localCache::TTL_UNCACHEABLE, |
| 1419 |
function () use ( $revId, $queryFlags, $page, $ttl ) { |
| 1420 |
return $this->cache->getWithSetCallback( |
| 1421 |
$this->cache->makeKey( |
| 1422 |
'revision-slots', |
| 1423 |
$page->getWikiId(), |
| 1424 |
$page->getId( $page->getWikiId() ), |
| 1425 |
$revId |
| 1426 |
), |
| 1427 |
$ttl['WAN'] ?? WANObjectCache::TTL_UNCACHEABLE, |
| 1428 |
function () use ( $revId, $queryFlags, $page ) { |
| 1429 |
$res = $this->loadSlotRecordsFromDb( $revId, $queryFlags, $page ); |
| 1430 |
if ( !$res ) { |
| 1431 |
|
| 1432 |
return false; |
| 1433 |
} |
| 1434 |
return $res; |
| 1435 |
} |
| 1436 |
); |
| 1437 |
} |
| 1438 |
); |
| 1439 |
if ( !$res ) { |
| 1440 |
$res = []; |
| 1441 |
} |
| 1442 |
|
| 1443 |
return $this->constructSlotRecords( $revId, $res, $queryFlags, $page ); |
| 1444 |
} |
| 1445 |
|
| 1446 |
private function loadSlotRecordsFromDb( $revId, $queryFlags, PageIdentity $page ): array { |
| 1447 |
$revQuery = $this->getSlotsQueryInfo( [ 'content' ] ); |
| 1448 |
|
| 1449 |
$db = $this->getDBConnectionRefForQueryFlags( $queryFlags ); |
| 1450 |
$res = $db->newSelectQueryBuilder() |
| 1451 |
->queryInfo( $revQuery ) |
| 1452 |
->where( [ 'slot_revision_id' => $revId ] ) |
| 1453 |
->recency( $queryFlags ) |
| 1454 |
->caller( __METHOD__ )->fetchResultSet(); |
| 1455 |
|
| 1456 |
if ( !$res->numRows() && !( $queryFlags & IDBAccessObject::READ_LATEST ) ) { |
| 1457 |
|
| 1458 |
$this->logger->info( |
| 1459 |
__METHOD__ . ' falling back to READ_LATEST.', |
| 1460 |
[ |
| 1461 |
'revid' => $revId, |
| 1462 |
'exception' => new RuntimeException(), |
| 1463 |
] |
| 1464 |
); |
| 1465 |
return $this->loadSlotRecordsFromDb( |
| 1466 |
$revId, |
| 1467 |
$queryFlags | IDBAccessObject::READ_LATEST, |
| 1468 |
$page |
| 1469 |
); |
| 1470 |
} |
| 1471 |
return iterator_to_array( $res ); |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
private function constructSlotRecords( |
| 1487 |
$revId, |
| 1488 |
$slotRows, |
| 1489 |
$queryFlags, |
| 1490 |
PageIdentity $page, |
| 1491 |
$slotContents = null |
| 1492 |
) { |
| 1493 |
$slots = []; |
| 1494 |
|
| 1495 |
foreach ( $slotRows as $row ) { |
| 1496 |
|
| 1497 |
if ( !isset( $row->role_name ) ) { |
| 1498 |
$row->role_name = $this->slotRoleStore->getName( (int)$row->slot_role_id ); |
| 1499 |
} |
| 1500 |
|
| 1501 |
if ( !isset( $row->model_name ) ) { |
| 1502 |
if ( isset( $row->content_model ) ) { |
| 1503 |
$row->model_name = $this->contentModelStore->getName( (int)$row->content_model ); |
| 1504 |
} else { |
| 1505 |
|
| 1506 |
|
| 1507 |
$slotRoleHandler = $this->slotRoleRegistry->getRoleHandler( $row->role_name ); |
| 1508 |
$row->model_name = $slotRoleHandler->getDefaultModel( $page ); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
if ( isset( $row->blob_data ) ) { |
| 1514 |
$slotContents[$row->content_address] = $row->blob_data; |
| 1515 |
} |
| 1516 |
|
| 1517 |
$contentCallback = function ( SlotRecord $slot ) use ( $slotContents, $queryFlags ) { |
| 1518 |
$blob = null; |
| 1519 |
if ( isset( $slotContents[$slot->getAddress()] ) ) { |
| 1520 |
$blob = $slotContents[$slot->getAddress()]; |
| 1521 |
if ( $blob instanceof Content ) { |
| 1522 |
return $blob; |
| 1523 |
} |
| 1524 |
} |
| 1525 |
return $this->loadSlotContent( $slot, $blob, null, null, $queryFlags ); |
| 1526 |
}; |
| 1527 |
|
| 1528 |
$slots[$row->role_name] = new SlotRecord( $row, $contentCallback ); |
| 1529 |
} |
| 1530 |
|
| 1531 |
if ( !isset( $slots[SlotRecord::MAIN] ) ) { |
| 1532 |
$this->logger->error( |
| 1533 |
__METHOD__ . ': Main slot of revision not found in database. See T212428.', |
| 1534 |
[ |
| 1535 |
'revid' => $revId, |
| 1536 |
'queryFlags' => $queryFlags, |
| 1537 |
'exception' => new RuntimeException(), |
| 1538 |
] |
| 1539 |
); |
| 1540 |
|
| 1541 |
throw new RevisionAccessException( |
| 1542 |
'Main slot of revision not found in database. See T212428.' |
| 1543 |
); |
| 1544 |
} |
| 1545 |
|
| 1546 |
return $slots; |
| 1547 |
} |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
private function newRevisionSlots( |
| 1563 |
$revId, |
| 1564 |
$slotRows, |
| 1565 |
$queryFlags, |
| 1566 |
PageIdentity $page |
| 1567 |
) { |
| 1568 |
if ( $slotRows ) { |
| 1569 |
$slots = new RevisionSlots( |
| 1570 |
$this->constructSlotRecords( $revId, $slotRows, $queryFlags, $page ) |
| 1571 |
); |
| 1572 |
} else { |
| 1573 |
$slots = new RevisionSlots( function () use( $revId, $queryFlags, $page ) { |
| 1574 |
return $this->loadSlotRecords( $revId, $queryFlags, $page ); |
| 1575 |
} ); |
| 1576 |
} |
| 1577 |
|
| 1578 |
return $slots; |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
public function newRevisionFromArchiveRow( |
| 1602 |
$row, |
| 1603 |
$queryFlags = 0, |
| 1604 |
?PageIdentity $page = null, |
| 1605 |
array $overrides = [] |
| 1606 |
) { |
| 1607 |
return $this->newRevisionFromArchiveRowAndSlots( $row, null, $queryFlags, $page, $overrides ); |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
public function newRevisionFromRow( |
| 1623 |
$row, |
| 1624 |
$queryFlags = 0, |
| 1625 |
?PageIdentity $page = null, |
| 1626 |
$fromCache = false |
| 1627 |
) { |
| 1628 |
return $this->newRevisionFromRowAndSlots( $row, null, $queryFlags, $page, $fromCache ); |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
public function newRevisionFromArchiveRowAndSlots( |
| 1650 |
stdClass $row, |
| 1651 |
$slots, |
| 1652 |
int $queryFlags = 0, |
| 1653 |
?PageIdentity $page = null, |
| 1654 |
array $overrides = [] |
| 1655 |
) { |
| 1656 |
if ( !$page && isset( $overrides['title'] ) ) { |
| 1657 |
if ( !( $overrides['title'] instanceof PageIdentity ) ) { |
| 1658 |
throw new InvalidArgumentException( 'title field override must contain a PageIdentity object.' ); |
| 1659 |
} |
| 1660 |
|
| 1661 |
$page = $overrides['title']; |
| 1662 |
} |
| 1663 |
|
| 1664 |
if ( !isset( $page ) ) { |
| 1665 |
if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) { |
| 1666 |
$page = Title::makeTitle( $row->ar_namespace, $row->ar_title ); |
| 1667 |
} else { |
| 1668 |
throw new InvalidArgumentException( |
| 1669 |
'A Title or ar_namespace and ar_title must be given' |
| 1670 |
); |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
foreach ( $overrides as $key => $value ) { |
| 1675 |
$field = "ar_$key"; |
| 1676 |
$row->$field = $value; |
| 1677 |
} |
| 1678 |
|
| 1679 |
try { |
| 1680 |
$user = $this->actorStore->newActorFromRowFields( |
| 1681 |
$row->ar_user ?? null, |
| 1682 |
$row->ar_user_text ?? null, |
| 1683 |
$row->ar_actor ?? null |
| 1684 |
); |
| 1685 |
} catch ( InvalidArgumentException $ex ) { |
| 1686 |
$this->logger->warning( 'Could not load user for archive revision {rev_id}', [ |
| 1687 |
'ar_rev_id' => $row->ar_rev_id, |
| 1688 |
'ar_actor' => $row->ar_actor ?? 'null', |
| 1689 |
'ar_user_text' => $row->ar_user_text ?? 'null', |
| 1690 |
'ar_user' => $row->ar_user ?? 'null', |
| 1691 |
'exception' => $ex |
| 1692 |
] ); |
| 1693 |
$user = $this->actorStore->getUnknownActor(); |
| 1694 |
} |
| 1695 |
|
| 1696 |
$db = $this->getDBConnectionRefForQueryFlags( $queryFlags ); |
| 1697 |
|
| 1698 |
$comment = $this->commentStore->getCommentLegacy( $db, 'ar_comment', $row, true ); |
| 1699 |
|
| 1700 |
if ( !( $slots instanceof RevisionSlots ) ) { |
| 1701 |
$slots = $this->newRevisionSlots( (int)$row->ar_rev_id, $slots, $queryFlags, $page ); |
| 1702 |
} |
| 1703 |
return new RevisionArchiveRecord( $page, $user, $comment, $row, $slots, $this->wikiId ); |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
public function newRevisionFromRowAndSlots( |
| 1724 |
stdClass $row, |
| 1725 |
$slots, |
| 1726 |
int $queryFlags = 0, |
| 1727 |
?PageIdentity $page = null, |
| 1728 |
bool $fromCache = false |
| 1729 |
) { |
| 1730 |
if ( !$page ) { |
| 1731 |
if ( isset( $row->page_id ) |
| 1732 |
&& isset( $row->page_namespace ) |
| 1733 |
&& isset( $row->page_title ) |
| 1734 |
) { |
| 1735 |
$page = new PageIdentityValue( |
| 1736 |
(int)$row->page_id, |
| 1737 |
(int)$row->page_namespace, |
| 1738 |
$row->page_title, |
| 1739 |
$this->wikiId |
| 1740 |
); |
| 1741 |
|
| 1742 |
$page = $this->wrapPage( $page ); |
| 1743 |
} else { |
| 1744 |
$pageId = (int)( $row->rev_page ?? 0 ); |
| 1745 |
$revId = (int)( $row->rev_id ?? 0 ); |
| 1746 |
|
| 1747 |
$page = $this->getPage( $pageId, $revId, $queryFlags ); |
| 1748 |
} |
| 1749 |
} else { |
| 1750 |
$page = $this->ensureRevisionRowMatchesPage( $row, $page ); |
| 1751 |
} |
| 1752 |
|
| 1753 |
if ( !$page ) { |
| 1754 |
|
| 1755 |
|
| 1756 |
throw new RevisionAccessException( |
| 1757 |
"Failed to determine page associated with revision {$row->rev_id}" |
| 1758 |
); |
| 1759 |
} |
| 1760 |
|
| 1761 |
try { |
| 1762 |
$user = $this->actorStore->newActorFromRowFields( |
| 1763 |
$row->rev_user ?? null, |
| 1764 |
$row->rev_user_text ?? null, |
| 1765 |
$row->rev_actor ?? null |
| 1766 |
); |
| 1767 |
} catch ( InvalidArgumentException $ex ) { |
| 1768 |
$this->logger->warning( 'Could not load user for revision {rev_id}', [ |
| 1769 |
'rev_id' => $row->rev_id, |
| 1770 |
'rev_actor' => $row->rev_actor ?? 'null', |
| 1771 |
'rev_user_text' => $row->rev_user_text ?? 'null', |
| 1772 |
'rev_user' => $row->rev_user ?? 'null', |
| 1773 |
'exception' => $ex |
| 1774 |
] ); |
| 1775 |
$user = $this->actorStore->getUnknownActor(); |
| 1776 |
} |
| 1777 |
|
| 1778 |
$db = $this->getDBConnectionRefForQueryFlags( $queryFlags ); |
| 1779 |
|
| 1780 |
$comment = $this->commentStore->getCommentLegacy( $db, 'rev_comment', $row, true ); |
| 1781 |
|
| 1782 |
if ( !( $slots instanceof RevisionSlots ) ) { |
| 1783 |
$slots = $this->newRevisionSlots( (int)$row->rev_id, $slots, $queryFlags, $page ); |
| 1784 |
} |
| 1785 |
|
| 1786 |
|
| 1787 |
if ( $fromCache ) { |
| 1788 |
$rev = new RevisionStoreCacheRecord( |
| 1789 |
function ( $revId ) use ( $queryFlags ) { |
| 1790 |
$db = $this->getDBConnectionRefForQueryFlags( $queryFlags ); |
| 1791 |
$row = $this->fetchRevisionRowFromConds( |
| 1792 |
$db, |
| 1793 |
[ 'rev_id' => intval( $revId ) ] |
| 1794 |
); |
| 1795 |
if ( !$row && !( $queryFlags & IDBAccessObject::READ_LATEST ) ) { |
| 1796 |
|
| 1797 |
$this->logger->info( |
| 1798 |
'RevisionStoreCacheRecord refresh callback falling back to READ_LATEST.', |
| 1799 |
[ |
| 1800 |
'revid' => $revId, |
| 1801 |
'exception' => new RuntimeException(), |
| 1802 |
] |
| 1803 |
); |
| 1804 |
$dbw = $this->getDBConnectionRefForQueryFlags( IDBAccessObject::READ_LATEST ); |
| 1805 |
$row = $this->fetchRevisionRowFromConds( |
| 1806 |
$dbw, |
| 1807 |
[ 'rev_id' => intval( $revId ) ] |
| 1808 |
); |
| 1809 |
} |
| 1810 |
if ( !$row ) { |
| 1811 |
return [ null, null ]; |
| 1812 |
} |
| 1813 |
return [ |
| 1814 |
$row->rev_deleted, |
| 1815 |
$this->actorStore->newActorFromRowFields( |
| 1816 |
$row->rev_user ?? null, |
| 1817 |
$row->rev_user_text ?? null, |
| 1818 |
$row->rev_actor ?? null |
| 1819 |
) |
| 1820 |
]; |
| 1821 |
}, |
| 1822 |
$page, $user, $comment, $row, $slots, $this->wikiId |
| 1823 |
); |
| 1824 |
} else { |
| 1825 |
$rev = new RevisionStoreRecord( |
| 1826 |
$page, $user, $comment, $row, $slots, $this->wikiId ); |
| 1827 |
} |
| 1828 |
return $rev; |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
private function ensureRevisionRowMatchesPage( $row, PageIdentity $page, $context = [] ) { |
| 1843 |
$revId = (int)( $row->rev_id ?? 0 ); |
| 1844 |
$revPageId = (int)( $row->rev_page ?? 0 ); |
| 1845 |
$expectedPageId = $page->getId( $this->wikiId ); |
| 1846 |
|
| 1847 |
if ( $revPageId && $expectedPageId && $revPageId !== $expectedPageId ) { |
| 1848 |
|
| 1849 |
$pageRec = $this->pageStore->getPageByName( |
| 1850 |
$page->getNamespace(), |
| 1851 |
$page->getDBkey(), |
| 1852 |
IDBAccessObject::READ_LATEST |
| 1853 |
); |
| 1854 |
$masterPageId = $pageRec->getId( $this->wikiId ); |
| 1855 |
$masterLatest = $pageRec->getLatest( $this->wikiId ); |
| 1856 |
if ( $revPageId === $masterPageId ) { |
| 1857 |
if ( $page instanceof Title ) { |
| 1858 |
|
| 1859 |
|
| 1860 |
$page->resetArticleID( $masterPageId ); |
| 1861 |
|
| 1862 |
} else { |
| 1863 |
$page = $pageRec; |
| 1864 |
} |
| 1865 |
|
| 1866 |
$this->logger->info( |
| 1867 |
"Encountered stale Title object", |
| 1868 |
[ |
| 1869 |
'page_id_stale' => $expectedPageId, |
| 1870 |
'page_id_reloaded' => $masterPageId, |
| 1871 |
'page_latest' => $masterLatest, |
| 1872 |
'rev_id' => $revId, |
| 1873 |
'exception' => new RuntimeException(), |
| 1874 |
] + $context |
| 1875 |
); |
| 1876 |
} else { |
| 1877 |
$expectedTitle = (string)$page; |
| 1878 |
if ( $page instanceof Title ) { |
| 1879 |
|
| 1880 |
$page = $this->titleFactory->newFromID( $revPageId ); |
| 1881 |
} else { |
| 1882 |
$page = $pageRec; |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
$this->logger->error( |
| 1892 |
"Encountered mismatching Title object (see T259022, T268910, T279832, T263340)", |
| 1893 |
[ |
| 1894 |
'expected_page_id' => $masterPageId, |
| 1895 |
'expected_page_title' => $expectedTitle, |
| 1896 |
'rev_page' => $revPageId, |
| 1897 |
'rev_page_title' => (string)$page, |
| 1898 |
'page_latest' => $masterLatest, |
| 1899 |
'rev_id' => $revId, |
| 1900 |
'exception' => new RuntimeException(), |
| 1901 |
] + $context |
| 1902 |
); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
|
| 1907 |
return $page; |
| 1908 |
} |
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
public function newRevisionsFromBatch( |
| 1936 |
$rows, |
| 1937 |
array $options = [], |
| 1938 |
$queryFlags = 0, |
| 1939 |
?PageIdentity $page = null |
| 1940 |
) { |
| 1941 |
$result = new StatusValue(); |
| 1942 |
$archiveMode = $options['archive'] ?? false; |
| 1943 |
|
| 1944 |
if ( $archiveMode ) { |
| 1945 |
$revIdField = 'ar_rev_id'; |
| 1946 |
} else { |
| 1947 |
$revIdField = 'rev_id'; |
| 1948 |
} |
| 1949 |
|
| 1950 |
$rowsByRevId = []; |
| 1951 |
$pageIdsToFetchTitles = []; |
| 1952 |
$titlesByPageKey = []; |
| 1953 |
foreach ( $rows as $row ) { |
| 1954 |
if ( isset( $rowsByRevId[$row->$revIdField] ) ) { |
| 1955 |
$result->warning( |
| 1956 |
'internalerror_info', |
| 1957 |
"Duplicate rows in newRevisionsFromBatch, $revIdField {$row->$revIdField}" |
| 1958 |
); |
| 1959 |
} |
| 1960 |
|
| 1961 |
|
| 1962 |
$row->_page_key = |
| 1963 |
$archiveMode ? $row->ar_namespace . ':' . $row->ar_title : $row->rev_page; |
| 1964 |
|
| 1965 |
if ( $page ) { |
| 1966 |
if ( !$archiveMode && $row->rev_page != $this->getArticleId( $page ) ) { |
| 1967 |
throw new InvalidArgumentException( |
| 1968 |
"Revision {$row->$revIdField} doesn't belong to page " |
| 1969 |
. $this->getArticleId( $page ) |
| 1970 |
); |
| 1971 |
} |
| 1972 |
|
| 1973 |
if ( $archiveMode |
| 1974 |
&& ( $row->ar_namespace != $page->getNamespace() |
| 1975 |
|| $row->ar_title !== $page->getDBkey() ) |
| 1976 |
) { |
| 1977 |
throw new InvalidArgumentException( |
| 1978 |
"Revision {$row->$revIdField} doesn't belong to page " |
| 1979 |
. $page |
| 1980 |
); |
| 1981 |
} |
| 1982 |
} elseif ( !isset( $titlesByPageKey[ $row->_page_key ] ) ) { |
| 1983 |
if ( isset( $row->page_namespace ) && isset( $row->page_title ) |
| 1984 |
|
| 1985 |
|
| 1986 |
&& isset( $row->page_id ) && isset( $row->rev_page ) |
| 1987 |
&& $row->rev_page === $row->page_id |
| 1988 |
) { |
| 1989 |
$titlesByPageKey[ $row->_page_key ] = Title::newFromRow( $row ); |
| 1990 |
} elseif ( $archiveMode ) { |
| 1991 |
|
| 1992 |
$titlesByPageKey[ $row->_page_key ] = |
| 1993 |
Title::makeTitle( $row->ar_namespace, $row->ar_title ); |
| 1994 |
} else { |
| 1995 |
$pageIdsToFetchTitles[] = $row->rev_page; |
| 1996 |
} |
| 1997 |
} |
| 1998 |
$rowsByRevId[$row->$revIdField] = $row; |
| 1999 |
} |
| 2000 |
|
| 2001 |
if ( !$rowsByRevId ) { |
| 2002 |
$result->setResult( true, [] ); |
| 2003 |
return $result; |
| 2004 |
} |
| 2005 |
|
| 2006 |
|
| 2007 |
if ( $page ) { |
| 2008 |
|
| 2009 |
$pageKey = $archiveMode |
| 2010 |
? $page->getNamespace() . ':' . $page->getDBkey() |
| 2011 |
: $this->getArticleId( $page ); |
| 2012 |
|
| 2013 |
$titlesByPageKey[$pageKey] = $page; |
| 2014 |
} elseif ( $pageIdsToFetchTitles ) { |
| 2015 |
|
| 2016 |
|
| 2017 |
Assert::invariant( !$archiveMode, 'Titles are not loaded by ID in archive mode.' ); |
| 2018 |
|
| 2019 |
$pageIdsToFetchTitles = array_unique( $pageIdsToFetchTitles ); |
| 2020 |
$pageRecords = $this->pageStore |
| 2021 |
->newSelectQueryBuilder() |
| 2022 |
->wherePageIds( $pageIdsToFetchTitles ) |
| 2023 |
->caller( __METHOD__ ) |
| 2024 |
->fetchPageRecordArray(); |
| 2025 |
|
| 2026 |
$titlesByPageKey = $pageRecords + $titlesByPageKey; |
| 2027 |
} |
| 2028 |
|
| 2029 |
|
| 2030 |
$newRevisionRecord = $archiveMode |
| 2031 |
? [ $this, 'newRevisionFromArchiveRowAndSlots' ] |
| 2032 |
: [ $this, 'newRevisionFromRowAndSlots' ]; |
| 2033 |
|
| 2034 |
if ( !isset( $options['slots'] ) ) { |
| 2035 |
$result->setResult( |
| 2036 |
true, |
| 2037 |
array_map( |
| 2038 |
static function ( $row ) |
| 2039 |
use ( $queryFlags, $titlesByPageKey, $result, $newRevisionRecord, $revIdField ) { |
| 2040 |
try { |
| 2041 |
if ( !isset( $titlesByPageKey[$row->_page_key] ) ) { |
| 2042 |
$result->warning( |
| 2043 |
'internalerror_info', |
| 2044 |
"Couldn't find title for rev {$row->$revIdField} " |
| 2045 |
. "(page key {$row->_page_key})" |
| 2046 |
); |
| 2047 |
return null; |
| 2048 |
} |
| 2049 |
return $newRevisionRecord( $row, null, $queryFlags, |
| 2050 |
$titlesByPageKey[ $row->_page_key ] ); |
| 2051 |
} catch ( MWException $e ) { |
| 2052 |
$result->warning( 'internalerror_info', $e->getMessage() ); |
| 2053 |
return null; |
| 2054 |
} |
| 2055 |
}, |
| 2056 |
$rowsByRevId |
| 2057 |
) |
| 2058 |
); |
| 2059 |
return $result; |
| 2060 |
} |
| 2061 |
|
| 2062 |
$slotRowOptions = [ |
| 2063 |
'slots' => $options['slots'] ?? true, |
| 2064 |
'blobs' => $options['content'] ?? false, |
| 2065 |
]; |
| 2066 |
|
| 2067 |
if ( is_array( $slotRowOptions['slots'] ) |
| 2068 |
&& !in_array( SlotRecord::MAIN, $slotRowOptions['slots'] ) |
| 2069 |
) { |
| 2070 |
|
| 2071 |
$slotRowOptions['slots'][] = SlotRecord::MAIN; |
| 2072 |
} |
| 2073 |
|
| 2074 |
$slotRowsStatus = $this->getSlotRowsForBatch( $rowsByRevId, $slotRowOptions, $queryFlags ); |
| 2075 |
|
| 2076 |
$result->merge( $slotRowsStatus ); |
| 2077 |
$slotRowsByRevId = $slotRowsStatus->getValue(); |
| 2078 |
|
| 2079 |
$result->setResult( |
| 2080 |
true, |
| 2081 |
array_map( |
| 2082 |
function ( $row ) |
| 2083 |
use ( $slotRowsByRevId, $queryFlags, $titlesByPageKey, $result, |
| 2084 |
$revIdField, $newRevisionRecord |
| 2085 |
) { |
| 2086 |
if ( !isset( $slotRowsByRevId[$row->$revIdField] ) ) { |
| 2087 |
$result->warning( |
| 2088 |
'internalerror_info', |
| 2089 |
"Couldn't find slots for rev {$row->$revIdField}" |
| 2090 |
); |
| 2091 |
return null; |
| 2092 |
} |
| 2093 |
if ( !isset( $titlesByPageKey[$row->_page_key] ) ) { |
| 2094 |
$result->warning( |
| 2095 |
'internalerror_info', |
| 2096 |
"Couldn't find title for rev {$row->$revIdField} " |
| 2097 |
. "(page key {$row->_page_key})" |
| 2098 |
); |
| 2099 |
return null; |
| 2100 |
} |
| 2101 |
try { |
| 2102 |
return $newRevisionRecord( |
| 2103 |
$row, |
| 2104 |
new RevisionSlots( |
| 2105 |
$this->constructSlotRecords( |
| 2106 |
$row->$revIdField, |
| 2107 |
$slotRowsByRevId[$row->$revIdField], |
| 2108 |
$queryFlags, |
| 2109 |
$titlesByPageKey[$row->_page_key] |
| 2110 |
) |
| 2111 |
), |
| 2112 |
$queryFlags, |
| 2113 |
$titlesByPageKey[$row->_page_key] |
| 2114 |
); |
| 2115 |
} catch ( MWException $e ) { |
| 2116 |
$result->warning( 'internalerror_info', $e->getMessage() ); |
| 2117 |
return null; |
| 2118 |
} |
| 2119 |
}, |
| 2120 |
$rowsByRevId |
| 2121 |
) |
| 2122 |
); |
| 2123 |
return $result; |
| 2124 |
} |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
private function getSlotRowsForBatch( |
| 2150 |
$rowsOrIds, |
| 2151 |
array $options = [], |
| 2152 |
$queryFlags = 0 |
| 2153 |
) { |
| 2154 |
$result = new StatusValue(); |
| 2155 |
|
| 2156 |
$revIds = []; |
| 2157 |
foreach ( $rowsOrIds as $row ) { |
| 2158 |
if ( is_object( $row ) ) { |
| 2159 |
$revIds[] = isset( $row->ar_rev_id ) ? (int)$row->ar_rev_id : (int)$row->rev_id; |
| 2160 |
} else { |
| 2161 |
$revIds[] = (int)$row; |
| 2162 |
} |
| 2163 |
} |
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
if ( !$revIds ) { |
| 2168 |
$result->setResult( true, [] ); |
| 2169 |
return $result; |
| 2170 |
} |
| 2171 |
|
| 2172 |
|
| 2173 |
$slotQueryInfo = $this->getSlotsQueryInfo( [ 'content' ] ); |
| 2174 |
$revIdField = $slotQueryInfo['keys']['rev_id']; |
| 2175 |
$slotQueryConds = [ $revIdField => $revIds ]; |
| 2176 |
|
| 2177 |
if ( isset( $options['slots'] ) && is_array( $options['slots'] ) ) { |
| 2178 |
$slotIds = []; |
| 2179 |
foreach ( $options['slots'] as $slot ) { |
| 2180 |
try { |
| 2181 |
$slotIds[] = $this->slotRoleStore->getId( $slot ); |
| 2182 |
} catch ( NameTableAccessException $exception ) { |
| 2183 |
|
| 2184 |
|
| 2185 |
} |
| 2186 |
} |
| 2187 |
if ( $slotIds === [] ) { |
| 2188 |
|
| 2189 |
$result->setResult( true, array_fill_keys( $revIds, [] ) ); |
| 2190 |
return $result; |
| 2191 |
} |
| 2192 |
|
| 2193 |
$roleIdField = $slotQueryInfo['keys']['role_id']; |
| 2194 |
$slotQueryConds[$roleIdField] = $slotIds; |
| 2195 |
} |
| 2196 |
|
| 2197 |
$db = $this->getDBConnectionRefForQueryFlags( $queryFlags ); |
| 2198 |
$slotRows = $db->newSelectQueryBuilder() |
| 2199 |
->queryInfo( $slotQueryInfo ) |
| 2200 |
->where( $slotQueryConds ) |
| 2201 |
->caller( __METHOD__ ) |
| 2202 |
->fetchResultSet(); |
| 2203 |
|
| 2204 |
$slotContents = null; |
| 2205 |
if ( $options['blobs'] ?? false ) { |
| 2206 |
$blobAddresses = []; |
| 2207 |
foreach ( $slotRows as $slotRow ) { |
| 2208 |
$blobAddresses[] = $slotRow->content_address; |
| 2209 |
} |
| 2210 |
$slotContentFetchStatus = $this->blobStore |
| 2211 |
->getBlobBatch( $blobAddresses, $queryFlags ); |
| 2212 |
foreach ( $slotContentFetchStatus->getMessages() as $msg ) { |
| 2213 |
$result->warning( $msg ); |
| 2214 |
} |
| 2215 |
$slotContents = $slotContentFetchStatus->getValue(); |
| 2216 |
} |
| 2217 |
|
| 2218 |
$slotRowsByRevId = []; |
| 2219 |
foreach ( $slotRows as $slotRow ) { |
| 2220 |
if ( $slotContents === null ) { |
| 2221 |
|
| 2222 |
} elseif ( isset( $slotContents[$slotRow->content_address] ) ) { |
| 2223 |
$slotRow->blob_data = $slotContents[$slotRow->content_address]; |
| 2224 |
} else { |
| 2225 |
$result->warning( |
| 2226 |
'internalerror_info', |
| 2227 |
"Couldn't find blob data for rev {$slotRow->slot_revision_id}" |
| 2228 |
); |
| 2229 |
$slotRow->blob_data = null; |
| 2230 |
} |
| 2231 |
|
| 2232 |
|
| 2233 |
if ( !isset( $slotRow->role_name ) && isset( $slotRow->slot_role_id ) ) { |
| 2234 |
$slotRow->role_name = $this->slotRoleStore->getName( (int)$slotRow->slot_role_id ); |
| 2235 |
} |
| 2236 |
|
| 2237 |
|
| 2238 |
if ( !isset( $slotRow->model_name ) && isset( $slotRow->content_model ) ) { |
| 2239 |
$slotRow->model_name = $this->contentModelStore->getName( (int)$slotRow->content_model ); |
| 2240 |
} |
| 2241 |
|
| 2242 |
$slotRowsByRevId[$slotRow->slot_revision_id][$slotRow->role_name] = $slotRow; |
| 2243 |
} |
| 2244 |
|
| 2245 |
$result->setResult( true, $slotRowsByRevId ); |
| 2246 |
return $result; |
| 2247 |
} |
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
public function getContentBlobsForBatch( |
| 2270 |
$rowsOrIds, |
| 2271 |
$slots = null, |
| 2272 |
$queryFlags = 0 |
| 2273 |
) { |
| 2274 |
$result = $this->getSlotRowsForBatch( |
| 2275 |
$rowsOrIds, |
| 2276 |
[ 'slots' => $slots, 'blobs' => true ], |
| 2277 |
$queryFlags |
| 2278 |
); |
| 2279 |
|
| 2280 |
if ( $result->isOK() ) { |
| 2281 |
|
| 2282 |
foreach ( $result->value as $revId => $rowsByRole ) { |
| 2283 |
foreach ( $rowsByRole as $role => $slotRow ) { |
| 2284 |
if ( is_array( $slots ) && !in_array( $role, $slots ) ) { |
| 2285 |
|
| 2286 |
|
| 2287 |
unset( $result->value[$revId][$role] ); |
| 2288 |
continue; |
| 2289 |
} |
| 2290 |
|
| 2291 |
$result->value[$revId][$role] = (object)[ |
| 2292 |
'blob_data' => $slotRow->blob_data, |
| 2293 |
'model_name' => $slotRow->model_name, |
| 2294 |
]; |
| 2295 |
} |
| 2296 |
} |
| 2297 |
} |
| 2298 |
|
| 2299 |
return $result; |
| 2300 |
} |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
private function newRevisionFromConds( |
| 2319 |
array $conditions, |
| 2320 |
int $flags = IDBAccessObject::READ_NORMAL, |
| 2321 |
?PageIdentity $page = null, |
| 2322 |
array $options = [] |
| 2323 |
) { |
| 2324 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 2325 |
$rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $page, $options ); |
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
if ( !$rev |
| 2330 |
&& !( $flags & IDBAccessObject::READ_LATEST ) |
| 2331 |
&& $this->loadBalancer->hasStreamingReplicaServers() |
| 2332 |
&& $this->loadBalancer->hasOrMadeRecentPrimaryChanges() |
| 2333 |
) { |
| 2334 |
$flags = IDBAccessObject::READ_LATEST; |
| 2335 |
$dbw = $this->getPrimaryConnection(); |
| 2336 |
$rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $page, $options ); |
| 2337 |
} |
| 2338 |
|
| 2339 |
return $rev; |
| 2340 |
} |
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
private function loadRevisionFromConds( |
| 2357 |
IReadableDatabase $db, |
| 2358 |
array $conditions, |
| 2359 |
int $flags = IDBAccessObject::READ_NORMAL, |
| 2360 |
?PageIdentity $page = null, |
| 2361 |
array $options = [] |
| 2362 |
) { |
| 2363 |
$row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags, $options ); |
| 2364 |
if ( $row ) { |
| 2365 |
return $this->newRevisionFromRow( $row, $flags, $page ); |
| 2366 |
} |
| 2367 |
|
| 2368 |
return null; |
| 2369 |
} |
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
private function checkDatabaseDomain( IReadableDatabase $db ) { |
| 2378 |
$dbDomain = $db->getDomainID(); |
| 2379 |
$storeDomain = $this->loadBalancer->resolveDomainID( $this->wikiId ); |
| 2380 |
if ( $dbDomain === $storeDomain ) { |
| 2381 |
return; |
| 2382 |
} |
| 2383 |
|
| 2384 |
throw new RuntimeException( "DB connection domain '$dbDomain' does not match '$storeDomain'" ); |
| 2385 |
} |
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
private function fetchRevisionRowFromConds( |
| 2401 |
IReadableDatabase $db, |
| 2402 |
array $conditions, |
| 2403 |
int $flags = IDBAccessObject::READ_NORMAL, |
| 2404 |
array $options = [] |
| 2405 |
) { |
| 2406 |
$this->checkDatabaseDomain( $db ); |
| 2407 |
|
| 2408 |
$queryBuilder = $this->newSelectQueryBuilder( $db ) |
| 2409 |
->joinComment() |
| 2410 |
->joinPage() |
| 2411 |
->joinUser() |
| 2412 |
->where( $conditions ) |
| 2413 |
->options( $options ); |
| 2414 |
if ( ( $flags & IDBAccessObject::READ_LOCKING ) == IDBAccessObject::READ_LOCKING ) { |
| 2415 |
$queryBuilder->forUpdate(); |
| 2416 |
} |
| 2417 |
return $queryBuilder->caller( __METHOD__ )->fetchRow(); |
| 2418 |
} |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
public function getQueryInfo( $options = [] ) { |
| 2443 |
$ret = [ |
| 2444 |
'tables' => [], |
| 2445 |
'fields' => [], |
| 2446 |
'joins' => [], |
| 2447 |
]; |
| 2448 |
|
| 2449 |
$ret['tables'] = array_merge( $ret['tables'], [ |
| 2450 |
'revision', |
| 2451 |
'actor_rev_user' => 'actor', |
| 2452 |
] ); |
| 2453 |
$ret['fields'] = array_merge( $ret['fields'], [ |
| 2454 |
'rev_id', |
| 2455 |
'rev_page', |
| 2456 |
'rev_actor' => 'rev_actor', |
| 2457 |
'rev_user' => 'actor_rev_user.actor_user', |
| 2458 |
'rev_user_text' => 'actor_rev_user.actor_name', |
| 2459 |
'rev_timestamp', |
| 2460 |
'rev_minor_edit', |
| 2461 |
'rev_deleted', |
| 2462 |
'rev_len', |
| 2463 |
'rev_parent_id', |
| 2464 |
'rev_sha1', |
| 2465 |
] ); |
| 2466 |
$ret['joins']['actor_rev_user'] = [ 'JOIN', "actor_rev_user.actor_id = rev_actor" ]; |
| 2467 |
|
| 2468 |
$commentQuery = $this->commentStore->getJoin( 'rev_comment' ); |
| 2469 |
$ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] ); |
| 2470 |
$ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] ); |
| 2471 |
$ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] ); |
| 2472 |
|
| 2473 |
if ( in_array( 'page', $options, true ) ) { |
| 2474 |
$ret['tables'][] = 'page'; |
| 2475 |
$ret['fields'] = array_merge( $ret['fields'], [ |
| 2476 |
'page_namespace', |
| 2477 |
'page_title', |
| 2478 |
'page_id', |
| 2479 |
'page_latest', |
| 2480 |
'page_is_redirect', |
| 2481 |
'page_len', |
| 2482 |
] ); |
| 2483 |
$ret['joins']['page'] = [ 'JOIN', [ 'page_id = rev_page' ] ]; |
| 2484 |
} |
| 2485 |
|
| 2486 |
if ( in_array( 'user', $options, true ) ) { |
| 2487 |
$ret['tables'][] = 'user'; |
| 2488 |
$ret['fields'] = array_merge( $ret['fields'], [ |
| 2489 |
'user_name', |
| 2490 |
] ); |
| 2491 |
$ret['joins']['user'] = [ |
| 2492 |
'LEFT JOIN', |
| 2493 |
[ 'actor_rev_user.actor_user != 0', 'user_id = actor_rev_user.actor_user' ] |
| 2494 |
]; |
| 2495 |
} |
| 2496 |
|
| 2497 |
if ( in_array( 'text', $options, true ) ) { |
| 2498 |
throw new InvalidArgumentException( |
| 2499 |
'The `text` option is no longer supported in MediaWiki 1.35 and later.' |
| 2500 |
); |
| 2501 |
} |
| 2502 |
|
| 2503 |
return $ret; |
| 2504 |
} |
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
public function newSelectQueryBuilder( IReadableDatabase $dbr ): RevisionSelectQueryBuilder { |
| 2510 |
return new RevisionSelectQueryBuilder( $dbr ); |
| 2511 |
} |
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
public function newArchiveSelectQueryBuilder( IReadableDatabase $dbr ): ArchiveSelectQueryBuilder { |
| 2517 |
return new ArchiveSelectQueryBuilder( $dbr ); |
| 2518 |
} |
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
|
| 2541 |
public function getSlotsQueryInfo( $options = [] ) { |
| 2542 |
$ret = [ |
| 2543 |
'tables' => [], |
| 2544 |
'fields' => [], |
| 2545 |
'joins' => [], |
| 2546 |
'keys' => [], |
| 2547 |
]; |
| 2548 |
|
| 2549 |
$ret['keys']['rev_id'] = 'slot_revision_id'; |
| 2550 |
$ret['keys']['role_id'] = 'slot_role_id'; |
| 2551 |
|
| 2552 |
$ret['tables'][] = 'slots'; |
| 2553 |
$ret['fields'] = array_merge( $ret['fields'], [ |
| 2554 |
'slot_revision_id', |
| 2555 |
'slot_content_id', |
| 2556 |
'slot_origin', |
| 2557 |
'slot_role_id', |
| 2558 |
] ); |
| 2559 |
|
| 2560 |
if ( in_array( 'role', $options, true ) ) { |
| 2561 |
|
| 2562 |
|
| 2563 |
$ret['tables'][] = 'slot_roles'; |
| 2564 |
$ret['joins']['slot_roles'] = [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ]; |
| 2565 |
$ret['fields'][] = 'role_name'; |
| 2566 |
} |
| 2567 |
|
| 2568 |
if ( in_array( 'content', $options, true ) ) { |
| 2569 |
$ret['keys']['model_id'] = 'content_model'; |
| 2570 |
|
| 2571 |
$ret['tables'][] = 'content'; |
| 2572 |
$ret['fields'] = array_merge( $ret['fields'], [ |
| 2573 |
'content_size', |
| 2574 |
'content_sha1', |
| 2575 |
'content_address', |
| 2576 |
'content_model', |
| 2577 |
] ); |
| 2578 |
$ret['joins']['content'] = [ 'JOIN', [ 'slot_content_id = content_id' ] ]; |
| 2579 |
|
| 2580 |
if ( in_array( 'model', $options, true ) ) { |
| 2581 |
|
| 2582 |
|
| 2583 |
$ret['tables'][] = 'content_models'; |
| 2584 |
$ret['joins']['content_models'] = [ 'LEFT JOIN', [ 'content_model = model_id' ] ]; |
| 2585 |
$ret['fields'][] = 'model_name'; |
| 2586 |
} |
| 2587 |
|
| 2588 |
} |
| 2589 |
|
| 2590 |
return $ret; |
| 2591 |
} |
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
public function isRevisionRow( $row, string $table = '' ) { |
| 2602 |
if ( !( $row instanceof stdClass ) ) { |
| 2603 |
return false; |
| 2604 |
} |
| 2605 |
$queryInfo = $table === 'archive' ? $this->getArchiveQueryInfo() : $this->getQueryInfo(); |
| 2606 |
foreach ( $queryInfo['fields'] as $alias => $field ) { |
| 2607 |
$name = is_numeric( $alias ) ? $field : $alias; |
| 2608 |
if ( !property_exists( $row, $name ) ) { |
| 2609 |
return false; |
| 2610 |
} |
| 2611 |
} |
| 2612 |
return true; |
| 2613 |
} |
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
public function getArchiveQueryInfo() { |
| 2635 |
$commentQuery = $this->commentStore->getJoin( 'ar_comment' ); |
| 2636 |
$ret = [ |
| 2637 |
'tables' => [ |
| 2638 |
'archive', |
| 2639 |
'archive_actor' => 'actor' |
| 2640 |
] + $commentQuery['tables'], |
| 2641 |
'fields' => [ |
| 2642 |
'ar_id', |
| 2643 |
'ar_page_id', |
| 2644 |
'ar_namespace', |
| 2645 |
'ar_title', |
| 2646 |
'ar_rev_id', |
| 2647 |
'ar_timestamp', |
| 2648 |
'ar_minor_edit', |
| 2649 |
'ar_deleted', |
| 2650 |
'ar_len', |
| 2651 |
'ar_parent_id', |
| 2652 |
'ar_sha1', |
| 2653 |
'ar_actor', |
| 2654 |
'ar_user' => 'archive_actor.actor_user', |
| 2655 |
'ar_user_text' => 'archive_actor.actor_name', |
| 2656 |
] + $commentQuery['fields'], |
| 2657 |
'joins' => [ |
| 2658 |
'archive_actor' => [ 'JOIN', 'actor_id=ar_actor' ] |
| 2659 |
] + $commentQuery['joins'], |
| 2660 |
]; |
| 2661 |
|
| 2662 |
return $ret; |
| 2663 |
} |
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
public function getRevisionSizes( array $revIds ) { |
| 2675 |
$dbr = $this->getReplicaConnection(); |
| 2676 |
$revLens = []; |
| 2677 |
if ( !$revIds ) { |
| 2678 |
return $revLens; |
| 2679 |
} |
| 2680 |
|
| 2681 |
$res = $dbr->newSelectQueryBuilder() |
| 2682 |
->select( [ 'rev_id', 'rev_len' ] ) |
| 2683 |
->from( 'revision' ) |
| 2684 |
->where( [ 'rev_id' => $revIds ] ) |
| 2685 |
->caller( __METHOD__ )->fetchResultSet(); |
| 2686 |
|
| 2687 |
foreach ( $res as $row ) { |
| 2688 |
$revLens[$row->rev_id] = intval( $row->rev_len ); |
| 2689 |
} |
| 2690 |
|
| 2691 |
return $revLens; |
| 2692 |
} |
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
private function getRelativeRevision( RevisionRecord $rev, $flags, $dir ) { |
| 2703 |
$op = $dir === 'next' ? '>' : '<'; |
| 2704 |
$sort = $dir === 'next' ? 'ASC' : 'DESC'; |
| 2705 |
|
| 2706 |
$revisionIdValue = $rev->getId( $this->wikiId ); |
| 2707 |
|
| 2708 |
if ( !$revisionIdValue || !$rev->getPageId( $this->wikiId ) ) { |
| 2709 |
|
| 2710 |
return null; |
| 2711 |
} |
| 2712 |
|
| 2713 |
if ( $rev instanceof RevisionArchiveRecord ) { |
| 2714 |
|
| 2715 |
return null; |
| 2716 |
} |
| 2717 |
|
| 2718 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 2719 |
|
| 2720 |
$ts = $rev->getTimestamp() ?? $this->getTimestampFromId( $revisionIdValue, $flags ); |
| 2721 |
if ( $ts === false ) { |
| 2722 |
|
| 2723 |
$ts = $db->newSelectQueryBuilder() |
| 2724 |
->select( 'ar_timestamp' ) |
| 2725 |
->from( 'archive' ) |
| 2726 |
->where( [ 'ar_rev_id' => $revisionIdValue ] ) |
| 2727 |
->caller( __METHOD__ )->fetchField(); |
| 2728 |
if ( $ts === false ) { |
| 2729 |
|
| 2730 |
return null; |
| 2731 |
} |
| 2732 |
} |
| 2733 |
|
| 2734 |
$revId = $db->newSelectQueryBuilder() |
| 2735 |
->select( 'rev_id' ) |
| 2736 |
->from( 'revision' ) |
| 2737 |
->where( [ |
| 2738 |
'rev_page' => $rev->getPageId( $this->wikiId ), |
| 2739 |
$db->buildComparison( $op, [ |
| 2740 |
'rev_timestamp' => $db->timestamp( $ts ), |
| 2741 |
'rev_id' => $revisionIdValue, |
| 2742 |
] ), |
| 2743 |
] ) |
| 2744 |
->orderBy( [ 'rev_timestamp', 'rev_id' ], $sort ) |
| 2745 |
->ignoreIndex( 'rev_timestamp' ) |
| 2746 |
->caller( __METHOD__ ) |
| 2747 |
->fetchField(); |
| 2748 |
|
| 2749 |
if ( $revId === false ) { |
| 2750 |
return null; |
| 2751 |
} |
| 2752 |
|
| 2753 |
return $this->getRevisionById( intval( $revId ), $flags ); |
| 2754 |
} |
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
public function getPreviousRevision( RevisionRecord $rev, $flags = IDBAccessObject::READ_NORMAL ) { |
| 2771 |
return $this->getRelativeRevision( $rev, $flags, 'prev' ); |
| 2772 |
} |
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
public function getNextRevision( RevisionRecord $rev, $flags = IDBAccessObject::READ_NORMAL ) { |
| 2786 |
return $this->getRelativeRevision( $rev, $flags, 'next' ); |
| 2787 |
} |
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
private function getPreviousRevisionId( IReadableDatabase $db, RevisionRecord $rev ) { |
| 2801 |
$this->checkDatabaseDomain( $db ); |
| 2802 |
|
| 2803 |
if ( $rev->getPageId( $this->wikiId ) === null ) { |
| 2804 |
return 0; |
| 2805 |
} |
| 2806 |
|
| 2807 |
if ( !$rev->getId( $this->wikiId ) ) { |
| 2808 |
$prevId = $db->newSelectQueryBuilder() |
| 2809 |
->select( 'page_latest' ) |
| 2810 |
->from( 'page' ) |
| 2811 |
->where( [ 'page_id' => $rev->getPageId( $this->wikiId ) ] ) |
| 2812 |
->caller( __METHOD__ )->fetchField(); |
| 2813 |
} else { |
| 2814 |
$prevId = $db->newSelectQueryBuilder() |
| 2815 |
->select( 'rev_id' ) |
| 2816 |
->from( 'revision' ) |
| 2817 |
->where( [ 'rev_page' => $rev->getPageId( $this->wikiId ) ] ) |
| 2818 |
->andWhere( $db->expr( 'rev_id', '<', $rev->getId( $this->wikiId ) ) ) |
| 2819 |
->orderBy( 'rev_id DESC' ) |
| 2820 |
->caller( __METHOD__ )->fetchField(); |
| 2821 |
} |
| 2822 |
return intval( $prevId ); |
| 2823 |
} |
| 2824 |
|
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
|
| 2830 |
|
| 2831 |
|
| 2832 |
|
| 2833 |
|
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
public function getTimestampFromId( $id, $flags = 0 ) { |
| 2838 |
if ( $id instanceof Title ) { |
| 2839 |
|
| 2840 |
$id = $flags; |
| 2841 |
$flags = func_num_args() > 2 ? func_get_arg( 2 ) : 0; |
| 2842 |
} |
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
if ( $id === null || $id <= 0 ) { |
| 2850 |
return false; |
| 2851 |
} |
| 2852 |
|
| 2853 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 2854 |
|
| 2855 |
$timestamp = |
| 2856 |
$db->newSelectQueryBuilder() |
| 2857 |
->select( 'rev_timestamp' ) |
| 2858 |
->from( 'revision' ) |
| 2859 |
->where( [ 'rev_id' => $id ] ) |
| 2860 |
->caller( __METHOD__ )->fetchField(); |
| 2861 |
|
| 2862 |
return ( $timestamp !== false ) ? MWTimestamp::convert( TS_MW, $timestamp ) : false; |
| 2863 |
} |
| 2864 |
|
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
public function countRevisionsByPageId( IReadableDatabase $db, $id ) { |
| 2875 |
$this->checkDatabaseDomain( $db ); |
| 2876 |
|
| 2877 |
$row = $db->newSelectQueryBuilder() |
| 2878 |
->select( [ 'revCount' => 'COUNT(*)' ] ) |
| 2879 |
->from( 'revision' ) |
| 2880 |
->where( [ 'rev_page' => $id ] ) |
| 2881 |
->caller( __METHOD__ )->fetchRow(); |
| 2882 |
if ( $row ) { |
| 2883 |
return intval( $row->revCount ); |
| 2884 |
} |
| 2885 |
return 0; |
| 2886 |
} |
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
public function countRevisionsByTitle( IReadableDatabase $db, PageIdentity $page ) { |
| 2898 |
$id = $this->getArticleId( $page ); |
| 2899 |
if ( $id ) { |
| 2900 |
return $this->countRevisionsByPageId( $db, $id ); |
| 2901 |
} |
| 2902 |
return 0; |
| 2903 |
} |
| 2904 |
|
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
|
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
public function userWasLastToEdit( IReadableDatabase $db, $pageId, $userId, $since ) { |
| 2924 |
$this->checkDatabaseDomain( $db ); |
| 2925 |
|
| 2926 |
if ( !$userId ) { |
| 2927 |
return false; |
| 2928 |
} |
| 2929 |
|
| 2930 |
$queryBuilder = $this->newSelectQueryBuilder( $db ) |
| 2931 |
->where( [ |
| 2932 |
'rev_page' => $pageId, |
| 2933 |
$db->expr( 'rev_timestamp', '>', $db->timestamp( $since ) ) |
| 2934 |
] ) |
| 2935 |
->orderBy( 'rev_timestamp', SelectQueryBuilder::SORT_ASC ) |
| 2936 |
->limit( 50 ); |
| 2937 |
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 2938 |
foreach ( $res as $row ) { |
| 2939 |
if ( $row->rev_user != $userId ) { |
| 2940 |
return false; |
| 2941 |
} |
| 2942 |
} |
| 2943 |
return true; |
| 2944 |
} |
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
public function getKnownCurrentRevision( PageIdentity $page, $revId = 0 ) { |
| 2960 |
$db = $this->getReplicaConnection(); |
| 2961 |
$revIdPassed = $revId; |
| 2962 |
$pageId = $this->getArticleId( $page ); |
| 2963 |
if ( !$pageId ) { |
| 2964 |
return false; |
| 2965 |
} |
| 2966 |
|
| 2967 |
if ( !$revId ) { |
| 2968 |
if ( $page instanceof Title ) { |
| 2969 |
$revId = $page->getLatestRevID(); |
| 2970 |
} else { |
| 2971 |
$pageRecord = $this->pageStore->getPageByReference( $page ); |
| 2972 |
if ( $pageRecord ) { |
| 2973 |
$revId = $pageRecord->getLatest( $this->getWikiId() ); |
| 2974 |
} |
| 2975 |
} |
| 2976 |
} |
| 2977 |
|
| 2978 |
if ( !$revId ) { |
| 2979 |
$this->logger->warning( |
| 2980 |
'No latest revision known for page {page} even though it exists with page ID {page_id}', [ |
| 2981 |
'page' => $page->__toString(), |
| 2982 |
'page_id' => $pageId, |
| 2983 |
'wiki_id' => $this->getWikiId() ?: 'local', |
| 2984 |
] ); |
| 2985 |
return false; |
| 2986 |
} |
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
$fromCache = true; |
| 2992 |
$row = $this->cache->getWithSetCallback( |
| 2993 |
|
| 2994 |
$this->getRevisionRowCacheKey( $db, $pageId, $revId ), |
| 2995 |
WANObjectCache::TTL_WEEK, |
| 2996 |
function ( $curValue, &$ttl, array &$setOpts ) use ( |
| 2997 |
$db, $revId, &$fromCache |
| 2998 |
) { |
| 2999 |
$setOpts += Database::getCacheSetOptions( $db ); |
| 3000 |
$row = $this->fetchRevisionRowFromConds( $db, [ 'rev_id' => intval( $revId ) ] ); |
| 3001 |
if ( $row ) { |
| 3002 |
$fromCache = false; |
| 3003 |
} |
| 3004 |
return $row; |
| 3005 |
} |
| 3006 |
); |
| 3007 |
|
| 3008 |
|
| 3009 |
if ( $row ) { |
| 3010 |
$title = $this->ensureRevisionRowMatchesPage( $row, $page, [ |
| 3011 |
'from_cache_flag' => $fromCache, |
| 3012 |
'page_id_initial' => $pageId, |
| 3013 |
'rev_id_used' => $revId, |
| 3014 |
'rev_id_requested' => $revIdPassed, |
| 3015 |
] ); |
| 3016 |
|
| 3017 |
return $this->newRevisionFromRow( $row, 0, $title, $fromCache ); |
| 3018 |
} else { |
| 3019 |
return false; |
| 3020 |
} |
| 3021 |
} |
| 3022 |
|
| 3023 |
|
| 3024 |
|
| 3025 |
|
| 3026 |
|
| 3027 |
|
| 3028 |
|
| 3029 |
|
| 3030 |
|
| 3031 |
public function getFirstRevision( |
| 3032 |
$page, |
| 3033 |
int $flags = IDBAccessObject::READ_NORMAL |
| 3034 |
): ?RevisionRecord { |
| 3035 |
if ( $page instanceof LinkTarget ) { |
| 3036 |
|
| 3037 |
$page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) : null; |
| 3038 |
} |
| 3039 |
return $this->newRevisionFromConds( |
| 3040 |
[ |
| 3041 |
'page_namespace' => $page->getNamespace(), |
| 3042 |
'page_title' => $page->getDBkey() |
| 3043 |
], |
| 3044 |
$flags, |
| 3045 |
$page, |
| 3046 |
[ |
| 3047 |
'ORDER BY' => [ 'rev_timestamp ASC', 'rev_id ASC' ], |
| 3048 |
'IGNORE INDEX' => [ 'revision' => 'rev_timestamp' ], |
| 3049 |
] |
| 3050 |
); |
| 3051 |
} |
| 3052 |
|
| 3053 |
|
| 3054 |
|
| 3055 |
|
| 3056 |
|
| 3057 |
|
| 3058 |
|
| 3059 |
|
| 3060 |
|
| 3061 |
|
| 3062 |
|
| 3063 |
|
| 3064 |
private function getRevisionRowCacheKey( IReadableDatabase $db, $pageId, $revId ) { |
| 3065 |
return $this->cache->makeGlobalKey( |
| 3066 |
self::ROW_CACHE_KEY, |
| 3067 |
$db->getDomainID(), |
| 3068 |
$pageId, |
| 3069 |
$revId |
| 3070 |
); |
| 3071 |
} |
| 3072 |
|
| 3073 |
|
| 3074 |
|
| 3075 |
|
| 3076 |
|
| 3077 |
|
| 3078 |
|
| 3079 |
private function assertRevisionParameter( $paramName, $pageId, ?RevisionRecord $rev = null ) { |
| 3080 |
if ( $rev ) { |
| 3081 |
if ( $rev->getId( $this->wikiId ) === null ) { |
| 3082 |
throw new InvalidArgumentException( "Unsaved {$paramName} revision passed" ); |
| 3083 |
} |
| 3084 |
if ( $rev->getPageId( $this->wikiId ) !== $pageId ) { |
| 3085 |
throw new InvalidArgumentException( |
| 3086 |
"Revision {$rev->getId( $this->wikiId )} doesn't belong to page {$pageId}" |
| 3087 |
); |
| 3088 |
} |
| 3089 |
} |
| 3090 |
} |
| 3091 |
|
| 3092 |
|
| 3093 |
|
| 3094 |
|
| 3095 |
|
| 3096 |
|
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
|
| 3105 |
|
| 3106 |
private function getRevisionLimitConditions( |
| 3107 |
ISQLPlatform $dbr, |
| 3108 |
?RevisionRecord $old = null, |
| 3109 |
?RevisionRecord $new = null, |
| 3110 |
$options = [] |
| 3111 |
) { |
| 3112 |
$options = (array)$options; |
| 3113 |
if ( in_array( self::INCLUDE_OLD, $options ) || in_array( self::INCLUDE_BOTH, $options ) ) { |
| 3114 |
$oldCmp = '>='; |
| 3115 |
} else { |
| 3116 |
$oldCmp = '>'; |
| 3117 |
} |
| 3118 |
if ( in_array( self::INCLUDE_NEW, $options ) || in_array( self::INCLUDE_BOTH, $options ) ) { |
| 3119 |
$newCmp = '<='; |
| 3120 |
} else { |
| 3121 |
$newCmp = '<'; |
| 3122 |
} |
| 3123 |
|
| 3124 |
$conds = []; |
| 3125 |
if ( $old ) { |
| 3126 |
$conds[] = $dbr->buildComparison( $oldCmp, [ |
| 3127 |
'rev_timestamp' => $dbr->timestamp( $old->getTimestamp() ), |
| 3128 |
'rev_id' => $old->getId( $this->wikiId ), |
| 3129 |
] ); |
| 3130 |
} |
| 3131 |
if ( $new ) { |
| 3132 |
$conds[] = $dbr->buildComparison( $newCmp, [ |
| 3133 |
'rev_timestamp' => $dbr->timestamp( $new->getTimestamp() ), |
| 3134 |
'rev_id' => $new->getId( $this->wikiId ), |
| 3135 |
] ); |
| 3136 |
} |
| 3137 |
return $conds; |
| 3138 |
} |
| 3139 |
|
| 3140 |
|
| 3141 |
|
| 3142 |
|
| 3143 |
|
| 3144 |
|
| 3145 |
|
| 3146 |
|
| 3147 |
|
| 3148 |
|
| 3149 |
|
| 3150 |
|
| 3151 |
|
| 3152 |
|
| 3153 |
|
| 3154 |
|
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
|
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
public function getRevisionIdsBetween( |
| 3167 |
int $pageId, |
| 3168 |
?RevisionRecord $old = null, |
| 3169 |
?RevisionRecord $new = null, |
| 3170 |
?int $max = null, |
| 3171 |
$options = [], |
| 3172 |
?string $order = null, |
| 3173 |
int $flags = IDBAccessObject::READ_NORMAL |
| 3174 |
): array { |
| 3175 |
$this->assertRevisionParameter( 'old', $pageId, $old ); |
| 3176 |
$this->assertRevisionParameter( 'new', $pageId, $new ); |
| 3177 |
|
| 3178 |
$options = (array)$options; |
| 3179 |
$includeOld = in_array( self::INCLUDE_OLD, $options ) || |
| 3180 |
in_array( self::INCLUDE_BOTH, $options ); |
| 3181 |
$includeNew = in_array( self::INCLUDE_NEW, $options ) || |
| 3182 |
in_array( self::INCLUDE_BOTH, $options ); |
| 3183 |
|
| 3184 |
|
| 3185 |
|
| 3186 |
|
| 3187 |
|
| 3188 |
if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) { |
| 3189 |
return $includeOld || $includeNew ? [ $new->getId( $this->wikiId ) ] : []; |
| 3190 |
} |
| 3191 |
|
| 3192 |
$db = $this->getDBConnectionRefForQueryFlags( $flags ); |
| 3193 |
$queryBuilder = $db->newSelectQueryBuilder() |
| 3194 |
->select( 'rev_id' ) |
| 3195 |
->from( 'revision' ) |
| 3196 |
->where( [ |
| 3197 |
'rev_page' => $pageId, |
| 3198 |
$db->bitAnd( 'rev_deleted', RevisionRecord::DELETED_TEXT ) . ' = 0' |
| 3199 |
] ) |
| 3200 |
->andWhere( $this->getRevisionLimitConditions( $db, $old, $new, $options ) ); |
| 3201 |
|
| 3202 |
if ( $order !== null ) { |
| 3203 |
$queryBuilder->orderBy( [ 'rev_timestamp', 'rev_id' ], $order ); |
| 3204 |
} |
| 3205 |
if ( $max !== null ) { |
| 3206 |
|
| 3207 |
$queryBuilder->limit( $max + 1 ); |
| 3208 |
} |
| 3209 |
|
| 3210 |
$values = $queryBuilder->caller( __METHOD__ )->fetchFieldValues(); |
| 3211 |
return array_map( 'intval', $values ); |
| 3212 |
} |
| 3213 |
|
| 3214 |
|
| 3215 |
|
| 3216 |
|
| 3217 |
|
| 3218 |
|
| 3219 |
|
| 3220 |
|
| 3221 |
|
| 3222 |
|
| 3223 |
|
| 3224 |
|
| 3225 |
|
| 3226 |
|
| 3227 |
|
| 3228 |
|
| 3229 |
|
| 3230 |
|
| 3231 |
|
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
public function getAuthorsBetween( |
| 3236 |
$pageId, |
| 3237 |
?RevisionRecord $old = null, |
| 3238 |
?RevisionRecord $new = null, |
| 3239 |
?Authority $performer = null, |
| 3240 |
$max = null, |
| 3241 |
$options = [] |
| 3242 |
) { |
| 3243 |
$this->assertRevisionParameter( 'old', $pageId, $old ); |
| 3244 |
$this->assertRevisionParameter( 'new', $pageId, $new ); |
| 3245 |
$options = (array)$options; |
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
|
| 3250 |
|
| 3251 |
if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) { |
| 3252 |
if ( !$options ) { |
| 3253 |
return []; |
| 3254 |
} elseif ( $performer ) { |
| 3255 |
return [ $new->getUser( RevisionRecord::FOR_THIS_USER, $performer ) ]; |
| 3256 |
} else { |
| 3257 |
return [ $new->getUser() ]; |
| 3258 |
} |
| 3259 |
} |
| 3260 |
|
| 3261 |
$dbr = $this->getReplicaConnection(); |
| 3262 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 3263 |
->select( [ |
| 3264 |
'rev_actor', |
| 3265 |
'rev_user' => 'revision_actor.actor_user', |
| 3266 |
'rev_user_text' => 'revision_actor.actor_name', |
| 3267 |
] ) |
| 3268 |
->distinct() |
| 3269 |
->from( 'revision' ) |
| 3270 |
->join( 'actor', 'revision_actor', 'revision_actor.actor_id = rev_actor' ) |
| 3271 |
->where( [ |
| 3272 |
'rev_page' => $pageId, |
| 3273 |
$dbr->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . " = 0" |
| 3274 |
] ) |
| 3275 |
->andWhere( $this->getRevisionLimitConditions( $dbr, $old, $new, $options ) ) |
| 3276 |
->caller( __METHOD__ ); |
| 3277 |
if ( $max !== null ) { |
| 3278 |
$queryBuilder->limit( $max + 1 ); |
| 3279 |
} |
| 3280 |
|
| 3281 |
return array_map( |
| 3282 |
function ( $row ) { |
| 3283 |
return $this->actorStore->newActorFromRowFields( |
| 3284 |
$row->rev_user, |
| 3285 |
$row->rev_user_text, |
| 3286 |
$row->rev_actor |
| 3287 |
); |
| 3288 |
}, |
| 3289 |
iterator_to_array( $queryBuilder->fetchResultSet() ) |
| 3290 |
); |
| 3291 |
} |
| 3292 |
|
| 3293 |
|
| 3294 |
|
| 3295 |
|
| 3296 |
|
| 3297 |
|
| 3298 |
|
| 3299 |
|
| 3300 |
|
| 3301 |
|
| 3302 |
|
| 3303 |
|
| 3304 |
|
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
|
| 3310 |
|
| 3311 |
|
| 3312 |
|
| 3313 |
|
| 3314 |
public function countAuthorsBetween( |
| 3315 |
$pageId, |
| 3316 |
?RevisionRecord $old = null, |
| 3317 |
?RevisionRecord $new = null, |
| 3318 |
?Authority $performer = null, |
| 3319 |
$max = null, |
| 3320 |
$options = [] |
| 3321 |
) { |
| 3322 |
|
| 3323 |
|
| 3324 |
return count( $this->getAuthorsBetween( $pageId, $old, $new, $performer, $max, $options ) ); |
| 3325 |
} |
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
|
| 3333 |
|
| 3334 |
|
| 3335 |
|
| 3336 |
|
| 3337 |
|
| 3338 |
|
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
|
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
public function countRevisionsBetween( |
| 3348 |
$pageId, |
| 3349 |
?RevisionRecord $old = null, |
| 3350 |
?RevisionRecord $new = null, |
| 3351 |
$max = null, |
| 3352 |
$options = [] |
| 3353 |
) { |
| 3354 |
$this->assertRevisionParameter( 'old', $pageId, $old ); |
| 3355 |
$this->assertRevisionParameter( 'new', $pageId, $new ); |
| 3356 |
|
| 3357 |
|
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) { |
| 3362 |
return 0; |
| 3363 |
} |
| 3364 |
|
| 3365 |
$dbr = $this->getReplicaConnection(); |
| 3366 |
$conds = array_merge( |
| 3367 |
[ |
| 3368 |
'rev_page' => $pageId, |
| 3369 |
$dbr->bitAnd( 'rev_deleted', RevisionRecord::DELETED_TEXT ) . " = 0" |
| 3370 |
], |
| 3371 |
$this->getRevisionLimitConditions( $dbr, $old, $new, $options ) |
| 3372 |
); |
| 3373 |
if ( $max !== null ) { |
| 3374 |
return $dbr->newSelectQueryBuilder() |
| 3375 |
->select( '1' ) |
| 3376 |
->from( 'revision' ) |
| 3377 |
->where( $conds ) |
| 3378 |
->caller( __METHOD__ ) |
| 3379 |
->limit( $max + 1 ) |
| 3380 |
->fetchRowCount(); |
| 3381 |
} else { |
| 3382 |
return (int)$dbr->newSelectQueryBuilder() |
| 3383 |
->select( 'count(*)' ) |
| 3384 |
->from( 'revision' ) |
| 3385 |
->where( $conds ) |
| 3386 |
->caller( __METHOD__ )->fetchField(); |
| 3387 |
} |
| 3388 |
} |
| 3389 |
|
| 3390 |
|
| 3391 |
|
| 3392 |
|
| 3393 |
|
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
|
| 3399 |
|
| 3400 |
|
| 3401 |
public function findIdenticalRevision( |
| 3402 |
RevisionRecord $revision, |
| 3403 |
int $searchLimit |
| 3404 |
): ?RevisionRecord { |
| 3405 |
$revision->assertWiki( $this->wikiId ); |
| 3406 |
$db = $this->getReplicaConnection(); |
| 3407 |
$subquery = $this->newSelectQueryBuilder( $db ) |
| 3408 |
->joinComment() |
| 3409 |
->where( [ 'rev_page' => $revision->getPageId( $this->wikiId ) ] ) |
| 3410 |
|
| 3411 |
->orderBy( [ 'rev_timestamp', 'rev_id' ], SelectQueryBuilder::SORT_DESC ) |
| 3412 |
|
| 3413 |
->useIndex( [ 'revision' => 'rev_page_timestamp' ] ) |
| 3414 |
->limit( $searchLimit ) |
| 3415 |
|
| 3416 |
->offset( 1 ) |
| 3417 |
->caller( __METHOD__ ); |
| 3418 |
|
| 3419 |
|
| 3420 |
$revisionRow = $db->newSelectQueryBuilder() |
| 3421 |
->select( '*' ) |
| 3422 |
->from( $subquery, 'recent_revs' ) |
| 3423 |
->where( [ 'rev_sha1' => $revision->getSha1() ] ) |
| 3424 |
->caller( __METHOD__ )->fetchRow(); |
| 3425 |
|
| 3426 |
return $revisionRow ? $this->newRevisionFromRow( $revisionRow ) : null; |
| 3427 |
} |
| 3428 |
|
| 3429 |
|
| 3430 |
} |