91 use LegacyArticleIdAccess;
116 private $loadBalancer;
131 private $commentStore;
136 private $actorMigration;
149 private $contentModelStore;
154 private $slotRoleStore;
157 private $slotRoleRegistry;
160 private $contentHandlerFactory;
169 private $titleFactory;
211 $wikiId = WikiAwareEntity::LOCAL
213 Assert::parameterType( [
'string',
'false' ], $wikiId,
'$wikiId' );
215 $this->loadBalancer = $loadBalancer;
216 $this->blobStore = $blobStore;
218 $this->localCache = $localCache;
219 $this->commentStore = $commentStore;
220 $this->contentModelStore = $contentModelStore;
221 $this->slotRoleStore = $slotRoleStore;
222 $this->slotRoleRegistry = $slotRoleRegistry;
223 $this->actorMigration = $actorMigration;
224 $this->actorStore = $actorStore;
225 $this->wikiId = $wikiId;
226 $this->logger =
new NullLogger();
227 $this->contentHandlerFactory = $contentHandlerFactory;
228 $this->pageStore = $pageStore;
229 $this->titleFactory = $titleFactory;
230 $this->hookRunner =
new HookRunner( $hookContainer );
234 $this->logger = $logger;
241 return $this->blobStore->isReadOnly();
247 private function getDBLoadBalancer() {
248 return $this->loadBalancer;
257 return $this->wikiId;
265 private function getDBConnectionRefForQueryFlags( $queryFlags ) {
266 list( $mode, ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
267 return $this->getDBConnectionRef( $mode );
276 private function getDBConnectionRef( $mode, $groups = [] ) {
277 $lb = $this->getDBLoadBalancer();
278 return $lb->getConnectionRef( $mode, $groups, $this->wikiId );
297 public function getTitle( $pageId, $revId, $queryFlags = self::READ_NORMAL ) {
299 if ( $this->wikiId !== WikiAwareEntity::LOCAL ) {
300 wfDeprecatedMsg(
'Using a Title object to refer to a page on another site.',
'1.36' );
303 $page = $this->getPage( $pageId, $revId, $queryFlags );
305 return $this->titleFactory->castFromPageIdentity( $page );
318 private function getPage( ?
int $pageId, ?
int $revId,
int $queryFlags = self::READ_NORMAL ) {
319 if ( !$pageId && !$revId ) {
320 throw new InvalidArgumentException(
'$pageId and $revId cannot both be 0 or null' );
325 if ( DBAccessObjectUtils::hasFlags( $queryFlags, self::READ_LATEST_IMMUTABLE ) ) {
326 $queryFlags = self::READ_NORMAL;
330 if ( $pageId !==
null && $pageId > 0 ) {
331 $page = $this->pageStore->getPageById( $pageId, $queryFlags );
333 return $this->wrapPage( $page );
338 if ( $revId !==
null && $revId > 0 ) {
339 $pageQuery = $this->pageStore->newSelectQueryBuilder( $queryFlags )
340 ->join(
'revision',
null,
'page_id=rev_page' )
341 ->conds( [
'rev_id' => $revId ] )
342 ->caller( __METHOD__ );
344 $page = $pageQuery->fetchPageRecord();
346 return $this->wrapPage( $page );
351 if ( $queryFlags === self::READ_NORMAL ) {
352 $title = $this->getPage( $pageId, $revId, self::READ_LATEST );
355 __METHOD__ .
' fell back to READ_LATEST and got a Title.',
356 [
'exception' =>
new RuntimeException() ]
362 throw new RevisionAccessException(
363 'Could not determine title for page ID {page_id} and revision ID {rev_id}',
365 'page_id' => $pageId,
376 private function wrapPage( PageIdentity $page ): PageIdentity {
377 if ( $this->wikiId === WikiAwareEntity::LOCAL ) {
385 return $this->titleFactory->castFromPageIdentity( $page );
398 private function failOnNull( $value, $name ) {
399 if ( $value ===
null ) {
400 throw new IncompleteRevisionException(
401 "$name must not be " . var_export( $value,
true ) .
"!"
415 private function failOnEmpty( $value, $name ) {
416 if ( $value ===
null || $value === 0 || $value ===
'' ) {
417 throw new IncompleteRevisionException(
418 "$name must not be " . var_export( $value,
true ) .
"!"
438 $this->checkDatabaseDomain( $dbw );
445 'main slot must be provided'
450 $this->failOnNull( $rev->
getSize(),
'size field' );
451 $this->failOnEmpty( $rev->
getSha1(),
'sha1 field' );
452 $this->failOnEmpty( $rev->
getTimestamp(),
'timestamp field' );
455 $this->failOnNull( $user->getId(),
'user field' );
456 $this->failOnEmpty( $user->getName(),
'user_text field' );
468 Assert::precondition(
469 $mainSlot->getSize() === $rev->
getSize(),
470 'The revisions\'s size must match the main slot\'s size (see T239717)'
472 Assert::precondition(
473 $mainSlot->getSha1() === $rev->
getSha1(),
474 'The revisions\'s SHA1 hash must match the main slot\'s SHA1 hash (see T239717)'
478 $pageId = $this->failOnEmpty( $rev->
getPageId( $this->wikiId ),
'rev_page field' );
480 $parentId = $rev->
getParentId() ?? $this->getPreviousRevisionId( $dbw, $rev );
485 function (
IDatabase $dbw, $fname ) use (
492 return $this->insertRevisionInternal(
504 Assert::postcondition( $rev->
getId( $this->wikiId ) > 0,
'revision must have an ID' );
505 Assert::postcondition( $rev->
getPageId( $this->wikiId ) > 0,
'revision must have a page ID' );
506 Assert::postcondition(
508 'revision must have a comment'
510 Assert::postcondition(
512 'revision must have a user'
521 foreach ( $slotRoles as $role ) {
523 Assert::postcondition(
525 $role .
' slot must have content'
527 Assert::postcondition(
529 $role .
' slot must have a revision associated'
533 $this->hookRunner->onRevisionRecordInserted( $rev );
555 $this->checkDatabaseDomain( $dbw );
559 Assert::precondition(
560 $this->slotRoleRegistry->getRoleHandler( $role )->isDerived(),
561 'Trying to modify a slot that is not derived'
565 $isDerived = $this->slotRoleRegistry->getRoleHandler( $role )->isDerived();
566 Assert::precondition(
568 'Trying to remove a slot that is not derived'
570 throw new LogicException(
'Removing derived slots is not yet implemented. See T277394.' );
576 function ( IDatabase $dbw, $fname ) use (
580 return $this->updateSlotsInternal(
582 $revisionSlotsUpdate,
588 foreach ( $slotRecords as $role => $slot ) {
589 Assert::postcondition(
591 $role .
' slot must have content'
593 Assert::postcondition(
595 $role .
' slot must have a revision associated'
608 private function updateSlotsInternal(
609 RevisionRecord $revision,
610 RevisionSlotsUpdate $revisionSlotsUpdate,
613 $page = $revision->getPage();
614 $revId = $revision->
getId( $this->wikiId );
616 BlobStore::PAGE_HINT => $page->
getId( $this->wikiId ),
617 BlobStore::REVISION_HINT => $revId,
618 BlobStore::PARENT_HINT => $revision->getParentId( $this->wikiId ),
622 foreach ( $revisionSlotsUpdate->getModifiedRoles() as $role ) {
623 $slot = $revisionSlotsUpdate->getModifiedSlot( $role );
624 $newSlots[$role] = $this->insertSlotOn( $dbw, $revId, $slot, $page, $blobHints );
630 private function insertRevisionInternal(
639 $slotRoles = $rev->getSlotRoles();
641 $revisionRow = $this->insertRevisionRowOn(
647 $revisionId = $revisionRow[
'rev_id'];
650 BlobStore::PAGE_HINT => $pageId,
651 BlobStore::REVISION_HINT => $revisionId,
652 BlobStore::PARENT_HINT => $parentId,
656 foreach ( $slotRoles as $role ) {
657 $slot = $rev->getSlot( $role, RevisionRecord::RAW );
670 'slot role ' . $slot->
getRole(),
671 'Existing slot should belong to revision '
672 . $revisionId .
', but belongs to revision ' . $slot->
getRevision() .
'!'
678 $newSlots[$role] = $slot;
680 $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $page, $blobHints );
684 $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
686 $rev =
new RevisionStoreRecord(
690 (
object)$revisionRow,
691 new RevisionSlots( $newSlots ),
706 private function insertSlotOn(
709 SlotRecord $protoSlot,
711 array $blobHints = []
713 if ( $protoSlot->hasAddress() ) {
714 $blobAddress = $protoSlot->getAddress();
716 $blobAddress = $this->storeContentBlob( $protoSlot, $page, $blobHints );
719 if ( $protoSlot->hasContentId() ) {
720 $contentId = $protoSlot->getContentId();
722 $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
725 $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
727 return SlotRecord::newSaved(
742 private function insertIpChangesRow(
748 if ( !$user->isRegistered() && IPUtils::isValid( $user->getName() ) ) {
750 'ipc_rev_id' => $revisionId,
751 'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
752 'ipc_hex' => IPUtils::toHex( $user->getName() ),
754 $dbw->insert(
'ip_changes', $ipcRow, __METHOD__ );
768 private function insertRevisionRowOn(
773 $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $parentId );
775 list( $commentFields, $commentCallback ) =
776 $this->commentStore->insertWithTempTable(
779 $rev->getComment( RevisionRecord::RAW )
781 $revisionRow += $commentFields;
783 list( $actorFields, $actorCallback ) =
784 $this->actorMigration->getInsertValuesWithTempTable(
787 $rev->getUser( RevisionRecord::RAW )
789 $revisionRow += $actorFields;
791 $dbw->insert(
'revision', $revisionRow, __METHOD__ );
793 if ( !isset( $revisionRow[
'rev_id'] ) ) {
795 $revisionRow[
'rev_id'] = intval( $dbw->insertId() );
797 if ( $dbw->getType() ===
'mysql' ) {
802 $maxRevId = intval( $dbw->selectField(
'archive',
'MAX(ar_rev_id)',
'', __METHOD__ ) );
804 $maxRevId2 = intval( $dbw->selectField(
'slots',
'MAX(slot_revision_id)',
'', __METHOD__ ) );
805 if ( $maxRevId2 >= $maxRevId ) {
806 $maxRevId = $maxRevId2;
810 if ( $maxRevId >= $revisionRow[
'rev_id'] ) {
811 $this->logger->debug(
812 '__METHOD__: Inserted revision {revid} but {table} has revisions up to {maxrevid}.'
813 .
' Trying to fix it.',
815 'revid' => $revisionRow[
'rev_id'],
817 'maxrevid' => $maxRevId,
821 if ( !$dbw->lock(
'fix-for-T202032', __METHOD__ ) ) {
822 throw new MWException(
'Failed to get database lock for T202032' );
825 $dbw->onTransactionResolution(
826 static function ( $trigger, IDatabase $dbw ) use ( $fname ) {
827 $dbw->unlock(
'fix-for-T202032', $fname );
832 $dbw->delete(
'revision', [
'rev_id' => $revisionRow[
'rev_id'] ], __METHOD__ );
844 $dbw->selectSQLText(
'archive', [
'v' =>
"MAX(ar_rev_id)" ],
'', __METHOD__ ) .
' FOR UPDATE',
849 $dbw->selectSQLText(
'slots', [
'v' =>
"MAX(slot_revision_id)" ],
'', __METHOD__ )
856 $row1 ? intval( $row1->v ) : 0,
857 $row2 ? intval( $row2->v ) : 0
863 $revisionRow[
'rev_id'] = $maxRevId + 1;
864 $dbw->insert(
'revision', $revisionRow, __METHOD__ );
869 $commentCallback( $revisionRow[
'rev_id'] );
870 $actorCallback( $revisionRow[
'rev_id'], $revisionRow );
882 private function getBaseRevisionRow(
889 'rev_page' => $rev->getPageId( $this->wikiId ),
890 'rev_parent_id' => $parentId,
891 'rev_minor_edit' => $rev->isMinor() ? 1 : 0,
892 'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
893 'rev_deleted' => $rev->getVisibility(),
894 'rev_len' => $rev->getSize(),
895 'rev_sha1' => $rev->getSha1(),
898 if ( $rev->getId( $this->wikiId ) !==
null ) {
900 $revisionRow[
'rev_id'] = $rev->getId( $this->wikiId );
914 private function storeContentBlob(
917 array $blobHints = []
920 $format =
$content->getDefaultFormat();
923 $this->checkContent(
$content, $page, $slot->getRole() );
925 return $this->blobStore->storeBlob(
932 BlobStore::DESIGNATION_HINT =>
'page-content',
933 BlobStore::ROLE_HINT => $slot->getRole(),
934 BlobStore::SHA1_HINT => $slot->getSha1(),
935 BlobStore::MODEL_HINT => $model,
936 BlobStore::FORMAT_HINT => $format,
948 private function insertSlotRowOn( SlotRecord $slot, IDatabase $dbw, $revisionId, $contentId ) {
950 'slot_revision_id' => $revisionId,
951 'slot_role_id' => $this->slotRoleStore->acquireId( $slot->getRole() ),
952 'slot_content_id' => $contentId,
955 'slot_origin' => $slot->hasOrigin() ? $slot->getOrigin() : $revisionId,
957 $dbw->insert(
'slots', $slotRow, __METHOD__ );
966 private function insertContentRowOn( SlotRecord $slot, IDatabase $dbw, $blobAddress ) {
968 'content_size' => $slot->getSize(),
969 'content_sha1' => $slot->getSha1(),
970 'content_model' => $this->contentModelStore->acquireId( $slot->getModel() ),
971 'content_address' => $blobAddress,
973 $dbw->insert(
'content', $contentRow, __METHOD__ );
974 return intval( $dbw->insertId() );
987 private function checkContent(
Content $content, PageIdentity $page,
string $role ) {
991 $format =
$content->getDefaultFormat();
992 $handler =
$content->getContentHandler();
994 if ( !$handler->isSupportedFormat( $format ) ) {
996 "Can't use format $format with content model $model on $page role $role"
1002 "New content for $page role $role is not valid! Content model is $model"
1039 $this->checkDatabaseDomain( $dbw );
1041 $pageId = $this->getArticleId( $page );
1049 [
'page_id' => $pageId ],
1054 if ( !$pageLatest ) {
1055 $msg =
'T235589: Failed to select table row during null revision creation' .
1056 " Page id '$pageId' does not exist.";
1057 $this->logger->error(
1059 [
'exception' =>
new RuntimeException( $msg ) ]
1066 $oldRevision = $this->loadRevisionFromConds(
1068 [
'rev_id' => intval( $pageLatest ) ],
1073 if ( !$oldRevision ) {
1074 $msg =
"Failed to load latest revision ID $pageLatest of page ID $pageId.";
1075 $this->logger->error(
1077 [
'exception' =>
new RuntimeException( $msg ) ]
1083 $timestamp = MWTimestamp::now( TS_MW );
1084 $newRevision = MutableRevisionRecord::newFromParentRevision( $oldRevision );
1086 $newRevision->setComment( $comment );
1087 $newRevision->setUser( $user );
1088 $newRevision->setTimestamp( $timestamp );
1089 $newRevision->setMinorEdit( $minor );
1091 return $newRevision;
1104 $rc = $this->getRecentChange( $rev );
1105 if ( $rc && $rc->getAttribute(
'rc_patrolled' ) == RecentChange::PRC_UNPATROLLED ) {
1106 return $rc->getAttribute(
'rc_id' );
1126 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
1128 $rc = RecentChange::newFromConds(
1129 [
'rc_this_oldid' => $rev->
getId( $this->wikiId ) ],
1157 private function loadSlotContent(
1159 ?
string $blobData =
null,
1160 ?
string $blobFlags =
null,
1161 ?
string $blobFormat =
null,
1164 if ( $blobData !==
null ) {
1167 if ( $blobFlags ===
null ) {
1171 $data = $this->blobStore->expandBlob( $blobData, $blobFlags, $cacheKey );
1172 if ( $data ===
false ) {
1173 throw new RevisionAccessException(
1174 'Failed to expand blob data using flags {flags} (key: {cache_key})',
1176 'flags' => $blobFlags,
1177 'cache_key' => $cacheKey,
1186 $data = $this->blobStore->getBlob( $address, $queryFlags );
1187 }
catch ( BlobAccessException $e ) {
1188 throw new RevisionAccessException(
1189 'Failed to load data blob from {address}'
1190 .
'If this problem persist, use the findBadBlobs maintenance script '
1191 .
'to investigate the issue and mark bad blobs.',
1192 [
'address' => $e->getMessage() ],
1202 if ( !$this->contentHandlerFactory->isDefinedModel( $model ) ) {
1203 $this->logger->warning(
1204 "Undefined content model '$model', falling back to FallbackContent",
1208 'role_name' => $slot->
getRole(),
1209 'model_name' => $model,
1210 'exception' =>
new RuntimeException()
1217 return $this->contentHandlerFactory
1218 ->getContentHandler( $model )
1219 ->unserializeContent( $data, $blobFormat );
1240 return $this->newRevisionFromConds( [
'rev_id' => intval( $id ) ], $flags, $page );
1261 'page_namespace' => $page->getNamespace(),
1262 'page_title' => $page->getDBkey()
1267 $page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) :
null;
1276 $conds[
'rev_id'] = $revId;
1277 return $this->newRevisionFromConds( $conds, $flags, $page );
1284 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1285 $conds[] =
'rev_id=page_latest';
1286 return $this->loadRevisionFromConds( $db, $conds, $flags, $page );
1307 $conds = [
'page_id' => $pageId ];
1314 $conds[
'rev_id'] = $revId;
1315 return $this->newRevisionFromConds( $conds, $flags );
1322 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1324 $conds[] =
'rev_id=page_latest';
1326 return $this->loadRevisionFromConds( $db, $conds, $flags );
1348 int $flags = IDBAccessObject::READ_NORMAL
1352 $page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) :
null;
1354 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1355 return $this->newRevisionFromConds(
1357 'rev_timestamp' => $db->timestamp( $timestamp ),
1358 'page_namespace' => $page->getNamespace(),
1359 'page_title' => $page->getDBkey()
1373 private function loadSlotRecords( $revId, $queryFlags,
PageIdentity $page ) {
1376 $res = $this->loadSlotRecordsFromDb( $revId, $queryFlags, $page );
1377 return $this->constructSlotRecords( $revId,
$res, $queryFlags, $page );
1381 $res = $this->localCache->getWithSetCallback(
1382 $this->localCache->makeKey(
1388 $this->localCache::TTL_HOUR,
1389 function () use ( $revId, $queryFlags, $page ) {
1390 return $this->cache->getWithSetCallback(
1391 $this->cache->makeKey(
1397 WANObjectCache::TTL_DAY,
1398 function () use ( $revId, $queryFlags, $page ) {
1399 $res = $this->loadSlotRecordsFromDb( $revId, $queryFlags, $page );
1413 return $this->constructSlotRecords( $revId,
$res, $queryFlags, $page );
1416 private function loadSlotRecordsFromDb( $revId, $queryFlags, PageIdentity $page ): array {
1417 $revQuery = $this->getSlotsQueryInfo( [
'content' ] );
1420 $db = $this->getDBConnectionRef( $dbMode );
1426 'slot_revision_id' => $revId,
1433 if ( !
$res->numRows() && !( $queryFlags & self::READ_LATEST ) ) {
1435 $this->logger->info(
1436 __METHOD__ .
' falling back to READ_LATEST.',
1439 'exception' =>
new RuntimeException(),
1442 return $this->loadSlotRecordsFromDb(
1444 $queryFlags | self::READ_LATEST,
1448 return iterator_to_array(
$res );
1463 private function constructSlotRecords(
1468 $slotContents =
null
1472 foreach ( $slotRows as $row ) {
1474 if ( !isset( $row->role_name ) ) {
1475 $row->role_name = $this->slotRoleStore->getName( (
int)$row->slot_role_id );
1478 if ( !isset( $row->model_name ) ) {
1479 if ( isset( $row->content_model ) ) {
1480 $row->model_name = $this->contentModelStore->getName( (
int)$row->content_model );
1484 $slotRoleHandler = $this->slotRoleRegistry->getRoleHandler( $row->role_name );
1485 $row->model_name = $slotRoleHandler->getDefaultModel( $page );
1490 if ( isset( $row->blob_data ) ) {
1491 $slotContents[$row->content_address] = $row->blob_data;
1494 $contentCallback =
function ( SlotRecord $slot ) use ( $slotContents, $queryFlags ) {
1496 if ( isset( $slotContents[$slot->
getAddress()] ) ) {
1502 return $this->loadSlotContent( $slot,
$blob,
null,
null, $queryFlags );
1505 $slots[$row->role_name] =
new SlotRecord( $row, $contentCallback );
1508 if ( !isset( $slots[SlotRecord::MAIN] ) ) {
1509 $this->logger->error(
1510 __METHOD__ .
': Main slot of revision not found in database. See T212428.',
1513 'queryFlags' => $queryFlags,
1514 'exception' =>
new RuntimeException(),
1518 throw new RevisionAccessException(
1519 'Main slot of revision not found in database. See T212428.'
1541 private function newRevisionSlots(
1549 $slots =
new RevisionSlots(
1550 $this->constructSlotRecords( $revId, $slotRows, $queryFlags, $page )
1553 $slots =
new RevisionSlots(
function () use( $revId, $queryFlags, $page ) {
1554 return $this->loadSlotRecords( $revId, $queryFlags, $page );
1586 array $overrides = []
1588 return $this->newRevisionFromArchiveRowAndSlots( $row,
null, $queryFlags, $page, $overrides );
1609 return $this->newRevisionFromRowAndSlots( $row,
null, $queryFlags, $page, $fromCache );
1634 int $queryFlags = 0,
1636 array $overrides = []
1638 if ( !$page && isset( $overrides[
'title'] ) ) {
1639 if ( !( $overrides[
'title'] instanceof
PageIdentity ) ) {
1640 throw new MWException(
'title field override must contain a PageIdentity object.' );
1643 $page = $overrides[
'title'];
1646 if ( !isset( $page ) ) {
1647 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
1648 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
1650 throw new InvalidArgumentException(
1651 'A Title or ar_namespace and ar_title must be given'
1656 foreach ( $overrides as $key => $value ) {
1658 $row->$field = $value;
1662 $user = $this->actorStore->newActorFromRowFields(
1663 $row->ar_user ??
null,
1664 $row->ar_user_text ??
null,
1665 $row->ar_actor ??
null
1667 }
catch ( InvalidArgumentException $ex ) {
1668 $this->logger->warning(
'Could not load user for archive revision {rev_id}', [
1669 'ar_rev_id' => $row->ar_rev_id,
1670 'ar_actor' => $row->ar_actor ??
'null',
1671 'ar_user_text' => $row->ar_user_text ??
'null',
1672 'ar_user' => $row->ar_user ??
'null',
1675 $user = $this->actorStore->getUnknownActor();
1678 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1680 $comment = $this->commentStore->getCommentLegacy( $db,
'ar_comment', $row,
true );
1683 $slots = $this->newRevisionSlots( (
int)$row->ar_rev_id, $row, $slots, $queryFlags, $page );
1709 int $queryFlags = 0,
1711 bool $fromCache =
false
1714 if ( isset( $row->page_id )
1715 && isset( $row->page_namespace )
1716 && isset( $row->page_title )
1720 (
int)$row->page_namespace,
1725 $page = $this->wrapPage( $page );
1727 $pageId = (int)( $row->rev_page ?? 0 );
1728 $revId = (int)( $row->rev_id ?? 0 );
1730 $page = $this->getPage( $pageId, $revId, $queryFlags );
1733 $page = $this->ensureRevisionRowMatchesPage( $row, $page );
1740 "Failed to determine page associated with revision {$row->rev_id}"
1745 $user = $this->actorStore->newActorFromRowFields(
1746 $row->rev_user ??
null,
1747 $row->rev_user_text ??
null,
1748 $row->rev_actor ??
null
1750 }
catch ( InvalidArgumentException $ex ) {
1751 $this->logger->warning(
'Could not load user for revision {rev_id}', [
1752 'rev_id' => $row->rev_id,
1753 'rev_actor' => $row->rev_actor ??
'null',
1754 'rev_user_text' => $row->rev_user_text ??
'null',
1755 'rev_user' => $row->rev_user ??
'null',
1758 $user = $this->actorStore->getUnknownActor();
1761 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1763 $comment = $this->commentStore->getCommentLegacy( $db,
'rev_comment', $row,
true );
1766 $slots = $this->newRevisionSlots( (
int)$row->rev_id, $row, $slots, $queryFlags, $page );
1772 function ( $revId ) use ( $queryFlags ) {
1773 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1774 $row = $this->fetchRevisionRowFromConds(
1776 [
'rev_id' => intval( $revId ) ]
1778 if ( !$row && !( $queryFlags & self::READ_LATEST ) ) {
1780 $this->logger->info(
1781 'RevisionStoreCacheRecord refresh callback falling back to READ_LATEST.',
1784 'exception' =>
new RuntimeException(),
1787 $dbw = $this->getDBConnectionRefForQueryFlags( self::READ_LATEST );
1788 $row = $this->fetchRevisionRowFromConds(
1790 [
'rev_id' => intval( $revId ) ]
1794 return [
null, null ];
1798 $this->actorStore->newActorFromRowFields(
1799 $row->rev_user ??
null,
1800 $row->rev_user_text ??
null,
1801 $row->rev_actor ??
null
1805 $page, $user, $comment, $row, $slots, $this->wikiId
1809 $page, $user, $comment, $row, $slots, $this->wikiId );
1825 private function ensureRevisionRowMatchesPage( $row,
PageIdentity $page, $context = [] ) {
1826 $revId = (int)( $row->rev_id ?? 0 );
1827 $revPageId = (int)( $row->rev_page ?? 0 );
1828 $expectedPageId = $page->
getId( $this->wikiId );
1830 if ( $revPageId && $expectedPageId && $revPageId !== $expectedPageId ) {
1832 $pageRec = $this->pageStore->getPageByName(
1835 PageStore::READ_LATEST
1837 $masterPageId = $pageRec->getId( $this->wikiId );
1838 $masterLatest = $pageRec->getLatest( $this->wikiId );
1839 if ( $revPageId === $masterPageId ) {
1840 if ( $page instanceof
Title ) {
1843 $page->resetArticleID( $masterPageId );
1849 $this->logger->info(
1850 "Encountered stale Title object",
1852 'page_id_stale' => $expectedPageId,
1853 'page_id_reloaded' => $masterPageId,
1854 'page_latest' => $masterLatest,
1856 'exception' =>
new RuntimeException(),
1860 $expectedTitle = (string)$page;
1861 if ( $page instanceof
Title ) {
1863 $page = $this->titleFactory->newFromID( $revPageId );
1874 $this->logger->error(
1875 "Encountered mismatching Title object (see T259022, T268910, T279832, T263340)",
1877 'expected_page_id' => $masterPageId,
1878 'expected_page_title' => $expectedTitle,
1879 'rev_page' => $revPageId,
1880 'rev_page_title' => (
string)$page,
1881 'page_latest' => $masterLatest,
1883 'exception' =>
new RuntimeException(),
1920 array $options = [],
1925 $archiveMode = $options[
'archive'] ??
false;
1927 if ( $archiveMode ) {
1928 $revIdField =
'ar_rev_id';
1930 $revIdField =
'rev_id';
1934 $pageIdsToFetchTitles = [];
1935 $titlesByPageKey = [];
1936 foreach ( $rows as $row ) {
1937 if ( isset( $rowsByRevId[$row->$revIdField] ) ) {
1939 'internalerror_info',
1940 "Duplicate rows in newRevisionsFromBatch, $revIdField {$row->$revIdField}"
1946 $archiveMode ? $row->ar_namespace .
':' . $row->ar_title : $row->rev_page;
1949 if ( !$archiveMode && $row->rev_page != $this->getArticleId( $page ) ) {
1950 throw new InvalidArgumentException(
1951 "Revision {$row->$revIdField} doesn't belong to page "
1952 . $this->getArticleId( $page )
1958 || $row->ar_title !== $page->
getDBkey() )
1960 throw new InvalidArgumentException(
1961 "Revision {$row->$revIdField} doesn't belong to page "
1965 } elseif ( !isset( $titlesByPageKey[ $row->_page_key ] ) ) {
1966 if ( isset( $row->page_namespace ) && isset( $row->page_title )
1969 && isset( $row->page_id ) && isset( $row->rev_page )
1970 && $row->rev_page === $row->page_id
1972 $titlesByPageKey[ $row->_page_key ] = Title::newFromRow( $row );
1973 } elseif ( $archiveMode ) {
1975 $titlesByPageKey[ $row->_page_key ] =
1976 Title::makeTitle( $row->ar_namespace, $row->ar_title );
1978 $pageIdsToFetchTitles[] = $row->rev_page;
1981 $rowsByRevId[$row->$revIdField] = $row;
1984 if ( empty( $rowsByRevId ) ) {
1985 $result->setResult(
true, [] );
1992 $pageKey = $archiveMode
1994 : $this->getArticleId( $page );
1996 $titlesByPageKey[$pageKey] = $page;
1997 } elseif ( !empty( $pageIdsToFetchTitles ) ) {
2000 Assert::invariant( !$archiveMode,
'Titles are not loaded by ID in archive mode.' );
2002 $pageIdsToFetchTitles = array_unique( $pageIdsToFetchTitles );
2003 $pageRecords = $this->pageStore
2004 ->newSelectQueryBuilder()
2005 ->wherePageIds( $pageIdsToFetchTitles )
2006 ->caller( __METHOD__ )
2007 ->fetchPageRecordArray();
2009 $titlesByPageKey = $pageRecords + $titlesByPageKey;
2013 $newRevisionRecord = [
2015 $archiveMode ?
'newRevisionFromArchiveRowAndSlots' :
'newRevisionFromRowAndSlots'
2018 if ( !isset( $options[
'slots'] ) ) {
2022 static function ( $row )
2023 use ( $queryFlags, $titlesByPageKey, $result, $newRevisionRecord, $revIdField ) {
2025 if ( !isset( $titlesByPageKey[$row->_page_key] ) ) {
2027 'internalerror_info',
2028 "Couldn't find title for rev {$row->$revIdField} "
2029 .
"(page key {$row->_page_key})"
2033 return $newRevisionRecord( $row,
null, $queryFlags,
2034 $titlesByPageKey[ $row->_page_key ] );
2036 $result->warning(
'internalerror_info', $e->getMessage() );
2047 'slots' => $options[
'slots'] ??
true,
2048 'blobs' => $options[
'content'] ??
false,
2051 if ( is_array( $slotRowOptions[
'slots'] )
2052 && !in_array( SlotRecord::MAIN, $slotRowOptions[
'slots'] )
2055 $slotRowOptions[
'slots'][] = SlotRecord::MAIN;
2058 $slotRowsStatus = $this->getSlotRowsForBatch( $rowsByRevId, $slotRowOptions, $queryFlags );
2060 $result->merge( $slotRowsStatus );
2061 $slotRowsByRevId = $slotRowsStatus->getValue();
2067 use ( $slotRowsByRevId, $queryFlags, $titlesByPageKey, $result,
2068 $revIdField, $newRevisionRecord
2070 if ( !isset( $slotRowsByRevId[$row->$revIdField] ) ) {
2072 'internalerror_info',
2073 "Couldn't find slots for rev {$row->$revIdField}"
2077 if ( !isset( $titlesByPageKey[$row->_page_key] ) ) {
2079 'internalerror_info',
2080 "Couldn't find title for rev {$row->$revIdField} "
2081 .
"(page key {$row->_page_key})"
2086 return $newRevisionRecord(
2089 $this->constructSlotRecords(
2091 $slotRowsByRevId[$row->$revIdField],
2093 $titlesByPageKey[$row->_page_key]
2097 $titlesByPageKey[$row->_page_key]
2100 $result->warning(
'internalerror_info', $e->getMessage() );
2133 private function getSlotRowsForBatch(
2135 array $options = [],
2141 foreach ( $rowsOrIds as $row ) {
2142 if ( is_object( $row ) ) {
2143 $revIds[] = isset( $row->ar_rev_id ) ? (int)$row->ar_rev_id : (int)$row->rev_id;
2145 $revIds[] = (int)$row;
2151 if ( empty( $revIds ) ) {
2152 $result->setResult(
true, [] );
2157 $slotQueryInfo = $this->getSlotsQueryInfo( [
'content' ] );
2158 $revIdField = $slotQueryInfo[
'keys'][
'rev_id'];
2159 $slotQueryConds = [ $revIdField => $revIds ];
2161 if ( isset( $options[
'slots'] ) && is_array( $options[
'slots'] ) ) {
2162 if ( empty( $options[
'slots'] ) ) {
2164 $result->setResult(
true, array_fill_keys( $revIds, [] ) );
2168 $roleIdField = $slotQueryInfo[
'keys'][
'role_id'];
2169 $slotQueryConds[$roleIdField] = array_map(
2170 [ $this->slotRoleStore,
'getId' ],
2175 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
2176 $slotRows = $db->select(
2177 $slotQueryInfo[
'tables'],
2178 $slotQueryInfo[
'fields'],
2182 $slotQueryInfo[
'joins']
2185 $slotContents =
null;
2186 if ( $options[
'blobs'] ??
false ) {
2187 $blobAddresses = [];
2188 foreach ( $slotRows as $slotRow ) {
2189 $blobAddresses[] = $slotRow->content_address;
2191 $slotContentFetchStatus = $this->blobStore
2192 ->getBlobBatch( $blobAddresses, $queryFlags );
2193 foreach ( $slotContentFetchStatus->getErrors() as $error ) {
2194 $result->warning( $error[
'message'], ...$error[
'params'] );
2196 $slotContents = $slotContentFetchStatus->getValue();
2199 $slotRowsByRevId = [];
2200 foreach ( $slotRows as $slotRow ) {
2201 if ( $slotContents ===
null ) {
2203 } elseif ( isset( $slotContents[$slotRow->content_address] ) ) {
2204 $slotRow->blob_data = $slotContents[$slotRow->content_address];
2207 'internalerror_info',
2208 "Couldn't find blob data for rev {$slotRow->slot_revision_id}"
2210 $slotRow->blob_data =
null;
2214 if ( !isset( $slotRow->role_name ) && isset( $slotRow->slot_role_id ) ) {
2215 $slotRow->role_name = $this->slotRoleStore->getName( (
int)$slotRow->slot_role_id );
2219 if ( !isset( $slotRow->model_name ) && isset( $slotRow->content_model ) ) {
2220 $slotRow->model_name = $this->contentModelStore->getName( (
int)$slotRow->content_model );
2223 $slotRowsByRevId[$slotRow->slot_revision_id][$slotRow->role_name] = $slotRow;
2226 $result->setResult(
true, $slotRowsByRevId );
2255 $result = $this->getSlotRowsForBatch(
2257 [
'slots' => $slots,
'blobs' =>
true ],
2261 if ( $result->isOK() ) {
2263 foreach ( $result->value as $revId => $rowsByRole ) {
2264 foreach ( $rowsByRole as $role => $slotRow ) {
2265 if ( is_array( $slots ) && !in_array( $role, $slots ) ) {
2268 unset( $result->value[$revId][$role] );
2272 $result->value[$revId][$role] = (object)[
2273 'blob_data' => $slotRow->blob_data,
2274 'model_name' => $slotRow->model_name,
2299 private function newRevisionFromConds(
2301 int $flags = IDBAccessObject::READ_NORMAL,
2305 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2306 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $page, $options );
2308 $lb = $this->getDBLoadBalancer();
2313 && !( $flags & self::READ_LATEST )
2314 && $lb->hasStreamingReplicaServers()
2315 && $lb->hasOrMadeRecentPrimaryChanges()
2317 $flags = self::READ_LATEST;
2318 $dbw = $this->getDBConnectionRef(
DB_PRIMARY );
2319 $rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $page, $options );
2339 private function loadRevisionFromConds(
2342 int $flags = IDBAccessObject::READ_NORMAL,
2343 PageIdentity $page =
null,
2346 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags, $options );
2348 return $this->newRevisionFromRow( $row, $flags, $page );
2361 private function checkDatabaseDomain( IDatabase $db ) {
2362 $dbDomain = $db->getDomainID();
2363 $storeDomain = $this->loadBalancer->resolveDomainID( $this->wikiId );
2364 if ( $dbDomain === $storeDomain ) {
2368 throw new MWException(
"DB connection domain '$dbDomain' does not match '$storeDomain'" );
2384 private function fetchRevisionRowFromConds(
2387 int $flags = IDBAccessObject::READ_NORMAL,
2390 $this->checkDatabaseDomain( $db );
2392 $revQuery = $this->getQueryInfo( [
'page',
'user' ] );
2393 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
2394 $options[] =
'FOR UPDATE';
2396 return $db->selectRow(
2434 $ret[
'tables'][] =
'revision';
2435 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2446 $commentQuery = $this->commentStore->getJoin(
'rev_comment' );
2447 $ret[
'tables'] = array_merge( $ret[
'tables'], $commentQuery[
'tables'] );
2448 $ret[
'fields'] = array_merge( $ret[
'fields'], $commentQuery[
'fields'] );
2449 $ret[
'joins'] = array_merge( $ret[
'joins'], $commentQuery[
'joins'] );
2451 $actorQuery = $this->actorMigration->getJoin(
'rev_user' );
2452 $ret[
'tables'] = array_merge( $ret[
'tables'], $actorQuery[
'tables'] );
2453 $ret[
'fields'] = array_merge( $ret[
'fields'], $actorQuery[
'fields'] );
2454 $ret[
'joins'] = array_merge( $ret[
'joins'], $actorQuery[
'joins'] );
2456 if ( in_array(
'page', $options,
true ) ) {
2457 $ret[
'tables'][] =
'page';
2458 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2466 $ret[
'joins'][
'page'] = [
'JOIN', [
'page_id = rev_page' ] ];
2469 if ( in_array(
'user', $options,
true ) ) {
2470 $ret[
'tables'][] =
'user';
2471 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2474 $u = $actorQuery[
'fields'][
'rev_user'];
2475 $ret[
'joins'][
'user'] = [
'LEFT JOIN', [
"$u != 0",
"user_id = $u" ] ];
2478 if ( in_array(
'text', $options,
true ) ) {
2479 throw new InvalidArgumentException(
2480 'The `text` option is no longer supported in MediaWiki 1.35 and later.'
2516 $ret[
'keys'][
'rev_id'] =
'slot_revision_id';
2517 $ret[
'keys'][
'role_id'] =
'slot_role_id';
2519 $ret[
'tables'][] =
'slots';
2520 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2527 if ( in_array(
'role', $options,
true ) ) {
2530 $ret[
'tables'][] =
'slot_roles';
2531 $ret[
'joins'][
'slot_roles'] = [
'LEFT JOIN', [
'slot_role_id = role_id' ] ];
2532 $ret[
'fields'][] =
'role_name';
2535 if ( in_array(
'content', $options,
true ) ) {
2536 $ret[
'keys'][
'model_id'] =
'content_model';
2538 $ret[
'tables'][] =
'content';
2539 $ret[
'fields'] = array_merge( $ret[
'fields'], [
2545 $ret[
'joins'][
'content'] = [
'JOIN', [
'slot_content_id = content_id' ] ];
2547 if ( in_array(
'model', $options,
true ) ) {
2550 $ret[
'tables'][] =
'content_models';
2551 $ret[
'joins'][
'content_models'] = [
'LEFT JOIN', [
'content_model = model_id' ] ];
2552 $ret[
'fields'][] =
'model_name';
2569 if ( !( $row instanceof stdClass ) ) {
2572 $queryInfo = $table ===
'archive' ? $this->getArchiveQueryInfo() : $this->getQueryInfo();
2573 foreach ( $queryInfo[
'fields'] as $alias => $field ) {
2574 $name = is_numeric( $alias ) ? $field : $alias;
2575 if ( !property_exists( $row, $name ) ) {
2601 $commentQuery = $this->commentStore->getJoin(
'ar_comment' );
2605 'archive_actor' =>
'actor'
2606 ] + $commentQuery[
'tables'],
2620 'ar_user' =>
'archive_actor.actor_user',
2621 'ar_user_text' =>
'archive_actor.actor_name',
2622 ] + $commentQuery[
'fields'],
2624 'archive_actor' => [
'JOIN',
'actor_id=ar_actor' ]
2625 ] + $commentQuery[
'joins'],
2649 [
'rev_id',
'rev_len' ],
2650 [
'rev_id' => $revIds ],
2654 foreach (
$res as $row ) {
2655 $revLens[$row->rev_id] = intval( $row->rev_len );
2669 private function getRelativeRevision(
RevisionRecord $rev, $flags, $dir ) {
2670 $op = $dir ===
'next' ?
'>' :
'<';
2671 $sort = $dir ===
'next' ?
'ASC' :
'DESC';
2673 $revisionIdValue = $rev->
getId( $this->wikiId );
2675 if ( !$revisionIdValue || !$rev->
getPageId( $this->wikiId ) ) {
2680 if ( $rev instanceof RevisionArchiveRecord ) {
2685 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
2686 $db = $this->getDBConnectionRef( $dbType, [
'contributions' ] );
2689 if ( $ts ===
null ) {
2690 $ts = $this->getTimestampFromId( $revisionIdValue, $flags );
2692 if ( $ts ===
false ) {
2694 $ts = $db->selectField(
'archive',
'ar_timestamp',
2695 [
'ar_rev_id' => $revisionIdValue ], __METHOD__ );
2696 if ( $ts ===
false ) {
2701 $dbts = $db->addQuotes( $db->timestamp( $ts ) );
2703 $revId = $db->selectField(
'revision',
'rev_id',
2705 'rev_page' => $rev->
getPageId( $this->wikiId ),
2706 "rev_timestamp $op $dbts OR (rev_timestamp = $dbts AND rev_id $op $revisionIdValue )"
2710 'ORDER BY' => [
"rev_timestamp $sort",
"rev_id $sort" ],
2711 'IGNORE INDEX' =>
'rev_timestamp',
2715 if ( $revId ===
false ) {
2719 return $this->getRevisionById( intval( $revId ), $flags );
2737 return $this->getRelativeRevision( $rev, $flags,
'prev' );
2752 return $this->getRelativeRevision( $rev, $flags,
'next' );
2767 $this->checkDatabaseDomain( $db );
2769 if ( $rev->
getPageId( $this->wikiId ) ===
null ) {
2772 # Use page_latest if ID is not given
2773 if ( !$rev->
getId( $this->wikiId ) ) {
2775 'page',
'page_latest',
2776 [
'page_id' => $rev->
getPageId( $this->wikiId ) ],
2781 'revision',
'rev_id',
2782 [
'rev_page' => $rev->
getPageId( $this->wikiId ),
'rev_id < ' . $rev->
getId( $this->wikiId ) ],
2784 [
'ORDER BY' =>
'rev_id DESC' ]
2787 return intval( $prevId );
2803 if ( $id instanceof
Title ) {
2806 $flags = func_num_args() > 2 ? func_get_arg( 2 ) : 0;
2814 if ( $id ===
null || $id <= 0 ) {
2818 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2821 $db->
selectField(
'revision',
'rev_timestamp', [
'rev_id' => $id ], __METHOD__ );
2823 return ( $timestamp !==
false ) ? MWTimestamp::convert( TS_MW, $timestamp ) :
false;
2836 $this->checkDatabaseDomain( $db );
2839 [
'revCount' =>
'COUNT(*)' ],
2840 [
'rev_page' => $id ],
2844 return intval( $row->revCount );
2859 $id = $this->getArticleId( $page );
2861 return $this->countRevisionsByPageId( $db, $id );
2885 $this->checkDatabaseDomain( $db );
2895 'rev_user' =>
$revQuery[
'fields'][
'rev_user'],
2898 'rev_page' => $pageId,
2902 [
'ORDER BY' =>
'rev_timestamp ASC',
'LIMIT' => 50 ],
2905 foreach (
$res as $row ) {
2906 if ( $row->rev_user != $userId ) {
2927 $db = $this->getDBConnectionRef(
DB_REPLICA );
2928 $revIdPassed = $revId;
2929 $pageId = $this->getArticleId( $page );
2935 if ( $page instanceof
Title ) {
2936 $revId = $page->getLatestRevID();
2938 $pageRecord = $this->pageStore->getPageByReference( $page );
2939 if ( $pageRecord ) {
2940 $revId = $pageRecord->getLatest( $this->getWikiId() );
2946 $this->logger->warning(
2947 'No latest revision known for page {page} even though it exists with page ID {page_id}', [
2949 'page_id' => $pageId,
2950 'wiki_id' => $this->getWikiId() ?:
'local',
2959 $row = $this->cache->getWithSetCallback(
2961 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2962 WANObjectCache::TTL_WEEK,
2963 function ( $curValue, &$ttl, array &$setOpts ) use (
2964 $db, $revId, &$fromCache
2966 $setOpts += Database::getCacheSetOptions( $db );
2967 $row = $this->fetchRevisionRowFromConds( $db, [
'rev_id' => intval( $revId ) ] );
2977 $title = $this->ensureRevisionRowMatchesPage( $row, $page, [
2978 'from_cache_flag' => $fromCache,
2979 'page_id_initial' => $pageId,
2980 'rev_id_used' => $revId,
2981 'rev_id_requested' => $revIdPassed,
2984 return $this->newRevisionFromRow( $row, 0,
$title, $fromCache );
3000 int $flags = IDBAccessObject::READ_NORMAL
3004 $page = $this->wikiId === WikiAwareEntity::LOCAL ? Title::castFromLinkTarget( $page ) :
null;
3006 return $this->newRevisionFromConds(
3014 'ORDER BY' => [
'rev_timestamp ASC',
'rev_id ASC' ],
3015 'IGNORE INDEX' => [
'revision' =>
'rev_timestamp' ],
3031 private function getRevisionRowCacheKey( IDatabase $db, $pageId, $revId ) {
3032 return $this->cache->makeGlobalKey(
3033 self::ROW_CACHE_KEY,
3047 private function assertRevisionParameter( $paramName, $pageId, RevisionRecord $rev =
null ) {
3049 if ( $rev->
getId( $this->wikiId ) ===
null ) {
3050 throw new InvalidArgumentException(
"Unsaved {$paramName} revision passed" );
3052 if ( $rev->
getPageId( $this->wikiId ) !== $pageId ) {
3053 throw new InvalidArgumentException(
3054 "Revision {$rev->getId( $this->wikiId )} doesn't belong to page {$pageId}"
3074 private function getRevisionLimitConditions(
3076 RevisionRecord $old =
null,
3077 RevisionRecord $new =
null,
3080 $options = (array)$options;
3083 if ( in_array( self::INCLUDE_OLD, $options ) ) {
3086 if ( in_array( self::INCLUDE_NEW, $options ) ) {
3089 if ( in_array( self::INCLUDE_BOTH, $options ) ) {
3096 $oldTs =
$dbr->addQuotes(
$dbr->timestamp( $old->getTimestamp() ) );
3097 $conds[] =
"(rev_timestamp = {$oldTs} AND rev_id {$oldCmp} {$old->getId( $this->wikiId )}) " .
3098 "OR rev_timestamp > {$oldTs}";
3101 $newTs =
$dbr->addQuotes(
$dbr->timestamp( $new->getTimestamp() ) );
3102 $conds[] =
"(rev_timestamp = {$newTs} AND rev_id {$newCmp} {$new->getId( $this->wikiId )}) " .
3103 "OR rev_timestamp < {$newTs}";
3140 ?
string $order =
null,
3141 int $flags = IDBAccessObject::READ_NORMAL
3143 $this->assertRevisionParameter(
'old', $pageId, $old );
3144 $this->assertRevisionParameter(
'new', $pageId, $new );
3146 $options = (array)$options;
3147 $includeOld = in_array( self::INCLUDE_OLD, $options ) ||
3148 in_array( self::INCLUDE_BOTH, $options );
3149 $includeNew = in_array( self::INCLUDE_NEW, $options ) ||
3150 in_array( self::INCLUDE_BOTH, $options );
3156 if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) {
3157 return $includeOld || $includeNew ? [ $new->getId( $this->wikiId ) ] : [];
3160 $db = $this->getDBConnectionRefForQueryFlags( $flags );
3161 $conds = array_merge(
3163 'rev_page' => $pageId,
3164 $db->bitAnd(
'rev_deleted', RevisionRecord::DELETED_TEXT ) .
' = 0'
3166 $this->getRevisionLimitConditions( $db, $old, $new, $options )
3170 if ( $order !==
null ) {
3171 $queryOptions[
'ORDER BY'] = [
"rev_timestamp $order",
"rev_id $order" ];
3173 if ( $max !==
null ) {
3174 $queryOptions[
'LIMIT'] = $max + 1;
3177 $values = $db->selectFieldValues(
3184 return array_map(
'intval', $values );
3216 $this->assertRevisionParameter(
'old', $pageId, $old );
3217 $this->assertRevisionParameter(
'new', $pageId, $new );
3218 $options = (array)$options;
3224 if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) {
3225 if ( empty( $options ) ) {
3227 } elseif ( $performer ) {
3228 return [ $new->getUser( RevisionRecord::FOR_THIS_USER, $performer ) ];
3230 return [ $new->getUser() ];
3235 $conds = array_merge(
3237 'rev_page' => $pageId,
3238 $dbr->bitAnd(
'rev_deleted', RevisionRecord::DELETED_USER ) .
" = 0"
3240 $this->getRevisionLimitConditions(
$dbr, $old, $new, $options )
3243 $queryOpts = [
'DISTINCT' ];
3244 if ( $max !==
null ) {
3245 $queryOpts[
'LIMIT'] = $max + 1;
3248 $actorQuery = $this->actorMigration->getJoin(
'rev_user' );
3249 return array_map(
function ( $row ) {
3250 return $this->actorStore->newActorFromRowFields(
3252 $row->rev_user_text,
3255 }, iterator_to_array(
$dbr->select(
3256 array_merge( [
'revision' ], $actorQuery[
'tables'] ),
3257 $actorQuery[
'fields'],
3260 $actorQuery[
'joins']
3295 return count( $this->getAuthorsBetween( $pageId, $old, $new, $performer, $max, $options ) );
3325 $this->assertRevisionParameter(
'old', $pageId, $old );
3326 $this->assertRevisionParameter(
'new', $pageId, $new );
3332 if ( $old && $new && $new->getId( $this->wikiId ) === $old->getId( $this->wikiId ) ) {
3337 $conds = array_merge(
3339 'rev_page' => $pageId,
3340 $dbr->bitAnd(
'rev_deleted', RevisionRecord::DELETED_TEXT ) .
" = 0"
3342 $this->getRevisionLimitConditions(
$dbr, $old, $new, $options )
3344 if ( $max !==
null ) {
3345 return $dbr->selectRowCount(
'revision',
'1',
3348 [
'LIMIT' => $max + 1 ]
3351 return (
int)
$dbr->selectField(
'revision',
'count(*)', $conds, __METHOD__ );
3370 $revision->assertWiki( $this->wikiId );
3371 $db = $this->getDBConnectionRef(
DB_REPLICA );
3373 $subquery = $db->buildSelectSubquery(
3376 [
'rev_page' => $revision->
getPageId( $this->wikiId ) ],
3380 'rev_timestamp DESC',
3384 'LIMIT' => $searchLimit,
3392 $revisionRow = $db->selectRow(
3393 [
'recent_revs' => $subquery ],
3395 [
'rev_sha1' => $revision->
getSha1() ],
3399 return $revisionRow ? $this->newRevisionFromRow( $revisionRow ) : null;