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\Storage
;
31 use CommentStoreComment
;
34 use DBAccessObjectUtils
;
37 use InvalidArgumentException
;
40 use MediaWiki\Linker\LinkTarget
;
41 use MediaWiki\User\UserIdentity
;
42 use MediaWiki\User\UserIdentityValue
;
45 use MWUnknownContentModelException
;
46 use Psr\Log\LoggerAwareInterface
;
47 use Psr\Log\LoggerInterface
;
48 use Psr\Log\NullLogger
;
55 use Wikimedia\Assert\Assert
;
56 use Wikimedia\Rdbms\Database
;
57 use Wikimedia\Rdbms\DBConnRef
;
58 use Wikimedia\Rdbms\IDatabase
;
59 use Wikimedia\Rdbms\LoadBalancer
;
62 * Service for looking up page revisions.
66 * @note This was written to act as a drop-in replacement for the corresponding
67 * static methods in Revision.
70 implements IDBAccessObject
, RevisionFactory
, RevisionLookup
, LoggerAwareInterface
{
72 const ROW_CACHE_KEY
= 'revision-row-1.29';
86 * @see $wgContentHandlerUseDB
88 private $contentHandlerUseDB = true;
93 private $loadBalancer;
103 private $commentStore;
106 * @var ActorMigration
108 private $actorMigration;
111 * @var LoggerInterface
116 * @var NameTableStore
118 private $contentModelStore;
121 * @var NameTableStore
123 private $slotRoleStore;
125 /** @var int An appropriate combination of SCHEMA_COMPAT_XXX flags. */
126 private $mcrMigrationStage;
129 * @todo $blobStore should be allowed to be any BlobStore!
131 * @param LoadBalancer $loadBalancer
132 * @param SqlBlobStore $blobStore
133 * @param WANObjectCache $cache
134 * @param CommentStore $commentStore
135 * @param NameTableStore $contentModelStore
136 * @param NameTableStore $slotRoleStore
137 * @param int $mcrMigrationStage An appropriate combination of SCHEMA_COMPAT_XXX flags
138 * @param ActorMigration $actorMigration
139 * @param bool|string $wikiId
141 * @throws MWException if $mcrMigrationStage or $wikiId is invalid.
143 public function __construct(
144 LoadBalancer
$loadBalancer,
145 SqlBlobStore
$blobStore,
146 WANObjectCache
$cache,
147 CommentStore
$commentStore,
148 NameTableStore
$contentModelStore,
149 NameTableStore
$slotRoleStore,
151 ActorMigration
$actorMigration,
154 Assert
::parameterType( 'string|boolean', $wikiId, '$wikiId' );
155 Assert
::parameterType( 'integer', $mcrMigrationStage, '$mcrMigrationStage' );
157 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH
) !== SCHEMA_COMPAT_READ_BOTH
,
158 '$mcrMigrationStage',
159 'Reading from the old and the new schema at the same time is not supported.'
162 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH
) !== 0,
163 '$mcrMigrationStage',
164 'Reading needs to be enabled for the old or the new schema.'
167 ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_BOTH
) !== 0,
168 '$mcrMigrationStage',
169 'Writing needs to be enabled for the old or the new schema.'
172 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_OLD
) === 0
173 ||
( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) !== 0,
174 '$mcrMigrationStage',
175 'Cannot read the old schema when not also writing it.'
178 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_NEW
) === 0
179 ||
( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) !== 0,
180 '$mcrMigrationStage',
181 'Cannot read the new schema when not also writing it.'
184 $this->loadBalancer
= $loadBalancer;
185 $this->blobStore
= $blobStore;
186 $this->cache
= $cache;
187 $this->commentStore
= $commentStore;
188 $this->contentModelStore
= $contentModelStore;
189 $this->slotRoleStore
= $slotRoleStore;
190 $this->mcrMigrationStage
= $mcrMigrationStage;
191 $this->actorMigration
= $actorMigration;
192 $this->wikiId
= $wikiId;
193 $this->logger
= new NullLogger();
197 * @param int $flags A combination of SCHEMA_COMPAT_XXX flags.
198 * @return bool True if all the given flags were set in the $mcrMigrationStage
199 * parameter passed to the constructor.
201 private function hasMcrSchemaFlags( $flags ) {
202 return ( $this->mcrMigrationStage
& $flags ) === $flags;
205 public function setLogger( LoggerInterface
$logger ) {
206 $this->logger
= $logger;
210 * @return bool Whether the store is read-only
212 public function isReadOnly() {
213 return $this->blobStore
->isReadOnly();
219 public function getContentHandlerUseDB() {
220 return $this->contentHandlerUseDB
;
224 * @see $wgContentHandlerUseDB
225 * @param bool $contentHandlerUseDB
226 * @throws MWException
228 public function setContentHandlerUseDB( $contentHandlerUseDB ) {
229 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
)
230 ||
$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
)
232 if ( !$contentHandlerUseDB ) {
233 throw new MWException(
234 'Content model must be stored in the database for multi content revision migration.'
238 $this->contentHandlerUseDB
= $contentHandlerUseDB;
242 * @return LoadBalancer
244 private function getDBLoadBalancer() {
245 return $this->loadBalancer
;
249 * @param int $mode DB_MASTER or DB_REPLICA
253 private function getDBConnection( $mode ) {
254 $lb = $this->getDBLoadBalancer();
255 return $lb->getConnection( $mode, [], $this->wikiId
);
259 * @param IDatabase $connection
261 private function releaseDBConnection( IDatabase
$connection ) {
262 $lb = $this->getDBLoadBalancer();
263 $lb->reuseConnection( $connection );
267 * @param int $mode DB_MASTER or DB_REPLICA
271 private function getDBConnectionRef( $mode ) {
272 $lb = $this->getDBLoadBalancer();
273 return $lb->getConnectionRef( $mode, [], $this->wikiId
);
277 * Determines the page Title based on the available information.
279 * MCR migration note: this corresponds to Revision::getTitle
281 * @note this method should be private, external use should be avoided!
283 * @param int|null $pageId
284 * @param int|null $revId
285 * @param int $queryFlags
288 * @throws RevisionAccessException
290 public function getTitle( $pageId, $revId, $queryFlags = self
::READ_NORMAL
) {
291 if ( !$pageId && !$revId ) {
292 throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
295 // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
296 // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
297 if ( DBAccessObjectUtils
::hasFlags( $queryFlags, self
::READ_LATEST_IMMUTABLE
) ) {
298 $queryFlags = self
::READ_NORMAL
;
301 $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->wikiId
=== false );
302 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
303 $titleFlags = ( $dbMode == DB_MASTER ? Title
::GAID_FOR_UPDATE
: 0 );
305 // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
306 if ( $canUseTitleNewFromId ) {
307 // TODO: better foreign title handling (introduce TitleFactory)
308 $title = Title
::newFromID( $pageId, $titleFlags );
314 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
315 $canUseRevId = ( $revId !== null && $revId > 0 );
317 if ( $canUseRevId ) {
318 $dbr = $this->getDBConnectionRef( $dbMode );
319 // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
320 $row = $dbr->selectRow(
321 [ 'revision', 'page' ],
330 [ 'rev_id' => $revId ],
333 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
336 // TODO: better foreign title handling (introduce TitleFactory)
337 return Title
::newFromRow( $row );
341 // If we still don't have a title, fallback to master if that wasn't already happening.
342 if ( $dbMode !== DB_MASTER
) {
343 $title = $this->getTitle( $pageId, $revId, self
::READ_LATEST
);
346 __METHOD__
. ' fell back to READ_LATEST and got a Title.',
347 [ 'trace' => wfBacktrace() ]
353 throw new RevisionAccessException(
354 "Could not determine title for page ID $pageId and revision ID $revId"
359 * @param mixed $value
360 * @param string $name
362 * @throws IncompleteRevisionException if $value is null
363 * @return mixed $value, if $value is not null
365 private function failOnNull( $value, $name ) {
366 if ( $value === null ) {
367 throw new IncompleteRevisionException(
368 "$name must not be " . var_export( $value, true ) . "!"
376 * @param mixed $value
377 * @param string $name
379 * @throws IncompleteRevisionException if $value is empty
380 * @return mixed $value, if $value is not null
382 private function failOnEmpty( $value, $name ) {
383 if ( $value === null ||
$value === 0 ||
$value === '' ) {
384 throw new IncompleteRevisionException(
385 "$name must not be " . var_export( $value, true ) . "!"
393 * Insert a new revision into the database, returning the new revision record
394 * on success and dies horribly on failure.
396 * MCR migration note: this replaces Revision::insertOn
398 * @param RevisionRecord $rev
399 * @param IDatabase $dbw (master connection)
401 * @throws InvalidArgumentException
402 * @return RevisionRecord the new revision record.
404 public function insertRevisionOn( RevisionRecord
$rev, IDatabase
$dbw ) {
405 // TODO: pass in a DBTransactionContext instead of a database connection.
406 $this->checkDatabaseWikiId( $dbw );
408 $slotRoles = $rev->getSlotRoles();
410 // Make sure the main slot is always provided throughout migration
411 if ( !in_array( 'main', $slotRoles ) ) {
412 throw new InvalidArgumentException(
413 'main slot must be provided'
417 // While inserting into the old schema make sure only the main slot is allowed.
418 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) && $slotRoles !== [ 'main' ] ) {
419 throw new InvalidArgumentException(
420 'Only the main slot is supported when writing to the pre-MCR schema!'
425 $this->failOnNull( $rev->getSize(), 'size field' );
426 $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
427 $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
428 $comment = $this->failOnNull( $rev->getComment( RevisionRecord
::RAW
), 'comment' );
429 $user = $this->failOnNull( $rev->getUser( RevisionRecord
::RAW
), 'user' );
430 $this->failOnNull( $user->getId(), 'user field' );
431 $this->failOnEmpty( $user->getName(), 'user_text field' );
433 // TODO: we shouldn't need an actual Title here.
434 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
435 $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
437 $parentId = $rev->getParentId() === null
438 ?
$this->getPreviousRevisionId( $dbw, $rev )
439 : $rev->getParentId();
441 /** @var RevisionRecord $rev */
442 $rev = $dbw->doAtomicSection(
444 function ( IDatabase
$dbw, $fname ) use (
452 return $this->insertRevisionInternal(
465 Assert
::postcondition( $rev->getId() > 0, 'revision must have an ID' );
466 Assert
::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
467 Assert
::postcondition(
468 $rev->getComment( RevisionRecord
::RAW
) !== null,
469 'revision must have a comment'
471 Assert
::postcondition(
472 $rev->getUser( RevisionRecord
::RAW
) !== null,
473 'revision must have a user'
476 // Trigger exception if the main slot is missing.
477 // Technically, this could go away after MCR migration: while
478 // calling code may require a main slot to exist, RevisionStore
479 // really should not know or care about that requirement.
480 $rev->getSlot( 'main', RevisionRecord
::RAW
);
482 foreach ( $slotRoles as $role ) {
483 $slot = $rev->getSlot( $role, RevisionRecord
::RAW
);
484 Assert
::postcondition(
485 $slot->getContent() !== null,
486 $role . ' slot must have content'
488 Assert
::postcondition(
489 $slot->hasRevision(),
490 $role . ' slot must have a revision associated'
494 Hooks
::run( 'RevisionRecordInserted', [ $rev ] );
496 // TODO: deprecate in 1.32!
497 $legacyRevision = new Revision( $rev );
498 Hooks
::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
503 private function insertRevisionInternal(
507 CommentStoreComment
$comment,
512 $slotRoles = $rev->getSlotRoles();
514 $revisionRow = $this->insertRevisionRowOn(
521 $revisionId = $revisionRow['rev_id'];
524 BlobStore
::PAGE_HINT
=> $pageId,
525 BlobStore
::REVISION_HINT
=> $revisionId,
526 BlobStore
::PARENT_HINT
=> $parentId,
530 foreach ( $slotRoles as $role ) {
531 $slot = $rev->getSlot( $role, RevisionRecord
::RAW
);
533 if ( $slot->hasRevision() ) {
534 // If the SlotRecord already has a revision ID set, this means it already exists
535 // in the database, and should already belong to the current revision.
536 // TODO: properly abort transaction if the assertion fails!
538 $slot->getRevision() === $revisionId,
539 'slot role ' . $slot->getRole(),
540 'Existing slot should belong to revision '
541 . $revisionId . ', but belongs to revision ' . $slot->getRevision() . '!'
544 // Slot exists, nothing to do, move along.
545 // This happens when restoring archived revisions.
547 $newSlots[$role] = $slot;
549 // Write the main slot's text ID to the revision table for backwards compatibility
550 if ( $slot->getRole() === 'main'
551 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
)
553 $blobAddress = $slot->getAddress();
554 $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
557 $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $title, $blobHints );
561 $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
563 $rev = new RevisionStoreRecord(
567 (object)$revisionRow,
568 new RevisionSlots( $newSlots ),
576 * @param IDatabase $dbw
577 * @param int $revisionId
578 * @param string &$blobAddress (may change!)
580 private function updateRevisionTextId( IDatabase
$dbw, $revisionId, &$blobAddress ) {
581 $textId = $this->blobStore
->getTextIdFromAddress( $blobAddress );
583 throw new LogicException(
584 'Blob address not supported in 1.29 database schema: ' . $blobAddress
588 // getTextIdFromAddress() is free to insert something into the text table, so $textId
589 // may be a new value, not anything already contained in $blobAddress.
590 $blobAddress = SqlBlobStore
::makeAddressFromTextId( $textId );
594 [ 'rev_text_id' => $textId ],
595 [ 'rev_id' => $revisionId ],
601 * @param IDatabase $dbw
602 * @param int $revisionId
603 * @param SlotRecord $protoSlot
604 * @param Title $title
605 * @param array $blobHints See the BlobStore::XXX_HINT constants
608 private function insertSlotOn(
611 SlotRecord
$protoSlot,
613 array $blobHints = []
615 if ( $protoSlot->hasAddress() ) {
616 $blobAddress = $protoSlot->getAddress();
618 $blobAddress = $this->storeContentBlob( $protoSlot, $title, $blobHints );
621 // Write the main slot's text ID to the revision table for backwards compatibility
622 if ( $protoSlot->getRole() === 'main'
623 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
)
625 $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
628 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
629 if ( $protoSlot->hasContentId() ) {
630 $contentId = $protoSlot->getContentId();
632 $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
635 $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
640 $savedSlot = SlotRecord
::newSaved(
651 * Insert IP revision into ip_changes for use when querying for a range.
652 * @param IDatabase $dbw
654 * @param RevisionRecord $rev
655 * @param int $revisionId
657 private function insertIpChangesRow(
663 if ( $user->getId() === 0 && IP
::isValid( $user->getName() ) ) {
665 'ipc_rev_id' => $revisionId,
666 'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
667 'ipc_hex' => IP
::toHex( $user->getName() ),
669 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__
);
674 * @param IDatabase $dbw
675 * @param RevisionRecord $rev
676 * @param Title $title
677 * @param int $parentId
679 * @return array a revision table row
681 * @throws MWException
682 * @throws MWUnknownContentModelException
684 private function insertRevisionRowOn(
690 $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $title, $parentId );
692 list( $commentFields, $commentCallback ) =
693 $this->commentStore
->insertWithTempTable(
696 $rev->getComment( RevisionRecord
::RAW
)
698 $revisionRow +
= $commentFields;
700 list( $actorFields, $actorCallback ) =
701 $this->actorMigration
->getInsertValuesWithTempTable(
704 $rev->getUser( RevisionRecord
::RAW
)
706 $revisionRow +
= $actorFields;
708 $dbw->insert( 'revision', $revisionRow, __METHOD__
);
710 if ( !isset( $revisionRow['rev_id'] ) ) {
711 // only if auto-increment was used
712 $revisionRow['rev_id'] = intval( $dbw->insertId() );
715 $commentCallback( $revisionRow['rev_id'] );
716 $actorCallback( $revisionRow['rev_id'], $revisionRow );
722 * @param IDatabase $dbw
723 * @param RevisionRecord $rev
724 * @param Title $title
725 * @param int $parentId
727 * @return array [ 0 => array $revisionRow, 1 => callable ]
728 * @throws MWException
729 * @throws MWUnknownContentModelException
731 private function getBaseRevisionRow(
737 // Record the edit in revisions
739 'rev_page' => $rev->getPageId(),
740 'rev_parent_id' => $parentId,
741 'rev_minor_edit' => $rev->isMinor() ?
1 : 0,
742 'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
743 'rev_deleted' => $rev->getVisibility(),
744 'rev_len' => $rev->getSize(),
745 'rev_sha1' => $rev->getSha1(),
748 if ( $rev->getId() !== null ) {
749 // Needed to restore revisions with their original ID
750 $revisionRow['rev_id'] = $rev->getId();
753 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) ) {
754 // In non MCR mode this IF section will relate to the main slot
755 $mainSlot = $rev->getSlot( 'main' );
756 $model = $mainSlot->getModel();
757 $format = $mainSlot->getFormat();
759 // MCR migration note: rev_content_model and rev_content_format will go away
760 if ( $this->contentHandlerUseDB
) {
761 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
762 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
764 $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
765 $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
773 * @param SlotRecord $slot
774 * @param Title $title
775 * @param array $blobHints See the BlobStore::XXX_HINT constants
777 * @throws MWException
778 * @return string the blob address
780 private function storeContentBlob(
783 array $blobHints = []
785 $content = $slot->getContent();
786 $format = $content->getDefaultFormat();
787 $model = $content->getModel();
789 $this->checkContent( $content, $title );
791 return $this->blobStore
->storeBlob(
792 $content->serialize( $format ),
793 // These hints "leak" some information from the higher abstraction layer to
794 // low level storage to allow for optimization.
798 BlobStore
::DESIGNATION_HINT
=> 'page-content',
799 BlobStore
::ROLE_HINT
=> $slot->getRole(),
800 BlobStore
::SHA1_HINT
=> $slot->getSha1(),
801 BlobStore
::MODEL_HINT
=> $model,
802 BlobStore
::FORMAT_HINT
=> $format,
809 * @param SlotRecord $slot
810 * @param IDatabase $dbw
811 * @param int $revisionId
812 * @param int $contentId
814 private function insertSlotRowOn( SlotRecord
$slot, IDatabase
$dbw, $revisionId, $contentId ) {
816 'slot_revision_id' => $revisionId,
817 'slot_role_id' => $this->slotRoleStore
->acquireId( $slot->getRole() ),
818 'slot_content_id' => $contentId,
819 // If the slot has a specific origin use that ID, otherwise use the ID of the revision
820 // that we just inserted.
821 'slot_origin' => $slot->hasOrigin() ?
$slot->getOrigin() : $revisionId,
823 $dbw->insert( 'slots', $slotRow, __METHOD__
);
827 * @param SlotRecord $slot
828 * @param IDatabase $dbw
829 * @param string $blobAddress
830 * @return int content row ID
832 private function insertContentRowOn( SlotRecord
$slot, IDatabase
$dbw, $blobAddress ) {
834 'content_size' => $slot->getSize(),
835 'content_sha1' => $slot->getSha1(),
836 'content_model' => $this->contentModelStore
->acquireId( $slot->getModel() ),
837 'content_address' => $blobAddress,
839 $dbw->insert( 'content', $contentRow, __METHOD__
);
840 return intval( $dbw->insertId() );
844 * MCR migration note: this corresponds to Revision::checkContentModel
846 * @param Content $content
847 * @param Title $title
849 * @throws MWException
850 * @throws MWUnknownContentModelException
852 private function checkContent( Content
$content, Title
$title ) {
853 // Note: may return null for revisions that have not yet been inserted
855 $model = $content->getModel();
856 $format = $content->getDefaultFormat();
857 $handler = $content->getContentHandler();
861 if ( !$handler->isSupportedFormat( $format ) ) {
862 throw new MWException( "Can't use format $format with content model $model on $name" );
865 if ( !$this->contentHandlerUseDB
) {
866 // if $wgContentHandlerUseDB is not set,
867 // all revisions must use the default content model and format.
869 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
870 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
871 $defaultFormat = $defaultHandler->getDefaultFormat();
873 if ( $model != $defaultModel ) {
874 throw new MWException( "Can't save non-default content model with "
875 . "\$wgContentHandlerUseDB disabled: model is $model, "
876 . "default for $name is $defaultModel"
880 if ( $format != $defaultFormat ) {
881 throw new MWException( "Can't use non-default content format with "
882 . "\$wgContentHandlerUseDB disabled: format is $format, "
883 . "default for $name is $defaultFormat"
888 if ( !$content->isValid() ) {
889 throw new MWException(
890 "New content for $name is not valid! Content model is $model"
896 * Create a new null-revision for insertion into a page's
897 * history. This will not re-save the text, but simply refer
898 * to the text from the previous version.
900 * Such revisions can for instance identify page rename
901 * operations and other such meta-modifications.
903 * @note: This method grabs a FOR UPDATE lock on the relevant row of the page table,
904 * to prevent a new revision from being inserted before the null revision has been written
907 * MCR migration note: this replaces Revision::newNullRevision
909 * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
912 * @param IDatabase $dbw used for obtaining the lock on the page table row
913 * @param Title $title Title of the page to read from
914 * @param CommentStoreComment $comment RevisionRecord's summary
915 * @param bool $minor Whether the revision should be considered as minor
916 * @param User $user The user to attribute the revision to
918 * @return RevisionRecord|null RevisionRecord or null on error
920 public function newNullRevision(
923 CommentStoreComment
$comment,
927 $this->checkDatabaseWikiId( $dbw );
929 // T51581: Lock the page table row to ensure no other process
930 // is adding a revision to the page at the same time.
931 // Avoid locking extra tables, compare T191892.
932 $pageLatest = $dbw->selectField(
935 [ 'page_id' => $title->getArticleID() ],
940 if ( !$pageLatest ) {
944 // Fetch the actual revision row, without locking all extra tables.
945 $oldRevision = $this->loadRevisionFromId( $dbw, $pageLatest );
947 // Construct the new revision
948 $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
949 $newRevision = MutableRevisionRecord
::newFromParentRevision( $oldRevision );
951 $newRevision->setComment( $comment );
952 $newRevision->setUser( $user );
953 $newRevision->setTimestamp( $timestamp );
954 $newRevision->setMinorEdit( $minor );
960 * MCR migration note: this replaces Revision::isUnpatrolled
962 * @todo This is overly specific, so move or kill this method.
964 * @param RevisionRecord $rev
966 * @return int Rcid of the unpatrolled row, zero if there isn't one
968 public function getRcIdIfUnpatrolled( RevisionRecord
$rev ) {
969 $rc = $this->getRecentChange( $rev );
970 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange
::PRC_UNPATROLLED
) {
971 return $rc->getAttribute( 'rc_id' );
978 * Get the RC object belonging to the current revision, if there's one
980 * MCR migration note: this replaces Revision::getRecentChange
982 * @todo move this somewhere else?
984 * @param RevisionRecord $rev
985 * @param int $flags (optional) $flags include:
986 * IDBAccessObject::READ_LATEST: Select the data from the master
988 * @return null|RecentChange
990 public function getRecentChange( RevisionRecord
$rev, $flags = 0 ) {
991 $dbr = $this->getDBConnection( DB_REPLICA
);
993 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
995 $userIdentity = $rev->getUser( RevisionRecord
::RAW
);
997 if ( !$userIdentity ) {
998 // If the revision has no user identity, chances are it never went
999 // into the database, and doesn't have an RC entry.
1003 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
1004 $actorWhere = $this->actorMigration
->getWhere( $dbr, 'rc_user', $rev->getUser(), false );
1005 $rc = RecentChange
::newFromConds(
1007 $actorWhere['conds'],
1008 'rc_timestamp' => $dbr->timestamp( $rev->getTimestamp() ),
1009 'rc_this_oldid' => $rev->getId()
1015 $this->releaseDBConnection( $dbr );
1017 // XXX: cache this locally? Glue it to the RevisionRecord?
1022 * Maps fields of the archive row to corresponding revision rows.
1024 * @param object $archiveRow
1026 * @return object a revision row object, corresponding to $archiveRow.
1028 private static function mapArchiveFields( $archiveRow ) {
1030 // keep with ar prefix:
1033 // not the same suffix:
1034 'ar_page_id' => 'rev_page',
1035 'ar_rev_id' => 'rev_id',
1038 'ar_text_id' => 'rev_text_id',
1039 'ar_timestamp' => 'rev_timestamp',
1040 'ar_user_text' => 'rev_user_text',
1041 'ar_user' => 'rev_user',
1042 'ar_actor' => 'rev_actor',
1043 'ar_minor_edit' => 'rev_minor_edit',
1044 'ar_deleted' => 'rev_deleted',
1045 'ar_len' => 'rev_len',
1046 'ar_parent_id' => 'rev_parent_id',
1047 'ar_sha1' => 'rev_sha1',
1048 'ar_comment' => 'rev_comment',
1049 'ar_comment_cid' => 'rev_comment_cid',
1050 'ar_comment_id' => 'rev_comment_id',
1051 'ar_comment_text' => 'rev_comment_text',
1052 'ar_comment_data' => 'rev_comment_data',
1053 'ar_comment_old' => 'rev_comment_old',
1054 'ar_content_format' => 'rev_content_format',
1055 'ar_content_model' => 'rev_content_model',
1058 $revRow = new stdClass();
1059 foreach ( $fieldMap as $arKey => $revKey ) {
1060 if ( property_exists( $archiveRow, $arKey ) ) {
1061 $revRow->$revKey = $archiveRow->$arKey;
1069 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
1071 * @param object|array $row Either a database row or an array
1072 * @param int $queryFlags for callbacks
1073 * @param Title $title
1075 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
1076 * @throws MWException
1078 private function emulateMainSlot_1_29( $row, $queryFlags, Title
$title ) {
1079 $mainSlotRow = new stdClass();
1080 $mainSlotRow->role_name
= 'main';
1081 $mainSlotRow->model_name
= null;
1082 $mainSlotRow->slot_revision_id
= null;
1083 $mainSlotRow->content_address
= null;
1089 if ( is_object( $row ) ) {
1090 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
) ) {
1091 // Don't emulate from a row when using the new schema.
1092 // Emulating from an array is still OK.
1093 throw new LogicException( 'Can\'t emulate the main slot when using MCR schema.' );
1097 if ( !isset( $row->rev_id
) && ( isset( $row->ar_user
) ||
isset( $row->ar_actor
) ) ) {
1098 $row = $this->mapArchiveFields( $row );
1101 if ( isset( $row->rev_text_id
) && $row->rev_text_id
> 0 ) {
1102 $mainSlotRow->content_address
= SqlBlobStore
::makeAddressFromTextId(
1107 // This is used by null-revisions
1108 $mainSlotRow->slot_origin
= isset( $row->slot_origin
)
1109 ?
intval( $row->slot_origin
)
1112 if ( isset( $row->old_text
) ) {
1113 // this happens when the text-table gets joined directly, in the pre-1.30 schema
1114 $blobData = isset( $row->old_text
) ?
strval( $row->old_text
) : null;
1115 // Check against selects that might have not included old_flags
1116 if ( !property_exists( $row, 'old_flags' ) ) {
1117 throw new InvalidArgumentException( 'old_flags was not set in $row' );
1119 $blobFlags = ( $row->old_flags
=== null ) ?
'' : $row->old_flags
;
1122 $mainSlotRow->slot_revision_id
= intval( $row->rev_id
);
1124 $mainSlotRow->content_size
= isset( $row->rev_len
) ?
intval( $row->rev_len
) : null;
1125 $mainSlotRow->content_sha1
= isset( $row->rev_sha1
) ?
strval( $row->rev_sha1
) : null;
1126 $mainSlotRow->model_name
= isset( $row->rev_content_model
)
1127 ?
strval( $row->rev_content_model
)
1129 // XXX: in the future, we'll probably always use the default format, and drop content_format
1130 $mainSlotRow->format_name
= isset( $row->rev_content_format
)
1131 ?
strval( $row->rev_content_format
)
1133 } elseif ( is_array( $row ) ) {
1134 $mainSlotRow->slot_revision_id
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
1136 $mainSlotRow->slot_origin
= isset( $row['slot_origin'] )
1137 ?
intval( $row['slot_origin'] )
1139 $mainSlotRow->content_address
= isset( $row['text_id'] )
1140 ? SqlBlobStore
::makeAddressFromTextId( intval( $row['text_id'] ) )
1142 $mainSlotRow->content_size
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
1143 $mainSlotRow->content_sha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
1145 $mainSlotRow->model_name
= isset( $row['content_model'] )
1146 ?
strval( $row['content_model'] ) : null; // XXX: must be a string!
1147 // XXX: in the future, we'll probably always use the default format, and drop content_format
1148 $mainSlotRow->format_name
= isset( $row['content_format'] )
1149 ?
strval( $row['content_format'] ) : null;
1150 $blobData = isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
1151 // XXX: If the flags field is not set then $blobFlags should be null so that no
1152 // decoding will happen. An empty string will result in default decodings.
1153 $blobFlags = isset( $row['flags'] ) ?
trim( strval( $row['flags'] ) ) : null;
1155 // if we have a Content object, override mText and mContentModel
1156 if ( !empty( $row['content'] ) ) {
1157 if ( !( $row['content'] instanceof Content
) ) {
1158 throw new MWException( 'content field must contain a Content object.' );
1161 /** @var Content $content */
1162 $content = $row['content'];
1163 $handler = $content->getContentHandler();
1165 $mainSlotRow->model_name
= $content->getModel();
1167 // XXX: in the future, we'll probably always use the default format.
1168 if ( $mainSlotRow->format_name
=== null ) {
1169 $mainSlotRow->format_name
= $handler->getDefaultFormat();
1173 throw new MWException( 'Revision constructor passed invalid row format.' );
1176 // With the old schema, the content changes with every revision,
1177 // except for null-revisions.
1178 if ( !isset( $mainSlotRow->slot_origin
) ) {
1179 $mainSlotRow->slot_origin
= $mainSlotRow->slot_revision_id
;
1182 if ( $mainSlotRow->model_name
=== null ) {
1183 $mainSlotRow->model_name
= function ( SlotRecord
$slot ) use ( $title ) {
1184 // TODO: MCR: consider slot role in getDefaultModelFor()! Use LinkTarget!
1185 // TODO: MCR: deprecate $title->getModel().
1186 return ContentHandler
::getDefaultModelFor( $title );
1191 // XXX: We should perhaps fail if $blobData is null and $mainSlotRow->content_address
1192 // is missing, but "empty revisions" with no content are used in some edge cases.
1194 $content = function ( SlotRecord
$slot )
1195 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
1197 return $this->loadSlotContent(
1201 $mainSlotRow->format_name
,
1207 // NOTE: this callback will be looped through RevisionSlot::newInherited(), allowing
1208 // the inherited slot to have the same content_id as the original slot. In that case,
1209 // $slot will be the inherited slot, while $mainSlotRow still refers to the original slot.
1210 $mainSlotRow->slot_content_id
=
1211 function ( SlotRecord
$slot ) use ( $queryFlags, $mainSlotRow ) {
1212 list( $dbMode, ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
1213 $db = $this->getDBConnectionRef( $dbMode );
1214 return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id
, 'main' );
1217 return new SlotRecord( $mainSlotRow, $content );
1221 * Loads a Content object based on a slot row.
1223 * This method does not call $slot->getContent(), and may be used as a callback
1224 * called by $slot->getContent().
1226 * MCR migration note: this roughly corresponds to Revision::getContentInternal
1228 * @param SlotRecord $slot The SlotRecord to load content for
1229 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
1230 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
1231 * Use null if no processing should happen. That is in constrast to the empty string,
1232 * which causes the blob to be decoded according to the configured legacy encoding.
1233 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
1234 * @param int $queryFlags
1236 * @throws RevisionAccessException
1239 private function loadSlotContent(
1246 if ( $blobData !== null ) {
1247 Assert
::parameterType( 'string', $blobData, '$blobData' );
1248 Assert
::parameterType( 'string|null', $blobFlags, '$blobFlags' );
1250 $cacheKey = $slot->hasAddress() ?
$slot->getAddress() : null;
1252 if ( $blobFlags === null ) {
1253 // No blob flags, so use the blob verbatim.
1256 $data = $this->blobStore
->expandBlob( $blobData, $blobFlags, $cacheKey );
1257 if ( $data === false ) {
1258 throw new RevisionAccessException(
1259 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
1265 $address = $slot->getAddress();
1267 $data = $this->blobStore
->getBlob( $address, $queryFlags );
1268 } catch ( BlobAccessException
$e ) {
1269 throw new RevisionAccessException(
1270 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
1275 // Unserialize content
1276 $handler = ContentHandler
::getForModelID( $slot->getModel() );
1278 $content = $handler->unserializeContent( $data, $blobFormat );
1283 * Load a page revision from a given revision ID number.
1284 * Returns null if no such revision can be found.
1286 * MCR migration note: this replaces Revision::newFromId
1289 * IDBAccessObject::READ_LATEST: Select the data from the master
1290 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1293 * @param int $flags (optional)
1294 * @return RevisionRecord|null
1296 public function getRevisionById( $id, $flags = 0 ) {
1297 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
1301 * Load either the current, or a specified, revision
1302 * that's attached to a given link target. If not attached
1303 * to that link target, will return null.
1305 * MCR migration note: this replaces Revision::newFromTitle
1308 * IDBAccessObject::READ_LATEST: Select the data from the master
1309 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1311 * @param LinkTarget $linkTarget
1312 * @param int $revId (optional)
1313 * @param int $flags Bitfield (optional)
1314 * @return RevisionRecord|null
1316 public function getRevisionByTitle( LinkTarget
$linkTarget, $revId = 0, $flags = 0 ) {
1318 'page_namespace' => $linkTarget->getNamespace(),
1319 'page_title' => $linkTarget->getDBkey()
1322 // Use the specified revision ID.
1323 // Note that we use newRevisionFromConds here because we want to retry
1324 // and fall back to master if the page is not found on a replica.
1325 // Since the caller supplied a revision ID, we are pretty sure the revision is
1326 // supposed to exist, so we should try hard to find it.
1327 $conds['rev_id'] = $revId;
1328 return $this->newRevisionFromConds( $conds, $flags );
1330 // Use a join to get the latest revision.
1331 // Note that we don't use newRevisionFromConds here because we don't want to retry
1332 // and fall back to master. The assumption is that we only want to force the fallback
1333 // if we are quite sure the revision exists because the caller supplied a revision ID.
1334 // If the page isn't found at all on a replica, it probably simply does not exist.
1335 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1337 $conds[] = 'rev_id=page_latest';
1338 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1340 $this->releaseDBConnection( $db );
1346 * Load either the current, or a specified, revision
1347 * that's attached to a given page ID.
1348 * Returns null if no such revision can be found.
1350 * MCR migration note: this replaces Revision::newFromPageId
1353 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
1354 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1356 * @param int $pageId
1357 * @param int $revId (optional)
1358 * @param int $flags Bitfield (optional)
1359 * @return RevisionRecord|null
1361 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
1362 $conds = [ 'page_id' => $pageId ];
1364 // Use the specified revision ID.
1365 // Note that we use newRevisionFromConds here because we want to retry
1366 // and fall back to master if the page is not found on a replica.
1367 // Since the caller supplied a revision ID, we are pretty sure the revision is
1368 // supposed to exist, so we should try hard to find it.
1369 $conds['rev_id'] = $revId;
1370 return $this->newRevisionFromConds( $conds, $flags );
1372 // Use a join to get the latest revision.
1373 // Note that we don't use newRevisionFromConds here because we don't want to retry
1374 // and fall back to master. The assumption is that we only want to force the fallback
1375 // if we are quite sure the revision exists because the caller supplied a revision ID.
1376 // If the page isn't found at all on a replica, it probably simply does not exist.
1377 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1379 $conds[] = 'rev_id=page_latest';
1380 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1382 $this->releaseDBConnection( $db );
1388 * Load the revision for the given title with the given timestamp.
1389 * WARNING: Timestamps may in some circumstances not be unique,
1390 * so this isn't the best key to use.
1392 * MCR migration note: this replaces Revision::loadFromTimestamp
1394 * @param Title $title
1395 * @param string $timestamp
1396 * @return RevisionRecord|null
1398 public function getRevisionByTimestamp( $title, $timestamp ) {
1399 $db = $this->getDBConnection( DB_REPLICA
);
1400 return $this->newRevisionFromConds(
1402 'rev_timestamp' => $db->timestamp( $timestamp ),
1403 'page_namespace' => $title->getNamespace(),
1404 'page_title' => $title->getDBkey()
1412 * @param int $revId The revision to load slots for.
1413 * @param int $queryFlags
1415 * @return SlotRecord[]
1417 private function loadSlotRecords( $revId, $queryFlags ) {
1418 $revQuery = self
::getSlotsQueryInfo( [ 'content' ] );
1420 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
1421 $db = $this->getDBConnectionRef( $dbMode );
1424 $revQuery['tables'],
1425 $revQuery['fields'],
1427 'slot_revision_id' => $revId,
1436 foreach ( $res as $row ) {
1437 $contentCallback = function ( SlotRecord
$slot ) use ( $queryFlags, $row ) {
1438 return $this->loadSlotContent( $slot, null, null, null, $queryFlags );
1441 $slots[$row->role_name
] = new SlotRecord( $row, $contentCallback );
1444 if ( !isset( $slots['main'] ) ) {
1445 throw new RevisionAccessException(
1446 'Main slot of revision ' . $revId . ' not found in database!'
1454 * Factory method for RevisionSlots.
1456 * @note If other code has a need to construct RevisionSlots objects, this should be made
1457 * public, since RevisionSlots instances should not be constructed directly.
1460 * @param object $revisionRow
1461 * @param int $queryFlags
1462 * @param Title $title
1464 * @return RevisionSlots
1465 * @throws MWException
1467 private function newRevisionSlots(
1473 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW
) ) {
1474 $mainSlot = $this->emulateMainSlot_1_29( $revisionRow, $queryFlags, $title );
1475 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1477 // XXX: do we need the same kind of caching here
1478 // that getKnownCurrentRevision uses (if $revId == page_latest?)
1480 $slots = new RevisionSlots( function () use( $revId, $queryFlags ) {
1481 return $this->loadSlotRecords( $revId, $queryFlags );
1489 * Make a fake revision object from an archive table row. This is queried
1490 * for permissions or even inserted (as in Special:Undelete)
1492 * MCR migration note: this replaces Revision::newFromArchiveRow
1494 * @param object $row
1495 * @param int $queryFlags
1496 * @param Title|null $title
1497 * @param array $overrides associative array with fields of $row to override. This may be
1498 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1499 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1500 * override ar_parent_id.
1502 * @return RevisionRecord
1503 * @throws MWException
1505 public function newRevisionFromArchiveRow(
1508 Title
$title = null,
1509 array $overrides = []
1511 Assert
::parameterType( 'object', $row, '$row' );
1513 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1514 Assert
::parameterType( 'integer', $queryFlags, '$queryFlags' );
1516 if ( !$title && isset( $overrides['title'] ) ) {
1517 if ( !( $overrides['title'] instanceof Title
) ) {
1518 throw new MWException( 'title field override must contain a Title object.' );
1521 $title = $overrides['title'];
1524 if ( !isset( $title ) ) {
1525 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
1526 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
1528 throw new InvalidArgumentException(
1529 'A Title or ar_namespace and ar_title must be given'
1534 foreach ( $overrides as $key => $value ) {
1536 $row->$field = $value;
1540 $user = User
::newFromAnyId(
1541 $row->ar_user ??
null,
1542 $row->ar_user_text ??
null,
1543 $row->ar_actor ??
null
1545 } catch ( InvalidArgumentException
$ex ) {
1546 wfWarn( __METHOD__
. ': ' . $ex->getMessage() );
1547 $user = new UserIdentityValue( 0, '', 0 );
1550 $comment = $this->commentStore
1551 // Legacy because $row may have come from self::selectFields()
1552 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA
), 'ar_comment', $row, true );
1554 $slots = $this->newRevisionSlots( $row->ar_rev_id
, $row, $queryFlags, $title );
1556 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1560 * @see RevisionFactory::newRevisionFromRow
1562 * MCR migration note: this replaces Revision::newFromRow
1564 * @param object $row
1565 * @param int $queryFlags
1566 * @param Title|null $title
1568 * @return RevisionRecord
1570 public function newRevisionFromRow( $row, $queryFlags = 0, Title
$title = null ) {
1571 Assert
::parameterType( 'object', $row, '$row' );
1574 $pageId = $row->rev_page ??
0; // XXX: also check page_id?
1575 $revId = $row->rev_id ??
0;
1577 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1580 if ( !isset( $row->page_latest
) ) {
1581 $row->page_latest
= $title->getLatestRevID();
1582 if ( $row->page_latest
=== 0 && $title->exists() ) {
1583 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1588 $user = User
::newFromAnyId(
1589 $row->rev_user ??
null,
1590 $row->rev_user_text ??
null,
1591 $row->rev_actor ??
null
1593 } catch ( InvalidArgumentException
$ex ) {
1594 wfWarn( __METHOD__
. ': ' . $ex->getMessage() );
1595 $user = new UserIdentityValue( 0, '', 0 );
1598 $comment = $this->commentStore
1599 // Legacy because $row may have come from self::selectFields()
1600 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA
), 'rev_comment', $row, true );
1602 $slots = $this->newRevisionSlots( $row->rev_id
, $row, $queryFlags, $title );
1604 return new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1608 * Constructs a new MutableRevisionRecord based on the given associative array following
1609 * the MW1.29 convention for the Revision constructor.
1611 * MCR migration note: this replaces Revision::newFromRow
1613 * @param array $fields
1614 * @param int $queryFlags
1615 * @param Title|null $title
1617 * @return MutableRevisionRecord
1618 * @throws MWException
1619 * @throws RevisionAccessException
1621 public function newMutableRevisionFromArray(
1626 if ( !$title && isset( $fields['title'] ) ) {
1627 if ( !( $fields['title'] instanceof Title
) ) {
1628 throw new MWException( 'title field must contain a Title object.' );
1631 $title = $fields['title'];
1635 $pageId = $fields['page'] ??
0;
1636 $revId = $fields['id'] ??
0;
1638 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1641 if ( !isset( $fields['page'] ) ) {
1642 $fields['page'] = $title->getArticleID( $queryFlags );
1645 // if we have a content object, use it to set the model and type
1646 if ( !empty( $fields['content'] ) ) {
1647 if ( !( $fields['content'] instanceof Content
) && !is_array( $fields['content'] ) ) {
1648 throw new MWException(
1649 'content field must contain a Content object or an array of Content objects.'
1654 if ( !empty( $fields['text_id'] ) ) {
1655 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
1656 throw new MWException( "The text_id field is only available in the pre-MCR schema" );
1659 if ( !empty( $fields['content'] ) ) {
1660 throw new MWException(
1661 "Text already stored in external store (id {$fields['text_id']}), " .
1662 "can't specify content object"
1668 isset( $fields['comment'] )
1669 && !( $fields['comment'] instanceof CommentStoreComment
)
1671 $commentData = $fields['comment_data'] ??
null;
1673 if ( $fields['comment'] instanceof Message
) {
1674 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1679 $commentText = trim( strval( $fields['comment'] ) );
1680 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1687 $revision = new MutableRevisionRecord( $title, $this->wikiId
);
1688 $this->initializeMutableRevisionFromArray( $revision, $fields );
1690 if ( isset( $fields['content'] ) && is_array( $fields['content'] ) ) {
1691 foreach ( $fields['content'] as $role => $content ) {
1692 $revision->setContent( $role, $content );
1695 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1696 $revision->setSlot( $mainSlot );
1703 * @param MutableRevisionRecord $record
1704 * @param array $fields
1706 private function initializeMutableRevisionFromArray(
1707 MutableRevisionRecord
$record,
1710 /** @var UserIdentity $user */
1713 if ( isset( $fields['user'] ) && ( $fields['user'] instanceof UserIdentity
) ) {
1714 $user = $fields['user'];
1717 $user = User
::newFromAnyId(
1718 $fields['user'] ??
null,
1719 $fields['user_text'] ??
null,
1720 $fields['actor'] ??
null
1722 } catch ( InvalidArgumentException
$ex ) {
1728 $record->setUser( $user );
1731 $timestamp = isset( $fields['timestamp'] )
1732 ?
strval( $fields['timestamp'] )
1733 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1735 $record->setTimestamp( $timestamp );
1737 if ( isset( $fields['page'] ) ) {
1738 $record->setPageId( intval( $fields['page'] ) );
1741 if ( isset( $fields['id'] ) ) {
1742 $record->setId( intval( $fields['id'] ) );
1744 if ( isset( $fields['parent_id'] ) ) {
1745 $record->setParentId( intval( $fields['parent_id'] ) );
1748 if ( isset( $fields['sha1'] ) ) {
1749 $record->setSha1( $fields['sha1'] );
1751 if ( isset( $fields['size'] ) ) {
1752 $record->setSize( intval( $fields['size'] ) );
1755 if ( isset( $fields['minor_edit'] ) ) {
1756 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
1758 if ( isset( $fields['deleted'] ) ) {
1759 $record->setVisibility( intval( $fields['deleted'] ) );
1762 if ( isset( $fields['comment'] ) ) {
1763 Assert
::parameterType(
1764 CommentStoreComment
::class,
1768 $record->setComment( $fields['comment'] );
1773 * Load a page revision from a given revision ID number.
1774 * Returns null if no such revision can be found.
1776 * MCR migration note: this corresponds to Revision::loadFromId
1778 * @note direct use is deprecated!
1779 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
1781 * @param IDatabase $db
1784 * @return RevisionRecord|null
1786 public function loadRevisionFromId( IDatabase
$db, $id ) {
1787 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
1791 * Load either the current, or a specified, revision
1792 * that's attached to a given page. If not attached
1793 * to that page, will return null.
1795 * MCR migration note: this replaces Revision::loadFromPageId
1797 * @note direct use is deprecated!
1798 * @todo remove when unused!
1800 * @param IDatabase $db
1801 * @param int $pageid
1803 * @return RevisionRecord|null
1805 public function loadRevisionFromPageId( IDatabase
$db, $pageid, $id = 0 ) {
1806 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
1808 $conds['rev_id'] = intval( $id );
1810 $conds[] = 'rev_id=page_latest';
1812 return $this->loadRevisionFromConds( $db, $conds );
1816 * Load either the current, or a specified, revision
1817 * that's attached to a given page. If not attached
1818 * to that page, will return null.
1820 * MCR migration note: this replaces Revision::loadFromTitle
1822 * @note direct use is deprecated!
1823 * @todo remove when unused!
1825 * @param IDatabase $db
1826 * @param Title $title
1829 * @return RevisionRecord|null
1831 public function loadRevisionFromTitle( IDatabase
$db, $title, $id = 0 ) {
1833 $matchId = intval( $id );
1835 $matchId = 'page_latest';
1838 return $this->loadRevisionFromConds(
1842 'page_namespace' => $title->getNamespace(),
1843 'page_title' => $title->getDBkey()
1851 * Load the revision for the given title with the given timestamp.
1852 * WARNING: Timestamps may in some circumstances not be unique,
1853 * so this isn't the best key to use.
1855 * MCR migration note: this replaces Revision::loadFromTimestamp
1857 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
1858 * @todo remove when unused!
1860 * @param IDatabase $db
1861 * @param Title $title
1862 * @param string $timestamp
1863 * @return RevisionRecord|null
1865 public function loadRevisionFromTimestamp( IDatabase
$db, $title, $timestamp ) {
1866 return $this->loadRevisionFromConds( $db,
1868 'rev_timestamp' => $db->timestamp( $timestamp ),
1869 'page_namespace' => $title->getNamespace(),
1870 'page_title' => $title->getDBkey()
1878 * Given a set of conditions, fetch a revision
1880 * This method should be used if we are pretty sure the revision exists.
1881 * Unless $flags has READ_LATEST set, this method will first try to find the revision
1882 * on a replica before hitting the master database.
1884 * MCR migration note: this corresponds to Revision::newFromConds
1886 * @param array $conditions
1887 * @param int $flags (optional)
1888 * @param Title $title
1890 * @return RevisionRecord|null
1892 private function newRevisionFromConds( $conditions, $flags = 0, Title
$title = null ) {
1893 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1894 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1895 $this->releaseDBConnection( $db );
1897 $lb = $this->getDBLoadBalancer();
1899 // Make sure new pending/committed revision are visibile later on
1900 // within web requests to certain avoid bugs like T93866 and T94407.
1902 && !( $flags & self
::READ_LATEST
)
1903 && $lb->getServerCount() > 1
1904 && $lb->hasOrMadeRecentMasterChanges()
1906 $flags = self
::READ_LATEST
;
1907 $db = $this->getDBConnection( DB_MASTER
);
1908 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1909 $this->releaseDBConnection( $db );
1916 * Given a set of conditions, fetch a revision from
1917 * the given database connection.
1919 * MCR migration note: this corresponds to Revision::loadFromConds
1921 * @param IDatabase $db
1922 * @param array $conditions
1923 * @param int $flags (optional)
1924 * @param Title $title
1926 * @return RevisionRecord|null
1928 private function loadRevisionFromConds(
1934 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
1936 $rev = $this->newRevisionFromRow( $row, $flags, $title );
1945 * Throws an exception if the given database connection does not belong to the wiki this
1946 * RevisionStore is bound to.
1948 * @param IDatabase $db
1949 * @throws MWException
1951 private function checkDatabaseWikiId( IDatabase
$db ) {
1952 $storeWiki = $this->wikiId
;
1953 $dbWiki = $db->getDomainID();
1955 if ( $dbWiki === $storeWiki ) {
1959 // XXX: we really want the default database ID...
1960 $storeWiki = $storeWiki ?
: wfWikiID();
1961 $dbWiki = $dbWiki ?
: wfWikiID();
1963 if ( $dbWiki === $storeWiki ) {
1967 // HACK: counteract encoding imposed by DatabaseDomain
1968 $storeWiki = str_replace( '?h', '-', $storeWiki );
1969 $dbWiki = str_replace( '?h', '-', $dbWiki );
1971 if ( $dbWiki === $storeWiki ) {
1975 throw new MWException( "RevisionStore for $storeWiki "
1976 . "cannot be used with a DB connection for $dbWiki" );
1980 * Given a set of conditions, return a row with the
1981 * fields necessary to build RevisionRecord objects.
1983 * MCR migration note: this corresponds to Revision::fetchFromConds
1985 * @param IDatabase $db
1986 * @param array $conditions
1987 * @param int $flags (optional)
1989 * @return object|false data row as a raw object
1991 private function fetchRevisionRowFromConds( IDatabase
$db, $conditions, $flags = 0 ) {
1992 $this->checkDatabaseWikiId( $db );
1994 $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
1996 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
1997 $options[] = 'FOR UPDATE';
1999 return $db->selectRow(
2000 $revQuery['tables'],
2001 $revQuery['fields'],
2010 * Finds the ID of a content row for a given revision and slot role.
2011 * This can be used to re-use content rows even while the content ID
2012 * is still missing from SlotRecords, when writing to both the old and
2013 * the new schema during MCR schema migration.
2015 * @todo remove after MCR schema migration is complete.
2017 * @param IDatabase $db
2019 * @param string $role
2023 private function findSlotContentId( IDatabase
$db, $revId, $role ) {
2024 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW
) ) {
2029 $roleId = $this->slotRoleStore
->getId( $role );
2031 'slot_revision_id' => $revId,
2032 'slot_role_id' => $roleId,
2035 $contentId = $db->selectField( 'slots', 'slot_content_id', $conditions, __METHOD__
);
2037 return $contentId ?
: null;
2038 } catch ( NameTableAccessException
$ex ) {
2039 // If the role is missing from the slot_roles table,
2040 // the corresponding row in slots cannot exist.
2046 * Return the tables, fields, and join conditions to be selected to create
2047 * a new RevisionStoreRecord object.
2049 * MCR migration note: this replaces Revision::getQueryInfo
2051 * If the format of fields returned changes in any way then the cache key provided by
2052 * self::getRevisionRowCacheKey should be updated.
2056 * @param array $options Any combination of the following strings
2057 * - 'page': Join with the page table, and select fields to identify the page
2058 * - 'user': Join with the user table, and select the user name
2059 * - 'text': Join with the text table, and select fields to load page text. This
2060 * option is deprecated in MW 1.32 when the MCR migration flag SCHEMA_COMPAT_WRITE_NEW
2061 * is set, and disallowed when SCHEMA_COMPAT_READ_OLD is not set.
2063 * @return array With three keys:
2064 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2065 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2066 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2068 public function getQueryInfo( $options = [] ) {
2075 $ret['tables'][] = 'revision';
2076 $ret['fields'] = array_merge( $ret['fields'], [
2087 $commentQuery = $this->commentStore
->getJoin( 'rev_comment' );
2088 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
2089 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
2090 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
2092 $actorQuery = $this->actorMigration
->getJoin( 'rev_user' );
2093 $ret['tables'] = array_merge( $ret['tables'], $actorQuery['tables'] );
2094 $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
2095 $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
2097 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2098 $ret['fields'][] = 'rev_text_id';
2100 if ( $this->contentHandlerUseDB
) {
2101 $ret['fields'][] = 'rev_content_format';
2102 $ret['fields'][] = 'rev_content_model';
2106 if ( in_array( 'page', $options, true ) ) {
2107 $ret['tables'][] = 'page';
2108 $ret['fields'] = array_merge( $ret['fields'], [
2116 $ret['joins']['page'] = [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
2119 if ( in_array( 'user', $options, true ) ) {
2120 $ret['tables'][] = 'user';
2121 $ret['fields'] = array_merge( $ret['fields'], [
2124 $u = $actorQuery['fields']['rev_user'];
2125 $ret['joins']['user'] = [ 'LEFT JOIN', [ "$u != 0", "user_id = $u" ] ];
2128 if ( in_array( 'text', $options, true ) ) {
2129 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD
) ) {
2130 throw new InvalidArgumentException( 'text table can no longer be joined directly' );
2131 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2132 // NOTE: even when this class is set to not read from the old schema, callers
2133 // should still be able to join against the text table, as long as we are still
2134 // writing the old schema for compatibility.
2135 wfDeprecated( __METHOD__
. ' with `text` option', '1.32' );
2138 $ret['tables'][] = 'text';
2139 $ret['fields'] = array_merge( $ret['fields'], [
2143 $ret['joins']['text'] = [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ];
2150 * Return the tables, fields, and join conditions to be selected to create
2155 * @param array $options Any combination of the following strings
2156 * - 'content': Join with the content table, and select content meta-data fields
2158 * @return array With three keys:
2159 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2160 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2161 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2163 public function getSlotsQueryInfo( $options = [] ) {
2170 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2171 $db = $this->getDBConnectionRef( DB_REPLICA
);
2172 $ret['tables']['slots'] = 'revision';
2174 $ret['fields']['slot_revision_id'] = 'slots.rev_id';
2175 $ret['fields']['slot_content_id'] = 'NULL';
2176 $ret['fields']['slot_origin'] = 'slots.rev_id';
2177 $ret['fields']['role_name'] = $db->addQuotes( 'main' );
2179 if ( in_array( 'content', $options, true ) ) {
2180 $ret['fields']['content_size'] = 'slots.rev_len';
2181 $ret['fields']['content_sha1'] = 'slots.rev_sha1';
2182 $ret['fields']['content_address']
2183 = $db->buildConcat( [ $db->addQuotes( 'tt:' ), 'slots.rev_text_id' ] );
2185 if ( $this->contentHandlerUseDB
) {
2186 $ret['fields']['model_name'] = 'slots.rev_content_model';
2188 $ret['fields']['model_name'] = 'NULL';
2192 $ret['tables'][] = 'slots';
2193 $ret['tables'][] = 'slot_roles';
2194 $ret['fields'] = array_merge( $ret['fields'], [
2200 $ret['joins']['slot_roles'] = [ 'INNER JOIN', [ 'slot_role_id = role_id' ] ];
2202 if ( in_array( 'content', $options, true ) ) {
2203 $ret['tables'][] = 'content';
2204 $ret['tables'][] = 'content_models';
2205 $ret['fields'] = array_merge( $ret['fields'], [
2211 $ret['joins']['content'] = [ 'INNER JOIN', [ 'slot_content_id = content_id' ] ];
2212 $ret['joins']['content_models'] = [ 'INNER JOIN', [ 'content_model = model_id' ] ];
2220 * Return the tables, fields, and join conditions to be selected to create
2221 * a new RevisionArchiveRecord object.
2223 * MCR migration note: this replaces Revision::getArchiveQueryInfo
2227 * @return array With three keys:
2228 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2229 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2230 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2232 public function getArchiveQueryInfo() {
2233 $commentQuery = $this->commentStore
->getJoin( 'ar_comment' );
2234 $actorQuery = $this->actorMigration
->getJoin( 'ar_user' );
2236 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
2249 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
2250 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
2253 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD
) ) {
2254 $ret['fields'][] = 'ar_text_id';
2256 if ( $this->contentHandlerUseDB
) {
2257 $ret['fields'][] = 'ar_content_format';
2258 $ret['fields'][] = 'ar_content_model';
2266 * Do a batched query for the sizes of a set of revisions.
2268 * MCR migration note: this replaces Revision::getParentLengths
2270 * @param int[] $revIds
2271 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2272 * of the corresponding revision.
2274 public function getRevisionSizes( array $revIds ) {
2275 return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA
), $revIds );
2279 * Do a batched query for the sizes of a set of revisions.
2281 * MCR migration note: this replaces Revision::getParentLengths
2283 * @deprecated use RevisionStore::getRevisionSizes instead.
2285 * @param IDatabase $db
2286 * @param int[] $revIds
2287 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2288 * of the corresponding revision.
2290 public function listRevisionSizes( IDatabase
$db, array $revIds ) {
2291 $this->checkDatabaseWikiId( $db );
2295 return $revLens; // empty
2300 [ 'rev_id', 'rev_len' ],
2301 [ 'rev_id' => $revIds ],
2305 foreach ( $res as $row ) {
2306 $revLens[$row->rev_id
] = intval( $row->rev_len
);
2313 * Get previous revision for this title
2315 * MCR migration note: this replaces Revision::getPrevious
2317 * @param RevisionRecord $rev
2318 * @param Title $title if known (optional)
2320 * @return RevisionRecord|null
2322 public function getPreviousRevision( RevisionRecord
$rev, Title
$title = null ) {
2323 if ( $title === null ) {
2324 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
2326 $prev = $title->getPreviousRevisionID( $rev->getId() );
2328 return $this->getRevisionByTitle( $title, $prev );
2334 * Get next revision for this title
2336 * MCR migration note: this replaces Revision::getNext
2338 * @param RevisionRecord $rev
2339 * @param Title $title if known (optional)
2341 * @return RevisionRecord|null
2343 public function getNextRevision( RevisionRecord
$rev, Title
$title = null ) {
2344 if ( $title === null ) {
2345 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
2347 $next = $title->getNextRevisionID( $rev->getId() );
2349 return $this->getRevisionByTitle( $title, $next );
2355 * Get previous revision Id for this page_id
2356 * This is used to populate rev_parent_id on save
2358 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
2360 * @param IDatabase $db
2361 * @param RevisionRecord $rev
2365 private function getPreviousRevisionId( IDatabase
$db, RevisionRecord
$rev ) {
2366 $this->checkDatabaseWikiId( $db );
2368 if ( $rev->getPageId() === null ) {
2371 # Use page_latest if ID is not given
2372 if ( !$rev->getId() ) {
2373 $prevId = $db->selectField(
2374 'page', 'page_latest',
2375 [ 'page_id' => $rev->getPageId() ],
2379 $prevId = $db->selectField(
2380 'revision', 'rev_id',
2381 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
2383 [ 'ORDER BY' => 'rev_id DESC' ]
2386 return intval( $prevId );
2390 * Get rev_timestamp from rev_id, without loading the rest of the row
2392 * MCR migration note: this replaces Revision::getTimestampFromId
2394 * @param Title $title
2397 * @return string|bool False if not found
2399 public function getTimestampFromId( $title, $id, $flags = 0 ) {
2400 $db = $this->getDBConnection(
2401 ( $flags & IDBAccessObject
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
2404 $conds = [ 'rev_id' => $id ];
2405 $conds['rev_page'] = $title->getArticleID();
2406 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
2408 $this->releaseDBConnection( $db );
2409 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
2413 * Get count of revisions per page...not very efficient
2415 * MCR migration note: this replaces Revision::countByPageId
2417 * @param IDatabase $db
2418 * @param int $id Page id
2421 public function countRevisionsByPageId( IDatabase
$db, $id ) {
2422 $this->checkDatabaseWikiId( $db );
2424 $row = $db->selectRow( 'revision',
2425 [ 'revCount' => 'COUNT(*)' ],
2426 [ 'rev_page' => $id ],
2430 return intval( $row->revCount
);
2436 * Get count of revisions per page...not very efficient
2438 * MCR migration note: this replaces Revision::countByTitle
2440 * @param IDatabase $db
2441 * @param Title $title
2444 public function countRevisionsByTitle( IDatabase
$db, $title ) {
2445 $id = $title->getArticleID();
2447 return $this->countRevisionsByPageId( $db, $id );
2453 * Check if no edits were made by other users since
2454 * the time a user started editing the page. Limit to
2455 * 50 revisions for the sake of performance.
2457 * MCR migration note: this replaces Revision::userWasLastToEdit
2459 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
2460 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
2461 * has been deprecated since 1.24.
2463 * @param IDatabase $db The Database to perform the check on.
2464 * @param int $pageId The ID of the page in question
2465 * @param int $userId The ID of the user in question
2466 * @param string $since Look at edits since this time
2468 * @return bool True if the given user was the only one to edit since the given timestamp
2470 public function userWasLastToEdit( IDatabase
$db, $pageId, $userId, $since ) {
2471 $this->checkDatabaseWikiId( $db );
2477 $revQuery = $this->getQueryInfo();
2479 $revQuery['tables'],
2481 'rev_user' => $revQuery['fields']['rev_user'],
2484 'rev_page' => $pageId,
2485 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
2488 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
2491 foreach ( $res as $row ) {
2492 if ( $row->rev_user
!= $userId ) {
2500 * Load a revision based on a known page ID and current revision ID from the DB
2502 * This method allows for the use of caching, though accessing anything that normally
2503 * requires permission checks (aside from the text) will trigger a small DB lookup.
2505 * MCR migration note: this replaces Revision::newKnownCurrent
2507 * @param Title $title the associated page title
2508 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
2510 * @return RevisionRecord|bool Returns false if missing
2512 public function getKnownCurrentRevision( Title
$title, $revId ) {
2513 $db = $this->getDBConnectionRef( DB_REPLICA
);
2515 $pageId = $title->getArticleID();
2522 $revId = $title->getLatestRevID();
2527 'No latest revision known for page ' . $title->getPrefixedDBkey()
2528 . ' even though it exists with page ID ' . $pageId
2533 $row = $this->cache
->getWithSetCallback(
2534 // Page/rev IDs passed in from DB to reflect history merges
2535 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2536 WANObjectCache
::TTL_WEEK
,
2537 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
2538 $setOpts +
= Database
::getCacheSetOptions( $db );
2541 'rev_page' => intval( $pageId ),
2542 'page_id' => intval( $pageId ),
2543 'rev_id' => intval( $revId ),
2546 $row = $this->fetchRevisionRowFromConds( $db, $conds );
2547 return $row ?
: false; // don't cache negatives
2551 // Reflect revision deletion and user renames
2553 return $this->newRevisionFromRow( $row, 0, $title );
2560 * Get a cache key for use with a row as selected with getQueryInfo( [ 'page', 'user' ] )
2561 * Caching rows without 'page' or 'user' could lead to issues.
2562 * If the format of the rows returned by the query provided by getQueryInfo changes the
2563 * cache key should be updated to avoid conflicts.
2565 * @param IDatabase $db
2566 * @param int $pageId
2570 private function getRevisionRowCacheKey( IDatabase
$db, $pageId, $revId ) {
2571 return $this->cache
->makeGlobalKey(
2572 self
::ROW_CACHE_KEY
,
2579 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.