33 use InvalidArgumentException;
62 use Psr\Log\LoggerAwareInterface;
63 use Psr\Log\LoggerInterface;
64 use Psr\Log\NullLogger;
71 use Wikimedia\Assert\Assert;
72 use Wikimedia\IPUtils;
117 private $loadBalancer;
132 private $commentStore;
137 private $actorMigration;
150 private $contentModelStore;
155 private $slotRoleStore;
158 private $slotRoleRegistry;
161 private $contentHandlerFactory;
170 private $titleFactory;
212 $wikiId = WikiAwareEntity::LOCAL
214 Assert::parameterType( [
'string',
'false' ], $wikiId,
'$wikiId' );
216 $this->loadBalancer = $loadBalancer;
217 $this->blobStore = $blobStore;
218 $this->cache = $cache;
219 $this->localCache = $localCache;
220 $this->commentStore = $commentStore;
221 $this->contentModelStore = $contentModelStore;
222 $this->slotRoleStore = $slotRoleStore;
223 $this->slotRoleRegistry = $slotRoleRegistry;
224 $this->actorMigration = $actorMigration;
225 $this->actorStore = $actorStore;
226 $this->wikiId = $wikiId;
227 $this->logger =
new NullLogger();
228 $this->contentHandlerFactory = $contentHandlerFactory;
229 $this->pageStore = $pageStore;
230 $this->titleFactory = $titleFactory;
231 $this->hookRunner =
new HookRunner( $hookContainer );
235 $this->logger = $logger;
242 return $this->blobStore->isReadOnly();
251 return $this->wikiId;
259 private function getDBConnectionRefForQueryFlags( $queryFlags ) {
261 return $this->getDBConnectionRef( $mode );
269 private function getDBConnectionRef( $mode, $groups = [] ) {
270 return $this->loadBalancer->getConnectionRef( $mode, $groups, $this->wikiId );
289 public function getTitle( $pageId, $revId, $queryFlags = self::READ_NORMAL ) {
291 if ( $this->wikiId !== WikiAwareEntity::LOCAL ) {
292 wfDeprecatedMsg(
'Using a Title object to refer to a page on another site.',
'1.36' );
295 $page = $this->getPage( $pageId, $revId, $queryFlags );
296 return $this->titleFactory->newFromPageIdentity( $page );
309 private function getPage( ?
int $pageId, ?
int $revId,
int $queryFlags = self::READ_NORMAL ) {
310 if ( !$pageId && !$revId ) {
311 throw new InvalidArgumentException(
'$pageId and $revId cannot both be 0 or null' );
317 $queryFlags = self::READ_NORMAL;
321 if ( $pageId !==
null && $pageId > 0 ) {
322 $page = $this->pageStore->getPageById( $pageId, $queryFlags );
324 return $this->wrapPage( $page );
329 if ( $revId !==
null && $revId > 0 ) {
330 $pageQuery = $this->pageStore->newSelectQueryBuilder( $queryFlags )
331 ->join(
'revision',
null,
'page_id=rev_page' )
332 ->conds( [
'rev_id' => $revId ] )
333 ->caller( __METHOD__ );
335 $page = $pageQuery->fetchPageRecord();
337 return $this->wrapPage( $page );
342 if ( $queryFlags === self::READ_NORMAL ) {
343 $title = $this->getPage( $pageId, $revId, self::READ_LATEST );
346 __METHOD__ .
' fell back to READ_LATEST and got a Title.',
347 [
'exception' =>
new RuntimeException() ]
353 throw new RevisionAccessException(
354 'Could not determine title for page ID {page_id} and revision ID {rev_id}',
356 'page_id' => $pageId,
367 private function wrapPage( PageIdentity $page ): PageIdentity {
368 if ( $this->wikiId === WikiAwareEntity::LOCAL ) {
375 return $this->titleFactory->newFromPageIdentity( $page );
388 private function failOnNull( $value, $name ) {
389 if ( $value ===
null ) {
390 throw new IncompleteRevisionException(
391 "$name must not be " . var_export( $value,
true ) .
"!"
405 private function failOnEmpty( $value, $name ) {
406 if ( $value ===
null || $value === 0 || $value ===
'' ) {
407 throw new IncompleteRevisionException(
408 "$name must not be " . var_export( $value,
true ) .
"!"
428 $this->checkDatabaseDomain( $dbw );
435 'main slot must be provided'
440 $this->failOnNull( $rev->
getSize(),
'size field' );
441 $this->failOnEmpty( $rev->
getSha1(),
'sha1 field' );
442 $this->failOnEmpty( $rev->
getTimestamp(),
'timestamp field' );
445 $this->failOnNull( $user->getId(),
'user field' );
446 $this->failOnEmpty( $user->getName(),
'user_text field' );
458 Assert::precondition(
459 $mainSlot->getSize() === $rev->
getSize(),
460 'The revisions\'s size must match the main slot\'s size (see T239717)'
462 Assert::precondition(
463 $mainSlot->getSha1() === $rev->
getSha1(),
464 'The revisions\'s SHA1 hash must match the main slot\'s SHA1 hash (see T239717)'
468 $pageId = $this->failOnEmpty( $rev->
getPageId( $this->wikiId ),
'rev_page field' );
470 $parentId = $rev->
getParentId() ?? $this->getPreviousRevisionId( $dbw, $rev );
475 function (
IDatabase $dbw, $fname ) use (
482 return $this->insertRevisionInternal(
494 Assert::postcondition( $rev->
getId( $this->wikiId ) > 0,
'revision must have an ID' );
495 Assert::postcondition( $rev->
getPageId( $this->wikiId ) > 0,
'revision must have a page ID' );
496 Assert::postcondition(
498 'revision must have a comment'
500 Assert::postcondition(
502 'revision must have a user'
511 foreach ( $slotRoles as $role ) {
513 Assert::postcondition(
514 $slot->getContent() !==
null,
515 $role .
' slot must have content'
517 Assert::postcondition(
518 $slot->hasRevision(),
519 $role .
' slot must have a revision associated'
523 $this->hookRunner->onRevisionRecordInserted( $rev );
545 $this->checkDatabaseDomain( $dbw );
549 Assert::precondition(
550 $this->slotRoleRegistry->getRoleHandler( $role )->isDerived(),
551 'Trying to modify a slot that is not derived'
555 $isDerived = $this->slotRoleRegistry->getRoleHandler( $role )->isDerived();
556 Assert::precondition(
558 'Trying to remove a slot that is not derived'
560 throw new LogicException(
'Removing derived slots is not yet implemented. See T277394.' );
566 function ( IDatabase $dbw, $fname ) use (
570 return $this->updateSlotsInternal(
572 $revisionSlotsUpdate,
578 foreach ( $slotRecords as $role => $slot ) {
579 Assert::postcondition(
580 $slot->getContent() !==
null,
581 $role .
' slot must have content'
583 Assert::postcondition(
584 $slot->hasRevision(),
585 $role .
' slot must have a revision associated'
598 private function updateSlotsInternal(
599 RevisionRecord $revision,
600 RevisionSlotsUpdate $revisionSlotsUpdate,
603 $page = $revision->getPage();
604 $revId = $revision->
getId( $this->wikiId );
606 BlobStore::PAGE_HINT => $page->
getId( $this->wikiId ),
607 BlobStore::REVISION_HINT => $revId,
608 BlobStore::PARENT_HINT => $revision->getParentId( $this->wikiId ),
612 foreach ( $revisionSlotsUpdate->getModifiedRoles() as $role ) {
613 $slot = $revisionSlotsUpdate->getModifiedSlot( $role );
614 $newSlots[$role] = $this->insertSlotOn( $dbw, $revId, $slot, $page, $blobHints );
620 private function insertRevisionInternal(
624 CommentStoreComment $comment,
629 $slotRoles = $rev->getSlotRoles();
631 $revisionRow = $this->insertRevisionRowOn(
637 $revisionId = $revisionRow[
'rev_id'];
640 BlobStore::PAGE_HINT => $pageId,
641 BlobStore::REVISION_HINT => $revisionId,
642 BlobStore::PARENT_HINT => $parentId,
646 foreach ( $slotRoles as $role ) {
647 $slot = $rev->getSlot( $role, RevisionRecord::RAW );
656 if ( $slot->hasRevision() && $slot->hasContentId() ) {
659 $slot->getRevision() === $revisionId,
660 'slot role ' . $slot->getRole(),
661 'Existing slot should belong to revision '
662 . $revisionId .
', but belongs to revision ' . $slot->getRevision() .
'!'
668 $newSlots[$role] = $slot;
670 $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $page, $blobHints );
674 $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
676 $rev =
new RevisionStoreRecord(
680 (
object)$revisionRow,
681 new RevisionSlots( $newSlots ),
696 private function insertSlotOn(
699 SlotRecord $protoSlot,
701 array $blobHints = []
703 if ( $protoSlot->hasAddress() ) {
704 $blobAddress = $protoSlot->getAddress();
706 $blobAddress = $this->storeContentBlob( $protoSlot, $page, $blobHints );
709 if ( $protoSlot->hasContentId() ) {
710 $contentId = $protoSlot->getContentId();
712 $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
715 $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
717 return SlotRecord::newSaved(
732 private function insertIpChangesRow(
738 if ( !$user->isRegistered() && IPUtils::isValid( $user->getName() ) ) {
740 'ipc_rev_id' => $revisionId,
741 'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
742 'ipc_hex' => IPUtils::toHex( $user->getName() ),
744 $dbw->insert(
'ip_changes', $ipcRow, __METHOD__ );
758 private function insertRevisionRowOn(
763 $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $parentId );
765 [ $commentFields, $commentCallback ] =
766 $this->commentStore->insertWithTempTable(
769 $rev->getComment( RevisionRecord::RAW )
771 $revisionRow += $commentFields;
773 [ $actorFields, $actorCallback ] =
774 $this->actorMigration->getInsertValuesWithTempTable(
777 $rev->getUser( RevisionRecord::RAW )
779 $revisionRow += $actorFields;
781 $dbw->insert(
'revision', $revisionRow, __METHOD__ );
783 if ( !isset( $revisionRow[
'rev_id'] ) ) {
785 $revisionRow[
'rev_id'] = intval( $dbw->insertId() );
787 if ( $dbw->getType() ===
'mysql' ) {
792 $maxRevId = intval( $dbw->selectField(
'archive',
'MAX(ar_rev_id)',
'', __METHOD__ ) );
794 $maxRevId2 = intval( $dbw->selectField(
'slots',
'MAX(slot_revision_id)',
'', __METHOD__ ) );
795 if ( $maxRevId2 >= $maxRevId ) {
796 $maxRevId = $maxRevId2;
800 if ( $maxRevId >= $revisionRow[
'rev_id'] ) {
801 $this->logger->debug(
802 '__METHOD__: Inserted revision {revid} but {table} has revisions up to {maxrevid}.'
803 .
' Trying to fix it.',
805 'revid' => $revisionRow[
'rev_id'],
807 'maxrevid' => $maxRevId,
811 if ( !$dbw->lock(
'fix-for-T202032', __METHOD__ ) ) {
812 throw new MWException(
'Failed to get database lock for T202032' );
815 $dbw->onTransactionResolution(
816 static function ( $trigger, IDatabase $dbw ) use ( $fname ) {
817 $dbw->unlock(
'fix-for-T202032', $fname );
822 $dbw->delete(
'revision', [
'rev_id' => $revisionRow[
'rev_id'] ], __METHOD__ );
834 $dbw->selectSQLText(
'archive', [
'v' =>
"MAX(ar_rev_id)" ],
'', __METHOD__ ) .
' FOR UPDATE',
839 $dbw->selectSQLText(
'slots', [
'v' =>
"MAX(slot_revision_id)" ],
'', __METHOD__ )
846 $row1 ? intval( $row1->v ) : 0,
847 $row2 ? intval( $row2->v ) : 0
853 $revisionRow[
'rev_id'] = $maxRevId + 1;
854 $dbw->insert(
'revision', $revisionRow, __METHOD__ );
859 $commentCallback( $revisionRow[
'rev_id'] );
860 $actorCallback( $revisionRow[
'rev_id'], $revisionRow );
872 private function getBaseRevisionRow(
879 'rev_page' => $rev->getPageId( $this->wikiId ),
880 'rev_parent_id' => $parentId,
881 'rev_minor_edit' => $rev->isMinor() ? 1 : 0,
882 'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
883 'rev_deleted' => $rev->getVisibility(),
884 'rev_len' => $rev->getSize(),
885 'rev_sha1' => $rev->getSha1(),
888 if ( $rev->getId( $this->wikiId ) !==
null ) {
890 $revisionRow[
'rev_id'] = $rev->getId( $this->wikiId );
904 private function storeContentBlob(
907 array $blobHints = []
910 $format =
$content->getDefaultFormat();
913 $this->checkContent(
$content, $page, $slot->getRole() );
915 return $this->blobStore->storeBlob(
922 BlobStore::DESIGNATION_HINT =>
'page-content',
923 BlobStore::ROLE_HINT => $slot->getRole(),
924 BlobStore::SHA1_HINT => $slot->getSha1(),
925 BlobStore::MODEL_HINT => $model,
926 BlobStore::FORMAT_HINT => $format,
938 private function insertSlotRowOn( SlotRecord $slot, IDatabase $dbw, $revisionId, $contentId ) {
940 'slot_revision_id' => $revisionId,
941 'slot_role_id' => $this->slotRoleStore->acquireId( $slot->getRole() ),
942 'slot_content_id' => $contentId,
945 'slot_origin' => $slot->hasOrigin() ? $slot->getOrigin() : $revisionId,
947 $dbw->insert(
'slots', $slotRow, __METHOD__ );
956 private function insertContentRowOn( SlotRecord $slot, IDatabase $dbw, $blobAddress ) {
958 'content_size' => $slot->getSize(),
959 'content_sha1' => $slot->getSha1(),
960 'content_model' => $this->contentModelStore->acquireId( $slot->getModel() ),
961 'content_address' => $blobAddress,
963 $dbw->insert(
'content', $contentRow, __METHOD__ );
964 return intval( $dbw->insertId() );
977 private function checkContent(
Content $content, PageIdentity $page,
string $role ) {
981 $format =
$content->getDefaultFormat();
982 $handler =
$content->getContentHandler();
984 if ( !$handler->isSupportedFormat( $format ) ) {
986 "Can't use format $format with content model $model on $page role $role"
992 "New content for $page role $role is not valid! Content model is $model"
1029 $this->checkDatabaseDomain( $dbw );
1031 $pageId = $this->getArticleId( $page );
1039 [
'page_id' => $pageId ],
1044 if ( !$pageLatest ) {
1045 $msg =
'T235589: Failed to select table row during null revision creation' .
1046 " Page id '$pageId' does not exist.";
1047 $this->logger->error(
1049 [
'exception' =>
new RuntimeException( $msg ) ]
1056 $oldRevision = $this->loadRevisionFromConds(
1058 [
'rev_id' => intval( $pageLatest ) ],
1063 if ( !$oldRevision ) {
1064 $msg =
"Failed to load latest revision ID $pageLatest of page ID $pageId.";
1065 $this->logger->error(
1067 [
'exception' =>
new RuntimeException( $msg ) ]
1073 $timestamp = MWTimestamp::now( TS_MW );
1074 $newRevision = MutableRevisionRecord::newFromParentRevision( $oldRevision );
1076 $newRevision->setComment( $comment );
1077 $newRevision->setUser( $user );
1078 $newRevision->setTimestamp( $timestamp );
1079 $newRevision->setMinorEdit( $minor );
1081 return $newRevision;
1094 $rc = $this->getRecentChange( $rev );
1096 return $rc->getAttribute(
'rc_id' );
1120 'rc_this_oldid' => $rev->
getId( $this->wikiId ),
1154 private function loadSlotContent(
1156 ?
string $blobData =
null,
1157 ?
string $blobFlags =
null,
1158 ?
string $blobFormat =
null,
1161 if ( $blobData !==
null ) {
1164 if ( $blobFlags ===
null ) {
1169 $data = $this->blobStore->expandBlob( $blobData, $blobFlags, $blobAddress );
1170 }
catch ( BadBlobException $e ) {
1171 throw new BadRevisionException( $e->getMessage(), [], 0, $e );
1174 if ( $data ===
false ) {
1175 throw new RevisionAccessException(
1176 'Failed to expand blob data using flags {flags} (key: {cache_key})',
1178 'flags' => $blobFlags,
1179 'cache_key' => $blobAddress,
1188 $data = $this->blobStore->getBlob( $address, $queryFlags );
1189 }
catch ( BadBlobException $e ) {
1190 throw new BadRevisionException( $e->getMessage(), [], 0, $e );
1191 }
catch ( BlobAccessException $e ) {
1192 throw new RevisionAccessException(
1193 'Failed to load data blob from {address} for revision {revision}. '
1194 .
'If this problem persist, use the findBadBlobs maintenance script '
1195 .
'to investigate the issue and mark bad blobs.',
1196 [
'address' => $e->getMessage(),
'revision' => $slot->
getRevision() ],
1206 if ( !$this->contentHandlerFactory->isDefinedModel( $model ) ) {
1207 $this->logger->warning(
1208 "Undefined content model '$model', falling back to FallbackContent",
1212 'role_name' => $slot->
getRole(),
1213 'model_name' => $model,
1214 'exception' =>
new RuntimeException()
1221 return $this->contentHandlerFactory
1222 ->getContentHandler( $model )
1223 ->unserializeContent( $data, $blobFormat );
1244 return $this->newRevisionFromConds( [
'rev_id' => intval( $id ) ], $flags, $page );
1265 'page_namespace' => $page->getNamespace(),
1266 'page_title' => $page->getDBkey()
1271 $page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) :
null;
1280 $conds[
'rev_id'] = $revId;
1281 return $this->newRevisionFromConds( $conds, $flags, $page );
1288 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1289 $conds[] =
'rev_id=page_latest';
1290 return $this->loadRevisionFromConds( $db, $conds, $flags, $page );
1311 $conds = [
'page_id' => $pageId ];
1318 $conds[
'rev_id'] = $revId;
1319 return $this->newRevisionFromConds( $conds, $flags );
1326 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1328 $conds[] =
'rev_id=page_latest';
1330 return $this->loadRevisionFromConds( $db, $conds, $flags );
1352 int $flags = IDBAccessObject::READ_NORMAL
1356 $page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) :
null;
1358 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1359 return $this->newRevisionFromConds(
1361 'rev_timestamp' => $db->timestamp( $timestamp ),
1362 'page_namespace' => $page->getNamespace(),
1363 'page_title' => $page->getDBkey()
1377 private function loadSlotRecords( $revId, $queryFlags,
PageIdentity $page ) {
1380 $res = $this->loadSlotRecordsFromDb( $revId, $queryFlags, $page );
1381 return $this->constructSlotRecords( $revId,
$res, $queryFlags, $page );
1385 $res = $this->localCache->getWithSetCallback(
1386 $this->localCache->makeKey(
1392 $this->localCache::TTL_HOUR,
1393 function () use ( $revId, $queryFlags, $page ) {
1394 return $this->cache->getWithSetCallback(
1395 $this->cache->makeKey(
1401 WANObjectCache::TTL_DAY,
1402 function () use ( $revId, $queryFlags, $page ) {
1403 $res = $this->loadSlotRecordsFromDb( $revId, $queryFlags, $page );
1417 return $this->constructSlotRecords( $revId,
$res, $queryFlags, $page );
1420 private function loadSlotRecordsFromDb( $revId, $queryFlags, PageIdentity $page ): array {
1421 $revQuery = $this->getSlotsQueryInfo( [
'content' ] );
1424 $db = $this->getDBConnectionRef( $dbMode );
1430 'slot_revision_id' => $revId,
1437 if ( !
$res->numRows() && !( $queryFlags & self::READ_LATEST ) ) {
1439 $this->logger->info(
1440 __METHOD__ .
' falling back to READ_LATEST.',
1443 'exception' =>
new RuntimeException(),
1446 return $this->loadSlotRecordsFromDb(
1448 $queryFlags | self::READ_LATEST,
1452 return iterator_to_array(
$res );
1467 private function constructSlotRecords(
1472 $slotContents =
null
1476 foreach ( $slotRows as $row ) {
1478 if ( !isset( $row->role_name ) ) {
1479 $row->role_name = $this->slotRoleStore->getName( (
int)$row->slot_role_id );
1482 if ( !isset( $row->model_name ) ) {
1483 if ( isset( $row->content_model ) ) {
1484 $row->model_name = $this->contentModelStore->getName( (
int)$row->content_model );
1488 $slotRoleHandler = $this->slotRoleRegistry->getRoleHandler( $row->role_name );
1489 $row->model_name = $slotRoleHandler->getDefaultModel( $page );
1494 if ( isset( $row->blob_data ) ) {
1495 $slotContents[$row->content_address] = $row->blob_data;
1498 $contentCallback =
function ( SlotRecord $slot ) use ( $slotContents, $queryFlags ) {
1500 if ( isset( $slotContents[$slot->
getAddress()] ) ) {
1506 return $this->loadSlotContent( $slot,
$blob,
null,
null, $queryFlags );
1509 $slots[$row->role_name] =
new SlotRecord( $row, $contentCallback );
1512 if ( !isset( $slots[SlotRecord::MAIN] ) ) {
1513 $this->logger->error(
1514 __METHOD__ .
': Main slot of revision not found in database. See T212428.',
1517 'queryFlags' => $queryFlags,
1518 'exception' =>
new RuntimeException(),
1522 throw new RevisionAccessException(
1523 'Main slot of revision not found in database. See T212428.'
1544 private function newRevisionSlots(
1551 $slots =
new RevisionSlots(
1552 $this->constructSlotRecords( $revId, $slotRows, $queryFlags, $page )
1555 $slots =
new RevisionSlots(
function () use( $revId, $queryFlags, $page ) {
1556 return $this->loadSlotRecords( $revId, $queryFlags, $page );
1588 array $overrides = []
1590 return $this->newRevisionFromArchiveRowAndSlots( $row,
null, $queryFlags, $page, $overrides );
1611 return $this->newRevisionFromRowAndSlots( $row,
null, $queryFlags, $page, $fromCache );
1636 int $queryFlags = 0,
1638 array $overrides = []
1640 if ( !$page && isset( $overrides[
'title'] ) ) {
1641 if ( !( $overrides[
'title'] instanceof
PageIdentity ) ) {
1642 throw new MWException(
'title field override must contain a PageIdentity object.' );
1645 $page = $overrides[
'title'];
1648 if ( !isset( $page ) ) {
1649 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
1650 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
1652 throw new InvalidArgumentException(
1653 'A Title or ar_namespace and ar_title must be given'
1658 foreach ( $overrides as $key => $value ) {
1660 $row->$field = $value;
1664 $user = $this->actorStore->newActorFromRowFields(
1665 $row->ar_user ??
null,
1666 $row->ar_user_text ??
null,
1667 $row->ar_actor ??
null
1669 }
catch ( InvalidArgumentException $ex ) {
1670 $this->logger->warning(
'Could not load user for archive revision {rev_id}', [
1671 'ar_rev_id' => $row->ar_rev_id,
1672 'ar_actor' => $row->ar_actor ??
'null',
1673 'ar_user_text' => $row->ar_user_text ??
'null',
1674 'ar_user' => $row->ar_user ??
'null',
1677 $user = $this->actorStore->getUnknownActor();
1680 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1682 $comment = $this->commentStore->getCommentLegacy( $db,
'ar_comment', $row,
true );
1685 $slots = $this->newRevisionSlots( (
int)$row->ar_rev_id, $slots, $queryFlags, $page );
1711 int $queryFlags = 0,
1713 bool $fromCache =
false
1716 if ( isset( $row->page_id )
1717 && isset( $row->page_namespace )
1718 && isset( $row->page_title )
1722 (
int)$row->page_namespace,
1727 $page = $this->wrapPage( $page );
1729 $pageId = (int)( $row->rev_page ?? 0 );
1730 $revId = (int)( $row->rev_id ?? 0 );
1732 $page = $this->getPage( $pageId, $revId, $queryFlags );
1735 $page = $this->ensureRevisionRowMatchesPage( $row, $page );
1742 "Failed to determine page associated with revision {$row->rev_id}"
1747 $user = $this->actorStore->newActorFromRowFields(
1748 $row->rev_user ??
null,
1749 $row->rev_user_text ??
null,
1750 $row->rev_actor ??
null
1752 }
catch ( InvalidArgumentException $ex ) {
1753 $this->logger->warning(
'Could not load user for revision {rev_id}', [
1754 'rev_id' => $row->rev_id,
1755 'rev_actor' => $row->rev_actor ??
'null',
1756 'rev_user_text' => $row->rev_user_text ??
'null',
1757 'rev_user' => $row->rev_user ??
'null',
1760 $user = $this->actorStore->getUnknownActor();
1763 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1765 $comment = $this->commentStore->getCommentLegacy( $db,
'rev_comment', $row,
true );
1768 $slots = $this->newRevisionSlots( (
int)$row->rev_id, $slots, $queryFlags, $page );
1774 function ( $revId ) use ( $queryFlags ) {
1775 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1776 $row = $this->fetchRevisionRowFromConds(
1778 [
'rev_id' => intval( $revId ) ]
1780 if ( !$row && !( $queryFlags & self::READ_LATEST ) ) {
1782 $this->logger->info(
1783 'RevisionStoreCacheRecord refresh callback falling back to READ_LATEST.',
1786 'exception' =>
new RuntimeException(),
1789 $dbw = $this->getDBConnectionRefForQueryFlags( self::READ_LATEST );
1790 $row = $this->fetchRevisionRowFromConds(
1792 [
'rev_id' => intval( $revId ) ]
1796 return [
null, null ];
1800 $this->actorStore->newActorFromRowFields(
1801 $row->rev_user ??
null,
1802 $row->rev_user_text ??
null,
1803 $row->rev_actor ??
null
1807 $page, $user, $comment, $row, $slots, $this->wikiId
1811 $page, $user, $comment, $row, $slots, $this->wikiId );
1827 private function ensureRevisionRowMatchesPage( $row,
PageIdentity $page, $context = [] ) {
1828 $revId = (int)( $row->rev_id ?? 0 );
1829 $revPageId = (int)( $row->rev_page ?? 0 );
1830 $expectedPageId = $page->
getId( $this->wikiId );
1832 if ( $revPageId && $expectedPageId && $revPageId !== $expectedPageId ) {
1834 $pageRec = $this->pageStore->getPageByName(
1837 PageStore::READ_LATEST
1839 $masterPageId = $pageRec->getId( $this->wikiId );
1840 $masterLatest = $pageRec->getLatest( $this->wikiId );
1841 if ( $revPageId === $masterPageId ) {
1842 if ( $page instanceof
Title ) {
1845 $page->resetArticleID( $masterPageId );
1851 $this->logger->info(
1852 "Encountered stale Title object",
1854 'page_id_stale' => $expectedPageId,
1855 'page_id_reloaded' => $masterPageId,
1856 'page_latest' => $masterLatest,
1858 'exception' =>
new RuntimeException(),
1862 $expectedTitle = (string)$page;
1863 if ( $page instanceof Title ) {
1865 $page = $this->titleFactory->newFromID( $revPageId );
1876 $this->logger->error(
1877 "Encountered mismatching Title object (see T259022, T268910, T279832, T263340)",
1879 'expected_page_id' => $masterPageId,
1880 'expected_page_title' => $expectedTitle,
1881 'rev_page' => $revPageId,
1882 'rev_page_title' => (
string)$page,
1883 'page_latest' => $masterLatest,
1885 'exception' =>
new RuntimeException(),
1922 array $options = [],
1927 $archiveMode = $options[
'archive'] ??
false;
1929 if ( $archiveMode ) {
1930 $revIdField =
'ar_rev_id';
1932 $revIdField =
'rev_id';
1936 $pageIdsToFetchTitles = [];
1937 $titlesByPageKey = [];
1938 foreach ( $rows as $row ) {
1939 if ( isset( $rowsByRevId[$row->$revIdField] ) ) {
1941 'internalerror_info',
1942 "Duplicate rows in newRevisionsFromBatch, $revIdField {$row->$revIdField}"
1948 $archiveMode ? $row->ar_namespace .
':' . $row->ar_title : $row->rev_page;
1951 if ( !$archiveMode && $row->rev_page != $this->getArticleId( $page ) ) {
1952 throw new InvalidArgumentException(
1953 "Revision {$row->$revIdField} doesn't belong to page "
1954 . $this->getArticleId( $page )
1960 || $row->ar_title !== $page->
getDBkey() )
1962 throw new InvalidArgumentException(
1963 "Revision {$row->$revIdField} doesn't belong to page "
1967 } elseif ( !isset( $titlesByPageKey[ $row->_page_key ] ) ) {
1968 if ( isset( $row->page_namespace ) && isset( $row->page_title )
1971 && isset( $row->page_id ) && isset( $row->rev_page )
1972 && $row->rev_page === $row->page_id
1974 $titlesByPageKey[ $row->_page_key ] = Title::newFromRow( $row );
1975 } elseif ( $archiveMode ) {
1977 $titlesByPageKey[ $row->_page_key ] =
1978 Title::makeTitle( $row->ar_namespace, $row->ar_title );
1980 $pageIdsToFetchTitles[] = $row->rev_page;
1983 $rowsByRevId[$row->$revIdField] = $row;
1986 if ( empty( $rowsByRevId ) ) {
1987 $result->setResult(
true, [] );
1994 $pageKey = $archiveMode
1996 : $this->getArticleId( $page );
1998 $titlesByPageKey[$pageKey] = $page;
1999 } elseif ( !empty( $pageIdsToFetchTitles ) ) {
2002 Assert::invariant( !$archiveMode,
'Titles are not loaded by ID in archive mode.' );
2004 $pageIdsToFetchTitles = array_unique( $pageIdsToFetchTitles );
2005 $pageRecords = $this->pageStore
2006 ->newSelectQueryBuilder()
2007 ->wherePageIds( $pageIdsToFetchTitles )
2008 ->caller( __METHOD__ )
2009 ->fetchPageRecordArray();
2011 $titlesByPageKey = $pageRecords + $titlesByPageKey;
2015 $newRevisionRecord = [
2017 $archiveMode ?
'newRevisionFromArchiveRowAndSlots' :
'newRevisionFromRowAndSlots'
2020 if ( !isset( $options[
'slots'] ) ) {
2024 static function ( $row )
2025 use ( $queryFlags, $titlesByPageKey, $result, $newRevisionRecord, $revIdField ) {
2027 if ( !isset( $titlesByPageKey[$row->_page_key] ) ) {
2029 'internalerror_info',
2030 "Couldn't find title for rev {$row->$revIdField} "
2031 .
"(page key {$row->_page_key})"
2035 return $newRevisionRecord( $row,
null, $queryFlags,
2036 $titlesByPageKey[ $row->_page_key ] );
2038 $result->warning(
'internalerror_info', $e->getMessage() );
2049 'slots' => $options[
'slots'] ??
true,
2050 'blobs' => $options[
'content'] ??
false,
2053 if ( is_array( $slotRowOptions[
'slots'] )
2054 && !in_array( SlotRecord::MAIN, $slotRowOptions[
'slots'] )
2057 $slotRowOptions[
'slots'][] = SlotRecord::MAIN;
2060 $slotRowsStatus = $this->getSlotRowsForBatch( $rowsByRevId, $slotRowOptions, $queryFlags );
2062 $result->merge( $slotRowsStatus );
2063 $slotRowsByRevId = $slotRowsStatus->getValue();
2069 use ( $slotRowsByRevId, $queryFlags, $titlesByPageKey, $result,
2070 $revIdField, $newRevisionRecord
2072 if ( !isset( $slotRowsByRevId[$row->$revIdField] ) ) {
2074 'internalerror_info',
2075 "Couldn't find slots for rev {$row->$revIdField}"
2079 if ( !isset( $titlesByPageKey[$row->_page_key] ) ) {
2081 'internalerror_info',
2082 "Couldn't find title for rev {$row->$revIdField} "
2083 .
"(page key {$row->_page_key})"
2088 return $newRevisionRecord(
2091 $this->constructSlotRecords(
2093 $slotRowsByRevId[$row->$revIdField],
2095 $titlesByPageKey[$row->_page_key]
2099 $titlesByPageKey[$row->_page_key]
2102 $result->warning(
'internalerror_info', $e->getMessage() );
2135 private function getSlotRowsForBatch(
2137 array $options = [],
2143 foreach ( $rowsOrIds as $row ) {
2144 if ( is_object( $row ) ) {
2145 $revIds[] = isset( $row->ar_rev_id ) ? (int)$row->ar_rev_id : (
int)$row->rev_id;
2147 $revIds[] = (int)$row;
2153 if ( empty( $revIds ) ) {
2154 $result->setResult(
true, [] );
2159 $slotQueryInfo = $this->getSlotsQueryInfo( [
'content' ] );
2160 $revIdField = $slotQueryInfo[
'keys'][
'rev_id'];
2161 $slotQueryConds = [ $revIdField => $revIds ];
2163 if ( isset( $options[
'slots'] ) && is_array( $options[
'slots'] ) ) {
2165 foreach ( $options[
'slots'] as $slot ) {
2167 $slotIds[] = $this->slotRoleStore->getId( $slot );
2168 }
catch ( NameTableAccessException $exception ) {
2173 if ( $slotIds === [] ) {
2175 $result->setResult(
true, array_fill_keys( $revIds, [] ) );
2179 $roleIdField = $slotQueryInfo[
'keys'][
'role_id'];
2180 $slotQueryConds[$roleIdField] = $slotIds;
2183 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
2184 $slotRows = $db->select(
2185 $slotQueryInfo[
'tables'],
2186 $slotQueryInfo[
'fields'],
2190 $slotQueryInfo[
'joins']
2193 $slotContents =
null;
2194 if ( $options[
'blobs'] ??
false ) {
2195 $blobAddresses = [];
2196 foreach ( $slotRows as $slotRow ) {
2197 $blobAddresses[] = $slotRow->content_address;
2199 $slotContentFetchStatus = $this->blobStore
2200 ->getBlobBatch( $blobAddresses, $queryFlags );
2201 foreach ( $slotContentFetchStatus->getErrors() as $error ) {
2202 $result->warning( $error[
'message'], ...$error[
'params'] );
2204 $slotContents = $slotContentFetchStatus->getValue();
2207 $slotRowsByRevId = [];
2208 foreach ( $slotRows as $slotRow ) {
2209 if ( $slotContents ===
null ) {
2211 } elseif ( isset( $slotContents[$slotRow->content_address] ) ) {
2212 $slotRow->blob_data = $slotContents[$slotRow->content_address];
2215 'internalerror_info',
2216 "Couldn't find blob data for rev {$slotRow->slot_revision_id}"
2218 $slotRow->blob_data =
null;
2222 if ( !isset( $slotRow->role_name ) && isset( $slotRow->slot_role_id ) ) {
2223 $slotRow->role_name = $this->slotRoleStore->getName( (
int)$slotRow->slot_role_id );
2227 if ( !isset( $slotRow->model_name ) && isset( $slotRow->content_model ) ) {
2228 $slotRow->model_name = $this->contentModelStore->getName( (
int)$slotRow->content_model );
2231 $slotRowsByRevId[$slotRow->slot_revision_id][$slotRow->role_name] = $slotRow;
2234 $result->setResult(
true, $slotRowsByRevId );
2263 $result = $this->getSlotRowsForBatch(
2265 [
'slots' => $slots,
'blobs' =>
true ],
2269 if ( $result->isOK() ) {
2271 foreach ( $result->value as $revId => $rowsByRole ) {
2272 foreach ( $rowsByRole as $role => $slotRow ) {
2273 if ( is_array( $slots ) && !in_array( $role, $slots ) ) {
2276 unset( $result->value[$revId][$role] );
2280 $result->value[$revId][$role] = (object)[
2281 'blob_data' => $slotRow->blob_data,
2282 'model_name' => $slotRow->model_name,
2307 private function newRevisionFromConds(
2309 int $flags = IDBAccessObject::READ_NORMAL,
2313 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2314 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $page, $options );
2319 && !( $flags & self::READ_LATEST )
2320 && $this->loadBalancer->hasStreamingReplicaServers()
2321 && $this->loadBalancer->hasOrMadeRecentPrimaryChanges()
2323 $flags = self::READ_LATEST;
2324 $dbw = $this->getDBConnectionRef(
DB_PRIMARY );
2325 $rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $page, $options );
2345 private function loadRevisionFromConds(
2348 int $flags = IDBAccessObject::READ_NORMAL,
2349 PageIdentity $page =
null,
2352 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags, $options );
2354 return $this->newRevisionFromRow( $row, $flags, $page );
2367 private function checkDatabaseDomain( IReadableDatabase $db ) {
2368 $dbDomain = $db->getDomainID();
2369 $storeDomain = $this->loadBalancer->resolveDomainID( $this->wikiId );
2370 if ( $dbDomain === $storeDomain ) {
2374 throw new MWException(
"DB connection domain '$dbDomain' does not match '$storeDomain'" );
2390 private function fetchRevisionRowFromConds(
2393 int $flags = IDBAccessObject::READ_NORMAL,
2396 $this->checkDatabaseDomain( $db );
2398 $revQuery = $this->getQueryInfo( [
'page',
'user' ] );
2399 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
2400 $options[] =
'FOR UPDATE';
2402 return $db->selectRow(
2440 $ret[
'tables'][] =
'revision';
2441 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2452 $commentQuery = $this->commentStore->getJoin(
'rev_comment' );
2453 $ret[
'tables'] = array_merge( $ret[
'tables'], $commentQuery[
'tables'] );
2454 $ret[
'fields'] = array_merge( $ret[
'fields'], $commentQuery[
'fields'] );
2455 $ret[
'joins'] = array_merge( $ret[
'joins'], $commentQuery[
'joins'] );
2457 $actorQuery = $this->actorMigration->getJoin(
'rev_user' );
2458 $ret[
'tables'] = array_merge( $ret[
'tables'], $actorQuery[
'tables'] );
2459 $ret[
'fields'] = array_merge( $ret[
'fields'], $actorQuery[
'fields'] );
2460 $ret[
'joins'] = array_merge( $ret[
'joins'], $actorQuery[
'joins'] );
2462 if ( in_array(
'page', $options,
true ) ) {
2463 $ret[
'tables'][] =
'page';
2464 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2472 $ret[
'joins'][
'page'] = [
'JOIN', [
'page_id = rev_page' ] ];
2475 if ( in_array(
'user', $options,
true ) ) {
2476 $ret[
'tables'][] =
'user';
2477 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2480 $u = $actorQuery[
'fields'][
'rev_user'];
2481 $ret[
'joins'][
'user'] = [
'LEFT JOIN', [
"$u != 0",
"user_id = $u" ] ];
2484 if ( in_array(
'text', $options,
true ) ) {
2485 throw new InvalidArgumentException(
2486 'The `text` option is no longer supported in MediaWiki 1.35 and later.'
2522 $ret[
'keys'][
'rev_id'] =
'slot_revision_id';
2523 $ret[
'keys'][
'role_id'] =
'slot_role_id';
2525 $ret[
'tables'][] =
'slots';
2526 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2533 if ( in_array(
'role', $options,
true ) ) {
2536 $ret[
'tables'][] =
'slot_roles';
2537 $ret[
'joins'][
'slot_roles'] = [
'LEFT JOIN', [
'slot_role_id = role_id' ] ];
2538 $ret[
'fields'][] =
'role_name';
2541 if ( in_array(
'content', $options,
true ) ) {
2542 $ret[
'keys'][
'model_id'] =
'content_model';
2544 $ret[
'tables'][] =
'content';
2545 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2551 $ret[
'joins'][
'content'] = [
'JOIN', [
'slot_content_id = content_id' ] ];
2553 if ( in_array(
'model', $options,
true ) ) {
2556 $ret[
'tables'][] =
'content_models';
2557 $ret[
'joins'][
'content_models'] = [
'LEFT JOIN', [
'content_model = model_id' ] ];
2558 $ret[
'fields'][] =
'model_name';
2575 if ( !( $row instanceof stdClass ) ) {
2578 $queryInfo = $table ===
'archive' ? $this->getArchiveQueryInfo() : $this->getQueryInfo();
2579 foreach ( $queryInfo[
'fields'] as $alias => $field ) {
2580 $name = is_numeric( $alias ) ? $field : $alias;
2581 if ( !property_exists( $row, $name ) ) {
2607 $commentQuery = $this->commentStore->getJoin(
'ar_comment' );
2611 'archive_actor' =>
'actor'
2612 ] + $commentQuery[
'tables'],
2626 'ar_user' =>
'archive_actor.actor_user',
2627 'ar_user_text' =>
'archive_actor.actor_name',
2628 ] + $commentQuery[
'fields'],
2630 'archive_actor' => [
'JOIN',
'actor_id=ar_actor' ]
2631 ] + $commentQuery[
'joins'],
2655 [
'rev_id',
'rev_len' ],
2656 [
'rev_id' => $revIds ],
2660 foreach (
$res as $row ) {
2661 $revLens[$row->rev_id] = intval( $row->rev_len );
2675 private function getRelativeRevision(
RevisionRecord $rev, $flags, $dir ) {
2676 $op = $dir ===
'next' ?
'>' :
'<';
2677 $sort = $dir ===
'next' ?
'ASC' :
'DESC';
2679 $revisionIdValue = $rev->
getId( $this->wikiId );
2681 if ( !$revisionIdValue || !$rev->
getPageId( $this->wikiId ) ) {
2686 if ( $rev instanceof RevisionArchiveRecord ) {
2692 $db = $this->getDBConnectionRef( $dbType );
2694 $ts = $rev->
getTimestamp() ?? $this->getTimestampFromId( $revisionIdValue, $flags );
2695 if ( $ts ===
false ) {
2697 $ts = $db->selectField(
'archive',
'ar_timestamp',
2698 [
'ar_rev_id' => $revisionIdValue ], __METHOD__ );
2699 if ( $ts ===
false ) {
2705 $revId = $db->selectField(
'revision',
'rev_id',
2707 'rev_page' => $rev->
getPageId( $this->wikiId ),
2708 $db->buildComparison( $op, [
2709 'rev_timestamp' => $db->timestamp( $ts ),
2710 'rev_id' => $revisionIdValue,
2715 'ORDER BY' => [
"rev_timestamp $sort",
"rev_id $sort" ],
2716 'IGNORE INDEX' =>
'rev_timestamp',
2720 if ( $revId ===
false ) {
2724 return $this->getRevisionById( intval( $revId ), $flags );
2742 return $this->getRelativeRevision( $rev, $flags,
'prev' );
2757 return $this->getRelativeRevision( $rev, $flags,
'next' );
2772 $this->checkDatabaseDomain( $db );
2774 if ( $rev->
getPageId( $this->wikiId ) ===
null ) {
2777 # Use page_latest if ID is not given
2778 if ( !$rev->
getId( $this->wikiId ) ) {
2780 'page',
'page_latest',
2781 [
'page_id' => $rev->
getPageId( $this->wikiId ) ],
2786 'revision',
'rev_id',
2787 [
'rev_page' => $rev->
getPageId( $this->wikiId ),
'rev_id < ' . $rev->
getId( $this->wikiId ) ],
2789 [
'ORDER BY' =>
'rev_id DESC' ]
2792 return intval( $prevId );
2808 if ( $id instanceof
Title ) {
2811 $flags = func_num_args() > 2 ? func_get_arg( 2 ) : 0;
2819 if ( $id ===
null || $id <= 0 ) {
2823 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2826 $db->
selectField(
'revision',
'rev_timestamp', [
'rev_id' => $id ], __METHOD__ );
2828 return ( $timestamp !==
false ) ? MWTimestamp::convert( TS_MW, $timestamp ) :
false;
2841 $this->checkDatabaseDomain( $db );
2844 [
'revCount' =>
'COUNT(*)' ],
2845 [
'rev_page' => $id ],
2849 return intval( $row->revCount );
2864 $id = $this->getArticleId( $page );
2866 return $this->countRevisionsByPageId( $db, $id );
2890 $this->checkDatabaseDomain( $db );
2900 'rev_user' =>
$revQuery[
'fields'][
'rev_user'],
2903 'rev_page' => $pageId,
2907 [
'ORDER BY' =>
'rev_timestamp ASC',
'LIMIT' => 50 ],
2910 foreach (
$res as $row ) {
2911 if ( $row->rev_user != $userId ) {
2932 $db = $this->getDBConnectionRef(
DB_REPLICA );
2933 $revIdPassed = $revId;
2934 $pageId = $this->getArticleId( $page );
2940 if ( $page instanceof
Title ) {
2941 $revId = $page->getLatestRevID();
2943 $pageRecord = $this->pageStore->getPageByReference( $page );
2944 if ( $pageRecord ) {
2945 $revId = $pageRecord->getLatest( $this->getWikiId() );
2951 $this->logger->warning(
2952 'No latest revision known for page {page} even though it exists with page ID {page_id}', [
2954 'page_id' => $pageId,
2955 'wiki_id' => $this->getWikiId() ?:
'local',
2964 $row = $this->cache->getWithSetCallback(
2966 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2967 WANObjectCache::TTL_WEEK,
2968 function ( $curValue, &$ttl, array &$setOpts ) use (
2969 $db, $revId, &$fromCache
2971 $setOpts += Database::getCacheSetOptions( $db );
2972 $row = $this->fetchRevisionRowFromConds( $db, [
'rev_id' => intval( $revId ) ] );
2982 $title = $this->ensureRevisionRowMatchesPage( $row, $page, [
2983 'from_cache_flag' => $fromCache,
2984 'page_id_initial' => $pageId,
2985 'rev_id_used' => $revId,
2986 'rev_id_requested' => $revIdPassed,
2989 return $this->newRevisionFromRow( $row, 0,
$title, $fromCache );
3005 int $flags = IDBAccessObject::READ_NORMAL
3009 $page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) :
null;
3011 return $this->newRevisionFromConds(
3019 'ORDER BY' => [
'rev_timestamp ASC',
'rev_id ASC' ],
3020 'IGNORE INDEX' => [
'revision' =>
'rev_timestamp' ],
3036 private function getRevisionRowCacheKey( IDatabase $db, $pageId, $revId ) {
3037 return $this->cache->makeGlobalKey(
3038 self::ROW_CACHE_KEY,
3052 private function assertRevisionParameter( $paramName, $pageId, RevisionRecord $rev =
null ) {
3054 if ( $rev->
getId( $this->wikiId ) ===
null ) {
3055 throw new InvalidArgumentException(
"Unsaved {$paramName} revision passed" );
3057 if ( $rev->
getPageId( $this->wikiId ) !== $pageId ) {
3058 throw new InvalidArgumentException(
3059 "Revision {$rev->getId( $this->wikiId )} doesn't belong to page {$pageId}"
3079 private function getRevisionLimitConditions(
3081 RevisionRecord $old =
null,
3082 RevisionRecord $new =
null,
3085 $options = (array)$options;
3086 if ( in_array( self::INCLUDE_OLD, $options ) || in_array( self::INCLUDE_BOTH, $options ) ) {
3091 if ( in_array( self::INCLUDE_NEW, $options ) || in_array( self::INCLUDE_BOTH, $options ) ) {
3099 $conds[] =
$dbr->buildComparison( $oldCmp, [
3100 'rev_timestamp' =>
$dbr->timestamp( $old->getTimestamp() ),
3101 'rev_id' => $old->getId( $this->wikiId ),
3105 $conds[] =
$dbr->buildComparison( $newCmp, [
3106 'rev_timestamp' =>
$dbr->timestamp( $new->getTimestamp() ),
3107 'rev_id' => $new->getId( $this->wikiId ),
3145 ?
string $order =
null,
3146 int $flags = IDBAccessObject::READ_NORMAL
3148 $this->assertRevisionParameter(
'old', $pageId, $old );
3149 $this->assertRevisionParameter(
'new', $pageId, $new );
3151 $options = (array)$options;
3152 $includeOld = in_array( self::INCLUDE_OLD, $options ) ||
3153 in_array( self::INCLUDE_BOTH, $options );
3154 $includeNew = in_array( self::INCLUDE_NEW, $options ) ||
3155 in_array( self::INCLUDE_BOTH, $options );
3161 if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) {
3162 return $includeOld || $includeNew ? [ $new->getId( $this->wikiId ) ] : [];
3165 $db = $this->getDBConnectionRefForQueryFlags( $flags );
3166 $conds = array_merge(
3168 'rev_page' => $pageId,
3169 $db->bitAnd(
'rev_deleted', RevisionRecord::DELETED_TEXT ) .
' = 0'
3171 $this->getRevisionLimitConditions( $db, $old, $new, $options )
3175 if ( $order !==
null ) {
3176 $queryOptions[
'ORDER BY'] = [
"rev_timestamp $order",
"rev_id $order" ];
3178 if ( $max !==
null ) {
3179 $queryOptions[
'LIMIT'] = $max + 1;
3182 $values = $db->selectFieldValues(
3189 return array_map(
'intval', $values );
3221 $this->assertRevisionParameter(
'old', $pageId, $old );
3222 $this->assertRevisionParameter(
'new', $pageId, $new );
3223 $options = (array)$options;
3229 if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) {
3230 if ( empty( $options ) ) {
3232 } elseif ( $performer ) {
3233 return [ $new->getUser( RevisionRecord::FOR_THIS_USER, $performer ) ];
3235 return [ $new->getUser() ];
3240 $conds = array_merge(
3242 'rev_page' => $pageId,
3243 $dbr->bitAnd(
'rev_deleted', RevisionRecord::DELETED_USER ) .
" = 0"
3245 $this->getRevisionLimitConditions(
$dbr, $old, $new, $options )
3248 $queryOpts = [
'DISTINCT' ];
3249 if ( $max !==
null ) {
3250 $queryOpts[
'LIMIT'] = $max + 1;
3253 $actorQuery = $this->actorMigration->getJoin(
'rev_user' );
3254 return array_map(
function ( $row ) {
3255 return $this->actorStore->newActorFromRowFields(
3257 $row->rev_user_text,
3260 }, iterator_to_array(
$dbr->select(
3261 array_merge( [
'revision' ], $actorQuery[
'tables'] ),
3262 $actorQuery[
'fields'],
3265 $actorQuery[
'joins']
3300 return count( $this->getAuthorsBetween( $pageId, $old, $new, $performer, $max, $options ) );
3330 $this->assertRevisionParameter(
'old', $pageId, $old );
3331 $this->assertRevisionParameter(
'new', $pageId, $new );
3337 if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) {
3342 $conds = array_merge(
3344 'rev_page' => $pageId,
3345 $dbr->bitAnd(
'rev_deleted', RevisionRecord::DELETED_TEXT ) .
" = 0"
3347 $this->getRevisionLimitConditions(
$dbr, $old, $new, $options )
3349 if ( $max !==
null ) {
3350 return $dbr->selectRowCount(
'revision',
'1',
3353 [
'LIMIT' => $max + 1 ]
3356 return (
int)
$dbr->selectField(
'revision',
'count(*)', $conds, __METHOD__ );
3376 $db = $this->getDBConnectionRef(
DB_REPLICA );
3378 $subquery = $db->buildSelectSubquery(
3381 [
'rev_page' => $revision->
getPageId( $this->wikiId ) ],
3385 'rev_timestamp DESC',
3389 'LIMIT' => $searchLimit,
3397 $revisionRow = $db->selectRow(
3398 [
'recent_revs' => $subquery ],
3400 [
'rev_sha1' => $revision->
getSha1() ],
3404 return $revisionRow ? $this->newRevisionFromRow( $revisionRow ) : null;
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
if(!defined('MW_SETUP_CALLBACK'))
Class representing a cache/ephemeral data store.
Helper class for DAO classes.
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
static hasFlags( $bitfield, $flags)
Content object implementation representing unknown content.
Library for creating and parsing MW-style timestamps.
Exception thrown when an unregistered content model is requested.
Immutable value object representing a page identity.
Utility class for creating new RC entries.
static newFromConds( $conds, $fname=__METHOD__, $dbType=DB_REPLICA)
Find the first recent change matching some specific conditions.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Multi-datacenter aware caching interface.
Base interface for representing page content.
Interface for database access objects.
Interface for objects (potentially) representing an editable wiki page.
getId( $wikiId=self::LOCAL)
Returns the page ID.
trait LegacyArticleIdAccess
Convenience trait for conversion to PageIdentity.