3 * Service for looking up page revisions.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * Attribution notice: when this file was created, much of its content was taken
21 * from the Revision.php file as present in release 1.30. Refer to the history
22 * of that file for original authorship.
27 namespace MediaWiki\Revision
;
31 use CommentStoreComment
;
34 use DBAccessObjectUtils
;
37 use InvalidArgumentException
;
40 use MediaWiki\Linker\LinkTarget
;
41 use MediaWiki\Storage\BlobAccessException
;
42 use MediaWiki\Storage\BlobStore
;
43 use MediaWiki\Storage\NameTableAccessException
;
44 use MediaWiki\Storage\NameTableStore
;
45 use MediaWiki\Storage\SqlBlobStore
;
46 use MediaWiki\User\UserIdentity
;
47 use MediaWiki\User\UserIdentityValue
;
50 use MWUnknownContentModelException
;
51 use Psr\Log\LoggerAwareInterface
;
52 use Psr\Log\LoggerInterface
;
53 use Psr\Log\NullLogger
;
61 use Wikimedia\Assert\Assert
;
62 use Wikimedia\Rdbms\Database
;
63 use Wikimedia\Rdbms\DBConnRef
;
64 use Wikimedia\Rdbms\IDatabase
;
65 use Wikimedia\Rdbms\ILoadBalancer
;
66 use Wikimedia\Rdbms\IResultWrapper
;
69 * Service for looking up page revisions.
72 * @since 1.32 Renamed from MediaWiki\Storage\RevisionStore
74 * @note This was written to act as a drop-in replacement for the corresponding
75 * static methods in Revision.
78 implements IDBAccessObject
, RevisionFactory
, RevisionLookup
, LoggerAwareInterface
{
80 const ROW_CACHE_KEY
= 'revision-row-1.29';
94 * @see $wgContentHandlerUseDB
96 private $contentHandlerUseDB = true;
101 private $loadBalancer;
104 * @var WANObjectCache
111 private $commentStore;
114 * @var ActorMigration
116 private $actorMigration;
119 * @var LoggerInterface
124 * @var NameTableStore
126 private $contentModelStore;
129 * @var NameTableStore
131 private $slotRoleStore;
133 /** @var int An appropriate combination of SCHEMA_COMPAT_XXX flags. */
134 private $mcrMigrationStage;
136 /** @var SlotRoleRegistry */
137 private $slotRoleRegistry;
140 * @todo $blobStore should be allowed to be any BlobStore!
142 * @param ILoadBalancer $loadBalancer
143 * @param SqlBlobStore $blobStore
144 * @param WANObjectCache $cache A cache for caching revision rows. This can be the local
145 * wiki's default instance even if $dbDomain refers to a different wiki, since
146 * makeGlobalKey() is used to constructed a key that allows cached revision rows from
147 * the same database to be re-used between wikis. For example, enwiki and frwiki will
148 * use the same cache keys for revision rows from the wikidatawiki database, regardless
149 * of the cache's default key space.
150 * @param CommentStore $commentStore
151 * @param NameTableStore $contentModelStore
152 * @param NameTableStore $slotRoleStore
153 * @param SlotRoleRegistry $slotRoleRegistry
154 * @param int $mcrMigrationStage An appropriate combination of SCHEMA_COMPAT_XXX flags
155 * @param ActorMigration $actorMigration
156 * @param bool|string $dbDomain DB domain of the relevant wiki or false for the current one
158 public function __construct(
159 ILoadBalancer
$loadBalancer,
160 SqlBlobStore
$blobStore,
161 WANObjectCache
$cache,
162 CommentStore
$commentStore,
163 NameTableStore
$contentModelStore,
164 NameTableStore
$slotRoleStore,
165 SlotRoleRegistry
$slotRoleRegistry,
167 ActorMigration
$actorMigration,
170 Assert
::parameterType( 'string|boolean', $dbDomain, '$dbDomain' );
171 Assert
::parameterType( 'integer', $mcrMigrationStage, '$mcrMigrationStage' );
173 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH
) !== SCHEMA_COMPAT_READ_BOTH
,
174 '$mcrMigrationStage',
175 'Reading from the old and the new schema at the same time is not supported.'
178 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH
) !== 0,
179 '$mcrMigrationStage',
180 'Reading needs to be enabled for the old or the new schema.'
183 ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_BOTH
) !== 0,
184 '$mcrMigrationStage',
185 'Writing needs to be enabled for the old or the new schema.'
188 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_OLD
) === 0
189 ||
( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) !== 0,
190 '$mcrMigrationStage',
191 'Cannot read the old schema when not also writing it.'
194 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_NEW
) === 0
195 ||
( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) !== 0,
196 '$mcrMigrationStage',
197 'Cannot read the new schema when not also writing it.'
200 $this->loadBalancer
= $loadBalancer;
201 $this->blobStore
= $blobStore;
202 $this->cache
= $cache;
203 $this->commentStore
= $commentStore;
204 $this->contentModelStore
= $contentModelStore;
205 $this->slotRoleStore
= $slotRoleStore;
206 $this->slotRoleRegistry
= $slotRoleRegistry;
207 $this->mcrMigrationStage
= $mcrMigrationStage;
208 $this->actorMigration
= $actorMigration;
209 $this->dbDomain
= $dbDomain;
210 $this->logger
= new NullLogger();
214 * @param int $flags A combination of SCHEMA_COMPAT_XXX flags.
215 * @return bool True if all the given flags were set in the $mcrMigrationStage
216 * parameter passed to the constructor.
218 private function hasMcrSchemaFlags( $flags ) {
219 return ( $this->mcrMigrationStage
& $flags ) === $flags;
223 * Throws a RevisionAccessException if this RevisionStore is configured for cross-wiki loading
224 * and still reading from the old DB schema.
226 * @throws RevisionAccessException
228 private function assertCrossWikiContentLoadingIsSafe() {
229 if ( $this->dbDomain
!== false && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
230 throw new RevisionAccessException(
231 "Cross-wiki content loading is not supported by the pre-MCR schema"
236 public function setLogger( LoggerInterface
$logger ) {
237 $this->logger
= $logger;
241 * @return bool Whether the store is read-only
243 public function isReadOnly() {
244 return $this->blobStore
->isReadOnly();
250 public function getContentHandlerUseDB() {
251 return $this->contentHandlerUseDB
;
255 * @see $wgContentHandlerUseDB
256 * @param bool $contentHandlerUseDB
257 * @throws MWException
259 public function setContentHandlerUseDB( $contentHandlerUseDB ) {
260 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
)
261 ||
$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
)
263 if ( !$contentHandlerUseDB ) {
264 throw new MWException(
265 'Content model must be stored in the database for multi content revision migration.'
269 $this->contentHandlerUseDB
= $contentHandlerUseDB;
273 * @return ILoadBalancer
275 private function getDBLoadBalancer() {
276 return $this->loadBalancer
;
280 * @param int $mode DB_MASTER or DB_REPLICA
281 * @param array $groups
285 private function getDBConnection( $mode, $groups = [] ) {
286 $lb = $this->getDBLoadBalancer();
287 return $lb->getConnection( $mode, $groups, $this->dbDomain
);
291 * @param int $queryFlags a bit field composed of READ_XXX flags
295 private function getDBConnectionRefForQueryFlags( $queryFlags ) {
296 list( $mode, ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
297 return $this->getDBConnectionRef( $mode );
301 * @param IDatabase $connection
303 private function releaseDBConnection( IDatabase
$connection ) {
304 $lb = $this->getDBLoadBalancer();
305 $lb->reuseConnection( $connection );
309 * @param int $mode DB_MASTER or DB_REPLICA
313 private function getDBConnectionRef( $mode ) {
314 $lb = $this->getDBLoadBalancer();
315 return $lb->getConnectionRef( $mode, [], $this->dbDomain
);
319 * Determines the page Title based on the available information.
321 * MCR migration note: this corresponds to Revision::getTitle
323 * @note this method should be private, external use should be avoided!
325 * @param int|null $pageId
326 * @param int|null $revId
327 * @param int $queryFlags
330 * @throws RevisionAccessException
332 public function getTitle( $pageId, $revId, $queryFlags = self
::READ_NORMAL
) {
333 if ( !$pageId && !$revId ) {
334 throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
337 // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
338 // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
339 if ( DBAccessObjectUtils
::hasFlags( $queryFlags, self
::READ_LATEST_IMMUTABLE
) ) {
340 $queryFlags = self
::READ_NORMAL
;
343 $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->dbDomain
=== false );
344 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
345 $titleFlags = ( $dbMode == DB_MASTER ? Title
::GAID_FOR_UPDATE
: 0 );
347 // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
348 if ( $canUseTitleNewFromId ) {
349 // TODO: better foreign title handling (introduce TitleFactory)
350 $title = Title
::newFromID( $pageId, $titleFlags );
356 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
357 $canUseRevId = ( $revId !== null && $revId > 0 );
359 if ( $canUseRevId ) {
360 $dbr = $this->getDBConnectionRef( $dbMode );
361 // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
362 $row = $dbr->selectRow(
363 [ 'revision', 'page' ],
372 [ 'rev_id' => $revId ],
375 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
378 // TODO: better foreign title handling (introduce TitleFactory)
379 return Title
::newFromRow( $row );
383 // If we still don't have a title, fallback to master if that wasn't already happening.
384 if ( $dbMode !== DB_MASTER
) {
385 $title = $this->getTitle( $pageId, $revId, self
::READ_LATEST
);
388 __METHOD__
. ' fell back to READ_LATEST and got a Title.',
389 [ 'trace' => wfBacktrace() ]
395 throw new RevisionAccessException(
396 "Could not determine title for page ID $pageId and revision ID $revId"
401 * @param mixed $value
402 * @param string $name
404 * @throws IncompleteRevisionException if $value is null
405 * @return mixed $value, if $value is not null
407 private function failOnNull( $value, $name ) {
408 if ( $value === null ) {
409 throw new IncompleteRevisionException(
410 "$name must not be " . var_export( $value, true ) . "!"
418 * @param mixed $value
419 * @param string $name
421 * @throws IncompleteRevisionException if $value is empty
422 * @return mixed $value, if $value is not null
424 private function failOnEmpty( $value, $name ) {
425 if ( $value === null ||
$value === 0 ||
$value === '' ) {
426 throw new IncompleteRevisionException(
427 "$name must not be " . var_export( $value, true ) . "!"
435 * Insert a new revision into the database, returning the new revision record
436 * on success and dies horribly on failure.
438 * MCR migration note: this replaces Revision::insertOn
440 * @param RevisionRecord $rev
441 * @param IDatabase $dbw (master connection)
443 * @throws InvalidArgumentException
444 * @return RevisionRecord the new revision record.
446 public function insertRevisionOn( RevisionRecord
$rev, IDatabase
$dbw ) {
447 // TODO: pass in a DBTransactionContext instead of a database connection.
448 $this->checkDatabaseWikiId( $dbw );
450 $slotRoles = $rev->getSlotRoles();
452 // Make sure the main slot is always provided throughout migration
453 if ( !in_array( SlotRecord
::MAIN
, $slotRoles ) ) {
454 throw new InvalidArgumentException(
455 'main slot must be provided'
459 // If we are not writing into the new schema, we can't support extra slots.
460 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
)
461 && $slotRoles !== [ SlotRecord
::MAIN
]
463 throw new InvalidArgumentException(
464 'Only the main slot is supported when not writing to the MCR enabled schema!'
468 // As long as we are not reading from the new schema, we don't want to write extra slots.
469 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
)
470 && $slotRoles !== [ SlotRecord
::MAIN
]
472 throw new InvalidArgumentException(
473 'Only the main slot is supported when not reading from the MCR enabled schema!'
478 $this->failOnNull( $rev->getSize(), 'size field' );
479 $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
480 $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
481 $comment = $this->failOnNull( $rev->getComment( RevisionRecord
::RAW
), 'comment' );
482 $user = $this->failOnNull( $rev->getUser( RevisionRecord
::RAW
), 'user' );
483 $this->failOnNull( $user->getId(), 'user field' );
484 $this->failOnEmpty( $user->getName(), 'user_text field' );
486 if ( !$rev->isReadyForInsertion() ) {
487 // This is here for future-proofing. At the time this check being added, it
488 // was redundant to the individual checks above.
489 throw new IncompleteRevisionException( 'Revision is incomplete' );
492 // TODO: we shouldn't need an actual Title here.
493 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
494 $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
496 $parentId = $rev->getParentId() === null
497 ?
$this->getPreviousRevisionId( $dbw, $rev )
498 : $rev->getParentId();
500 /** @var RevisionRecord $rev */
501 $rev = $dbw->doAtomicSection(
503 function ( IDatabase
$dbw, $fname ) use (
511 return $this->insertRevisionInternal(
524 Assert
::postcondition( $rev->getId() > 0, 'revision must have an ID' );
525 Assert
::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
526 Assert
::postcondition(
527 $rev->getComment( RevisionRecord
::RAW
) !== null,
528 'revision must have a comment'
530 Assert
::postcondition(
531 $rev->getUser( RevisionRecord
::RAW
) !== null,
532 'revision must have a user'
535 // Trigger exception if the main slot is missing.
536 // Technically, this could go away after MCR migration: while
537 // calling code may require a main slot to exist, RevisionStore
538 // really should not know or care about that requirement.
539 $rev->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
541 foreach ( $slotRoles as $role ) {
542 $slot = $rev->getSlot( $role, RevisionRecord
::RAW
);
543 Assert
::postcondition(
544 $slot->getContent() !== null,
545 $role . ' slot must have content'
547 Assert
::postcondition(
548 $slot->hasRevision(),
549 $role . ' slot must have a revision associated'
553 Hooks
::run( 'RevisionRecordInserted', [ $rev ] );
555 // TODO: deprecate in 1.32!
556 $legacyRevision = new Revision( $rev );
557 Hooks
::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
562 private function insertRevisionInternal(
566 CommentStoreComment
$comment,
571 $slotRoles = $rev->getSlotRoles();
573 $revisionRow = $this->insertRevisionRowOn(
580 $revisionId = $revisionRow['rev_id'];
583 BlobStore
::PAGE_HINT
=> $pageId,
584 BlobStore
::REVISION_HINT
=> $revisionId,
585 BlobStore
::PARENT_HINT
=> $parentId,
589 foreach ( $slotRoles as $role ) {
590 $slot = $rev->getSlot( $role, RevisionRecord
::RAW
);
592 // If the SlotRecord already has a revision ID set, this means it already exists
593 // in the database, and should already belong to the current revision.
594 // However, a slot may already have a revision, but no content ID, if the slot
595 // is emulated based on the archive table, because we are in SCHEMA_COMPAT_READ_OLD
596 // mode, and the respective archive row was not yet migrated to the new schema.
597 // In that case, a new slot row (and content row) must be inserted even during
599 if ( $slot->hasRevision() && $slot->hasContentId() ) {
600 // TODO: properly abort transaction if the assertion fails!
602 $slot->getRevision() === $revisionId,
603 'slot role ' . $slot->getRole(),
604 'Existing slot should belong to revision '
605 . $revisionId . ', but belongs to revision ' . $slot->getRevision() . '!'
608 // Slot exists, nothing to do, move along.
609 // This happens when restoring archived revisions.
611 $newSlots[$role] = $slot;
613 // Write the main slot's text ID to the revision table for backwards compatibility
614 if ( $slot->getRole() === SlotRecord
::MAIN
615 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
)
617 $blobAddress = $slot->getAddress();
618 $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
621 $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $title, $blobHints );
625 $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
627 $rev = new RevisionStoreRecord(
631 (object)$revisionRow,
632 new RevisionSlots( $newSlots ),
640 * @param IDatabase $dbw
641 * @param int $revisionId
642 * @param string &$blobAddress (may change!)
644 * @return int the text row id
646 private function updateRevisionTextId( IDatabase
$dbw, $revisionId, &$blobAddress ) {
647 $textId = $this->blobStore
->getTextIdFromAddress( $blobAddress );
649 throw new LogicException(
650 'Blob address not supported in 1.29 database schema: ' . $blobAddress
654 // getTextIdFromAddress() is free to insert something into the text table, so $textId
655 // may be a new value, not anything already contained in $blobAddress.
656 $blobAddress = SqlBlobStore
::makeAddressFromTextId( $textId );
660 [ 'rev_text_id' => $textId ],
661 [ 'rev_id' => $revisionId ],
669 * @param IDatabase $dbw
670 * @param int $revisionId
671 * @param SlotRecord $protoSlot
672 * @param Title $title
673 * @param array $blobHints See the BlobStore::XXX_HINT constants
676 private function insertSlotOn(
679 SlotRecord
$protoSlot,
681 array $blobHints = []
683 if ( $protoSlot->hasAddress() ) {
684 $blobAddress = $protoSlot->getAddress();
686 $blobAddress = $this->storeContentBlob( $protoSlot, $title, $blobHints );
691 // Write the main slot's text ID to the revision table for backwards compatibility
692 if ( $protoSlot->getRole() === SlotRecord
::MAIN
693 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
)
695 // If SCHEMA_COMPAT_WRITE_NEW is also set, the fake content ID is overwritten
696 // with the real content ID below.
697 $textId = $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
698 $contentId = $this->emulateContentId( $textId );
701 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
702 if ( $protoSlot->hasContentId() ) {
703 $contentId = $protoSlot->getContentId();
705 $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
708 $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
711 $savedSlot = SlotRecord
::newSaved(
722 * Insert IP revision into ip_changes for use when querying for a range.
723 * @param IDatabase $dbw
725 * @param RevisionRecord $rev
726 * @param int $revisionId
728 private function insertIpChangesRow(
734 if ( $user->getId() === 0 && IP
::isValid( $user->getName() ) ) {
736 'ipc_rev_id' => $revisionId,
737 'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
738 'ipc_hex' => IP
::toHex( $user->getName() ),
740 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__
);
745 * @param IDatabase $dbw
746 * @param RevisionRecord $rev
747 * @param Title $title
748 * @param int $parentId
750 * @return array a revision table row
752 * @throws MWException
753 * @throws MWUnknownContentModelException
755 private function insertRevisionRowOn(
761 $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $title, $parentId );
763 list( $commentFields, $commentCallback ) =
764 $this->commentStore
->insertWithTempTable(
767 $rev->getComment( RevisionRecord
::RAW
)
769 $revisionRow +
= $commentFields;
771 list( $actorFields, $actorCallback ) =
772 $this->actorMigration
->getInsertValuesWithTempTable(
775 $rev->getUser( RevisionRecord
::RAW
)
777 $revisionRow +
= $actorFields;
779 $dbw->insert( 'revision', $revisionRow, __METHOD__
);
781 if ( !isset( $revisionRow['rev_id'] ) ) {
782 // only if auto-increment was used
783 $revisionRow['rev_id'] = intval( $dbw->insertId() );
785 if ( $dbw->getType() === 'mysql' ) {
786 // (T202032) MySQL until 8.0 and MariaDB until some version after 10.1.34 don't save the
787 // auto-increment value to disk, so on server restart it might reuse IDs from deleted
788 // revisions. We can fix that with an insert with an explicit rev_id value, if necessary.
790 $maxRevId = intval( $dbw->selectField( 'archive', 'MAX(ar_rev_id)', '', __METHOD__
) );
792 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
793 $maxRevId2 = intval( $dbw->selectField( 'slots', 'MAX(slot_revision_id)', '', __METHOD__
) );
794 if ( $maxRevId2 >= $maxRevId ) {
795 $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 function ( $trigger, IDatabase
$dbw ) use ( $fname ) {
817 $dbw->unlock( 'fix-for-T202032', $fname );
821 $dbw->delete( 'revision', [ 'rev_id' => $revisionRow['rev_id'] ], __METHOD__
);
823 // The locking here is mostly to make MySQL bypass the REPEATABLE-READ transaction
824 // isolation (weird MySQL "feature"). It does seem to block concurrent auto-incrementing
825 // inserts too, though, at least on MariaDB 10.1.29.
827 // Don't try to lock `revision` in this way, it'll deadlock if there are concurrent
828 // transactions in this code path thanks to the row lock from the original ->insert() above.
830 // And we have to use raw SQL to bypass the "aggregation used with a locking SELECT" warning
831 // that's for non-MySQL DBs.
833 $dbw->selectSQLText( 'archive', [ 'v' => "MAX(ar_rev_id)" ], '', __METHOD__
) . ' FOR UPDATE'
835 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
837 $dbw->selectSQLText( 'slots', [ 'v' => "MAX(slot_revision_id)" ], '', __METHOD__
)
845 $row1 ?
intval( $row1->v
) : 0,
846 $row2 ?
intval( $row2->v
) : 0
849 // If we don't have SCHEMA_COMPAT_WRITE_NEW, all except the first of any concurrent
850 // transactions will throw a duplicate key error here. It doesn't seem worth trying
852 $revisionRow['rev_id'] = $maxRevId +
1;
853 $dbw->insert( 'revision', $revisionRow, __METHOD__
);
858 $commentCallback( $revisionRow['rev_id'] );
859 $actorCallback( $revisionRow['rev_id'], $revisionRow );
865 * @param IDatabase $dbw
866 * @param RevisionRecord $rev
867 * @param Title $title
868 * @param int $parentId
870 * @return array [ 0 => array $revisionRow, 1 => callable ]
871 * @throws MWException
872 * @throws MWUnknownContentModelException
874 private function getBaseRevisionRow(
880 // Record the edit in revisions
882 'rev_page' => $rev->getPageId(),
883 'rev_parent_id' => $parentId,
884 'rev_minor_edit' => $rev->isMinor() ?
1 : 0,
885 'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
886 'rev_deleted' => $rev->getVisibility(),
887 'rev_len' => $rev->getSize(),
888 'rev_sha1' => $rev->getSha1(),
891 if ( $rev->getId() !== null ) {
892 // Needed to restore revisions with their original ID
893 $revisionRow['rev_id'] = $rev->getId();
896 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) ) {
897 // In non MCR mode this IF section will relate to the main slot
898 $mainSlot = $rev->getSlot( SlotRecord
::MAIN
);
899 $model = $mainSlot->getModel();
900 $format = $mainSlot->getFormat();
902 // MCR migration note: rev_content_model and rev_content_format will go away
903 if ( $this->contentHandlerUseDB
) {
904 $this->assertCrossWikiContentLoadingIsSafe();
906 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
907 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
909 $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
910 $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
918 * @param SlotRecord $slot
919 * @param Title $title
920 * @param array $blobHints See the BlobStore::XXX_HINT constants
922 * @throws MWException
923 * @return string the blob address
925 private function storeContentBlob(
928 array $blobHints = []
930 $content = $slot->getContent();
931 $format = $content->getDefaultFormat();
932 $model = $content->getModel();
934 $this->checkContent( $content, $title, $slot->getRole() );
936 return $this->blobStore
->storeBlob(
937 $content->serialize( $format ),
938 // These hints "leak" some information from the higher abstraction layer to
939 // low level storage to allow for optimization.
943 BlobStore
::DESIGNATION_HINT
=> 'page-content',
944 BlobStore
::ROLE_HINT
=> $slot->getRole(),
945 BlobStore
::SHA1_HINT
=> $slot->getSha1(),
946 BlobStore
::MODEL_HINT
=> $model,
947 BlobStore
::FORMAT_HINT
=> $format,
954 * @param SlotRecord $slot
955 * @param IDatabase $dbw
956 * @param int $revisionId
957 * @param int $contentId
959 private function insertSlotRowOn( SlotRecord
$slot, IDatabase
$dbw, $revisionId, $contentId ) {
961 'slot_revision_id' => $revisionId,
962 'slot_role_id' => $this->slotRoleStore
->acquireId( $slot->getRole() ),
963 'slot_content_id' => $contentId,
964 // If the slot has a specific origin use that ID, otherwise use the ID of the revision
965 // that we just inserted.
966 'slot_origin' => $slot->hasOrigin() ?
$slot->getOrigin() : $revisionId,
968 $dbw->insert( 'slots', $slotRow, __METHOD__
);
972 * @param SlotRecord $slot
973 * @param IDatabase $dbw
974 * @param string $blobAddress
975 * @return int content row ID
977 private function insertContentRowOn( SlotRecord
$slot, IDatabase
$dbw, $blobAddress ) {
979 'content_size' => $slot->getSize(),
980 'content_sha1' => $slot->getSha1(),
981 'content_model' => $this->contentModelStore
->acquireId( $slot->getModel() ),
982 'content_address' => $blobAddress,
984 $dbw->insert( 'content', $contentRow, __METHOD__
);
985 return intval( $dbw->insertId() );
989 * MCR migration note: this corresponds to Revision::checkContentModel
991 * @param Content $content
992 * @param Title $title
993 * @param string $role
995 * @throws MWException
996 * @throws MWUnknownContentModelException
998 private function checkContent( Content
$content, Title
$title, $role ) {
999 // Note: may return null for revisions that have not yet been inserted
1001 $model = $content->getModel();
1002 $format = $content->getDefaultFormat();
1003 $handler = $content->getContentHandler();
1007 if ( !$handler->isSupportedFormat( $format ) ) {
1008 throw new MWException( "Can't use format $format with content model $model on $name" );
1011 if ( !$this->contentHandlerUseDB
) {
1012 // if $wgContentHandlerUseDB is not set,
1013 // all revisions must use the default content model and format.
1015 $this->assertCrossWikiContentLoadingIsSafe();
1017 $roleHandler = $this->slotRoleRegistry
->getRoleHandler( $role );
1018 $defaultModel = $roleHandler->getDefaultModel( $title );
1019 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1020 $defaultFormat = $defaultHandler->getDefaultFormat();
1022 if ( $model != $defaultModel ) {
1023 throw new MWException( "Can't save non-default content model with "
1024 . "\$wgContentHandlerUseDB disabled: model is $model, "
1025 . "default for $name is $defaultModel"
1029 if ( $format != $defaultFormat ) {
1030 throw new MWException( "Can't use non-default content format with "
1031 . "\$wgContentHandlerUseDB disabled: format is $format, "
1032 . "default for $name is $defaultFormat"
1037 if ( !$content->isValid() ) {
1038 throw new MWException(
1039 "New content for $name is not valid! Content model is $model"
1045 * Create a new null-revision for insertion into a page's
1046 * history. This will not re-save the text, but simply refer
1047 * to the text from the previous version.
1049 * Such revisions can for instance identify page rename
1050 * operations and other such meta-modifications.
1052 * @note This method grabs a FOR UPDATE lock on the relevant row of the page table,
1053 * to prevent a new revision from being inserted before the null revision has been written
1056 * MCR migration note: this replaces Revision::newNullRevision
1058 * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
1061 * @param IDatabase $dbw used for obtaining the lock on the page table row
1062 * @param Title $title Title of the page to read from
1063 * @param CommentStoreComment $comment RevisionRecord's summary
1064 * @param bool $minor Whether the revision should be considered as minor
1065 * @param User $user The user to attribute the revision to
1067 * @return RevisionRecord|null RevisionRecord or null on error
1069 public function newNullRevision(
1072 CommentStoreComment
$comment,
1076 $this->checkDatabaseWikiId( $dbw );
1078 $pageId = $title->getArticleID();
1080 // T51581: Lock the page table row to ensure no other process
1081 // is adding a revision to the page at the same time.
1082 // Avoid locking extra tables, compare T191892.
1083 $pageLatest = $dbw->selectField(
1086 [ 'page_id' => $pageId ],
1091 if ( !$pageLatest ) {
1095 // Fetch the actual revision row from master, without locking all extra tables.
1096 $oldRevision = $this->loadRevisionFromConds(
1098 [ 'rev_id' => intval( $pageLatest ) ],
1103 if ( !$oldRevision ) {
1104 $msg = "Failed to load latest revision ID $pageLatest of page ID $pageId.";
1105 $this->logger
->error(
1107 [ 'exception' => new RuntimeException( $msg ) ]
1112 // Construct the new revision
1113 $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1114 $newRevision = MutableRevisionRecord
::newFromParentRevision( $oldRevision );
1116 $newRevision->setComment( $comment );
1117 $newRevision->setUser( $user );
1118 $newRevision->setTimestamp( $timestamp );
1119 $newRevision->setMinorEdit( $minor );
1121 return $newRevision;
1125 * MCR migration note: this replaces Revision::isUnpatrolled
1127 * @todo This is overly specific, so move or kill this method.
1129 * @param RevisionRecord $rev
1131 * @return int Rcid of the unpatrolled row, zero if there isn't one
1133 public function getRcIdIfUnpatrolled( RevisionRecord
$rev ) {
1134 $rc = $this->getRecentChange( $rev );
1135 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange
::PRC_UNPATROLLED
) {
1136 return $rc->getAttribute( 'rc_id' );
1143 * Get the RC object belonging to the current revision, if there's one
1145 * MCR migration note: this replaces Revision::getRecentChange
1147 * @todo move this somewhere else?
1149 * @param RevisionRecord $rev
1150 * @param int $flags (optional) $flags include:
1151 * IDBAccessObject::READ_LATEST: Select the data from the master
1153 * @return null|RecentChange
1155 public function getRecentChange( RevisionRecord
$rev, $flags = 0 ) {
1156 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
1157 $db = $this->getDBConnection( $dbType );
1159 $userIdentity = $rev->getUser( RevisionRecord
::RAW
);
1161 if ( !$userIdentity ) {
1162 // If the revision has no user identity, chances are it never went
1163 // into the database, and doesn't have an RC entry.
1167 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
1168 $actorWhere = $this->actorMigration
->getWhere( $db, 'rc_user', $rev->getUser(), false );
1169 $rc = RecentChange
::newFromConds(
1171 $actorWhere['conds'],
1172 'rc_timestamp' => $db->timestamp( $rev->getTimestamp() ),
1173 'rc_this_oldid' => $rev->getId()
1179 $this->releaseDBConnection( $db );
1181 // XXX: cache this locally? Glue it to the RevisionRecord?
1186 * Maps fields of the archive row to corresponding revision rows.
1188 * @param object $archiveRow
1190 * @return object a revision row object, corresponding to $archiveRow.
1192 private static function mapArchiveFields( $archiveRow ) {
1194 // keep with ar prefix:
1197 // not the same suffix:
1198 'ar_page_id' => 'rev_page',
1199 'ar_rev_id' => 'rev_id',
1202 'ar_text_id' => 'rev_text_id',
1203 'ar_timestamp' => 'rev_timestamp',
1204 'ar_user_text' => 'rev_user_text',
1205 'ar_user' => 'rev_user',
1206 'ar_actor' => 'rev_actor',
1207 'ar_minor_edit' => 'rev_minor_edit',
1208 'ar_deleted' => 'rev_deleted',
1209 'ar_len' => 'rev_len',
1210 'ar_parent_id' => 'rev_parent_id',
1211 'ar_sha1' => 'rev_sha1',
1212 'ar_comment' => 'rev_comment',
1213 'ar_comment_cid' => 'rev_comment_cid',
1214 'ar_comment_id' => 'rev_comment_id',
1215 'ar_comment_text' => 'rev_comment_text',
1216 'ar_comment_data' => 'rev_comment_data',
1217 'ar_comment_old' => 'rev_comment_old',
1218 'ar_content_format' => 'rev_content_format',
1219 'ar_content_model' => 'rev_content_model',
1222 $revRow = new stdClass();
1223 foreach ( $fieldMap as $arKey => $revKey ) {
1224 if ( property_exists( $archiveRow, $arKey ) ) {
1225 $revRow->$revKey = $archiveRow->$arKey;
1233 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
1235 * @param object|array $row Either a database row or an array
1236 * @param int $queryFlags for callbacks
1237 * @param Title $title
1239 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
1240 * @throws MWException
1242 private function emulateMainSlot_1_29( $row, $queryFlags, Title
$title ) {
1243 $mainSlotRow = new stdClass();
1244 $mainSlotRow->role_name
= SlotRecord
::MAIN
;
1245 $mainSlotRow->model_name
= null;
1246 $mainSlotRow->slot_revision_id
= null;
1247 $mainSlotRow->slot_content_id
= null;
1248 $mainSlotRow->content_address
= null;
1254 if ( is_object( $row ) ) {
1255 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
) ) {
1256 // Don't emulate from a row when using the new schema.
1257 // Emulating from an array is still OK.
1258 throw new LogicException( 'Can\'t emulate the main slot when using MCR schema.' );
1262 if ( !isset( $row->rev_id
) && ( isset( $row->ar_user
) ||
isset( $row->ar_actor
) ) ) {
1263 $row = $this->mapArchiveFields( $row );
1266 if ( isset( $row->rev_text_id
) && $row->rev_text_id
> 0 ) {
1267 $mainSlotRow->content_address
= SqlBlobStore
::makeAddressFromTextId(
1272 // This is used by null-revisions
1273 $mainSlotRow->slot_origin
= isset( $row->slot_origin
)
1274 ?
intval( $row->slot_origin
)
1277 if ( isset( $row->old_text
) ) {
1278 // this happens when the text-table gets joined directly, in the pre-1.30 schema
1279 $blobData = isset( $row->old_text
) ?
strval( $row->old_text
) : null;
1280 // Check against selects that might have not included old_flags
1281 if ( !property_exists( $row, 'old_flags' ) ) {
1282 throw new InvalidArgumentException( 'old_flags was not set in $row' );
1284 $blobFlags = $row->old_flags ??
'';
1287 $mainSlotRow->slot_revision_id
= intval( $row->rev_id
);
1289 $mainSlotRow->content_size
= isset( $row->rev_len
) ?
intval( $row->rev_len
) : null;
1290 $mainSlotRow->content_sha1
= isset( $row->rev_sha1
) ?
strval( $row->rev_sha1
) : null;
1291 $mainSlotRow->model_name
= isset( $row->rev_content_model
)
1292 ?
strval( $row->rev_content_model
)
1294 // XXX: in the future, we'll probably always use the default format, and drop content_format
1295 $mainSlotRow->format_name
= isset( $row->rev_content_format
)
1296 ?
strval( $row->rev_content_format
)
1299 if ( isset( $row->rev_text_id
) && intval( $row->rev_text_id
) > 0 ) {
1300 // Overwritten below for SCHEMA_COMPAT_WRITE_NEW
1301 $mainSlotRow->slot_content_id
1302 = $this->emulateContentId( intval( $row->rev_text_id
) );
1304 } elseif ( is_array( $row ) ) {
1305 $mainSlotRow->slot_revision_id
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
1307 $mainSlotRow->slot_origin
= isset( $row['slot_origin'] )
1308 ?
intval( $row['slot_origin'] )
1310 $mainSlotRow->content_address
= isset( $row['text_id'] )
1311 ? SqlBlobStore
::makeAddressFromTextId( intval( $row['text_id'] ) )
1313 $mainSlotRow->content_size
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
1314 $mainSlotRow->content_sha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
1316 $mainSlotRow->model_name
= isset( $row['content_model'] )
1317 ?
strval( $row['content_model'] ) : null; // XXX: must be a string!
1318 // XXX: in the future, we'll probably always use the default format, and drop content_format
1319 $mainSlotRow->format_name
= isset( $row['content_format'] )
1320 ?
strval( $row['content_format'] ) : null;
1321 $blobData = isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
1322 // XXX: If the flags field is not set then $blobFlags should be null so that no
1323 // decoding will happen. An empty string will result in default decodings.
1324 $blobFlags = isset( $row['flags'] ) ?
trim( strval( $row['flags'] ) ) : null;
1326 // if we have a Content object, override mText and mContentModel
1327 if ( !empty( $row['content'] ) ) {
1328 if ( !( $row['content'] instanceof Content
) ) {
1329 throw new MWException( 'content field must contain a Content object.' );
1332 /** @var Content $content */
1333 $content = $row['content'];
1334 $handler = $content->getContentHandler();
1336 $mainSlotRow->model_name
= $content->getModel();
1338 // XXX: in the future, we'll probably always use the default format.
1339 if ( $mainSlotRow->format_name
=== null ) {
1340 $mainSlotRow->format_name
= $handler->getDefaultFormat();
1344 if ( isset( $row['text_id'] ) && intval( $row['text_id'] ) > 0 ) {
1345 // Overwritten below for SCHEMA_COMPAT_WRITE_NEW
1346 $mainSlotRow->slot_content_id
1347 = $this->emulateContentId( intval( $row['text_id'] ) );
1350 throw new MWException( 'Revision constructor passed invalid row format.' );
1353 // With the old schema, the content changes with every revision,
1354 // except for null-revisions.
1355 if ( !isset( $mainSlotRow->slot_origin
) ) {
1356 $mainSlotRow->slot_origin
= $mainSlotRow->slot_revision_id
;
1359 if ( $mainSlotRow->model_name
=== null ) {
1360 $mainSlotRow->model_name
= function ( SlotRecord
$slot ) use ( $title ) {
1361 $this->assertCrossWikiContentLoadingIsSafe();
1363 return $this->slotRoleRegistry
->getRoleHandler( $slot->getRole() )
1364 ->getDefaultModel( $title );
1369 // XXX: We should perhaps fail if $blobData is null and $mainSlotRow->content_address
1370 // is missing, but "empty revisions" with no content are used in some edge cases.
1372 $content = function ( SlotRecord
$slot )
1373 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
1375 return $this->loadSlotContent(
1379 $mainSlotRow->format_name
,
1385 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
1386 // NOTE: this callback will be looped through RevisionSlot::newInherited(), allowing
1387 // the inherited slot to have the same content_id as the original slot. In that case,
1388 // $slot will be the inherited slot, while $mainSlotRow still refers to the original slot.
1389 $mainSlotRow->slot_content_id
=
1390 function ( SlotRecord
$slot ) use ( $queryFlags, $mainSlotRow ) {
1391 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1392 return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id
, SlotRecord
::MAIN
);
1396 return new SlotRecord( $mainSlotRow, $content );
1400 * Provides a content ID to use with emulated SlotRecords in SCHEMA_COMPAT_OLD mode,
1401 * based on the revision's text ID (rev_text_id or ar_text_id, respectively).
1402 * Note that in SCHEMA_COMPAT_WRITE_BOTH, a callback to findSlotContentId() should be used
1403 * instead, since in that mode, some revision rows may already have a real content ID,
1404 * while other's don't - and for the ones that don't, we should indicate that it
1405 * is missing and cause SlotRecords::hasContentId() to return false.
1407 * @param int $textId
1408 * @return int The emulated content ID
1410 private function emulateContentId( $textId ) {
1411 // Return a negative number to ensure the ID is distinct from any real content IDs
1412 // that will be assigned in SCHEMA_COMPAT_WRITE_NEW mode and read in SCHEMA_COMPAT_READ_NEW
1418 * Loads a Content object based on a slot row.
1420 * This method does not call $slot->getContent(), and may be used as a callback
1421 * called by $slot->getContent().
1423 * MCR migration note: this roughly corresponds to Revision::getContentInternal
1425 * @param SlotRecord $slot The SlotRecord to load content for
1426 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
1427 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
1428 * Use null if no processing should happen. That is in constrast to the empty string,
1429 * which causes the blob to be decoded according to the configured legacy encoding.
1430 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
1431 * @param int $queryFlags
1433 * @throws RevisionAccessException
1436 private function loadSlotContent(
1443 if ( $blobData !== null ) {
1444 Assert
::parameterType( 'string', $blobData, '$blobData' );
1445 Assert
::parameterType( 'string|null', $blobFlags, '$blobFlags' );
1447 $cacheKey = $slot->hasAddress() ?
$slot->getAddress() : null;
1449 if ( $blobFlags === null ) {
1450 // No blob flags, so use the blob verbatim.
1453 $data = $this->blobStore
->expandBlob( $blobData, $blobFlags, $cacheKey );
1454 if ( $data === false ) {
1455 throw new RevisionAccessException(
1456 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
1462 $address = $slot->getAddress();
1464 $data = $this->blobStore
->getBlob( $address, $queryFlags );
1465 } catch ( BlobAccessException
$e ) {
1466 throw new RevisionAccessException(
1467 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
1472 // Unserialize content
1473 $handler = ContentHandler
::getForModelID( $slot->getModel() );
1475 $content = $handler->unserializeContent( $data, $blobFormat );
1480 * Load a page revision from a given revision ID number.
1481 * Returns null if no such revision can be found.
1483 * MCR migration note: this replaces Revision::newFromId
1486 * IDBAccessObject::READ_LATEST: Select the data from the master
1487 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1490 * @param int $flags (optional)
1491 * @return RevisionRecord|null
1493 public function getRevisionById( $id, $flags = 0 ) {
1494 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
1498 * Load either the current, or a specified, revision
1499 * that's attached to a given link target. If not attached
1500 * to that link target, will return null.
1502 * MCR migration note: this replaces Revision::newFromTitle
1505 * IDBAccessObject::READ_LATEST: Select the data from the master
1506 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1508 * @param LinkTarget $linkTarget
1509 * @param int $revId (optional)
1510 * @param int $flags Bitfield (optional)
1511 * @return RevisionRecord|null
1513 public function getRevisionByTitle( LinkTarget
$linkTarget, $revId = 0, $flags = 0 ) {
1514 // TODO should not require Title in future (T206498)
1515 $title = Title
::newFromLinkTarget( $linkTarget );
1517 'page_namespace' => $title->getNamespace(),
1518 'page_title' => $title->getDBkey()
1521 // Use the specified revision ID.
1522 // Note that we use newRevisionFromConds here because we want to retry
1523 // and fall back to master if the page is not found on a replica.
1524 // Since the caller supplied a revision ID, we are pretty sure the revision is
1525 // supposed to exist, so we should try hard to find it.
1526 $conds['rev_id'] = $revId;
1527 return $this->newRevisionFromConds( $conds, $flags, $title );
1529 // Use a join to get the latest revision.
1530 // Note that we don't use newRevisionFromConds here because we don't want to retry
1531 // and fall back to master. The assumption is that we only want to force the fallback
1532 // if we are quite sure the revision exists because the caller supplied a revision ID.
1533 // If the page isn't found at all on a replica, it probably simply does not exist.
1534 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1536 $conds[] = 'rev_id=page_latest';
1537 $rev = $this->loadRevisionFromConds( $db, $conds, $flags, $title );
1544 * Load either the current, or a specified, revision
1545 * that's attached to a given page ID.
1546 * Returns null if no such revision can be found.
1548 * MCR migration note: this replaces Revision::newFromPageId
1551 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
1552 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1554 * @param int $pageId
1555 * @param int $revId (optional)
1556 * @param int $flags Bitfield (optional)
1557 * @return RevisionRecord|null
1559 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
1560 $conds = [ 'page_id' => $pageId ];
1562 // Use the specified revision ID.
1563 // Note that we use newRevisionFromConds here because we want to retry
1564 // and fall back to master if the page is not found on a replica.
1565 // Since the caller supplied a revision ID, we are pretty sure the revision is
1566 // supposed to exist, so we should try hard to find it.
1567 $conds['rev_id'] = $revId;
1568 return $this->newRevisionFromConds( $conds, $flags );
1570 // Use a join to get the latest revision.
1571 // Note that we don't use newRevisionFromConds here because we don't want to retry
1572 // and fall back to master. The assumption is that we only want to force the fallback
1573 // if we are quite sure the revision exists because the caller supplied a revision ID.
1574 // If the page isn't found at all on a replica, it probably simply does not exist.
1575 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1577 $conds[] = 'rev_id=page_latest';
1578 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1585 * Load the revision for the given title with the given timestamp.
1586 * WARNING: Timestamps may in some circumstances not be unique,
1587 * so this isn't the best key to use.
1589 * MCR migration note: this replaces Revision::loadFromTimestamp
1591 * @param Title $title
1592 * @param string $timestamp
1593 * @return RevisionRecord|null
1595 public function getRevisionByTimestamp( $title, $timestamp ) {
1596 $db = $this->getDBConnection( DB_REPLICA
);
1597 return $this->newRevisionFromConds(
1599 'rev_timestamp' => $db->timestamp( $timestamp ),
1600 'page_namespace' => $title->getNamespace(),
1601 'page_title' => $title->getDBkey()
1609 * @param int $revId The revision to load slots for.
1610 * @param int $queryFlags
1611 * @param Title $title
1613 * @return SlotRecord[]
1615 private function loadSlotRecords( $revId, $queryFlags, Title
$title ) {
1616 $revQuery = self
::getSlotsQueryInfo( [ 'content' ] );
1618 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
1619 $db = $this->getDBConnectionRef( $dbMode );
1622 $revQuery['tables'],
1623 $revQuery['fields'],
1625 'slot_revision_id' => $revId,
1632 $slots = $this->constructSlotRecords( $revId, $res, $queryFlags, $title );
1638 * Factory method for SlotRecords based on known slot rows.
1640 * @param int $revId The revision to load slots for.
1641 * @param object[]|IResultWrapper $slotRows
1642 * @param int $queryFlags
1643 * @param Title $title
1645 * @return SlotRecord[]
1647 private function constructSlotRecords( $revId, $slotRows, $queryFlags, Title
$title ) {
1650 foreach ( $slotRows as $row ) {
1651 // Resolve role names and model names from in-memory cache, if they were not joined in.
1652 if ( !isset( $row->role_name
) ) {
1653 $row->role_name
= $this->slotRoleStore
->getName( (int)$row->slot_role_id
);
1656 if ( !isset( $row->model_name
) ) {
1657 if ( isset( $row->content_model
) ) {
1658 $row->model_name
= $this->contentModelStore
->getName( (int)$row->content_model
);
1660 // We may get here if $row->model_name is set but null, perhaps because it
1661 // came from rev_content_model, which is NULL for the default model.
1662 $slotRoleHandler = $this->slotRoleRegistry
->getRoleHandler( $row->role_name
);
1663 $row->model_name
= $slotRoleHandler->getDefaultModel( $title );
1667 if ( !isset( $row->content_id
) && isset( $row->rev_text_id
) ) {
1668 $row->slot_content_id
1669 = $this->emulateContentId( intval( $row->rev_text_id
) );
1672 $contentCallback = function ( SlotRecord
$slot ) use ( $queryFlags ) {
1673 return $this->loadSlotContent( $slot, null, null, null, $queryFlags );
1676 $slots[$row->role_name
] = new SlotRecord( $row, $contentCallback );
1679 if ( !isset( $slots[SlotRecord
::MAIN
] ) ) {
1680 throw new RevisionAccessException(
1681 'Main slot of revision ' . $revId . ' not found in database!'
1689 * Factory method for RevisionSlots based on a revision ID.
1691 * @note If other code has a need to construct RevisionSlots objects, this should be made
1692 * public, since RevisionSlots instances should not be constructed directly.
1695 * @param object $revisionRow
1696 * @param object[]|null $slotRows
1697 * @param int $queryFlags
1698 * @param Title $title
1700 * @return RevisionSlots
1701 * @throws MWException
1703 private function newRevisionSlots(
1711 $slots = new RevisionSlots(
1712 $this->constructSlotRecords( $revId, $slotRows, $queryFlags, $title )
1714 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
) ) {
1715 $mainSlot = $this->emulateMainSlot_1_29( $revisionRow, $queryFlags, $title );
1716 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize false positive
1717 $slots = new RevisionSlots( [ SlotRecord
::MAIN
=> $mainSlot ] );
1719 // XXX: do we need the same kind of caching here
1720 // that getKnownCurrentRevision uses (if $revId == page_latest?)
1722 $slots = new RevisionSlots( function () use( $revId, $queryFlags, $title ) {
1723 return $this->loadSlotRecords( $revId, $queryFlags, $title );
1731 * Make a fake revision object from an archive table row. This is queried
1732 * for permissions or even inserted (as in Special:Undelete)
1734 * MCR migration note: this replaces Revision::newFromArchiveRow
1736 * @param object $row
1737 * @param int $queryFlags
1738 * @param Title|null $title
1739 * @param array $overrides associative array with fields of $row to override. This may be
1740 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1741 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1742 * override ar_parent_id.
1744 * @return RevisionRecord
1745 * @throws MWException
1747 public function newRevisionFromArchiveRow(
1750 Title
$title = null,
1751 array $overrides = []
1753 Assert
::parameterType( 'object', $row, '$row' );
1755 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1756 Assert
::parameterType( 'integer', $queryFlags, '$queryFlags' );
1758 if ( !$title && isset( $overrides['title'] ) ) {
1759 if ( !( $overrides['title'] instanceof Title
) ) {
1760 throw new MWException( 'title field override must contain a Title object.' );
1763 $title = $overrides['title'];
1766 if ( !isset( $title ) ) {
1767 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
1768 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
1770 throw new InvalidArgumentException(
1771 'A Title or ar_namespace and ar_title must be given'
1776 foreach ( $overrides as $key => $value ) {
1778 $row->$field = $value;
1782 $user = User
::newFromAnyId(
1783 $row->ar_user ??
null,
1784 $row->ar_user_text ??
null,
1785 $row->ar_actor ??
null,
1788 } catch ( InvalidArgumentException
$ex ) {
1789 wfWarn( __METHOD__
. ': ' . $title->getPrefixedDBkey() . ': ' . $ex->getMessage() );
1790 $user = new UserIdentityValue( 0, 'Unknown user', 0 );
1793 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1794 // Legacy because $row may have come from self::selectFields()
1795 $comment = $this->commentStore
->getCommentLegacy( $db, 'ar_comment', $row, true );
1797 $slots = $this->newRevisionSlots( $row->ar_rev_id
, $row, null, $queryFlags, $title );
1799 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->dbDomain
);
1803 * @see RevisionFactory::newRevisionFromRow
1805 * MCR migration note: this replaces Revision::newFromRow
1807 * @param object $row A database row generated from a query based on getQueryInfo()
1808 * @param int $queryFlags
1809 * @param Title|null $title
1810 * @param bool $fromCache if true, the returned RevisionRecord will ensure that no stale
1811 * data is returned from getters, by querying the database as needed
1812 * @return RevisionRecord
1814 public function newRevisionFromRow(
1817 Title
$title = null,
1820 return $this->newRevisionFromRowAndSlots( $row, null, $queryFlags, $title, $fromCache );
1824 * @param object $row A database row generated from a query based on getQueryInfo()
1825 * @param null|object[] $slotRows Database rows generated from a query based on
1826 * getSlotsQueryInfo with the 'content' flag set.
1827 * @param int $queryFlags
1828 * @param Title|null $title
1829 * @param bool $fromCache if true, the returned RevisionRecord will ensure that no stale
1830 * data is returned from getters, by querying the database as needed
1832 * @return RevisionRecord
1833 * @throws MWException
1834 * @see RevisionFactory::newRevisionFromRow
1836 * MCR migration note: this replaces Revision::newFromRow
1839 public function newRevisionFromRowAndSlots(
1843 Title
$title = null,
1846 Assert
::parameterType( 'object', $row, '$row' );
1849 $pageId = $row->rev_page ??
0; // XXX: also check page_id?
1850 $revId = $row->rev_id ??
0;
1852 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1855 if ( !isset( $row->page_latest
) ) {
1856 $row->page_latest
= $title->getLatestRevID();
1857 if ( $row->page_latest
=== 0 && $title->exists() ) {
1858 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1863 $user = User
::newFromAnyId(
1864 $row->rev_user ??
null,
1865 $row->rev_user_text ??
null,
1866 $row->rev_actor ??
null,
1869 } catch ( InvalidArgumentException
$ex ) {
1870 wfWarn( __METHOD__
. ': ' . $title->getPrefixedDBkey() . ': ' . $ex->getMessage() );
1871 $user = new UserIdentityValue( 0, 'Unknown user', 0 );
1874 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1875 // Legacy because $row may have come from self::selectFields()
1876 $comment = $this->commentStore
->getCommentLegacy( $db, 'rev_comment', $row, true );
1878 $slots = $this->newRevisionSlots( $row->rev_id
, $row, $slotRows, $queryFlags, $title );
1880 // If this is a cached row, instantiate a cache-aware revision class to avoid stale data.
1882 $rev = new RevisionStoreCacheRecord(
1883 function ( $revId ) use ( $queryFlags ) {
1884 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1885 return $this->fetchRevisionRowFromConds(
1887 [ 'rev_id' => intval( $revId ) ]
1890 $title, $user, $comment, $row, $slots, $this->dbDomain
1893 $rev = new RevisionStoreRecord(
1894 $title, $user, $comment, $row, $slots, $this->dbDomain
);
1900 * Constructs a new MutableRevisionRecord based on the given associative array following
1901 * the MW1.29 convention for the Revision constructor.
1903 * MCR migration note: this replaces Revision::newFromRow
1905 * @param array $fields
1906 * @param int $queryFlags
1907 * @param Title|null $title
1909 * @return MutableRevisionRecord
1910 * @throws MWException
1911 * @throws RevisionAccessException
1913 public function newMutableRevisionFromArray(
1918 if ( !$title && isset( $fields['title'] ) ) {
1919 if ( !( $fields['title'] instanceof Title
) ) {
1920 throw new MWException( 'title field must contain a Title object.' );
1923 $title = $fields['title'];
1927 $pageId = $fields['page'] ??
0;
1928 $revId = $fields['id'] ??
0;
1930 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1933 if ( !isset( $fields['page'] ) ) {
1934 $fields['page'] = $title->getArticleID( $queryFlags );
1937 // if we have a content object, use it to set the model and type
1938 if ( !empty( $fields['content'] ) && !( $fields['content'] instanceof Content
)
1939 && !is_array( $fields['content'] )
1941 throw new MWException(
1942 'content field must contain a Content object or an array of Content objects.'
1946 if ( !empty( $fields['text_id'] ) ) {
1947 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
1948 throw new MWException( "The text_id field is only available in the pre-MCR schema" );
1951 if ( !empty( $fields['content'] ) ) {
1952 throw new MWException(
1953 "Text already stored in external store (id {$fields['text_id']}), " .
1954 "can't specify content object"
1960 isset( $fields['comment'] )
1961 && !( $fields['comment'] instanceof CommentStoreComment
)
1963 $commentData = $fields['comment_data'] ??
null;
1965 if ( $fields['comment'] instanceof Message
) {
1966 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1971 $commentText = trim( strval( $fields['comment'] ) );
1972 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1979 $revision = new MutableRevisionRecord( $title, $this->dbDomain
);
1980 $this->initializeMutableRevisionFromArray( $revision, $fields );
1982 if ( isset( $fields['content'] ) && is_array( $fields['content'] ) ) {
1983 // @phan-suppress-next-line PhanTypeNoPropertiesForeach
1984 foreach ( $fields['content'] as $role => $content ) {
1985 $revision->setContent( $role, $content );
1988 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1989 $revision->setSlot( $mainSlot );
1996 * @param MutableRevisionRecord $record
1997 * @param array $fields
1999 private function initializeMutableRevisionFromArray(
2000 MutableRevisionRecord
$record,
2003 /** @var UserIdentity $user */
2006 // If a user is passed in, use it if possible. We cannot use a user from a
2007 // remote wiki with unsuppressed ids, due to issues described in T222212.
2008 if ( isset( $fields['user'] ) &&
2009 ( $fields['user'] instanceof UserIdentity
) &&
2010 ( $this->dbDomain
=== false ||
2011 ( !$fields['user']->getId() && !$fields['user']->getActorId() ) )
2013 $user = $fields['user'];
2016 $user = User
::newFromAnyId(
2017 $fields['user'] ??
null,
2018 $fields['user_text'] ??
null,
2019 $fields['actor'] ??
null,
2022 } catch ( InvalidArgumentException
$ex ) {
2028 $record->setUser( $user );
2031 $timestamp = isset( $fields['timestamp'] )
2032 ?
strval( $fields['timestamp'] )
2033 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
2035 $record->setTimestamp( $timestamp );
2037 if ( isset( $fields['page'] ) ) {
2038 $record->setPageId( intval( $fields['page'] ) );
2041 if ( isset( $fields['id'] ) ) {
2042 $record->setId( intval( $fields['id'] ) );
2044 if ( isset( $fields['parent_id'] ) ) {
2045 $record->setParentId( intval( $fields['parent_id'] ) );
2048 if ( isset( $fields['sha1'] ) ) {
2049 $record->setSha1( $fields['sha1'] );
2051 if ( isset( $fields['size'] ) ) {
2052 $record->setSize( intval( $fields['size'] ) );
2055 if ( isset( $fields['minor_edit'] ) ) {
2056 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
2058 if ( isset( $fields['deleted'] ) ) {
2059 $record->setVisibility( intval( $fields['deleted'] ) );
2062 if ( isset( $fields['comment'] ) ) {
2063 Assert
::parameterType(
2064 CommentStoreComment
::class,
2068 $record->setComment( $fields['comment'] );
2073 * Load a page revision from a given revision ID number.
2074 * Returns null if no such revision can be found.
2076 * MCR migration note: this corresponds to Revision::loadFromId
2078 * @note direct use is deprecated!
2079 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
2081 * @param IDatabase $db
2084 * @return RevisionRecord|null
2086 public function loadRevisionFromId( IDatabase
$db, $id ) {
2087 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
2091 * Load either the current, or a specified, revision
2092 * that's attached to a given page. If not attached
2093 * to that page, will return null.
2095 * MCR migration note: this replaces Revision::loadFromPageId
2097 * @note direct use is deprecated!
2098 * @todo remove when unused!
2100 * @param IDatabase $db
2101 * @param int $pageid
2103 * @return RevisionRecord|null
2105 public function loadRevisionFromPageId( IDatabase
$db, $pageid, $id = 0 ) {
2106 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
2108 $conds['rev_id'] = intval( $id );
2110 $conds[] = 'rev_id=page_latest';
2112 return $this->loadRevisionFromConds( $db, $conds );
2116 * Load either the current, or a specified, revision
2117 * that's attached to a given page. If not attached
2118 * to that page, will return null.
2120 * MCR migration note: this replaces Revision::loadFromTitle
2122 * @note direct use is deprecated!
2123 * @todo remove when unused!
2125 * @param IDatabase $db
2126 * @param Title $title
2129 * @return RevisionRecord|null
2131 public function loadRevisionFromTitle( IDatabase
$db, $title, $id = 0 ) {
2133 $matchId = intval( $id );
2135 $matchId = 'page_latest';
2138 return $this->loadRevisionFromConds(
2142 'page_namespace' => $title->getNamespace(),
2143 'page_title' => $title->getDBkey()
2151 * Load the revision for the given title with the given timestamp.
2152 * WARNING: Timestamps may in some circumstances not be unique,
2153 * so this isn't the best key to use.
2155 * MCR migration note: this replaces Revision::loadFromTimestamp
2157 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
2158 * @todo remove when unused!
2160 * @param IDatabase $db
2161 * @param Title $title
2162 * @param string $timestamp
2163 * @return RevisionRecord|null
2165 public function loadRevisionFromTimestamp( IDatabase
$db, $title, $timestamp ) {
2166 return $this->loadRevisionFromConds( $db,
2168 'rev_timestamp' => $db->timestamp( $timestamp ),
2169 'page_namespace' => $title->getNamespace(),
2170 'page_title' => $title->getDBkey()
2178 * Given a set of conditions, fetch a revision
2180 * This method should be used if we are pretty sure the revision exists.
2181 * Unless $flags has READ_LATEST set, this method will first try to find the revision
2182 * on a replica before hitting the master database.
2184 * MCR migration note: this corresponds to Revision::newFromConds
2186 * @param array $conditions
2187 * @param int $flags (optional)
2188 * @param Title|null $title
2190 * @return RevisionRecord|null
2192 private function newRevisionFromConds( $conditions, $flags = 0, Title
$title = null ) {
2193 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2194 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
2196 $lb = $this->getDBLoadBalancer();
2198 // Make sure new pending/committed revision are visibile later on
2199 // within web requests to certain avoid bugs like T93866 and T94407.
2201 && !( $flags & self
::READ_LATEST
)
2202 && $lb->hasStreamingReplicaServers()
2203 && $lb->hasOrMadeRecentMasterChanges()
2205 $flags = self
::READ_LATEST
;
2206 $dbw = $this->getDBConnection( DB_MASTER
);
2207 $rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $title );
2208 $this->releaseDBConnection( $dbw );
2215 * Given a set of conditions, fetch a revision from
2216 * the given database connection.
2218 * MCR migration note: this corresponds to Revision::loadFromConds
2220 * @param IDatabase $db
2221 * @param array $conditions
2222 * @param int $flags (optional)
2223 * @param Title|null $title
2225 * @return RevisionRecord|null
2227 private function loadRevisionFromConds(
2233 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
2235 $rev = $this->newRevisionFromRow( $row, $flags, $title );
2244 * Throws an exception if the given database connection does not belong to the wiki this
2245 * RevisionStore is bound to.
2247 * @param IDatabase $db
2248 * @throws MWException
2250 private function checkDatabaseWikiId( IDatabase
$db ) {
2251 $storeWiki = $this->dbDomain
;
2252 $dbWiki = $db->getDomainID();
2254 if ( $dbWiki === $storeWiki ) {
2258 $storeWiki = $storeWiki ?
: $this->loadBalancer
->getLocalDomainID();
2259 // @FIXME: when would getDomainID() be false here?
2260 $dbWiki = $dbWiki ?
: wfWikiID();
2262 if ( $dbWiki === $storeWiki ) {
2266 // HACK: counteract encoding imposed by DatabaseDomain
2267 $storeWiki = str_replace( '?h', '-', $storeWiki );
2268 $dbWiki = str_replace( '?h', '-', $dbWiki );
2270 if ( $dbWiki === $storeWiki ) {
2274 throw new MWException( "RevisionStore for $storeWiki "
2275 . "cannot be used with a DB connection for $dbWiki" );
2279 * Given a set of conditions, return a row with the
2280 * fields necessary to build RevisionRecord objects.
2282 * MCR migration note: this corresponds to Revision::fetchFromConds
2284 * @param IDatabase $db
2285 * @param array $conditions
2286 * @param int $flags (optional)
2288 * @return object|false data row as a raw object
2290 private function fetchRevisionRowFromConds( IDatabase
$db, $conditions, $flags = 0 ) {
2291 $this->checkDatabaseWikiId( $db );
2293 $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
2295 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
2296 $options[] = 'FOR UPDATE';
2298 return $db->selectRow(
2299 $revQuery['tables'],
2300 $revQuery['fields'],
2309 * Finds the ID of a content row for a given revision and slot role.
2310 * This can be used to re-use content rows even while the content ID
2311 * is still missing from SlotRecords, when writing to both the old and
2312 * the new schema during MCR schema migration.
2314 * @todo remove after MCR schema migration is complete.
2316 * @param IDatabase $db
2318 * @param string $role
2322 private function findSlotContentId( IDatabase
$db, $revId, $role ) {
2323 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
2328 $roleId = $this->slotRoleStore
->getId( $role );
2330 'slot_revision_id' => $revId,
2331 'slot_role_id' => $roleId,
2334 $contentId = $db->selectField( 'slots', 'slot_content_id', $conditions, __METHOD__
);
2336 return $contentId ?
: null;
2337 } catch ( NameTableAccessException
$ex ) {
2338 // If the role is missing from the slot_roles table,
2339 // the corresponding row in slots cannot exist.
2345 * Return the tables, fields, and join conditions to be selected to create
2346 * a new RevisionStoreRecord object.
2348 * MCR migration note: this replaces Revision::getQueryInfo
2350 * If the format of fields returned changes in any way then the cache key provided by
2351 * self::getRevisionRowCacheKey should be updated.
2355 * @param array $options Any combination of the following strings
2356 * - 'page': Join with the page table, and select fields to identify the page
2357 * - 'user': Join with the user table, and select the user name
2358 * - 'text': Join with the text table, and select fields to load page text. This
2359 * option is deprecated in MW 1.32 when the MCR migration flag SCHEMA_COMPAT_WRITE_NEW
2360 * is set, and disallowed when SCHEMA_COMPAT_READ_OLD is not set.
2362 * @return array With three keys:
2363 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2364 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2365 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2367 public function getQueryInfo( $options = [] ) {
2374 $ret['tables'][] = 'revision';
2375 $ret['fields'] = array_merge( $ret['fields'], [
2386 $commentQuery = $this->commentStore
->getJoin( 'rev_comment' );
2387 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
2388 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
2389 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
2391 $actorQuery = $this->actorMigration
->getJoin( 'rev_user' );
2392 $ret['tables'] = array_merge( $ret['tables'], $actorQuery['tables'] );
2393 $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
2394 $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
2396 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2397 $ret['fields'][] = 'rev_text_id';
2399 if ( $this->contentHandlerUseDB
) {
2400 $ret['fields'][] = 'rev_content_format';
2401 $ret['fields'][] = 'rev_content_model';
2405 if ( in_array( 'page', $options, true ) ) {
2406 $ret['tables'][] = 'page';
2407 $ret['fields'] = array_merge( $ret['fields'], [
2415 $ret['joins']['page'] = [ 'JOIN', [ 'page_id = rev_page' ] ];
2418 if ( in_array( 'user', $options, true ) ) {
2419 $ret['tables'][] = 'user';
2420 $ret['fields'] = array_merge( $ret['fields'], [
2423 $u = $actorQuery['fields']['rev_user'];
2424 $ret['joins']['user'] = [ 'LEFT JOIN', [ "$u != 0", "user_id = $u" ] ];
2427 if ( in_array( 'text', $options, true ) ) {
2428 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) ) {
2429 throw new InvalidArgumentException( 'text table can no longer be joined directly' );
2430 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2431 // NOTE: even when this class is set to not read from the old schema, callers
2432 // should still be able to join against the text table, as long as we are still
2433 // writing the old schema for compatibility.
2434 // TODO: This should trigger a deprecation warning eventually (T200918), but not
2435 // before all known usages are removed (see T198341 and T201164).
2436 // wfDeprecated( __METHOD__ . ' with `text` option', '1.32' );
2439 $ret['tables'][] = 'text';
2440 $ret['fields'] = array_merge( $ret['fields'], [
2444 $ret['joins']['text'] = [ 'JOIN', [ 'rev_text_id=old_id' ] ];
2451 * Return the tables, fields, and join conditions to be selected to create
2456 * @param array $options Any combination of the following strings
2457 * - 'content': Join with the content table, and select content meta-data fields
2458 * - 'model': Join with the content_models table, and select the model_name field.
2459 * Only applicable if 'content' is also set.
2460 * - 'role': Join with the slot_roles table, and select the role_name field
2462 * @return array With three keys:
2463 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2464 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2465 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2467 public function getSlotsQueryInfo( $options = [] ) {
2474 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2475 $db = $this->getDBConnectionRef( DB_REPLICA
);
2476 $ret['tables'][] = 'revision';
2478 $ret['fields']['slot_revision_id'] = 'rev_id';
2479 $ret['fields']['slot_content_id'] = 'NULL';
2480 $ret['fields']['slot_origin'] = 'rev_id';
2481 $ret['fields']['role_name'] = $db->addQuotes( SlotRecord
::MAIN
);
2483 if ( in_array( 'content', $options, true ) ) {
2484 $ret['fields']['content_size'] = 'rev_len';
2485 $ret['fields']['content_sha1'] = 'rev_sha1';
2486 $ret['fields']['content_address']
2487 = $db->buildConcat( [ $db->addQuotes( 'tt:' ), 'rev_text_id' ] );
2489 // Allow the content_id field to be emulated later
2490 $ret['fields']['rev_text_id'] = 'rev_text_id';
2492 if ( $this->contentHandlerUseDB
) {
2493 $ret['fields']['model_name'] = 'rev_content_model';
2495 $ret['fields']['model_name'] = 'NULL';
2499 $ret['tables'][] = 'slots';
2500 $ret['fields'] = array_merge( $ret['fields'], [
2507 if ( in_array( 'role', $options, true ) ) {
2508 // Use left join to attach role name, so we still find the revision row even
2509 // if the role name is missing. This triggers a more obvious failure mode.
2510 $ret['tables'][] = 'slot_roles';
2511 $ret['joins']['slot_roles'] = [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ];
2512 $ret['fields'][] = 'role_name';
2515 if ( in_array( 'content', $options, true ) ) {
2516 $ret['tables'][] = 'content';
2517 $ret['fields'] = array_merge( $ret['fields'], [
2523 $ret['joins']['content'] = [ 'JOIN', [ 'slot_content_id = content_id' ] ];
2525 if ( in_array( 'model', $options, true ) ) {
2526 // Use left join to attach model name, so we still find the revision row even
2527 // if the model name is missing. This triggers a more obvious failure mode.
2528 $ret['tables'][] = 'content_models';
2529 $ret['joins']['content_models'] = [ 'LEFT JOIN', [ 'content_model = model_id' ] ];
2530 $ret['fields'][] = 'model_name';
2540 * Return the tables, fields, and join conditions to be selected to create
2541 * a new RevisionArchiveRecord object.
2543 * MCR migration note: this replaces Revision::getArchiveQueryInfo
2547 * @return array With three keys:
2548 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2549 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2550 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2552 public function getArchiveQueryInfo() {
2553 $commentQuery = $this->commentStore
->getJoin( 'ar_comment' );
2554 $actorQuery = $this->actorMigration
->getJoin( 'ar_user' );
2556 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
2569 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
2570 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
2573 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2574 $ret['fields'][] = 'ar_text_id';
2576 if ( $this->contentHandlerUseDB
) {
2577 $ret['fields'][] = 'ar_content_format';
2578 $ret['fields'][] = 'ar_content_model';
2586 * Do a batched query for the sizes of a set of revisions.
2588 * MCR migration note: this replaces Revision::getParentLengths
2590 * @param int[] $revIds
2591 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2592 * of the corresponding revision.
2594 public function getRevisionSizes( array $revIds ) {
2595 return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA
), $revIds );
2599 * Do a batched query for the sizes of a set of revisions.
2601 * MCR migration note: this replaces Revision::getParentLengths
2603 * @deprecated use RevisionStore::getRevisionSizes instead.
2605 * @param IDatabase $db
2606 * @param int[] $revIds
2607 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2608 * of the corresponding revision.
2610 public function listRevisionSizes( IDatabase
$db, array $revIds ) {
2611 $this->checkDatabaseWikiId( $db );
2615 return $revLens; // empty
2620 [ 'rev_id', 'rev_len' ],
2621 [ 'rev_id' => $revIds ],
2625 foreach ( $res as $row ) {
2626 $revLens[$row->rev_id
] = intval( $row->rev_len
);
2633 * Implementation of getPreviousRevision and getNextRevision.
2635 * @param RevisionRecord $rev
2637 * @param string $dir 'next' or 'prev'
2638 * @return RevisionRecord|null
2640 private function getRelativeRevision( RevisionRecord
$rev, $flags, $dir ) {
2641 $op = $dir === 'next' ?
'>' : '<';
2642 $sort = $dir === 'next' ?
'ASC' : 'DESC';
2644 if ( !$rev->getId() ||
!$rev->getPageId() ) {
2645 // revision is unsaved or otherwise incomplete
2649 if ( $rev instanceof RevisionArchiveRecord
) {
2650 // revision is deleted, so it's not part of the page history
2654 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
2655 $db = $this->getDBConnection( $dbType, [ 'contributions' ] );
2657 $ts = $this->getTimestampFromId( $rev->getId(), $flags );
2658 if ( $ts === false ) {
2659 // XXX Should this be moved into getTimestampFromId?
2660 $ts = $db->selectField( 'archive', 'ar_timestamp',
2661 [ 'ar_rev_id' => $rev->getId() ], __METHOD__
);
2662 if ( $ts === false ) {
2663 // XXX Is this reachable? How can we have a page id but no timestamp?
2667 $ts = $db->addQuotes( $db->timestamp( $ts ) );
2669 $revId = $db->selectField( 'revision', 'rev_id',
2671 'rev_page' => $rev->getPageId(),
2672 "rev_timestamp $op $ts OR (rev_timestamp = $ts AND rev_id $op {$rev->getId()})"
2676 'ORDER BY' => "rev_timestamp $sort, rev_id $sort",
2677 'IGNORE INDEX' => 'rev_timestamp', // Probably needed for T159319
2681 if ( $revId === false ) {
2685 return $this->getRevisionById( intval( $revId ) );
2689 * Get the revision before $rev in the page's history, if any.
2690 * Will return null for the first revision but also for deleted or unsaved revisions.
2692 * MCR migration note: this replaces Revision::getPrevious
2694 * @see Title::getPreviousRevisionID
2695 * @see PageArchive::getPreviousRevision
2697 * @param RevisionRecord $rev
2698 * @param int $flags (optional) $flags include:
2699 * IDBAccessObject::READ_LATEST: Select the data from the master
2701 * @return RevisionRecord|null
2703 public function getPreviousRevision( RevisionRecord
$rev, $flags = 0 ) {
2704 if ( $flags instanceof Title
) {
2705 // Old calling convention, we don't use Title here anymore
2706 wfDeprecated( __METHOD__
. ' with Title', '1.34' );
2710 return $this->getRelativeRevision( $rev, $flags, 'prev' );
2714 * Get the revision after $rev in the page's history, if any.
2715 * Will return null for the latest revision but also for deleted or unsaved revisions.
2717 * MCR migration note: this replaces Revision::getNext
2719 * @see Title::getNextRevisionID
2721 * @param RevisionRecord $rev
2722 * @param int $flags (optional) $flags include:
2723 * IDBAccessObject::READ_LATEST: Select the data from the master
2724 * @return RevisionRecord|null
2726 public function getNextRevision( RevisionRecord
$rev, $flags = 0 ) {
2727 if ( $flags instanceof Title
) {
2728 // Old calling convention, we don't use Title here anymore
2729 wfDeprecated( __METHOD__
. ' with Title', '1.34' );
2733 return $this->getRelativeRevision( $rev, $flags, 'next' );
2737 * Get previous revision Id for this page_id
2738 * This is used to populate rev_parent_id on save
2740 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
2742 * @param IDatabase $db
2743 * @param RevisionRecord $rev
2747 private function getPreviousRevisionId( IDatabase
$db, RevisionRecord
$rev ) {
2748 $this->checkDatabaseWikiId( $db );
2750 if ( $rev->getPageId() === null ) {
2753 # Use page_latest if ID is not given
2754 if ( !$rev->getId() ) {
2755 $prevId = $db->selectField(
2756 'page', 'page_latest',
2757 [ 'page_id' => $rev->getPageId() ],
2761 $prevId = $db->selectField(
2762 'revision', 'rev_id',
2763 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
2765 [ 'ORDER BY' => 'rev_id DESC' ]
2768 return intval( $prevId );
2772 * Get rev_timestamp from rev_id, without loading the rest of the row.
2774 * Historically, there was an extra Title parameter that was passed before $id. This is no
2775 * longer needed and is deprecated in 1.34.
2777 * MCR migration note: this replaces Revision::getTimestampFromId
2781 * @return string|bool False if not found
2783 public function getTimestampFromId( $id, $flags = 0 ) {
2784 if ( $id instanceof Title
) {
2785 // Old deprecated calling convention supported for backwards compatibility
2787 $flags = func_num_args() > 2 ?
func_get_arg( 2 ) : 0;
2789 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2792 $db->selectField( 'revision', 'rev_timestamp', [ 'rev_id' => $id ], __METHOD__
);
2794 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
2798 * Get count of revisions per page...not very efficient
2800 * MCR migration note: this replaces Revision::countByPageId
2802 * @param IDatabase $db
2803 * @param int $id Page id
2806 public function countRevisionsByPageId( IDatabase
$db, $id ) {
2807 $this->checkDatabaseWikiId( $db );
2809 $row = $db->selectRow( 'revision',
2810 [ 'revCount' => 'COUNT(*)' ],
2811 [ 'rev_page' => $id ],
2815 return intval( $row->revCount
);
2821 * Get count of revisions per page...not very efficient
2823 * MCR migration note: this replaces Revision::countByTitle
2825 * @param IDatabase $db
2826 * @param Title $title
2829 public function countRevisionsByTitle( IDatabase
$db, $title ) {
2830 $id = $title->getArticleID();
2832 return $this->countRevisionsByPageId( $db, $id );
2838 * Check if no edits were made by other users since
2839 * the time a user started editing the page. Limit to
2840 * 50 revisions for the sake of performance.
2842 * MCR migration note: this replaces Revision::userWasLastToEdit
2844 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
2845 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
2846 * has been deprecated since 1.24.
2848 * @param IDatabase $db The Database to perform the check on.
2849 * @param int $pageId The ID of the page in question
2850 * @param int $userId The ID of the user in question
2851 * @param string $since Look at edits since this time
2853 * @return bool True if the given user was the only one to edit since the given timestamp
2855 public function userWasLastToEdit( IDatabase
$db, $pageId, $userId, $since ) {
2856 $this->checkDatabaseWikiId( $db );
2862 $revQuery = $this->getQueryInfo();
2864 $revQuery['tables'],
2866 'rev_user' => $revQuery['fields']['rev_user'],
2869 'rev_page' => $pageId,
2870 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
2873 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
2876 foreach ( $res as $row ) {
2877 if ( $row->rev_user
!= $userId ) {
2885 * Load a revision based on a known page ID and current revision ID from the DB
2887 * This method allows for the use of caching, though accessing anything that normally
2888 * requires permission checks (aside from the text) will trigger a small DB lookup.
2890 * MCR migration note: this replaces Revision::newKnownCurrent
2892 * @param Title $title the associated page title
2893 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
2895 * @return RevisionRecord|bool Returns false if missing
2897 public function getKnownCurrentRevision( Title
$title, $revId ) {
2898 $db = $this->getDBConnectionRef( DB_REPLICA
);
2900 $pageId = $title->getArticleID();
2907 $revId = $title->getLatestRevID();
2912 'No latest revision known for page ' . $title->getPrefixedDBkey()
2913 . ' even though it exists with page ID ' . $pageId
2918 // Load the row from cache if possible. If not possible, populate the cache.
2919 // As a minor optimization, remember if this was a cache hit or miss.
2920 // We can sometimes avoid a database query later if this is a cache miss.
2922 $row = $this->cache
->getWithSetCallback(
2923 // Page/rev IDs passed in from DB to reflect history merges
2924 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2925 WANObjectCache
::TTL_WEEK
,
2926 function ( $curValue, &$ttl, array &$setOpts ) use (
2927 $db, $pageId, $revId, &$fromCache
2929 $setOpts +
= Database
::getCacheSetOptions( $db );
2930 $row = $this->fetchRevisionRowFromConds( $db, [ 'rev_id' => intval( $revId ) ] );
2934 return $row; // don't cache negatives
2938 // Reflect revision deletion and user renames.
2940 return $this->newRevisionFromRow( $row, 0, $title, $fromCache );
2947 * Get a cache key for use with a row as selected with getQueryInfo( [ 'page', 'user' ] )
2948 * Caching rows without 'page' or 'user' could lead to issues.
2949 * If the format of the rows returned by the query provided by getQueryInfo changes the
2950 * cache key should be updated to avoid conflicts.
2952 * @param IDatabase $db
2953 * @param int $pageId
2957 private function getRevisionRowCacheKey( IDatabase
$db, $pageId, $revId ) {
2958 return $this->cache
->makeGlobalKey(
2959 self
::ROW_CACHE_KEY
,
2966 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.
2971 * Retain the old class name for backwards compatibility.
2972 * @deprecated since 1.32
2974 class_alias( RevisionStore
::class, 'MediaWiki\Storage\RevisionStore' );