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\ResultWrapper
;
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 $wikiId 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 $wikiId
159 public function __construct(
160 ILoadBalancer
$loadBalancer,
161 SqlBlobStore
$blobStore,
162 WANObjectCache
$cache,
163 CommentStore
$commentStore,
164 NameTableStore
$contentModelStore,
165 NameTableStore
$slotRoleStore,
166 SlotRoleRegistry
$slotRoleRegistry,
168 ActorMigration
$actorMigration,
171 Assert
::parameterType( 'string|boolean', $wikiId, '$wikiId' );
172 Assert
::parameterType( 'integer', $mcrMigrationStage, '$mcrMigrationStage' );
174 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH
) !== SCHEMA_COMPAT_READ_BOTH
,
175 '$mcrMigrationStage',
176 'Reading from the old and the new schema at the same time is not supported.'
179 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH
) !== 0,
180 '$mcrMigrationStage',
181 'Reading needs to be enabled for the old or the new schema.'
184 ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_BOTH
) !== 0,
185 '$mcrMigrationStage',
186 'Writing needs to be enabled for the old or the new schema.'
189 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_OLD
) === 0
190 ||
( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) !== 0,
191 '$mcrMigrationStage',
192 'Cannot read the old schema when not also writing it.'
195 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_NEW
) === 0
196 ||
( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) !== 0,
197 '$mcrMigrationStage',
198 'Cannot read the new schema when not also writing it.'
201 $this->loadBalancer
= $loadBalancer;
202 $this->blobStore
= $blobStore;
203 $this->cache
= $cache;
204 $this->commentStore
= $commentStore;
205 $this->contentModelStore
= $contentModelStore;
206 $this->slotRoleStore
= $slotRoleStore;
207 $this->slotRoleRegistry
= $slotRoleRegistry;
208 $this->mcrMigrationStage
= $mcrMigrationStage;
209 $this->actorMigration
= $actorMigration;
210 $this->wikiId
= $wikiId;
211 $this->logger
= new NullLogger();
215 * @param int $flags A combination of SCHEMA_COMPAT_XXX flags.
216 * @return bool True if all the given flags were set in the $mcrMigrationStage
217 * parameter passed to the constructor.
219 private function hasMcrSchemaFlags( $flags ) {
220 return ( $this->mcrMigrationStage
& $flags ) === $flags;
224 * Throws a RevisionAccessException if this RevisionStore is configured for cross-wiki loading
225 * and still reading from the old DB schema.
227 * @throws RevisionAccessException
229 private function assertCrossWikiContentLoadingIsSafe() {
230 if ( $this->wikiId
!== false && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
231 throw new RevisionAccessException(
232 "Cross-wiki content loading is not supported by the pre-MCR schema"
237 public function setLogger( LoggerInterface
$logger ) {
238 $this->logger
= $logger;
242 * @return bool Whether the store is read-only
244 public function isReadOnly() {
245 return $this->blobStore
->isReadOnly();
251 public function getContentHandlerUseDB() {
252 return $this->contentHandlerUseDB
;
256 * @see $wgContentHandlerUseDB
257 * @param bool $contentHandlerUseDB
258 * @throws MWException
260 public function setContentHandlerUseDB( $contentHandlerUseDB ) {
261 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
)
262 ||
$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
)
264 if ( !$contentHandlerUseDB ) {
265 throw new MWException(
266 'Content model must be stored in the database for multi content revision migration.'
270 $this->contentHandlerUseDB
= $contentHandlerUseDB;
274 * @return ILoadBalancer
276 private function getDBLoadBalancer() {
277 return $this->loadBalancer
;
281 * @param int $mode DB_MASTER or DB_REPLICA
282 * @param array $groups
286 private function getDBConnection( $mode, $groups = [] ) {
287 $lb = $this->getDBLoadBalancer();
288 return $lb->getConnection( $mode, $groups, $this->wikiId
);
292 * @param int $queryFlags a bit field composed of READ_XXX flags
296 private function getDBConnectionRefForQueryFlags( $queryFlags ) {
297 list( $mode, ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
298 return $this->getDBConnectionRef( $mode );
302 * @param IDatabase $connection
304 private function releaseDBConnection( IDatabase
$connection ) {
305 $lb = $this->getDBLoadBalancer();
306 $lb->reuseConnection( $connection );
310 * @param int $mode DB_MASTER or DB_REPLICA
314 private function getDBConnectionRef( $mode ) {
315 $lb = $this->getDBLoadBalancer();
316 return $lb->getConnectionRef( $mode, [], $this->wikiId
);
320 * Determines the page Title based on the available information.
322 * MCR migration note: this corresponds to Revision::getTitle
324 * @note this method should be private, external use should be avoided!
326 * @param int|null $pageId
327 * @param int|null $revId
328 * @param int $queryFlags
331 * @throws RevisionAccessException
333 public function getTitle( $pageId, $revId, $queryFlags = self
::READ_NORMAL
) {
334 if ( !$pageId && !$revId ) {
335 throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
338 // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
339 // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
340 if ( DBAccessObjectUtils
::hasFlags( $queryFlags, self
::READ_LATEST_IMMUTABLE
) ) {
341 $queryFlags = self
::READ_NORMAL
;
344 $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->wikiId
=== false );
345 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
346 $titleFlags = ( $dbMode == DB_MASTER ? Title
::GAID_FOR_UPDATE
: 0 );
348 // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
349 if ( $canUseTitleNewFromId ) {
350 // TODO: better foreign title handling (introduce TitleFactory)
351 $title = Title
::newFromID( $pageId, $titleFlags );
357 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
358 $canUseRevId = ( $revId !== null && $revId > 0 );
360 if ( $canUseRevId ) {
361 $dbr = $this->getDBConnectionRef( $dbMode );
362 // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
363 $row = $dbr->selectRow(
364 [ 'revision', 'page' ],
373 [ 'rev_id' => $revId ],
376 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
379 // TODO: better foreign title handling (introduce TitleFactory)
380 return Title
::newFromRow( $row );
384 // If we still don't have a title, fallback to master if that wasn't already happening.
385 if ( $dbMode !== DB_MASTER
) {
386 $title = $this->getTitle( $pageId, $revId, self
::READ_LATEST
);
389 __METHOD__
. ' fell back to READ_LATEST and got a Title.',
390 [ 'trace' => wfBacktrace() ]
396 throw new RevisionAccessException(
397 "Could not determine title for page ID $pageId and revision ID $revId"
402 * @param mixed $value
403 * @param string $name
405 * @throws IncompleteRevisionException if $value is null
406 * @return mixed $value, if $value is not null
408 private function failOnNull( $value, $name ) {
409 if ( $value === null ) {
410 throw new IncompleteRevisionException(
411 "$name must not be " . var_export( $value, true ) . "!"
419 * @param mixed $value
420 * @param string $name
422 * @throws IncompleteRevisionException if $value is empty
423 * @return mixed $value, if $value is not null
425 private function failOnEmpty( $value, $name ) {
426 if ( $value === null ||
$value === 0 ||
$value === '' ) {
427 throw new IncompleteRevisionException(
428 "$name must not be " . var_export( $value, true ) . "!"
436 * Insert a new revision into the database, returning the new revision record
437 * on success and dies horribly on failure.
439 * MCR migration note: this replaces Revision::insertOn
441 * @param RevisionRecord $rev
442 * @param IDatabase $dbw (master connection)
444 * @throws InvalidArgumentException
445 * @return RevisionRecord the new revision record.
447 public function insertRevisionOn( RevisionRecord
$rev, IDatabase
$dbw ) {
448 // TODO: pass in a DBTransactionContext instead of a database connection.
449 $this->checkDatabaseWikiId( $dbw );
451 $slotRoles = $rev->getSlotRoles();
453 // Make sure the main slot is always provided throughout migration
454 if ( !in_array( SlotRecord
::MAIN
, $slotRoles ) ) {
455 throw new InvalidArgumentException(
456 'main slot must be provided'
460 // If we are not writing into the new schema, we can't support extra slots.
461 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
)
462 && $slotRoles !== [ SlotRecord
::MAIN
]
464 throw new InvalidArgumentException(
465 'Only the main slot is supported when not writing to the MCR enabled schema!'
469 // As long as we are not reading from the new schema, we don't want to write extra slots.
470 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
)
471 && $slotRoles !== [ SlotRecord
::MAIN
]
473 throw new InvalidArgumentException(
474 'Only the main slot is supported when not reading from the MCR enabled schema!'
479 $this->failOnNull( $rev->getSize(), 'size field' );
480 $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
481 $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
482 $comment = $this->failOnNull( $rev->getComment( RevisionRecord
::RAW
), 'comment' );
483 $user = $this->failOnNull( $rev->getUser( RevisionRecord
::RAW
), 'user' );
484 $this->failOnNull( $user->getId(), 'user field' );
485 $this->failOnEmpty( $user->getName(), 'user_text field' );
487 if ( !$rev->isReadyForInsertion() ) {
488 // This is here for future-proofing. At the time this check being added, it
489 // was redundant to the individual checks above.
490 throw new IncompleteRevisionException( 'Revision is incomplete' );
493 // TODO: we shouldn't need an actual Title here.
494 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
495 $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
497 $parentId = $rev->getParentId() === null
498 ?
$this->getPreviousRevisionId( $dbw, $rev )
499 : $rev->getParentId();
501 /** @var RevisionRecord $rev */
502 $rev = $dbw->doAtomicSection(
504 function ( IDatabase
$dbw, $fname ) use (
512 return $this->insertRevisionInternal(
525 Assert
::postcondition( $rev->getId() > 0, 'revision must have an ID' );
526 Assert
::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
527 Assert
::postcondition(
528 $rev->getComment( RevisionRecord
::RAW
) !== null,
529 'revision must have a comment'
531 Assert
::postcondition(
532 $rev->getUser( RevisionRecord
::RAW
) !== null,
533 'revision must have a user'
536 // Trigger exception if the main slot is missing.
537 // Technically, this could go away after MCR migration: while
538 // calling code may require a main slot to exist, RevisionStore
539 // really should not know or care about that requirement.
540 $rev->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
542 foreach ( $slotRoles as $role ) {
543 $slot = $rev->getSlot( $role, RevisionRecord
::RAW
);
544 Assert
::postcondition(
545 $slot->getContent() !== null,
546 $role . ' slot must have content'
548 Assert
::postcondition(
549 $slot->hasRevision(),
550 $role . ' slot must have a revision associated'
554 Hooks
::run( 'RevisionRecordInserted', [ $rev ] );
556 // TODO: deprecate in 1.32!
557 $legacyRevision = new Revision( $rev );
558 Hooks
::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
563 private function insertRevisionInternal(
567 CommentStoreComment
$comment,
572 $slotRoles = $rev->getSlotRoles();
574 $revisionRow = $this->insertRevisionRowOn(
581 $revisionId = $revisionRow['rev_id'];
584 BlobStore
::PAGE_HINT
=> $pageId,
585 BlobStore
::REVISION_HINT
=> $revisionId,
586 BlobStore
::PARENT_HINT
=> $parentId,
590 foreach ( $slotRoles as $role ) {
591 $slot = $rev->getSlot( $role, RevisionRecord
::RAW
);
593 // If the SlotRecord already has a revision ID set, this means it already exists
594 // in the database, and should already belong to the current revision.
595 // However, a slot may already have a revision, but no content ID, if the slot
596 // is emulated based on the archive table, because we are in SCHEMA_COMPAT_READ_OLD
597 // mode, and the respective archive row was not yet migrated to the new schema.
598 // In that case, a new slot row (and content row) must be inserted even during
600 if ( $slot->hasRevision() && $slot->hasContentId() ) {
601 // TODO: properly abort transaction if the assertion fails!
603 $slot->getRevision() === $revisionId,
604 'slot role ' . $slot->getRole(),
605 'Existing slot should belong to revision '
606 . $revisionId . ', but belongs to revision ' . $slot->getRevision() . '!'
609 // Slot exists, nothing to do, move along.
610 // This happens when restoring archived revisions.
612 $newSlots[$role] = $slot;
614 // Write the main slot's text ID to the revision table for backwards compatibility
615 if ( $slot->getRole() === SlotRecord
::MAIN
616 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
)
618 $blobAddress = $slot->getAddress();
619 $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
622 $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $title, $blobHints );
626 $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
628 $rev = new RevisionStoreRecord(
632 (object)$revisionRow,
633 new RevisionSlots( $newSlots ),
641 * @param IDatabase $dbw
642 * @param int $revisionId
643 * @param string &$blobAddress (may change!)
645 * @return int the text row id
647 private function updateRevisionTextId( IDatabase
$dbw, $revisionId, &$blobAddress ) {
648 $textId = $this->blobStore
->getTextIdFromAddress( $blobAddress );
650 throw new LogicException(
651 'Blob address not supported in 1.29 database schema: ' . $blobAddress
655 // getTextIdFromAddress() is free to insert something into the text table, so $textId
656 // may be a new value, not anything already contained in $blobAddress.
657 $blobAddress = SqlBlobStore
::makeAddressFromTextId( $textId );
661 [ 'rev_text_id' => $textId ],
662 [ 'rev_id' => $revisionId ],
670 * @param IDatabase $dbw
671 * @param int $revisionId
672 * @param SlotRecord $protoSlot
673 * @param Title $title
674 * @param array $blobHints See the BlobStore::XXX_HINT constants
677 private function insertSlotOn(
680 SlotRecord
$protoSlot,
682 array $blobHints = []
684 if ( $protoSlot->hasAddress() ) {
685 $blobAddress = $protoSlot->getAddress();
687 $blobAddress = $this->storeContentBlob( $protoSlot, $title, $blobHints );
692 // Write the main slot's text ID to the revision table for backwards compatibility
693 if ( $protoSlot->getRole() === SlotRecord
::MAIN
694 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
)
696 // If SCHEMA_COMPAT_WRITE_NEW is also set, the fake content ID is overwritten
697 // with the real content ID below.
698 $textId = $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
699 $contentId = $this->emulateContentId( $textId );
702 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
703 if ( $protoSlot->hasContentId() ) {
704 $contentId = $protoSlot->getContentId();
706 $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
709 $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
712 $savedSlot = SlotRecord
::newSaved(
723 * Insert IP revision into ip_changes for use when querying for a range.
724 * @param IDatabase $dbw
726 * @param RevisionRecord $rev
727 * @param int $revisionId
729 private function insertIpChangesRow(
735 if ( $user->getId() === 0 && IP
::isValid( $user->getName() ) ) {
737 'ipc_rev_id' => $revisionId,
738 'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
739 'ipc_hex' => IP
::toHex( $user->getName() ),
741 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__
);
746 * @param IDatabase $dbw
747 * @param RevisionRecord $rev
748 * @param Title $title
749 * @param int $parentId
751 * @return array a revision table row
753 * @throws MWException
754 * @throws MWUnknownContentModelException
756 private function insertRevisionRowOn(
762 $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $title, $parentId );
764 list( $commentFields, $commentCallback ) =
765 $this->commentStore
->insertWithTempTable(
768 $rev->getComment( RevisionRecord
::RAW
)
770 $revisionRow +
= $commentFields;
772 list( $actorFields, $actorCallback ) =
773 $this->actorMigration
->getInsertValuesWithTempTable(
776 $rev->getUser( RevisionRecord
::RAW
)
778 $revisionRow +
= $actorFields;
780 $dbw->insert( 'revision', $revisionRow, __METHOD__
);
782 if ( !isset( $revisionRow['rev_id'] ) ) {
783 // only if auto-increment was used
784 $revisionRow['rev_id'] = intval( $dbw->insertId() );
786 if ( $dbw->getType() === 'mysql' ) {
787 // (T202032) MySQL until 8.0 and MariaDB until some version after 10.1.34 don't save the
788 // auto-increment value to disk, so on server restart it might reuse IDs from deleted
789 // revisions. We can fix that with an insert with an explicit rev_id value, if necessary.
791 $maxRevId = intval( $dbw->selectField( 'archive', 'MAX(ar_rev_id)', '', __METHOD__
) );
793 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
794 $maxRevId2 = intval( $dbw->selectField( 'slots', 'MAX(slot_revision_id)', '', __METHOD__
) );
795 if ( $maxRevId2 >= $maxRevId ) {
796 $maxRevId = $maxRevId2;
801 if ( $maxRevId >= $revisionRow['rev_id'] ) {
802 $this->logger
->debug(
803 '__METHOD__: Inserted revision {revid} but {table} has revisions up to {maxrevid}.'
804 . ' Trying to fix it.',
806 'revid' => $revisionRow['rev_id'],
808 'maxrevid' => $maxRevId,
812 if ( !$dbw->lock( 'fix-for-T202032', __METHOD__
) ) {
813 throw new MWException( 'Failed to get database lock for T202032' );
816 $dbw->onTransactionResolution( function ( $trigger, $dbw ) use ( $fname ) {
817 $dbw->unlock( 'fix-for-T202032', $fname );
820 $dbw->delete( 'revision', [ 'rev_id' => $revisionRow['rev_id'] ], __METHOD__
);
822 // The locking here is mostly to make MySQL bypass the REPEATABLE-READ transaction
823 // isolation (weird MySQL "feature"). It does seem to block concurrent auto-incrementing
824 // inserts too, though, at least on MariaDB 10.1.29.
826 // Don't try to lock `revision` in this way, it'll deadlock if there are concurrent
827 // transactions in this code path thanks to the row lock from the original ->insert() above.
829 // And we have to use raw SQL to bypass the "aggregation used with a locking SELECT" warning
830 // that's for non-MySQL DBs.
832 $dbw->selectSQLText( 'archive', [ 'v' => "MAX(ar_rev_id)" ], '', __METHOD__
) . ' FOR UPDATE'
834 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
836 $dbw->selectSQLText( 'slots', [ 'v' => "MAX(slot_revision_id)" ], '', __METHOD__
)
844 $row1 ?
intval( $row1->v
) : 0,
845 $row2 ?
intval( $row2->v
) : 0
848 // If we don't have SCHEMA_COMPAT_WRITE_NEW, all except the first of any concurrent
849 // transactions will throw a duplicate key error here. It doesn't seem worth trying
851 $revisionRow['rev_id'] = $maxRevId +
1;
852 $dbw->insert( 'revision', $revisionRow, __METHOD__
);
857 $commentCallback( $revisionRow['rev_id'] );
858 $actorCallback( $revisionRow['rev_id'], $revisionRow );
864 * @param IDatabase $dbw
865 * @param RevisionRecord $rev
866 * @param Title $title
867 * @param int $parentId
869 * @return array [ 0 => array $revisionRow, 1 => callable ]
870 * @throws MWException
871 * @throws MWUnknownContentModelException
873 private function getBaseRevisionRow(
879 // Record the edit in revisions
881 'rev_page' => $rev->getPageId(),
882 'rev_parent_id' => $parentId,
883 'rev_minor_edit' => $rev->isMinor() ?
1 : 0,
884 'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
885 'rev_deleted' => $rev->getVisibility(),
886 'rev_len' => $rev->getSize(),
887 'rev_sha1' => $rev->getSha1(),
890 if ( $rev->getId() !== null ) {
891 // Needed to restore revisions with their original ID
892 $revisionRow['rev_id'] = $rev->getId();
895 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) ) {
896 // In non MCR mode this IF section will relate to the main slot
897 $mainSlot = $rev->getSlot( SlotRecord
::MAIN
);
898 $model = $mainSlot->getModel();
899 $format = $mainSlot->getFormat();
901 // MCR migration note: rev_content_model and rev_content_format will go away
902 if ( $this->contentHandlerUseDB
) {
903 $this->assertCrossWikiContentLoadingIsSafe();
905 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
906 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
908 $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
909 $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
917 * @param SlotRecord $slot
918 * @param Title $title
919 * @param array $blobHints See the BlobStore::XXX_HINT constants
921 * @throws MWException
922 * @return string the blob address
924 private function storeContentBlob(
927 array $blobHints = []
929 $content = $slot->getContent();
930 $format = $content->getDefaultFormat();
931 $model = $content->getModel();
933 $this->checkContent( $content, $title, $slot->getRole() );
935 return $this->blobStore
->storeBlob(
936 $content->serialize( $format ),
937 // These hints "leak" some information from the higher abstraction layer to
938 // low level storage to allow for optimization.
942 BlobStore
::DESIGNATION_HINT
=> 'page-content',
943 BlobStore
::ROLE_HINT
=> $slot->getRole(),
944 BlobStore
::SHA1_HINT
=> $slot->getSha1(),
945 BlobStore
::MODEL_HINT
=> $model,
946 BlobStore
::FORMAT_HINT
=> $format,
953 * @param SlotRecord $slot
954 * @param IDatabase $dbw
955 * @param int $revisionId
956 * @param int $contentId
958 private function insertSlotRowOn( SlotRecord
$slot, IDatabase
$dbw, $revisionId, $contentId ) {
960 'slot_revision_id' => $revisionId,
961 'slot_role_id' => $this->slotRoleStore
->acquireId( $slot->getRole() ),
962 'slot_content_id' => $contentId,
963 // If the slot has a specific origin use that ID, otherwise use the ID of the revision
964 // that we just inserted.
965 'slot_origin' => $slot->hasOrigin() ?
$slot->getOrigin() : $revisionId,
967 $dbw->insert( 'slots', $slotRow, __METHOD__
);
971 * @param SlotRecord $slot
972 * @param IDatabase $dbw
973 * @param string $blobAddress
974 * @return int content row ID
976 private function insertContentRowOn( SlotRecord
$slot, IDatabase
$dbw, $blobAddress ) {
978 'content_size' => $slot->getSize(),
979 'content_sha1' => $slot->getSha1(),
980 'content_model' => $this->contentModelStore
->acquireId( $slot->getModel() ),
981 'content_address' => $blobAddress,
983 $dbw->insert( 'content', $contentRow, __METHOD__
);
984 return intval( $dbw->insertId() );
988 * MCR migration note: this corresponds to Revision::checkContentModel
990 * @param Content $content
991 * @param Title $title
992 * @param string $role
994 * @throws MWException
995 * @throws MWUnknownContentModelException
997 private function checkContent( Content
$content, Title
$title, $role ) {
998 // Note: may return null for revisions that have not yet been inserted
1000 $model = $content->getModel();
1001 $format = $content->getDefaultFormat();
1002 $handler = $content->getContentHandler();
1006 if ( !$handler->isSupportedFormat( $format ) ) {
1007 throw new MWException( "Can't use format $format with content model $model on $name" );
1010 if ( !$this->contentHandlerUseDB
) {
1011 // if $wgContentHandlerUseDB is not set,
1012 // all revisions must use the default content model and format.
1014 $this->assertCrossWikiContentLoadingIsSafe();
1016 $roleHandler = $this->slotRoleRegistry
->getRoleHandler( $role );
1017 $defaultModel = $roleHandler->getDefaultModel( $title );
1018 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
1019 $defaultFormat = $defaultHandler->getDefaultFormat();
1021 if ( $model != $defaultModel ) {
1022 throw new MWException( "Can't save non-default content model with "
1023 . "\$wgContentHandlerUseDB disabled: model is $model, "
1024 . "default for $name is $defaultModel"
1028 if ( $format != $defaultFormat ) {
1029 throw new MWException( "Can't use non-default content format with "
1030 . "\$wgContentHandlerUseDB disabled: format is $format, "
1031 . "default for $name is $defaultFormat"
1036 if ( !$content->isValid() ) {
1037 throw new MWException(
1038 "New content for $name is not valid! Content model is $model"
1044 * Create a new null-revision for insertion into a page's
1045 * history. This will not re-save the text, but simply refer
1046 * to the text from the previous version.
1048 * Such revisions can for instance identify page rename
1049 * operations and other such meta-modifications.
1051 * @note This method grabs a FOR UPDATE lock on the relevant row of the page table,
1052 * to prevent a new revision from being inserted before the null revision has been written
1055 * MCR migration note: this replaces Revision::newNullRevision
1057 * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
1060 * @param IDatabase $dbw used for obtaining the lock on the page table row
1061 * @param Title $title Title of the page to read from
1062 * @param CommentStoreComment $comment RevisionRecord's summary
1063 * @param bool $minor Whether the revision should be considered as minor
1064 * @param User $user The user to attribute the revision to
1066 * @return RevisionRecord|null RevisionRecord or null on error
1068 public function newNullRevision(
1071 CommentStoreComment
$comment,
1075 $this->checkDatabaseWikiId( $dbw );
1077 $pageId = $title->getArticleID();
1079 // T51581: Lock the page table row to ensure no other process
1080 // is adding a revision to the page at the same time.
1081 // Avoid locking extra tables, compare T191892.
1082 $pageLatest = $dbw->selectField(
1085 [ 'page_id' => $pageId ],
1090 if ( !$pageLatest ) {
1094 // Fetch the actual revision row from master, without locking all extra tables.
1095 $oldRevision = $this->loadRevisionFromConds(
1097 [ 'rev_id' => intval( $pageLatest ) ],
1102 if ( !$oldRevision ) {
1103 $msg = "Failed to load latest revision ID $pageLatest of page ID $pageId.";
1104 $this->logger
->error(
1106 [ 'exception' => new RuntimeException( $msg ) ]
1111 // Construct the new revision
1112 $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1113 $newRevision = MutableRevisionRecord
::newFromParentRevision( $oldRevision );
1115 $newRevision->setComment( $comment );
1116 $newRevision->setUser( $user );
1117 $newRevision->setTimestamp( $timestamp );
1118 $newRevision->setMinorEdit( $minor );
1120 return $newRevision;
1124 * MCR migration note: this replaces Revision::isUnpatrolled
1126 * @todo This is overly specific, so move or kill this method.
1128 * @param RevisionRecord $rev
1130 * @return int Rcid of the unpatrolled row, zero if there isn't one
1132 public function getRcIdIfUnpatrolled( RevisionRecord
$rev ) {
1133 $rc = $this->getRecentChange( $rev );
1134 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange
::PRC_UNPATROLLED
) {
1135 return $rc->getAttribute( 'rc_id' );
1142 * Get the RC object belonging to the current revision, if there's one
1144 * MCR migration note: this replaces Revision::getRecentChange
1146 * @todo move this somewhere else?
1148 * @param RevisionRecord $rev
1149 * @param int $flags (optional) $flags include:
1150 * IDBAccessObject::READ_LATEST: Select the data from the master
1152 * @return null|RecentChange
1154 public function getRecentChange( RevisionRecord
$rev, $flags = 0 ) {
1155 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
1156 $db = $this->getDBConnection( $dbType );
1158 $userIdentity = $rev->getUser( RevisionRecord
::RAW
);
1160 if ( !$userIdentity ) {
1161 // If the revision has no user identity, chances are it never went
1162 // into the database, and doesn't have an RC entry.
1166 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
1167 $actorWhere = $this->actorMigration
->getWhere( $db, 'rc_user', $rev->getUser(), false );
1168 $rc = RecentChange
::newFromConds(
1170 $actorWhere['conds'],
1171 'rc_timestamp' => $db->timestamp( $rev->getTimestamp() ),
1172 'rc_this_oldid' => $rev->getId()
1178 $this->releaseDBConnection( $db );
1180 // XXX: cache this locally? Glue it to the RevisionRecord?
1185 * Maps fields of the archive row to corresponding revision rows.
1187 * @param object $archiveRow
1189 * @return object a revision row object, corresponding to $archiveRow.
1191 private static function mapArchiveFields( $archiveRow ) {
1193 // keep with ar prefix:
1196 // not the same suffix:
1197 'ar_page_id' => 'rev_page',
1198 'ar_rev_id' => 'rev_id',
1201 'ar_text_id' => 'rev_text_id',
1202 'ar_timestamp' => 'rev_timestamp',
1203 'ar_user_text' => 'rev_user_text',
1204 'ar_user' => 'rev_user',
1205 'ar_actor' => 'rev_actor',
1206 'ar_minor_edit' => 'rev_minor_edit',
1207 'ar_deleted' => 'rev_deleted',
1208 'ar_len' => 'rev_len',
1209 'ar_parent_id' => 'rev_parent_id',
1210 'ar_sha1' => 'rev_sha1',
1211 'ar_comment' => 'rev_comment',
1212 'ar_comment_cid' => 'rev_comment_cid',
1213 'ar_comment_id' => 'rev_comment_id',
1214 'ar_comment_text' => 'rev_comment_text',
1215 'ar_comment_data' => 'rev_comment_data',
1216 'ar_comment_old' => 'rev_comment_old',
1217 'ar_content_format' => 'rev_content_format',
1218 'ar_content_model' => 'rev_content_model',
1221 $revRow = new stdClass();
1222 foreach ( $fieldMap as $arKey => $revKey ) {
1223 if ( property_exists( $archiveRow, $arKey ) ) {
1224 $revRow->$revKey = $archiveRow->$arKey;
1232 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
1234 * @param object|array $row Either a database row or an array
1235 * @param int $queryFlags for callbacks
1236 * @param Title $title
1238 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
1239 * @throws MWException
1241 private function emulateMainSlot_1_29( $row, $queryFlags, Title
$title ) {
1242 $mainSlotRow = new stdClass();
1243 $mainSlotRow->role_name
= SlotRecord
::MAIN
;
1244 $mainSlotRow->model_name
= null;
1245 $mainSlotRow->slot_revision_id
= null;
1246 $mainSlotRow->slot_content_id
= null;
1247 $mainSlotRow->content_address
= null;
1253 if ( is_object( $row ) ) {
1254 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
) ) {
1255 // Don't emulate from a row when using the new schema.
1256 // Emulating from an array is still OK.
1257 throw new LogicException( 'Can\'t emulate the main slot when using MCR schema.' );
1261 if ( !isset( $row->rev_id
) && ( isset( $row->ar_user
) ||
isset( $row->ar_actor
) ) ) {
1262 $row = $this->mapArchiveFields( $row );
1265 if ( isset( $row->rev_text_id
) && $row->rev_text_id
> 0 ) {
1266 $mainSlotRow->content_address
= SqlBlobStore
::makeAddressFromTextId(
1271 // This is used by null-revisions
1272 $mainSlotRow->slot_origin
= isset( $row->slot_origin
)
1273 ?
intval( $row->slot_origin
)
1276 if ( isset( $row->old_text
) ) {
1277 // this happens when the text-table gets joined directly, in the pre-1.30 schema
1278 $blobData = isset( $row->old_text
) ?
strval( $row->old_text
) : null;
1279 // Check against selects that might have not included old_flags
1280 if ( !property_exists( $row, 'old_flags' ) ) {
1281 throw new InvalidArgumentException( 'old_flags was not set in $row' );
1283 $blobFlags = $row->old_flags ??
'';
1286 $mainSlotRow->slot_revision_id
= intval( $row->rev_id
);
1288 $mainSlotRow->content_size
= isset( $row->rev_len
) ?
intval( $row->rev_len
) : null;
1289 $mainSlotRow->content_sha1
= isset( $row->rev_sha1
) ?
strval( $row->rev_sha1
) : null;
1290 $mainSlotRow->model_name
= isset( $row->rev_content_model
)
1291 ?
strval( $row->rev_content_model
)
1293 // XXX: in the future, we'll probably always use the default format, and drop content_format
1294 $mainSlotRow->format_name
= isset( $row->rev_content_format
)
1295 ?
strval( $row->rev_content_format
)
1298 if ( isset( $row->rev_text_id
) && intval( $row->rev_text_id
) > 0 ) {
1299 // Overwritten below for SCHEMA_COMPAT_WRITE_NEW
1300 $mainSlotRow->slot_content_id
1301 = $this->emulateContentId( intval( $row->rev_text_id
) );
1303 } elseif ( is_array( $row ) ) {
1304 $mainSlotRow->slot_revision_id
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
1306 $mainSlotRow->slot_origin
= isset( $row['slot_origin'] )
1307 ?
intval( $row['slot_origin'] )
1309 $mainSlotRow->content_address
= isset( $row['text_id'] )
1310 ? SqlBlobStore
::makeAddressFromTextId( intval( $row['text_id'] ) )
1312 $mainSlotRow->content_size
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
1313 $mainSlotRow->content_sha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
1315 $mainSlotRow->model_name
= isset( $row['content_model'] )
1316 ?
strval( $row['content_model'] ) : null; // XXX: must be a string!
1317 // XXX: in the future, we'll probably always use the default format, and drop content_format
1318 $mainSlotRow->format_name
= isset( $row['content_format'] )
1319 ?
strval( $row['content_format'] ) : null;
1320 $blobData = isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
1321 // XXX: If the flags field is not set then $blobFlags should be null so that no
1322 // decoding will happen. An empty string will result in default decodings.
1323 $blobFlags = isset( $row['flags'] ) ?
trim( strval( $row['flags'] ) ) : null;
1325 // if we have a Content object, override mText and mContentModel
1326 if ( !empty( $row['content'] ) ) {
1327 if ( !( $row['content'] instanceof Content
) ) {
1328 throw new MWException( 'content field must contain a Content object.' );
1331 /** @var Content $content */
1332 $content = $row['content'];
1333 $handler = $content->getContentHandler();
1335 $mainSlotRow->model_name
= $content->getModel();
1337 // XXX: in the future, we'll probably always use the default format.
1338 if ( $mainSlotRow->format_name
=== null ) {
1339 $mainSlotRow->format_name
= $handler->getDefaultFormat();
1343 if ( isset( $row['text_id'] ) && intval( $row['text_id'] ) > 0 ) {
1344 // Overwritten below for SCHEMA_COMPAT_WRITE_NEW
1345 $mainSlotRow->slot_content_id
1346 = $this->emulateContentId( intval( $row['text_id'] ) );
1349 throw new MWException( 'Revision constructor passed invalid row format.' );
1352 // With the old schema, the content changes with every revision,
1353 // except for null-revisions.
1354 if ( !isset( $mainSlotRow->slot_origin
) ) {
1355 $mainSlotRow->slot_origin
= $mainSlotRow->slot_revision_id
;
1358 if ( $mainSlotRow->model_name
=== null ) {
1359 $mainSlotRow->model_name
= function ( SlotRecord
$slot ) use ( $title ) {
1360 $this->assertCrossWikiContentLoadingIsSafe();
1362 return $this->slotRoleRegistry
->getRoleHandler( $slot->getRole() )
1363 ->getDefaultModel( $title );
1368 // XXX: We should perhaps fail if $blobData is null and $mainSlotRow->content_address
1369 // is missing, but "empty revisions" with no content are used in some edge cases.
1371 $content = function ( SlotRecord
$slot )
1372 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
1374 return $this->loadSlotContent(
1378 $mainSlotRow->format_name
,
1384 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
1385 // NOTE: this callback will be looped through RevisionSlot::newInherited(), allowing
1386 // the inherited slot to have the same content_id as the original slot. In that case,
1387 // $slot will be the inherited slot, while $mainSlotRow still refers to the original slot.
1388 $mainSlotRow->slot_content_id
=
1389 function ( SlotRecord
$slot ) use ( $queryFlags, $mainSlotRow ) {
1390 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1391 return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id
, SlotRecord
::MAIN
);
1395 return new SlotRecord( $mainSlotRow, $content );
1399 * Provides a content ID to use with emulated SlotRecords in SCHEMA_COMPAT_OLD mode,
1400 * based on the revision's text ID (rev_text_id or ar_text_id, respectively).
1401 * Note that in SCHEMA_COMPAT_WRITE_BOTH, a callback to findSlotContentId() should be used
1402 * instead, since in that mode, some revision rows may already have a real content ID,
1403 * while other's don't - and for the ones that don't, we should indicate that it
1404 * is missing and cause SlotRecords::hasContentId() to return false.
1406 * @param int $textId
1407 * @return int The emulated content ID
1409 private function emulateContentId( $textId ) {
1410 // Return a negative number to ensure the ID is distinct from any real content IDs
1411 // that will be assigned in SCHEMA_COMPAT_WRITE_NEW mode and read in SCHEMA_COMPAT_READ_NEW
1417 * Loads a Content object based on a slot row.
1419 * This method does not call $slot->getContent(), and may be used as a callback
1420 * called by $slot->getContent().
1422 * MCR migration note: this roughly corresponds to Revision::getContentInternal
1424 * @param SlotRecord $slot The SlotRecord to load content for
1425 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
1426 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
1427 * Use null if no processing should happen. That is in constrast to the empty string,
1428 * which causes the blob to be decoded according to the configured legacy encoding.
1429 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
1430 * @param int $queryFlags
1432 * @throws RevisionAccessException
1435 private function loadSlotContent(
1442 if ( $blobData !== null ) {
1443 Assert
::parameterType( 'string', $blobData, '$blobData' );
1444 Assert
::parameterType( 'string|null', $blobFlags, '$blobFlags' );
1446 $cacheKey = $slot->hasAddress() ?
$slot->getAddress() : null;
1448 if ( $blobFlags === null ) {
1449 // No blob flags, so use the blob verbatim.
1452 $data = $this->blobStore
->expandBlob( $blobData, $blobFlags, $cacheKey );
1453 if ( $data === false ) {
1454 throw new RevisionAccessException(
1455 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
1461 $address = $slot->getAddress();
1463 $data = $this->blobStore
->getBlob( $address, $queryFlags );
1464 } catch ( BlobAccessException
$e ) {
1465 throw new RevisionAccessException(
1466 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
1471 // Unserialize content
1472 $handler = ContentHandler
::getForModelID( $slot->getModel() );
1474 $content = $handler->unserializeContent( $data, $blobFormat );
1479 * Load a page revision from a given revision ID number.
1480 * Returns null if no such revision can be found.
1482 * MCR migration note: this replaces Revision::newFromId
1485 * IDBAccessObject::READ_LATEST: Select the data from the master
1486 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1489 * @param int $flags (optional)
1490 * @return RevisionRecord|null
1492 public function getRevisionById( $id, $flags = 0 ) {
1493 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
1497 * Load either the current, or a specified, revision
1498 * that's attached to a given link target. If not attached
1499 * to that link target, will return null.
1501 * MCR migration note: this replaces Revision::newFromTitle
1504 * IDBAccessObject::READ_LATEST: Select the data from the master
1505 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1507 * @param LinkTarget $linkTarget
1508 * @param int $revId (optional)
1509 * @param int $flags Bitfield (optional)
1510 * @return RevisionRecord|null
1512 public function getRevisionByTitle( LinkTarget
$linkTarget, $revId = 0, $flags = 0 ) {
1513 // TODO should not require Title in future (T206498)
1514 $title = Title
::newFromLinkTarget( $linkTarget );
1516 'page_namespace' => $title->getNamespace(),
1517 'page_title' => $title->getDBkey()
1520 // Use the specified revision ID.
1521 // Note that we use newRevisionFromConds here because we want to retry
1522 // and fall back to master if the page is not found on a replica.
1523 // Since the caller supplied a revision ID, we are pretty sure the revision is
1524 // supposed to exist, so we should try hard to find it.
1525 $conds['rev_id'] = $revId;
1526 return $this->newRevisionFromConds( $conds, $flags, $title );
1528 // Use a join to get the latest revision.
1529 // Note that we don't use newRevisionFromConds here because we don't want to retry
1530 // and fall back to master. The assumption is that we only want to force the fallback
1531 // if we are quite sure the revision exists because the caller supplied a revision ID.
1532 // If the page isn't found at all on a replica, it probably simply does not exist.
1533 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1535 $conds[] = 'rev_id=page_latest';
1536 $rev = $this->loadRevisionFromConds( $db, $conds, $flags, $title );
1543 * Load either the current, or a specified, revision
1544 * that's attached to a given page ID.
1545 * Returns null if no such revision can be found.
1547 * MCR migration note: this replaces Revision::newFromPageId
1550 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
1551 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1553 * @param int $pageId
1554 * @param int $revId (optional)
1555 * @param int $flags Bitfield (optional)
1556 * @return RevisionRecord|null
1558 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
1559 $conds = [ 'page_id' => $pageId ];
1561 // Use the specified revision ID.
1562 // Note that we use newRevisionFromConds here because we want to retry
1563 // and fall back to master if the page is not found on a replica.
1564 // Since the caller supplied a revision ID, we are pretty sure the revision is
1565 // supposed to exist, so we should try hard to find it.
1566 $conds['rev_id'] = $revId;
1567 return $this->newRevisionFromConds( $conds, $flags );
1569 // Use a join to get the latest revision.
1570 // Note that we don't use newRevisionFromConds here because we don't want to retry
1571 // and fall back to master. The assumption is that we only want to force the fallback
1572 // if we are quite sure the revision exists because the caller supplied a revision ID.
1573 // If the page isn't found at all on a replica, it probably simply does not exist.
1574 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1576 $conds[] = 'rev_id=page_latest';
1577 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1584 * Load the revision for the given title with the given timestamp.
1585 * WARNING: Timestamps may in some circumstances not be unique,
1586 * so this isn't the best key to use.
1588 * MCR migration note: this replaces Revision::loadFromTimestamp
1590 * @param Title $title
1591 * @param string $timestamp
1592 * @return RevisionRecord|null
1594 public function getRevisionByTimestamp( $title, $timestamp ) {
1595 $db = $this->getDBConnection( DB_REPLICA
);
1596 return $this->newRevisionFromConds(
1598 'rev_timestamp' => $db->timestamp( $timestamp ),
1599 'page_namespace' => $title->getNamespace(),
1600 'page_title' => $title->getDBkey()
1608 * @param int $revId The revision to load slots for.
1609 * @param int $queryFlags
1610 * @param Title $title
1612 * @return SlotRecord[]
1614 private function loadSlotRecords( $revId, $queryFlags, Title
$title ) {
1615 $revQuery = self
::getSlotsQueryInfo( [ 'content' ] );
1617 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
1618 $db = $this->getDBConnectionRef( $dbMode );
1621 $revQuery['tables'],
1622 $revQuery['fields'],
1624 'slot_revision_id' => $revId,
1631 $slots = $this->constructSlotRecords( $revId, $res, $queryFlags, $title );
1637 * Factory method for SlotRecords based on known slot rows.
1639 * @param int $revId The revision to load slots for.
1640 * @param object[]|ResultWrapper $slotRows
1641 * @param int $queryFlags
1642 * @param Title $title
1644 * @return SlotRecord[]
1646 private function constructSlotRecords( $revId, $slotRows, $queryFlags, Title
$title ) {
1649 foreach ( $slotRows as $row ) {
1650 // Resolve role names and model names from in-memory cache, if they were not joined in.
1651 if ( !isset( $row->role_name
) ) {
1652 $row->role_name
= $this->slotRoleStore
->getName( (int)$row->slot_role_id
);
1655 if ( !isset( $row->model_name
) ) {
1656 if ( isset( $row->content_model
) ) {
1657 $row->model_name
= $this->contentModelStore
->getName( (int)$row->content_model
);
1659 // We may get here if $row->model_name is set but null, perhaps because it
1660 // came from rev_content_model, which is NULL for the default model.
1661 $slotRoleHandler = $this->slotRoleRegistry
->getRoleHandler( $row->role_name
);
1662 $row->model_name
= $slotRoleHandler->getDefaultModel( $title );
1666 if ( !isset( $row->content_id
) && isset( $row->rev_text_id
) ) {
1667 $row->slot_content_id
1668 = $this->emulateContentId( intval( $row->rev_text_id
) );
1671 $contentCallback = function ( SlotRecord
$slot ) use ( $queryFlags ) {
1672 return $this->loadSlotContent( $slot, null, null, null, $queryFlags );
1675 $slots[$row->role_name
] = new SlotRecord( $row, $contentCallback );
1678 if ( !isset( $slots[SlotRecord
::MAIN
] ) ) {
1679 throw new RevisionAccessException(
1680 'Main slot of revision ' . $revId . ' not found in database!'
1688 * Factory method for RevisionSlots based on a revision ID.
1690 * @note If other code has a need to construct RevisionSlots objects, this should be made
1691 * public, since RevisionSlots instances should not be constructed directly.
1694 * @param object $revisionRow
1695 * @param object[]|null $slotRows
1696 * @param int $queryFlags
1697 * @param Title $title
1699 * @return RevisionSlots
1700 * @throws MWException
1702 private function newRevisionSlots(
1710 $slots = new RevisionSlots(
1711 $this->constructSlotRecords( $revId, $slotRows, $queryFlags, $title )
1713 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
) ) {
1714 $mainSlot = $this->emulateMainSlot_1_29( $revisionRow, $queryFlags, $title );
1715 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize false positive
1716 $slots = new RevisionSlots( [ SlotRecord
::MAIN
=> $mainSlot ] );
1718 // XXX: do we need the same kind of caching here
1719 // that getKnownCurrentRevision uses (if $revId == page_latest?)
1721 $slots = new RevisionSlots( function () use( $revId, $queryFlags, $title ) {
1722 return $this->loadSlotRecords( $revId, $queryFlags, $title );
1730 * Make a fake revision object from an archive table row. This is queried
1731 * for permissions or even inserted (as in Special:Undelete)
1733 * MCR migration note: this replaces Revision::newFromArchiveRow
1735 * @param object $row
1736 * @param int $queryFlags
1737 * @param Title|null $title
1738 * @param array $overrides associative array with fields of $row to override. This may be
1739 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1740 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1741 * override ar_parent_id.
1743 * @return RevisionRecord
1744 * @throws MWException
1746 public function newRevisionFromArchiveRow(
1749 Title
$title = null,
1750 array $overrides = []
1752 Assert
::parameterType( 'object', $row, '$row' );
1754 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1755 Assert
::parameterType( 'integer', $queryFlags, '$queryFlags' );
1757 if ( !$title && isset( $overrides['title'] ) ) {
1758 if ( !( $overrides['title'] instanceof Title
) ) {
1759 throw new MWException( 'title field override must contain a Title object.' );
1762 $title = $overrides['title'];
1765 if ( !isset( $title ) ) {
1766 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
1767 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
1769 throw new InvalidArgumentException(
1770 'A Title or ar_namespace and ar_title must be given'
1775 foreach ( $overrides as $key => $value ) {
1777 $row->$field = $value;
1781 $user = User
::newFromAnyId(
1782 $row->ar_user ??
null,
1783 $row->ar_user_text ??
null,
1784 $row->ar_actor ??
null,
1787 } catch ( InvalidArgumentException
$ex ) {
1788 wfWarn( __METHOD__
. ': ' . $title->getPrefixedDBkey() . ': ' . $ex->getMessage() );
1789 $user = new UserIdentityValue( 0, 'Unknown user', 0 );
1792 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1793 // Legacy because $row may have come from self::selectFields()
1794 $comment = $this->commentStore
->getCommentLegacy( $db, 'ar_comment', $row, true );
1796 $slots = $this->newRevisionSlots( $row->ar_rev_id
, $row, null, $queryFlags, $title );
1798 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1802 * @see RevisionFactory::newRevisionFromRow
1804 * MCR migration note: this replaces Revision::newFromRow
1806 * @param object $row A database row generated from a query based on getQueryInfo()
1807 * @param int $queryFlags
1808 * @param Title|null $title
1809 * @param bool $fromCache if true, the returned RevisionRecord will ensure that no stale
1810 * data is returned from getters, by querying the database as needed
1811 * @return RevisionRecord
1813 public function newRevisionFromRow(
1816 Title
$title = null,
1819 return $this->newRevisionFromRowAndSlots( $row, null, $queryFlags, $title, $fromCache );
1823 * @param object $row A database row generated from a query based on getQueryInfo()
1824 * @param null|object[] $slotRows Database rows generated from a query based on
1825 * getSlotsQueryInfo with the 'content' flag set.
1826 * @param int $queryFlags
1827 * @param Title|null $title
1828 * @param bool $fromCache if true, the returned RevisionRecord will ensure that no stale
1829 * data is returned from getters, by querying the database as needed
1831 * @return RevisionRecord
1832 * @throws MWException
1833 * @see RevisionFactory::newRevisionFromRow
1835 * MCR migration note: this replaces Revision::newFromRow
1838 public function newRevisionFromRowAndSlots(
1842 Title
$title = null,
1845 Assert
::parameterType( 'object', $row, '$row' );
1848 $pageId = $row->rev_page ??
0; // XXX: also check page_id?
1849 $revId = $row->rev_id ??
0;
1851 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1854 if ( !isset( $row->page_latest
) ) {
1855 $row->page_latest
= $title->getLatestRevID();
1856 if ( $row->page_latest
=== 0 && $title->exists() ) {
1857 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1862 $user = User
::newFromAnyId(
1863 $row->rev_user ??
null,
1864 $row->rev_user_text ??
null,
1865 $row->rev_actor ??
null,
1868 } catch ( InvalidArgumentException
$ex ) {
1869 wfWarn( __METHOD__
. ': ' . $title->getPrefixedDBkey() . ': ' . $ex->getMessage() );
1870 $user = new UserIdentityValue( 0, 'Unknown user', 0 );
1873 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1874 // Legacy because $row may have come from self::selectFields()
1875 $comment = $this->commentStore
->getCommentLegacy( $db, 'rev_comment', $row, true );
1877 $slots = $this->newRevisionSlots( $row->rev_id
, $row, $slotRows, $queryFlags, $title );
1879 // If this is a cached row, instantiate a cache-aware revision class to avoid stale data.
1881 $rev = new RevisionStoreCacheRecord(
1882 function ( $revId ) use ( $queryFlags ) {
1883 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1884 return $this->fetchRevisionRowFromConds(
1886 [ 'rev_id' => intval( $revId ) ]
1889 $title, $user, $comment, $row, $slots, $this->wikiId
1892 $rev = new RevisionStoreRecord(
1893 $title, $user, $comment, $row, $slots, $this->wikiId
);
1899 * Constructs a new MutableRevisionRecord based on the given associative array following
1900 * the MW1.29 convention for the Revision constructor.
1902 * MCR migration note: this replaces Revision::newFromRow
1904 * @param array $fields
1905 * @param int $queryFlags
1906 * @param Title|null $title
1908 * @return MutableRevisionRecord
1909 * @throws MWException
1910 * @throws RevisionAccessException
1912 public function newMutableRevisionFromArray(
1917 if ( !$title && isset( $fields['title'] ) ) {
1918 if ( !( $fields['title'] instanceof Title
) ) {
1919 throw new MWException( 'title field must contain a Title object.' );
1922 $title = $fields['title'];
1926 $pageId = $fields['page'] ??
0;
1927 $revId = $fields['id'] ??
0;
1929 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1932 if ( !isset( $fields['page'] ) ) {
1933 $fields['page'] = $title->getArticleID( $queryFlags );
1936 // if we have a content object, use it to set the model and type
1937 if ( !empty( $fields['content'] ) && !( $fields['content'] instanceof Content
)
1938 && !is_array( $fields['content'] )
1940 throw new MWException(
1941 'content field must contain a Content object or an array of Content objects.'
1945 if ( !empty( $fields['text_id'] ) ) {
1946 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
1947 throw new MWException( "The text_id field is only available in the pre-MCR schema" );
1950 if ( !empty( $fields['content'] ) ) {
1951 throw new MWException(
1952 "Text already stored in external store (id {$fields['text_id']}), " .
1953 "can't specify content object"
1959 isset( $fields['comment'] )
1960 && !( $fields['comment'] instanceof CommentStoreComment
)
1962 $commentData = $fields['comment_data'] ??
null;
1964 if ( $fields['comment'] instanceof Message
) {
1965 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1970 $commentText = trim( strval( $fields['comment'] ) );
1971 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1978 $revision = new MutableRevisionRecord( $title, $this->wikiId
);
1979 $this->initializeMutableRevisionFromArray( $revision, $fields );
1981 if ( isset( $fields['content'] ) && is_array( $fields['content'] ) ) {
1982 // @phan-suppress-next-line PhanTypeNoPropertiesForeach
1983 foreach ( $fields['content'] as $role => $content ) {
1984 $revision->setContent( $role, $content );
1987 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1988 $revision->setSlot( $mainSlot );
1995 * @param MutableRevisionRecord $record
1996 * @param array $fields
1998 private function initializeMutableRevisionFromArray(
1999 MutableRevisionRecord
$record,
2002 /** @var UserIdentity $user */
2005 // If a user is passed in, use it if possible. We cannot use a user from a
2006 // remote wiki with unsuppressed ids, due to issues described in T222212.
2007 if ( isset( $fields['user'] ) &&
2008 ( $fields['user'] instanceof UserIdentity
) &&
2009 ( $this->wikiId
=== false ||
2010 ( !$fields['user']->getId() && !$fields['user']->getActorId() ) )
2012 $user = $fields['user'];
2015 $user = User
::newFromAnyId(
2016 $fields['user'] ??
null,
2017 $fields['user_text'] ??
null,
2018 $fields['actor'] ??
null,
2021 } catch ( InvalidArgumentException
$ex ) {
2027 $record->setUser( $user );
2030 $timestamp = isset( $fields['timestamp'] )
2031 ?
strval( $fields['timestamp'] )
2032 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
2034 $record->setTimestamp( $timestamp );
2036 if ( isset( $fields['page'] ) ) {
2037 $record->setPageId( intval( $fields['page'] ) );
2040 if ( isset( $fields['id'] ) ) {
2041 $record->setId( intval( $fields['id'] ) );
2043 if ( isset( $fields['parent_id'] ) ) {
2044 $record->setParentId( intval( $fields['parent_id'] ) );
2047 if ( isset( $fields['sha1'] ) ) {
2048 $record->setSha1( $fields['sha1'] );
2050 if ( isset( $fields['size'] ) ) {
2051 $record->setSize( intval( $fields['size'] ) );
2054 if ( isset( $fields['minor_edit'] ) ) {
2055 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
2057 if ( isset( $fields['deleted'] ) ) {
2058 $record->setVisibility( intval( $fields['deleted'] ) );
2061 if ( isset( $fields['comment'] ) ) {
2062 Assert
::parameterType(
2063 CommentStoreComment
::class,
2067 $record->setComment( $fields['comment'] );
2072 * Load a page revision from a given revision ID number.
2073 * Returns null if no such revision can be found.
2075 * MCR migration note: this corresponds to Revision::loadFromId
2077 * @note direct use is deprecated!
2078 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
2080 * @param IDatabase $db
2083 * @return RevisionRecord|null
2085 public function loadRevisionFromId( IDatabase
$db, $id ) {
2086 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
2090 * Load either the current, or a specified, revision
2091 * that's attached to a given page. If not attached
2092 * to that page, will return null.
2094 * MCR migration note: this replaces Revision::loadFromPageId
2096 * @note direct use is deprecated!
2097 * @todo remove when unused!
2099 * @param IDatabase $db
2100 * @param int $pageid
2102 * @return RevisionRecord|null
2104 public function loadRevisionFromPageId( IDatabase
$db, $pageid, $id = 0 ) {
2105 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
2107 $conds['rev_id'] = intval( $id );
2109 $conds[] = 'rev_id=page_latest';
2111 return $this->loadRevisionFromConds( $db, $conds );
2115 * Load either the current, or a specified, revision
2116 * that's attached to a given page. If not attached
2117 * to that page, will return null.
2119 * MCR migration note: this replaces Revision::loadFromTitle
2121 * @note direct use is deprecated!
2122 * @todo remove when unused!
2124 * @param IDatabase $db
2125 * @param Title $title
2128 * @return RevisionRecord|null
2130 public function loadRevisionFromTitle( IDatabase
$db, $title, $id = 0 ) {
2132 $matchId = intval( $id );
2134 $matchId = 'page_latest';
2137 return $this->loadRevisionFromConds(
2141 'page_namespace' => $title->getNamespace(),
2142 'page_title' => $title->getDBkey()
2150 * Load the revision for the given title with the given timestamp.
2151 * WARNING: Timestamps may in some circumstances not be unique,
2152 * so this isn't the best key to use.
2154 * MCR migration note: this replaces Revision::loadFromTimestamp
2156 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
2157 * @todo remove when unused!
2159 * @param IDatabase $db
2160 * @param Title $title
2161 * @param string $timestamp
2162 * @return RevisionRecord|null
2164 public function loadRevisionFromTimestamp( IDatabase
$db, $title, $timestamp ) {
2165 return $this->loadRevisionFromConds( $db,
2167 'rev_timestamp' => $db->timestamp( $timestamp ),
2168 'page_namespace' => $title->getNamespace(),
2169 'page_title' => $title->getDBkey()
2177 * Given a set of conditions, fetch a revision
2179 * This method should be used if we are pretty sure the revision exists.
2180 * Unless $flags has READ_LATEST set, this method will first try to find the revision
2181 * on a replica before hitting the master database.
2183 * MCR migration note: this corresponds to Revision::newFromConds
2185 * @param array $conditions
2186 * @param int $flags (optional)
2187 * @param Title|null $title
2189 * @return RevisionRecord|null
2191 private function newRevisionFromConds( $conditions, $flags = 0, Title
$title = null ) {
2192 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2193 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
2195 $lb = $this->getDBLoadBalancer();
2197 // Make sure new pending/committed revision are visibile later on
2198 // within web requests to certain avoid bugs like T93866 and T94407.
2200 && !( $flags & self
::READ_LATEST
)
2201 && $lb->hasStreamingReplicaServers()
2202 && $lb->hasOrMadeRecentMasterChanges()
2204 $flags = self
::READ_LATEST
;
2205 $dbw = $this->getDBConnection( DB_MASTER
);
2206 $rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $title );
2207 $this->releaseDBConnection( $dbw );
2214 * Given a set of conditions, fetch a revision from
2215 * the given database connection.
2217 * MCR migration note: this corresponds to Revision::loadFromConds
2219 * @param IDatabase $db
2220 * @param array $conditions
2221 * @param int $flags (optional)
2222 * @param Title|null $title
2224 * @return RevisionRecord|null
2226 private function loadRevisionFromConds(
2232 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
2234 $rev = $this->newRevisionFromRow( $row, $flags, $title );
2243 * Throws an exception if the given database connection does not belong to the wiki this
2244 * RevisionStore is bound to.
2246 * @param IDatabase $db
2247 * @throws MWException
2249 private function checkDatabaseWikiId( IDatabase
$db ) {
2250 $storeWiki = $this->wikiId
;
2251 $dbWiki = $db->getDomainID();
2253 if ( $dbWiki === $storeWiki ) {
2257 $storeWiki = $storeWiki ?
: $this->loadBalancer
->getLocalDomainID();
2258 // @FIXME: when would getDomainID() be false here?
2259 $dbWiki = $dbWiki ?
: wfWikiID();
2261 if ( $dbWiki === $storeWiki ) {
2265 // HACK: counteract encoding imposed by DatabaseDomain
2266 $storeWiki = str_replace( '?h', '-', $storeWiki );
2267 $dbWiki = str_replace( '?h', '-', $dbWiki );
2269 if ( $dbWiki === $storeWiki ) {
2273 throw new MWException( "RevisionStore for $storeWiki "
2274 . "cannot be used with a DB connection for $dbWiki" );
2278 * Given a set of conditions, return a row with the
2279 * fields necessary to build RevisionRecord objects.
2281 * MCR migration note: this corresponds to Revision::fetchFromConds
2283 * @param IDatabase $db
2284 * @param array $conditions
2285 * @param int $flags (optional)
2287 * @return object|false data row as a raw object
2289 private function fetchRevisionRowFromConds( IDatabase
$db, $conditions, $flags = 0 ) {
2290 $this->checkDatabaseWikiId( $db );
2292 $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
2294 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
2295 $options[] = 'FOR UPDATE';
2297 return $db->selectRow(
2298 $revQuery['tables'],
2299 $revQuery['fields'],
2308 * Finds the ID of a content row for a given revision and slot role.
2309 * This can be used to re-use content rows even while the content ID
2310 * is still missing from SlotRecords, when writing to both the old and
2311 * the new schema during MCR schema migration.
2313 * @todo remove after MCR schema migration is complete.
2315 * @param IDatabase $db
2317 * @param string $role
2321 private function findSlotContentId( IDatabase
$db, $revId, $role ) {
2322 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
2327 $roleId = $this->slotRoleStore
->getId( $role );
2329 'slot_revision_id' => $revId,
2330 'slot_role_id' => $roleId,
2333 $contentId = $db->selectField( 'slots', 'slot_content_id', $conditions, __METHOD__
);
2335 return $contentId ?
: null;
2336 } catch ( NameTableAccessException
$ex ) {
2337 // If the role is missing from the slot_roles table,
2338 // the corresponding row in slots cannot exist.
2344 * Return the tables, fields, and join conditions to be selected to create
2345 * a new RevisionStoreRecord object.
2347 * MCR migration note: this replaces Revision::getQueryInfo
2349 * If the format of fields returned changes in any way then the cache key provided by
2350 * self::getRevisionRowCacheKey should be updated.
2354 * @param array $options Any combination of the following strings
2355 * - 'page': Join with the page table, and select fields to identify the page
2356 * - 'user': Join with the user table, and select the user name
2357 * - 'text': Join with the text table, and select fields to load page text. This
2358 * option is deprecated in MW 1.32 when the MCR migration flag SCHEMA_COMPAT_WRITE_NEW
2359 * is set, and disallowed when SCHEMA_COMPAT_READ_OLD is not set.
2361 * @return array With three keys:
2362 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2363 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2364 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2366 public function getQueryInfo( $options = [] ) {
2373 $ret['tables'][] = 'revision';
2374 $ret['fields'] = array_merge( $ret['fields'], [
2385 $commentQuery = $this->commentStore
->getJoin( 'rev_comment' );
2386 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
2387 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
2388 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
2390 $actorQuery = $this->actorMigration
->getJoin( 'rev_user' );
2391 $ret['tables'] = array_merge( $ret['tables'], $actorQuery['tables'] );
2392 $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
2393 $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
2395 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2396 $ret['fields'][] = 'rev_text_id';
2398 if ( $this->contentHandlerUseDB
) {
2399 $ret['fields'][] = 'rev_content_format';
2400 $ret['fields'][] = 'rev_content_model';
2404 if ( in_array( 'page', $options, true ) ) {
2405 $ret['tables'][] = 'page';
2406 $ret['fields'] = array_merge( $ret['fields'], [
2414 $ret['joins']['page'] = [ 'JOIN', [ 'page_id = rev_page' ] ];
2417 if ( in_array( 'user', $options, true ) ) {
2418 $ret['tables'][] = 'user';
2419 $ret['fields'] = array_merge( $ret['fields'], [
2422 $u = $actorQuery['fields']['rev_user'];
2423 $ret['joins']['user'] = [ 'LEFT JOIN', [ "$u != 0", "user_id = $u" ] ];
2426 if ( in_array( 'text', $options, true ) ) {
2427 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) ) {
2428 throw new InvalidArgumentException( 'text table can no longer be joined directly' );
2429 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2430 // NOTE: even when this class is set to not read from the old schema, callers
2431 // should still be able to join against the text table, as long as we are still
2432 // writing the old schema for compatibility.
2433 // TODO: This should trigger a deprecation warning eventually (T200918), but not
2434 // before all known usages are removed (see T198341 and T201164).
2435 // wfDeprecated( __METHOD__ . ' with `text` option', '1.32' );
2438 $ret['tables'][] = 'text';
2439 $ret['fields'] = array_merge( $ret['fields'], [
2443 $ret['joins']['text'] = [ 'JOIN', [ 'rev_text_id=old_id' ] ];
2450 * Return the tables, fields, and join conditions to be selected to create
2455 * @param array $options Any combination of the following strings
2456 * - 'content': Join with the content table, and select content meta-data fields
2457 * - 'model': Join with the content_models table, and select the model_name field.
2458 * Only applicable if 'content' is also set.
2459 * - 'role': Join with the slot_roles table, and select the role_name field
2461 * @return array With three keys:
2462 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2463 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2464 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2466 public function getSlotsQueryInfo( $options = [] ) {
2473 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2474 $db = $this->getDBConnectionRef( DB_REPLICA
);
2475 $ret['tables'][] = 'revision';
2477 $ret['fields']['slot_revision_id'] = 'rev_id';
2478 $ret['fields']['slot_content_id'] = 'NULL';
2479 $ret['fields']['slot_origin'] = 'rev_id';
2480 $ret['fields']['role_name'] = $db->addQuotes( SlotRecord
::MAIN
);
2482 if ( in_array( 'content', $options, true ) ) {
2483 $ret['fields']['content_size'] = 'rev_len';
2484 $ret['fields']['content_sha1'] = 'rev_sha1';
2485 $ret['fields']['content_address']
2486 = $db->buildConcat( [ $db->addQuotes( 'tt:' ), 'rev_text_id' ] );
2488 // Allow the content_id field to be emulated later
2489 $ret['fields']['rev_text_id'] = 'rev_text_id';
2491 if ( $this->contentHandlerUseDB
) {
2492 $ret['fields']['model_name'] = 'rev_content_model';
2494 $ret['fields']['model_name'] = 'NULL';
2498 $ret['tables'][] = 'slots';
2499 $ret['fields'] = array_merge( $ret['fields'], [
2506 if ( in_array( 'role', $options, true ) ) {
2507 // Use left join to attach role name, so we still find the revision row even
2508 // if the role name is missing. This triggers a more obvious failure mode.
2509 $ret['tables'][] = 'slot_roles';
2510 $ret['joins']['slot_roles'] = [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ];
2511 $ret['fields'][] = 'role_name';
2514 if ( in_array( 'content', $options, true ) ) {
2515 $ret['tables'][] = 'content';
2516 $ret['fields'] = array_merge( $ret['fields'], [
2522 $ret['joins']['content'] = [ 'JOIN', [ 'slot_content_id = content_id' ] ];
2524 if ( in_array( 'model', $options, true ) ) {
2525 // Use left join to attach model name, so we still find the revision row even
2526 // if the model name is missing. This triggers a more obvious failure mode.
2527 $ret['tables'][] = 'content_models';
2528 $ret['joins']['content_models'] = [ 'LEFT JOIN', [ 'content_model = model_id' ] ];
2529 $ret['fields'][] = 'model_name';
2539 * Return the tables, fields, and join conditions to be selected to create
2540 * a new RevisionArchiveRecord object.
2542 * MCR migration note: this replaces Revision::getArchiveQueryInfo
2546 * @return array With three keys:
2547 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2548 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2549 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2551 public function getArchiveQueryInfo() {
2552 $commentQuery = $this->commentStore
->getJoin( 'ar_comment' );
2553 $actorQuery = $this->actorMigration
->getJoin( 'ar_user' );
2555 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
2568 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
2569 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
2572 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2573 $ret['fields'][] = 'ar_text_id';
2575 if ( $this->contentHandlerUseDB
) {
2576 $ret['fields'][] = 'ar_content_format';
2577 $ret['fields'][] = 'ar_content_model';
2585 * Do a batched query for the sizes of a set of revisions.
2587 * MCR migration note: this replaces Revision::getParentLengths
2589 * @param int[] $revIds
2590 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2591 * of the corresponding revision.
2593 public function getRevisionSizes( array $revIds ) {
2594 return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA
), $revIds );
2598 * Do a batched query for the sizes of a set of revisions.
2600 * MCR migration note: this replaces Revision::getParentLengths
2602 * @deprecated use RevisionStore::getRevisionSizes instead.
2604 * @param IDatabase $db
2605 * @param int[] $revIds
2606 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2607 * of the corresponding revision.
2609 public function listRevisionSizes( IDatabase
$db, array $revIds ) {
2610 $this->checkDatabaseWikiId( $db );
2614 return $revLens; // empty
2619 [ 'rev_id', 'rev_len' ],
2620 [ 'rev_id' => $revIds ],
2624 foreach ( $res as $row ) {
2625 $revLens[$row->rev_id
] = intval( $row->rev_len
);
2632 * Implementation of getPreviousRevision and getNextRevision.
2634 * @param RevisionRecord $rev
2636 * @param string $dir 'next' or 'prev'
2637 * @return RevisionRecord|null
2639 private function getRelativeRevision( RevisionRecord
$rev, $flags, $dir ) {
2640 $op = $dir === 'next' ?
'>' : '<';
2641 $sort = $dir === 'next' ?
'ASC' : 'DESC';
2643 if ( !$rev->getId() ||
!$rev->getPageId() ) {
2644 // revision is unsaved or otherwise incomplete
2648 if ( $rev instanceof RevisionArchiveRecord
) {
2649 // revision is deleted, so it's not part of the page history
2653 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
2654 $db = $this->getDBConnection( $dbType, [ 'contributions' ] );
2656 $ts = $this->getTimestampFromId( $rev->getId(), $flags );
2657 if ( $ts === false ) {
2658 // XXX Should this be moved into getTimestampFromId?
2659 $ts = $db->selectField( 'archive', 'ar_timestamp',
2660 [ 'ar_rev_id' => $rev->getId() ], __METHOD__
);
2661 if ( $ts === false ) {
2662 // XXX Is this reachable? How can we have a page id but no timestamp?
2666 $ts = $db->addQuotes( $db->timestamp( $ts ) );
2668 $revId = $db->selectField( 'revision', 'rev_id',
2670 'rev_page' => $rev->getPageId(),
2671 "rev_timestamp $op $ts OR (rev_timestamp = $ts AND rev_id $op {$rev->getId()})"
2675 'ORDER BY' => "rev_timestamp $sort, rev_id $sort",
2676 'IGNORE INDEX' => 'rev_timestamp', // Probably needed for T159319
2680 if ( $revId === false ) {
2684 return $this->getRevisionById( intval( $revId ) );
2688 * Get the revision before $rev in the page's history, if any.
2689 * Will return null for the first revision but also for deleted or unsaved revisions.
2691 * MCR migration note: this replaces Revision::getPrevious
2693 * @see Title::getPreviousRevisionID
2694 * @see PageArchive::getPreviousRevision
2696 * @param RevisionRecord $rev
2697 * @param int $flags (optional) $flags include:
2698 * IDBAccessObject::READ_LATEST: Select the data from the master
2700 * @return RevisionRecord|null
2702 public function getPreviousRevision( RevisionRecord
$rev, $flags = 0 ) {
2703 if ( $flags instanceof Title
) {
2704 // Old calling convention, we don't use Title here anymore
2705 wfDeprecated( __METHOD__
. ' with Title', '1.34' );
2709 return $this->getRelativeRevision( $rev, $flags, 'prev' );
2713 * Get the revision after $rev in the page's history, if any.
2714 * Will return null for the latest revision but also for deleted or unsaved revisions.
2716 * MCR migration note: this replaces Revision::getNext
2718 * @see Title::getNextRevisionID
2720 * @param RevisionRecord $rev
2721 * @param int $flags (optional) $flags include:
2722 * IDBAccessObject::READ_LATEST: Select the data from the master
2723 * @return RevisionRecord|null
2725 public function getNextRevision( RevisionRecord
$rev, $flags = 0 ) {
2726 if ( $flags instanceof Title
) {
2727 // Old calling convention, we don't use Title here anymore
2728 wfDeprecated( __METHOD__
. ' with Title', '1.34' );
2732 return $this->getRelativeRevision( $rev, $flags, 'next' );
2736 * Get previous revision Id for this page_id
2737 * This is used to populate rev_parent_id on save
2739 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
2741 * @param IDatabase $db
2742 * @param RevisionRecord $rev
2746 private function getPreviousRevisionId( IDatabase
$db, RevisionRecord
$rev ) {
2747 $this->checkDatabaseWikiId( $db );
2749 if ( $rev->getPageId() === null ) {
2752 # Use page_latest if ID is not given
2753 if ( !$rev->getId() ) {
2754 $prevId = $db->selectField(
2755 'page', 'page_latest',
2756 [ 'page_id' => $rev->getPageId() ],
2760 $prevId = $db->selectField(
2761 'revision', 'rev_id',
2762 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
2764 [ 'ORDER BY' => 'rev_id DESC' ]
2767 return intval( $prevId );
2771 * Get rev_timestamp from rev_id, without loading the rest of the row.
2773 * Historically, there was an extra Title parameter that was passed before $id. This is no
2774 * longer needed and is deprecated in 1.34.
2776 * MCR migration note: this replaces Revision::getTimestampFromId
2780 * @return string|bool False if not found
2782 public function getTimestampFromId( $id, $flags = 0 ) {
2783 if ( $id instanceof Title
) {
2784 // Old deprecated calling convention supported for backwards compatibility
2786 $flags = func_num_args() > 2 ?
func_get_arg( 2 ) : 0;
2788 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2791 $db->selectField( 'revision', 'rev_timestamp', [ 'rev_id' => $id ], __METHOD__
);
2793 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
2797 * Get count of revisions per page...not very efficient
2799 * MCR migration note: this replaces Revision::countByPageId
2801 * @param IDatabase $db
2802 * @param int $id Page id
2805 public function countRevisionsByPageId( IDatabase
$db, $id ) {
2806 $this->checkDatabaseWikiId( $db );
2808 $row = $db->selectRow( 'revision',
2809 [ 'revCount' => 'COUNT(*)' ],
2810 [ 'rev_page' => $id ],
2814 return intval( $row->revCount
);
2820 * Get count of revisions per page...not very efficient
2822 * MCR migration note: this replaces Revision::countByTitle
2824 * @param IDatabase $db
2825 * @param Title $title
2828 public function countRevisionsByTitle( IDatabase
$db, $title ) {
2829 $id = $title->getArticleID();
2831 return $this->countRevisionsByPageId( $db, $id );
2837 * Check if no edits were made by other users since
2838 * the time a user started editing the page. Limit to
2839 * 50 revisions for the sake of performance.
2841 * MCR migration note: this replaces Revision::userWasLastToEdit
2843 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
2844 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
2845 * has been deprecated since 1.24.
2847 * @param IDatabase $db The Database to perform the check on.
2848 * @param int $pageId The ID of the page in question
2849 * @param int $userId The ID of the user in question
2850 * @param string $since Look at edits since this time
2852 * @return bool True if the given user was the only one to edit since the given timestamp
2854 public function userWasLastToEdit( IDatabase
$db, $pageId, $userId, $since ) {
2855 $this->checkDatabaseWikiId( $db );
2861 $revQuery = $this->getQueryInfo();
2863 $revQuery['tables'],
2865 'rev_user' => $revQuery['fields']['rev_user'],
2868 'rev_page' => $pageId,
2869 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
2872 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
2875 foreach ( $res as $row ) {
2876 if ( $row->rev_user
!= $userId ) {
2884 * Load a revision based on a known page ID and current revision ID from the DB
2886 * This method allows for the use of caching, though accessing anything that normally
2887 * requires permission checks (aside from the text) will trigger a small DB lookup.
2889 * MCR migration note: this replaces Revision::newKnownCurrent
2891 * @param Title $title the associated page title
2892 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
2894 * @return RevisionRecord|bool Returns false if missing
2896 public function getKnownCurrentRevision( Title
$title, $revId ) {
2897 $db = $this->getDBConnectionRef( DB_REPLICA
);
2899 $pageId = $title->getArticleID();
2906 $revId = $title->getLatestRevID();
2911 'No latest revision known for page ' . $title->getPrefixedDBkey()
2912 . ' even though it exists with page ID ' . $pageId
2917 // Load the row from cache if possible. If not possible, populate the cache.
2918 // As a minor optimization, remember if this was a cache hit or miss.
2919 // We can sometimes avoid a database query later if this is a cache miss.
2921 $row = $this->cache
->getWithSetCallback(
2922 // Page/rev IDs passed in from DB to reflect history merges
2923 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2924 WANObjectCache
::TTL_WEEK
,
2925 function ( $curValue, &$ttl, array &$setOpts ) use (
2926 $db, $pageId, $revId, &$fromCache
2928 $setOpts +
= Database
::getCacheSetOptions( $db );
2929 $row = $this->fetchRevisionRowFromConds( $db, [ 'rev_id' => intval( $revId ) ] );
2933 return $row; // don't cache negatives
2937 // Reflect revision deletion and user renames.
2939 return $this->newRevisionFromRow( $row, 0, $title, $fromCache );
2946 * Get a cache key for use with a row as selected with getQueryInfo( [ 'page', 'user' ] )
2947 * Caching rows without 'page' or 'user' could lead to issues.
2948 * If the format of the rows returned by the query provided by getQueryInfo changes the
2949 * cache key should be updated to avoid conflicts.
2951 * @param IDatabase $db
2952 * @param int $pageId
2956 private function getRevisionRowCacheKey( IDatabase
$db, $pageId, $revId ) {
2957 return $this->cache
->makeGlobalKey(
2958 self
::ROW_CACHE_KEY
,
2965 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.
2970 * Retain the old class name for backwards compatibility.
2971 * @deprecated since 1.32
2973 class_alias( RevisionStore
::class, 'MediaWiki\Storage\RevisionStore' );