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';
87 private $contentHandlerUseDB = true;
92 private $loadBalancer;
102 private $commentStore;
105 * @var ActorMigration
107 private $actorMigration;
110 * @var LoggerInterface
115 * @var NameTableStore
117 private $contentModelStore;
120 * @var NameTableStore
122 private $slotRoleStore;
124 /** @var int One of the MIGRATION_* constants */
125 private $mcrMigrationStage;
128 * @todo $blobStore should be allowed to be any BlobStore!
130 * @param LoadBalancer $loadBalancer
131 * @param SqlBlobStore $blobStore
132 * @param WANObjectCache $cache
133 * @param CommentStore $commentStore
134 * @param NameTableStore $contentModelStore
135 * @param NameTableStore $slotRoleStore
136 * @param int $migrationStage
137 * @param ActorMigration $actorMigration
138 * @param bool|string $wikiId
140 public function __construct(
141 LoadBalancer
$loadBalancer,
142 SqlBlobStore
$blobStore,
143 WANObjectCache
$cache,
144 CommentStore
$commentStore,
145 NameTableStore
$contentModelStore,
146 NameTableStore
$slotRoleStore,
148 ActorMigration
$actorMigration,
151 Assert
::parameterType( 'string|boolean', $wikiId, '$wikiId' );
152 Assert
::parameterType( 'integer', $migrationStage, '$migrationStage' );
154 if ( $migrationStage > MIGRATION_WRITE_BOTH
) {
155 throw new InvalidArgumentException( 'New schema is not fully supported yet' );
158 $this->loadBalancer
= $loadBalancer;
159 $this->blobStore
= $blobStore;
160 $this->cache
= $cache;
161 $this->commentStore
= $commentStore;
162 $this->contentModelStore
= $contentModelStore;
163 $this->slotRoleStore
= $slotRoleStore;
164 $this->mcrMigrationStage
= $migrationStage;
165 $this->actorMigration
= $actorMigration;
166 $this->wikiId
= $wikiId;
167 $this->logger
= new NullLogger();
170 public function setLogger( LoggerInterface
$logger ) {
171 $this->logger
= $logger;
175 * @return bool Whether the store is read-only
177 public function isReadOnly() {
178 return $this->blobStore
->isReadOnly();
184 public function getContentHandlerUseDB() {
185 return $this->contentHandlerUseDB
;
189 * @param bool $contentHandlerUseDB
190 * @throws MWException
192 public function setContentHandlerUseDB( $contentHandlerUseDB ) {
193 if ( !$contentHandlerUseDB && $this->mcrMigrationStage
> MIGRATION_OLD
) {
194 throw new MWException(
195 'Content model must be stored in the database for multi content revision migration.'
198 $this->contentHandlerUseDB
= $contentHandlerUseDB;
202 * @return LoadBalancer
204 private function getDBLoadBalancer() {
205 return $this->loadBalancer
;
209 * @param int $mode DB_MASTER or DB_REPLICA
213 private function getDBConnection( $mode ) {
214 $lb = $this->getDBLoadBalancer();
215 return $lb->getConnection( $mode, [], $this->wikiId
);
219 * @param IDatabase $connection
221 private function releaseDBConnection( IDatabase
$connection ) {
222 $lb = $this->getDBLoadBalancer();
223 $lb->reuseConnection( $connection );
227 * @param int $mode DB_MASTER or DB_REPLICA
231 private function getDBConnectionRef( $mode ) {
232 $lb = $this->getDBLoadBalancer();
233 return $lb->getConnectionRef( $mode, [], $this->wikiId
);
237 * Determines the page Title based on the available information.
239 * MCR migration note: this corresponds to Revision::getTitle
241 * @note this method should be private, external use should be avoided!
243 * @param int|null $pageId
244 * @param int|null $revId
245 * @param int $queryFlags
248 * @throws RevisionAccessException
250 public function getTitle( $pageId, $revId, $queryFlags = self
::READ_NORMAL
) {
251 if ( !$pageId && !$revId ) {
252 throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
255 // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
256 // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
257 if ( DBAccessObjectUtils
::hasFlags( $queryFlags, self
::READ_LATEST_IMMUTABLE
) ) {
258 $queryFlags = self
::READ_NORMAL
;
261 $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->wikiId
=== false );
262 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
263 $titleFlags = ( $dbMode == DB_MASTER ? Title
::GAID_FOR_UPDATE
: 0 );
265 // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
266 if ( $canUseTitleNewFromId ) {
267 // TODO: better foreign title handling (introduce TitleFactory)
268 $title = Title
::newFromID( $pageId, $titleFlags );
274 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
275 $canUseRevId = ( $revId !== null && $revId > 0 );
277 if ( $canUseRevId ) {
278 $dbr = $this->getDBConnectionRef( $dbMode );
279 // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
280 $row = $dbr->selectRow(
281 [ 'revision', 'page' ],
290 [ 'rev_id' => $revId ],
293 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
296 // TODO: better foreign title handling (introduce TitleFactory)
297 return Title
::newFromRow( $row );
301 // If we still don't have a title, fallback to master if that wasn't already happening.
302 if ( $dbMode !== DB_MASTER
) {
303 $title = $this->getTitle( $pageId, $revId, self
::READ_LATEST
);
306 __METHOD__
. ' fell back to READ_LATEST and got a Title.',
307 [ 'trace' => wfBacktrace() ]
313 throw new RevisionAccessException(
314 "Could not determine title for page ID $pageId and revision ID $revId"
319 * @param mixed $value
320 * @param string $name
322 * @throws IncompleteRevisionException if $value is null
323 * @return mixed $value, if $value is not null
325 private function failOnNull( $value, $name ) {
326 if ( $value === null ) {
327 throw new IncompleteRevisionException(
328 "$name must not be " . var_export( $value, true ) . "!"
336 * @param mixed $value
337 * @param string $name
339 * @throws IncompleteRevisionException if $value is empty
340 * @return mixed $value, if $value is not null
342 private function failOnEmpty( $value, $name ) {
343 if ( $value === null ||
$value === 0 ||
$value === '' ) {
344 throw new IncompleteRevisionException(
345 "$name must not be " . var_export( $value, true ) . "!"
353 * Insert a new revision into the database, returning the new revision record
354 * on success and dies horribly on failure.
356 * MCR migration note: this replaces Revision::insertOn
358 * @param RevisionRecord $rev
359 * @param IDatabase $dbw (master connection)
361 * @throws InvalidArgumentException
362 * @return RevisionRecord the new revision record.
364 public function insertRevisionOn( RevisionRecord
$rev, IDatabase
$dbw ) {
365 // TODO: pass in a DBTransactionContext instead of a database connection.
366 $this->checkDatabaseWikiId( $dbw );
368 if ( !$rev->getSlotRoles() ) {
369 throw new InvalidArgumentException( 'At least one slot needs to be defined!' );
372 // RevisionStore currently only supports writing a single slot
373 if ( $rev->getSlotRoles() !== [ 'main' ] ) {
374 throw new InvalidArgumentException( 'Only the main slot is supported for now!' );
377 // TODO: we shouldn't need an actual Title here.
378 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
379 $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
381 $parentId = $rev->getParentId() === null
382 ?
$this->getPreviousRevisionId( $dbw, $rev )
383 : $rev->getParentId();
385 // Record the text (or external storage URL) to the blob store
386 $mainSlot = $rev->getSlot( 'main', RevisionRecord
::RAW
);
388 $size = $this->failOnNull( $rev->getSize(), 'size field' );
389 $sha1 = $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
391 $dbw->startAtomic( __METHOD__
);
393 if ( !$mainSlot->hasAddress() ) {
394 $content = $mainSlot->getContent();
395 $format = $content->getDefaultFormat();
396 $model = $content->getModel();
398 $this->checkContentModel( $content, $title );
400 $data = $content->serialize( $format );
402 // Hints allow the blob store to optimize by "leaking" application level information to it.
403 // TODO: with the new MCR storage schema, we rev_id have this before storing the blobs.
404 // When we have it, add rev_id as a hint. Can be used with rev_parent_id for
405 // differential storage or compression of subsequent revisions.
407 BlobStore
::DESIGNATION_HINT
=> 'page-content', // BlobStore may be used for other things too.
408 BlobStore
::PAGE_HINT
=> $pageId,
409 BlobStore
::ROLE_HINT
=> $mainSlot->getRole(),
410 BlobStore
::PARENT_HINT
=> $parentId,
411 BlobStore
::SHA1_HINT
=> $mainSlot->getSha1(),
412 BlobStore
::MODEL_HINT
=> $model,
413 BlobStore
::FORMAT_HINT
=> $format,
416 $blobAddress = $this->blobStore
->storeBlob( $data, $blobHints );
418 $blobAddress = $mainSlot->getAddress();
419 $model = $mainSlot->getModel();
420 $format = $mainSlot->getFormat();
423 $textId = $this->blobStore
->getTextIdFromAddress( $blobAddress );
426 throw new LogicException(
427 'Blob address not supported in 1.29 database schema: ' . $blobAddress
431 // getTextIdFromAddress() is free to insert something into the text table, so $textId
432 // may be a new value, not anything already contained in $blobAddress.
433 $blobAddress = SqlBlobStore
::makeAddressFromTextId( $textId );
435 $comment = $this->failOnNull( $rev->getComment( RevisionRecord
::RAW
), 'comment' );
436 $user = $this->failOnNull( $rev->getUser( RevisionRecord
::RAW
), 'user' );
437 $timestamp = $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
440 $this->failOnNull( $user->getId(), 'user field' );
441 $this->failOnEmpty( $user->getName(), 'user_text field' );
443 // Record the edit in revisions
445 'rev_page' => $pageId,
446 'rev_parent_id' => $parentId,
447 'rev_minor_edit' => $rev->isMinor() ?
1 : 0,
448 'rev_timestamp' => $dbw->timestamp( $timestamp ),
449 'rev_deleted' => $rev->getVisibility(),
454 if ( $rev->getId() !== null ) {
455 // Needed to restore revisions with their original ID
456 $revisionRow['rev_id'] = $rev->getId();
459 list( $commentFields, $commentCallback ) =
460 $this->commentStore
->insertWithTempTable( $dbw, 'rev_comment', $comment );
461 $revisionRow +
= $commentFields;
463 list( $actorFields, $actorCallback ) =
464 $this->actorMigration
->getInsertValuesWithTempTable( $dbw, 'rev_user', $user );
465 $revisionRow +
= $actorFields;
467 if ( $this->mcrMigrationStage
<= MIGRATION_WRITE_BOTH
) {
468 $revisionRow['rev_text_id'] = $textId;
470 // MCR migration note: rev_content_model and rev_content_format will go away
471 if ( $this->contentHandlerUseDB
) {
472 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
473 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
475 $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
476 $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
480 * rev_text_id has NOT NULL and no DEFAULT, so set to 0 when we are not writing to it.
481 * WARNING: This should NOT be removed after migration until a schema change has been
482 * made in WMF production giving rev_text_id a DEFAULT value of 0 (otherwise inserts
484 * Task: https://phabricator.wikimedia.org/T190148#4064625
486 $revisionRow['rev_text_id'] = 0;
489 $dbw->insert( 'revision', $revisionRow, __METHOD__
);
494 if ( isset( $revisionRow['rev_id'] ) ) {
495 // Restoring a revision, slots should already exist,
496 // unless the archive row wasn't migrated yet.
497 if ( $this->mcrMigrationStage
=== MIGRATION_NEW
) {
500 $contentId = $this->findSlotContentId( $dbw, $revisionRow['rev_id'], 'main' );
501 $hasSlots = (bool)$contentId;
504 // not restoring a revision, use auto-increment value
505 $revisionRow['rev_id'] = intval( $dbw->insertId() );
508 if ( $this->mcrMigrationStage
> MIGRATION_OLD
&& $mainSlot->hasContentId() ) {
509 // re-use content row of inherited slot!
510 $contentId = $mainSlot->getContentId();
513 $revisionId = $revisionRow['rev_id'];
515 $commentCallback( $revisionId );
516 $actorCallback( $revisionId, $revisionRow );
518 // Insert IP revision into ip_changes for use when querying for a range.
519 if ( $user->getId() === 0 && IP
::isValid( $user->getName() ) ) {
521 'ipc_rev_id' => $revisionId,
522 'ipc_rev_timestamp' => $revisionRow['rev_timestamp'],
523 'ipc_hex' => IP
::toHex( $user->getName() ),
525 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__
);
528 if ( $this->mcrMigrationStage
>= MIGRATION_WRITE_BOTH
) {
530 // Only insert slot rows for new revisions (not restored revisions).
531 // Also, never insert content rows if not inserting slot rows.
534 // Only insert content rows for new content (not inherited content)
536 Assert
::invariant( !$hasSlots, 'Re-using slots, but not content ID is known' );
537 $contentId = $this->insertContentRowOn( $mainSlot, $dbw, $blobAddress );
540 $this->insertSlotRowOn( $mainSlot, $dbw, $revisionId, $contentId );
546 $dbw->endAtomic( __METHOD__
);
548 $newSlot = SlotRecord
::newSaved( $revisionId, $contentId, $blobAddress, $mainSlot );
549 $slots = new RevisionSlots( [ 'main' => $newSlot ] );
551 $rev = new RevisionStoreRecord(
555 (object)$revisionRow,
560 $newSlot = $rev->getSlot( 'main', RevisionRecord
::RAW
);
563 Assert
::postcondition( $rev->getId() > 0, 'revision must have an ID' );
564 Assert
::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
565 Assert
::postcondition(
566 $rev->getComment( RevisionRecord
::RAW
) !== null,
567 'revision must have a comment'
569 Assert
::postcondition(
570 $rev->getUser( RevisionRecord
::RAW
) !== null,
571 'revision must have a user'
574 Assert
::postcondition( $newSlot !== null, 'revision must have a main slot' );
575 Assert
::postcondition(
576 $newSlot->getAddress() !== null,
577 'main slot must have an address'
580 Hooks
::run( 'RevisionRecordInserted', [ $rev ] );
582 // TODO: deprecate in 1.32!
583 $legacyRevision = new Revision( $rev );
584 Hooks
::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
590 * @param SlotRecord $slot
591 * @param IDatabase $dbw
592 * @param int $revisionId
593 * @param int $contentId
595 private function insertSlotRowOn( SlotRecord
$slot, IDatabase
$dbw, $revisionId, $contentId ) {
597 'slot_revision_id' => $revisionId,
598 'slot_role_id' => $this->slotRoleStore
->acquireId( $slot->getRole() ),
599 'slot_content_id' => $contentId,
600 // If the slot has a specific origin use that ID, otherwise use the ID of the revision
601 // that we just inserted.
602 'slot_origin' => $slot->hasOrigin() ?
$slot->getOrigin() : $revisionId,
604 $dbw->insert( 'slots', $slotRow, __METHOD__
);
608 * @param SlotRecord $slot
609 * @param IDatabase $dbw
610 * @param string $blobAddress
611 * @return int content row ID
613 private function insertContentRowOn( SlotRecord
$slot, IDatabase
$dbw, $blobAddress ) {
615 'content_size' => $slot->getSize(),
616 'content_sha1' => $slot->getSha1(),
617 'content_model' => $this->contentModelStore
->acquireId( $slot->getModel() ),
618 'content_address' => $blobAddress,
620 $dbw->insert( 'content', $contentRow, __METHOD__
);
621 return intval( $dbw->insertId() );
625 * MCR migration note: this corresponds to Revision::checkContentModel
627 * @param Content $content
628 * @param Title $title
630 * @throws MWException
631 * @throws MWUnknownContentModelException
633 private function checkContentModel( Content
$content, Title
$title ) {
634 // Note: may return null for revisions that have not yet been inserted
636 $model = $content->getModel();
637 $format = $content->getDefaultFormat();
638 $handler = $content->getContentHandler();
642 if ( !$handler->isSupportedFormat( $format ) ) {
643 throw new MWException( "Can't use format $format with content model $model on $name" );
646 if ( !$this->contentHandlerUseDB
) {
647 // if $wgContentHandlerUseDB is not set,
648 // all revisions must use the default content model and format.
650 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
651 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
652 $defaultFormat = $defaultHandler->getDefaultFormat();
654 if ( $model != $defaultModel ) {
655 throw new MWException( "Can't save non-default content model with "
656 . "\$wgContentHandlerUseDB disabled: model is $model, "
657 . "default for $name is $defaultModel"
661 if ( $format != $defaultFormat ) {
662 throw new MWException( "Can't use non-default content format with "
663 . "\$wgContentHandlerUseDB disabled: format is $format, "
664 . "default for $name is $defaultFormat"
669 if ( !$content->isValid() ) {
670 throw new MWException(
671 "New content for $name is not valid! Content model is $model"
677 * Create a new null-revision for insertion into a page's
678 * history. This will not re-save the text, but simply refer
679 * to the text from the previous version.
681 * Such revisions can for instance identify page rename
682 * operations and other such meta-modifications.
684 * @note: This method grabs a FOR UPDATE lock on the relevant row of the page table,
685 * to prevent a new revision from being inserted before the null revision has been written
688 * MCR migration note: this replaces Revision::newNullRevision
690 * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
693 * @param IDatabase $dbw used for obtaining the lock on the page table row
694 * @param Title $title Title of the page to read from
695 * @param CommentStoreComment $comment RevisionRecord's summary
696 * @param bool $minor Whether the revision should be considered as minor
697 * @param User $user The user to attribute the revision to
699 * @return RevisionRecord|null RevisionRecord or null on error
701 public function newNullRevision(
704 CommentStoreComment
$comment,
708 $this->checkDatabaseWikiId( $dbw );
710 // T51581: Lock the page table row to ensure no other process
711 // is adding a revision to the page at the same time.
712 // Avoid locking extra tables, compare T191892.
713 $pageLatest = $dbw->selectField(
716 [ 'page_id' => $title->getArticleID() ],
721 if ( !$pageLatest ) {
725 // Fetch the actual revision row, without locking all extra tables.
726 $oldRevision = $this->loadRevisionFromId( $dbw, $pageLatest );
728 // Construct the new revision
729 $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
730 $newRevision = MutableRevisionRecord
::newFromParentRevision( $oldRevision );
732 $newRevision->setComment( $comment );
733 $newRevision->setUser( $user );
734 $newRevision->setTimestamp( $timestamp );
735 $newRevision->setMinorEdit( $minor );
741 * MCR migration note: this replaces Revision::isUnpatrolled
743 * @todo This is overly specific, so move or kill this method.
745 * @param RevisionRecord $rev
747 * @return int Rcid of the unpatrolled row, zero if there isn't one
749 public function getRcIdIfUnpatrolled( RevisionRecord
$rev ) {
750 $rc = $this->getRecentChange( $rev );
751 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange
::PRC_UNPATROLLED
) {
752 return $rc->getAttribute( 'rc_id' );
759 * Get the RC object belonging to the current revision, if there's one
761 * MCR migration note: this replaces Revision::getRecentChange
763 * @todo move this somewhere else?
765 * @param RevisionRecord $rev
766 * @param int $flags (optional) $flags include:
767 * IDBAccessObject::READ_LATEST: Select the data from the master
769 * @return null|RecentChange
771 public function getRecentChange( RevisionRecord
$rev, $flags = 0 ) {
772 $dbr = $this->getDBConnection( DB_REPLICA
);
774 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
776 $userIdentity = $rev->getUser( RevisionRecord
::RAW
);
778 if ( !$userIdentity ) {
779 // If the revision has no user identity, chances are it never went
780 // into the database, and doesn't have an RC entry.
784 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
785 $actorWhere = $this->actorMigration
->getWhere( $dbr, 'rc_user', $rev->getUser(), false );
786 $rc = RecentChange
::newFromConds(
788 $actorWhere['conds'],
789 'rc_timestamp' => $dbr->timestamp( $rev->getTimestamp() ),
790 'rc_this_oldid' => $rev->getId()
796 $this->releaseDBConnection( $dbr );
798 // XXX: cache this locally? Glue it to the RevisionRecord?
803 * Maps fields of the archive row to corresponding revision rows.
805 * @param object $archiveRow
807 * @return object a revision row object, corresponding to $archiveRow.
809 private static function mapArchiveFields( $archiveRow ) {
811 // keep with ar prefix:
814 // not the same suffix:
815 'ar_page_id' => 'rev_page',
816 'ar_rev_id' => 'rev_id',
819 'ar_text_id' => 'rev_text_id',
820 'ar_timestamp' => 'rev_timestamp',
821 'ar_user_text' => 'rev_user_text',
822 'ar_user' => 'rev_user',
823 'ar_actor' => 'rev_actor',
824 'ar_minor_edit' => 'rev_minor_edit',
825 'ar_deleted' => 'rev_deleted',
826 'ar_len' => 'rev_len',
827 'ar_parent_id' => 'rev_parent_id',
828 'ar_sha1' => 'rev_sha1',
829 'ar_comment' => 'rev_comment',
830 'ar_comment_cid' => 'rev_comment_cid',
831 'ar_comment_id' => 'rev_comment_id',
832 'ar_comment_text' => 'rev_comment_text',
833 'ar_comment_data' => 'rev_comment_data',
834 'ar_comment_old' => 'rev_comment_old',
835 'ar_content_format' => 'rev_content_format',
836 'ar_content_model' => 'rev_content_model',
839 $revRow = new stdClass();
840 foreach ( $fieldMap as $arKey => $revKey ) {
841 if ( property_exists( $archiveRow, $arKey ) ) {
842 $revRow->$revKey = $archiveRow->$arKey;
850 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
852 * @param object|array $row Either a database row or an array
853 * @param int $queryFlags for callbacks
854 * @param Title $title
856 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
857 * @throws MWException
859 private function emulateMainSlot_1_29( $row, $queryFlags, Title
$title ) {
860 $mainSlotRow = new stdClass();
861 $mainSlotRow->role_name
= 'main';
862 $mainSlotRow->model_name
= null;
863 $mainSlotRow->slot_revision_id
= null;
864 $mainSlotRow->content_address
= null;
870 if ( is_object( $row ) ) {
872 if ( !isset( $row->rev_id
) && ( isset( $row->ar_user
) ||
isset( $row->ar_actor
) ) ) {
873 $row = $this->mapArchiveFields( $row );
876 if ( isset( $row->rev_text_id
) && $row->rev_text_id
> 0 ) {
877 $mainSlotRow->content_address
= SqlBlobStore
::makeAddressFromTextId(
882 // This is used by null-revisions
883 $mainSlotRow->slot_origin
= isset( $row->slot_origin
)
884 ?
intval( $row->slot_origin
)
887 if ( isset( $row->old_text
) ) {
888 // this happens when the text-table gets joined directly, in the pre-1.30 schema
889 $blobData = isset( $row->old_text
) ?
strval( $row->old_text
) : null;
890 // Check against selects that might have not included old_flags
891 if ( !property_exists( $row, 'old_flags' ) ) {
892 throw new InvalidArgumentException( 'old_flags was not set in $row' );
894 $blobFlags = ( $row->old_flags
=== null ) ?
'' : $row->old_flags
;
897 $mainSlotRow->slot_revision_id
= intval( $row->rev_id
);
899 $mainSlotRow->content_size
= isset( $row->rev_len
) ?
intval( $row->rev_len
) : null;
900 $mainSlotRow->content_sha1
= isset( $row->rev_sha1
) ?
strval( $row->rev_sha1
) : null;
901 $mainSlotRow->model_name
= isset( $row->rev_content_model
)
902 ?
strval( $row->rev_content_model
)
904 // XXX: in the future, we'll probably always use the default format, and drop content_format
905 $mainSlotRow->format_name
= isset( $row->rev_content_format
)
906 ?
strval( $row->rev_content_format
)
908 } elseif ( is_array( $row ) ) {
909 $mainSlotRow->slot_revision_id
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
911 $mainSlotRow->slot_origin
= isset( $row['slot_origin'] )
912 ?
intval( $row['slot_origin'] )
914 $mainSlotRow->content_address
= isset( $row['text_id'] )
915 ? SqlBlobStore
::makeAddressFromTextId( intval( $row['text_id'] ) )
917 $mainSlotRow->content_size
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
918 $mainSlotRow->content_sha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
920 $mainSlotRow->model_name
= isset( $row['content_model'] )
921 ?
strval( $row['content_model'] ) : null; // XXX: must be a string!
922 // XXX: in the future, we'll probably always use the default format, and drop content_format
923 $mainSlotRow->format_name
= isset( $row['content_format'] )
924 ?
strval( $row['content_format'] ) : null;
925 $blobData = isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
926 // XXX: If the flags field is not set then $blobFlags should be null so that no
927 // decoding will happen. An empty string will result in default decodings.
928 $blobFlags = isset( $row['flags'] ) ?
trim( strval( $row['flags'] ) ) : null;
930 // if we have a Content object, override mText and mContentModel
931 if ( !empty( $row['content'] ) ) {
932 if ( !( $row['content'] instanceof Content
) ) {
933 throw new MWException( 'content field must contain a Content object.' );
936 /** @var Content $content */
937 $content = $row['content'];
938 $handler = $content->getContentHandler();
940 $mainSlotRow->model_name
= $content->getModel();
942 // XXX: in the future, we'll probably always use the default format.
943 if ( $mainSlotRow->format_name
=== null ) {
944 $mainSlotRow->format_name
= $handler->getDefaultFormat();
948 throw new MWException( 'Revision constructor passed invalid row format.' );
951 // With the old schema, the content changes with every revision,
952 // except for null-revisions.
953 if ( !isset( $mainSlotRow->slot_origin
) ) {
954 $mainSlotRow->slot_origin
= $mainSlotRow->slot_revision_id
;
957 if ( $mainSlotRow->model_name
=== null ) {
958 $mainSlotRow->model_name
= function ( SlotRecord
$slot ) use ( $title ) {
959 // TODO: MCR: consider slot role in getDefaultModelFor()! Use LinkTarget!
960 // TODO: MCR: deprecate $title->getModel().
961 return ContentHandler
::getDefaultModelFor( $title );
966 $content = function ( SlotRecord
$slot )
967 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
969 return $this->loadSlotContent(
973 $mainSlotRow->format_name
,
979 // NOTE: this callback will be looped through RevisionSlot::newInherited(), allowing
980 // the inherited slot to have the same content_id as the original slot. In that case,
981 // $slot will be the inherited slot, while $mainSlotRow still refers to the original slot.
982 $mainSlotRow->slot_content_id
=
983 function ( SlotRecord
$slot ) use ( $queryFlags, $mainSlotRow ) {
984 list( $dbMode, ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
985 $db = $this->getDBConnectionRef( $dbMode );
986 return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id
, 'main' );
989 // use negative IDs for fake slot records.
990 $mainSlotRow->slot_id
= -( $mainSlotRow->slot_revision_id
);
991 return new SlotRecord( $mainSlotRow, $content );
995 * Loads a Content object based on a slot row.
997 * This method does not call $slot->getContent(), and may be used as a callback
998 * called by $slot->getContent().
1000 * MCR migration note: this roughly corresponds to Revision::getContentInternal
1002 * @param SlotRecord $slot The SlotRecord to load content for
1003 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
1004 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
1005 * Use null if no processing should happen. That is in constrast to the empty string,
1006 * which causes the blob to be decoded according to the configured legacy encoding.
1007 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
1008 * @param int $queryFlags
1010 * @throws RevisionAccessException
1013 private function loadSlotContent(
1020 if ( $blobData !== null ) {
1021 Assert
::parameterType( 'string', $blobData, '$blobData' );
1022 Assert
::parameterType( 'string|null', $blobFlags, '$blobFlags' );
1024 $cacheKey = $slot->hasAddress() ?
$slot->getAddress() : null;
1026 if ( $blobFlags === null ) {
1027 // No blob flags, so use the blob verbatim.
1030 $data = $this->blobStore
->expandBlob( $blobData, $blobFlags, $cacheKey );
1031 if ( $data === false ) {
1032 throw new RevisionAccessException(
1033 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
1039 $address = $slot->getAddress();
1041 $data = $this->blobStore
->getBlob( $address, $queryFlags );
1042 } catch ( BlobAccessException
$e ) {
1043 throw new RevisionAccessException(
1044 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
1049 // Unserialize content
1050 $handler = ContentHandler
::getForModelID( $slot->getModel() );
1052 $content = $handler->unserializeContent( $data, $blobFormat );
1057 * Load a page revision from a given revision ID number.
1058 * Returns null if no such revision can be found.
1060 * MCR migration note: this replaces Revision::newFromId
1063 * IDBAccessObject::READ_LATEST: Select the data from the master
1064 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1067 * @param int $flags (optional)
1068 * @return RevisionRecord|null
1070 public function getRevisionById( $id, $flags = 0 ) {
1071 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
1075 * Load either the current, or a specified, revision
1076 * that's attached to a given link target. If not attached
1077 * to that link target, will return null.
1079 * MCR migration note: this replaces Revision::newFromTitle
1082 * IDBAccessObject::READ_LATEST: Select the data from the master
1083 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1085 * @param LinkTarget $linkTarget
1086 * @param int $revId (optional)
1087 * @param int $flags Bitfield (optional)
1088 * @return RevisionRecord|null
1090 public function getRevisionByTitle( LinkTarget
$linkTarget, $revId = 0, $flags = 0 ) {
1092 'page_namespace' => $linkTarget->getNamespace(),
1093 'page_title' => $linkTarget->getDBkey()
1096 // Use the specified revision ID.
1097 // Note that we use newRevisionFromConds here because we want to retry
1098 // and fall back to master if the page is not found on a replica.
1099 // Since the caller supplied a revision ID, we are pretty sure the revision is
1100 // supposed to exist, so we should try hard to find it.
1101 $conds['rev_id'] = $revId;
1102 return $this->newRevisionFromConds( $conds, $flags );
1104 // Use a join to get the latest revision.
1105 // Note that we don't use newRevisionFromConds here because we don't want to retry
1106 // and fall back to master. The assumption is that we only want to force the fallback
1107 // if we are quite sure the revision exists because the caller supplied a revision ID.
1108 // If the page isn't found at all on a replica, it probably simply does not exist.
1109 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1111 $conds[] = 'rev_id=page_latest';
1112 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1114 $this->releaseDBConnection( $db );
1120 * Load either the current, or a specified, revision
1121 * that's attached to a given page ID.
1122 * Returns null if no such revision can be found.
1124 * MCR migration note: this replaces Revision::newFromPageId
1127 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
1128 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1130 * @param int $pageId
1131 * @param int $revId (optional)
1132 * @param int $flags Bitfield (optional)
1133 * @return RevisionRecord|null
1135 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
1136 $conds = [ 'page_id' => $pageId ];
1138 // Use the specified revision ID.
1139 // Note that we use newRevisionFromConds here because we want to retry
1140 // and fall back to master if the page is not found on a replica.
1141 // Since the caller supplied a revision ID, we are pretty sure the revision is
1142 // supposed to exist, so we should try hard to find it.
1143 $conds['rev_id'] = $revId;
1144 return $this->newRevisionFromConds( $conds, $flags );
1146 // Use a join to get the latest revision.
1147 // Note that we don't use newRevisionFromConds here because we don't want to retry
1148 // and fall back to master. The assumption is that we only want to force the fallback
1149 // if we are quite sure the revision exists because the caller supplied a revision ID.
1150 // If the page isn't found at all on a replica, it probably simply does not exist.
1151 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1153 $conds[] = 'rev_id=page_latest';
1154 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1156 $this->releaseDBConnection( $db );
1162 * Load the revision for the given title with the given timestamp.
1163 * WARNING: Timestamps may in some circumstances not be unique,
1164 * so this isn't the best key to use.
1166 * MCR migration note: this replaces Revision::loadFromTimestamp
1168 * @param Title $title
1169 * @param string $timestamp
1170 * @return RevisionRecord|null
1172 public function getRevisionByTimestamp( $title, $timestamp ) {
1173 $db = $this->getDBConnection( DB_REPLICA
);
1174 return $this->newRevisionFromConds(
1176 'rev_timestamp' => $db->timestamp( $timestamp ),
1177 'page_namespace' => $title->getNamespace(),
1178 'page_title' => $title->getDBkey()
1186 * Make a fake revision object from an archive table row. This is queried
1187 * for permissions or even inserted (as in Special:Undelete)
1189 * MCR migration note: this replaces Revision::newFromArchiveRow
1191 * @param object $row
1192 * @param int $queryFlags
1193 * @param Title|null $title
1194 * @param array $overrides associative array with fields of $row to override. This may be
1195 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1196 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1197 * override ar_parent_id.
1199 * @return RevisionRecord
1200 * @throws MWException
1202 public function newRevisionFromArchiveRow(
1205 Title
$title = null,
1206 array $overrides = []
1208 Assert
::parameterType( 'object', $row, '$row' );
1210 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1211 Assert
::parameterType( 'integer', $queryFlags, '$queryFlags' );
1213 if ( !$title && isset( $overrides['title'] ) ) {
1214 if ( !( $overrides['title'] instanceof Title
) ) {
1215 throw new MWException( 'title field override must contain a Title object.' );
1218 $title = $overrides['title'];
1221 if ( !isset( $title ) ) {
1222 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
1223 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
1225 throw new InvalidArgumentException(
1226 'A Title or ar_namespace and ar_title must be given'
1231 foreach ( $overrides as $key => $value ) {
1233 $row->$field = $value;
1237 $user = User
::newFromAnyId(
1238 $row->ar_user ??
null,
1239 $row->ar_user_text ??
null,
1240 $row->ar_actor ??
null
1242 } catch ( InvalidArgumentException
$ex ) {
1243 wfWarn( __METHOD__
. ': ' . $ex->getMessage() );
1244 $user = new UserIdentityValue( 0, '', 0 );
1247 $comment = $this->commentStore
1248 // Legacy because $row may have come from self::selectFields()
1249 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA
), 'ar_comment', $row, true );
1251 $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
1252 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1254 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1258 * @see RevisionFactory::newRevisionFromRow_1_29
1260 * MCR migration note: this replaces Revision::newFromRow
1262 * @param object $row
1263 * @param int $queryFlags
1264 * @param Title|null $title
1266 * @return RevisionRecord
1267 * @throws MWException
1268 * @throws RevisionAccessException
1270 private function newRevisionFromRow_1_29( $row, $queryFlags = 0, Title
$title = null ) {
1271 Assert
::parameterType( 'object', $row, '$row' );
1274 $pageId = $row->rev_page ??
0; // XXX: also check page_id?
1275 $revId = $row->rev_id ??
0;
1277 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1280 if ( !isset( $row->page_latest
) ) {
1281 $row->page_latest
= $title->getLatestRevID();
1282 if ( $row->page_latest
=== 0 && $title->exists() ) {
1283 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1288 $user = User
::newFromAnyId(
1289 $row->rev_user ??
null,
1290 $row->rev_user_text ??
null,
1291 $row->rev_actor ??
null
1293 } catch ( InvalidArgumentException
$ex ) {
1294 wfWarn( __METHOD__
. ': ' . $ex->getMessage() );
1295 $user = new UserIdentityValue( 0, '', 0 );
1298 $comment = $this->commentStore
1299 // Legacy because $row may have come from self::selectFields()
1300 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA
), 'rev_comment', $row, true );
1302 $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
1303 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1305 return new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1309 * @see RevisionFactory::newRevisionFromRow
1311 * MCR migration note: this replaces Revision::newFromRow
1313 * @param object $row
1314 * @param int $queryFlags
1315 * @param Title|null $title
1317 * @return RevisionRecord
1319 public function newRevisionFromRow( $row, $queryFlags = 0, Title
$title = null ) {
1320 return $this->newRevisionFromRow_1_29( $row, $queryFlags, $title );
1324 * Constructs a new MutableRevisionRecord based on the given associative array following
1325 * the MW1.29 convention for the Revision constructor.
1327 * MCR migration note: this replaces Revision::newFromRow
1329 * @param array $fields
1330 * @param int $queryFlags
1331 * @param Title|null $title
1333 * @return MutableRevisionRecord
1334 * @throws MWException
1335 * @throws RevisionAccessException
1337 public function newMutableRevisionFromArray(
1342 if ( !$title && isset( $fields['title'] ) ) {
1343 if ( !( $fields['title'] instanceof Title
) ) {
1344 throw new MWException( 'title field must contain a Title object.' );
1347 $title = $fields['title'];
1351 $pageId = $fields['page'] ??
0;
1352 $revId = $fields['id'] ??
0;
1354 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1357 if ( !isset( $fields['page'] ) ) {
1358 $fields['page'] = $title->getArticleID( $queryFlags );
1361 // if we have a content object, use it to set the model and type
1362 if ( !empty( $fields['content'] ) ) {
1363 if ( !( $fields['content'] instanceof Content
) ) {
1364 throw new MWException( 'content field must contain a Content object.' );
1367 if ( !empty( $fields['text_id'] ) ) {
1368 throw new MWException(
1369 "Text already stored in external store (id {$fields['text_id']}), " .
1370 "can't serialize content object"
1376 isset( $fields['comment'] )
1377 && !( $fields['comment'] instanceof CommentStoreComment
)
1379 $commentData = $fields['comment_data'] ??
null;
1381 if ( $fields['comment'] instanceof Message
) {
1382 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1387 $commentText = trim( strval( $fields['comment'] ) );
1388 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1395 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1397 $revision = new MutableRevisionRecord( $title, $this->wikiId
);
1398 $this->initializeMutableRevisionFromArray( $revision, $fields );
1399 $revision->setSlot( $mainSlot );
1405 * @param MutableRevisionRecord $record
1406 * @param array $fields
1408 private function initializeMutableRevisionFromArray(
1409 MutableRevisionRecord
$record,
1412 /** @var UserIdentity $user */
1415 if ( isset( $fields['user'] ) && ( $fields['user'] instanceof UserIdentity
) ) {
1416 $user = $fields['user'];
1419 $user = User
::newFromAnyId(
1420 $fields['user'] ??
null,
1421 $fields['user_text'] ??
null,
1422 $fields['actor'] ??
null
1424 } catch ( InvalidArgumentException
$ex ) {
1430 $record->setUser( $user );
1433 $timestamp = isset( $fields['timestamp'] )
1434 ?
strval( $fields['timestamp'] )
1435 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1437 $record->setTimestamp( $timestamp );
1439 if ( isset( $fields['page'] ) ) {
1440 $record->setPageId( intval( $fields['page'] ) );
1443 if ( isset( $fields['id'] ) ) {
1444 $record->setId( intval( $fields['id'] ) );
1446 if ( isset( $fields['parent_id'] ) ) {
1447 $record->setParentId( intval( $fields['parent_id'] ) );
1450 if ( isset( $fields['sha1'] ) ) {
1451 $record->setSha1( $fields['sha1'] );
1453 if ( isset( $fields['size'] ) ) {
1454 $record->setSize( intval( $fields['size'] ) );
1457 if ( isset( $fields['minor_edit'] ) ) {
1458 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
1460 if ( isset( $fields['deleted'] ) ) {
1461 $record->setVisibility( intval( $fields['deleted'] ) );
1464 if ( isset( $fields['comment'] ) ) {
1465 Assert
::parameterType(
1466 CommentStoreComment
::class,
1470 $record->setComment( $fields['comment'] );
1475 * Load a page revision from a given revision ID number.
1476 * Returns null if no such revision can be found.
1478 * MCR migration note: this corresponds to Revision::loadFromId
1480 * @note direct use is deprecated!
1481 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
1483 * @param IDatabase $db
1486 * @return RevisionRecord|null
1488 public function loadRevisionFromId( IDatabase
$db, $id ) {
1489 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
1493 * Load either the current, or a specified, revision
1494 * that's attached to a given page. If not attached
1495 * to that page, will return null.
1497 * MCR migration note: this replaces Revision::loadFromPageId
1499 * @note direct use is deprecated!
1500 * @todo remove when unused!
1502 * @param IDatabase $db
1503 * @param int $pageid
1505 * @return RevisionRecord|null
1507 public function loadRevisionFromPageId( IDatabase
$db, $pageid, $id = 0 ) {
1508 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
1510 $conds['rev_id'] = intval( $id );
1512 $conds[] = 'rev_id=page_latest';
1514 return $this->loadRevisionFromConds( $db, $conds );
1518 * Load either the current, or a specified, revision
1519 * that's attached to a given page. If not attached
1520 * to that page, will return null.
1522 * MCR migration note: this replaces Revision::loadFromTitle
1524 * @note direct use is deprecated!
1525 * @todo remove when unused!
1527 * @param IDatabase $db
1528 * @param Title $title
1531 * @return RevisionRecord|null
1533 public function loadRevisionFromTitle( IDatabase
$db, $title, $id = 0 ) {
1535 $matchId = intval( $id );
1537 $matchId = 'page_latest';
1540 return $this->loadRevisionFromConds(
1544 'page_namespace' => $title->getNamespace(),
1545 'page_title' => $title->getDBkey()
1553 * Load the revision for the given title with the given timestamp.
1554 * WARNING: Timestamps may in some circumstances not be unique,
1555 * so this isn't the best key to use.
1557 * MCR migration note: this replaces Revision::loadFromTimestamp
1559 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
1560 * @todo remove when unused!
1562 * @param IDatabase $db
1563 * @param Title $title
1564 * @param string $timestamp
1565 * @return RevisionRecord|null
1567 public function loadRevisionFromTimestamp( IDatabase
$db, $title, $timestamp ) {
1568 return $this->loadRevisionFromConds( $db,
1570 'rev_timestamp' => $db->timestamp( $timestamp ),
1571 'page_namespace' => $title->getNamespace(),
1572 'page_title' => $title->getDBkey()
1580 * Given a set of conditions, fetch a revision
1582 * This method should be used if we are pretty sure the revision exists.
1583 * Unless $flags has READ_LATEST set, this method will first try to find the revision
1584 * on a replica before hitting the master database.
1586 * MCR migration note: this corresponds to Revision::newFromConds
1588 * @param array $conditions
1589 * @param int $flags (optional)
1590 * @param Title $title
1592 * @return RevisionRecord|null
1594 private function newRevisionFromConds( $conditions, $flags = 0, Title
$title = null ) {
1595 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1596 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1597 $this->releaseDBConnection( $db );
1599 $lb = $this->getDBLoadBalancer();
1601 // Make sure new pending/committed revision are visibile later on
1602 // within web requests to certain avoid bugs like T93866 and T94407.
1604 && !( $flags & self
::READ_LATEST
)
1605 && $lb->getServerCount() > 1
1606 && $lb->hasOrMadeRecentMasterChanges()
1608 $flags = self
::READ_LATEST
;
1609 $db = $this->getDBConnection( DB_MASTER
);
1610 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1611 $this->releaseDBConnection( $db );
1618 * Given a set of conditions, fetch a revision from
1619 * the given database connection.
1621 * MCR migration note: this corresponds to Revision::loadFromConds
1623 * @param IDatabase $db
1624 * @param array $conditions
1625 * @param int $flags (optional)
1626 * @param Title $title
1628 * @return RevisionRecord|null
1630 private function loadRevisionFromConds(
1636 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
1638 $rev = $this->newRevisionFromRow( $row, $flags, $title );
1647 * Throws an exception if the given database connection does not belong to the wiki this
1648 * RevisionStore is bound to.
1650 * @param IDatabase $db
1651 * @throws MWException
1653 private function checkDatabaseWikiId( IDatabase
$db ) {
1654 $storeWiki = $this->wikiId
;
1655 $dbWiki = $db->getDomainID();
1657 if ( $dbWiki === $storeWiki ) {
1661 // XXX: we really want the default database ID...
1662 $storeWiki = $storeWiki ?
: wfWikiID();
1663 $dbWiki = $dbWiki ?
: wfWikiID();
1665 if ( $dbWiki === $storeWiki ) {
1669 // HACK: counteract encoding imposed by DatabaseDomain
1670 $storeWiki = str_replace( '?h', '-', $storeWiki );
1671 $dbWiki = str_replace( '?h', '-', $dbWiki );
1673 if ( $dbWiki === $storeWiki ) {
1677 throw new MWException( "RevisionStore for $storeWiki "
1678 . "cannot be used with a DB connection for $dbWiki" );
1682 * Given a set of conditions, return a row with the
1683 * fields necessary to build RevisionRecord objects.
1685 * MCR migration note: this corresponds to Revision::fetchFromConds
1687 * @param IDatabase $db
1688 * @param array $conditions
1689 * @param int $flags (optional)
1691 * @return object|false data row as a raw object
1693 private function fetchRevisionRowFromConds( IDatabase
$db, $conditions, $flags = 0 ) {
1694 $this->checkDatabaseWikiId( $db );
1696 $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
1698 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
1699 $options[] = 'FOR UPDATE';
1701 return $db->selectRow(
1702 $revQuery['tables'],
1703 $revQuery['fields'],
1712 * Finds the ID of a content row for a given revision and slot role.
1713 * This can be used to re-use content rows even while the content ID
1714 * is still missing from SlotRecords, in MIGRATION_WRITE_BOTH mode.
1716 * @todo remove after MCR schema migration is complete.
1718 * @param IDatabase $db
1720 * @param string $role
1724 private function findSlotContentId( IDatabase
$db, $revId, $role ) {
1725 if ( $this->mcrMigrationStage
< MIGRATION_WRITE_BOTH
) {
1730 $roleId = $this->slotRoleStore
->getId( $role );
1732 'slot_revision_id' => $revId,
1733 'slot_role_id' => $roleId,
1736 $contentId = $db->selectField( 'slots', 'slot_content_id', $conditions, __METHOD__
);
1738 return $contentId ?
: null;
1739 } catch ( NameTableAccessException
$ex ) {
1740 // If the role is missing from the slot_roles table,
1741 // the corresponding row in slots cannot exist.
1747 * Return the tables, fields, and join conditions to be selected to create
1748 * a new revision object.
1750 * MCR migration note: this replaces Revision::getQueryInfo
1752 * If the format of fields returned changes in any way then the cache key provided by
1753 * self::getRevisionRowCacheKey should be updated.
1757 * @param array $options Any combination of the following strings
1758 * - 'page': Join with the page table, and select fields to identify the page
1759 * - 'user': Join with the user table, and select the user name
1760 * - 'text': Join with the text table, and select fields to load page text
1762 * @return array With three keys:
1763 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
1764 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
1765 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
1767 public function getQueryInfo( $options = [] ) {
1774 $ret['tables'][] = 'revision';
1775 $ret['fields'] = array_merge( $ret['fields'], [
1786 $commentQuery = $this->commentStore
->getJoin( 'rev_comment' );
1787 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
1788 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
1789 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
1791 $actorQuery = $this->actorMigration
->getJoin( 'rev_user' );
1792 $ret['tables'] = array_merge( $ret['tables'], $actorQuery['tables'] );
1793 $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
1794 $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
1796 if ( $this->mcrMigrationStage
< MIGRATION_NEW
) {
1797 $ret['fields'][] = 'rev_text_id';
1799 if ( $this->contentHandlerUseDB
) {
1800 $ret['fields'][] = 'rev_content_format';
1801 $ret['fields'][] = 'rev_content_model';
1805 if ( in_array( 'page', $options, true ) ) {
1806 $ret['tables'][] = 'page';
1807 $ret['fields'] = array_merge( $ret['fields'], [
1815 $ret['joins']['page'] = [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
1818 if ( in_array( 'user', $options, true ) ) {
1819 $ret['tables'][] = 'user';
1820 $ret['fields'] = array_merge( $ret['fields'], [
1823 $u = $actorQuery['fields']['rev_user'];
1824 $ret['joins']['user'] = [ 'LEFT JOIN', [ "$u != 0", "user_id = $u" ] ];
1827 if ( in_array( 'text', $options, true ) ) {
1828 if ( $this->mcrMigrationStage
=== MIGRATION_NEW
) {
1829 throw new InvalidArgumentException( 'text table can no longer be joined directly' );
1832 $ret['tables'][] = 'text';
1833 $ret['fields'] = array_merge( $ret['fields'], [
1837 $ret['joins']['text'] = [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ];
1844 * Return the tables, fields, and join conditions to be selected to create
1845 * a new archived revision object.
1847 * MCR migration note: this replaces Revision::getArchiveQueryInfo
1851 * @return array With three keys:
1852 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
1853 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
1854 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
1856 public function getArchiveQueryInfo() {
1857 $commentQuery = $this->commentStore
->getJoin( 'ar_comment' );
1858 $actorQuery = $this->actorMigration
->getJoin( 'ar_user' );
1860 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
1873 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
1874 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
1877 if ( $this->mcrMigrationStage
< MIGRATION_NEW
) {
1878 $ret['fields'][] = 'ar_text_id';
1880 if ( $this->contentHandlerUseDB
) {
1881 $ret['fields'][] = 'ar_content_format';
1882 $ret['fields'][] = 'ar_content_model';
1890 * Do a batched query for the sizes of a set of revisions.
1892 * MCR migration note: this replaces Revision::getParentLengths
1894 * @param int[] $revIds
1895 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
1896 * of the corresponding revision.
1898 public function getRevisionSizes( array $revIds ) {
1899 return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA
), $revIds );
1903 * Do a batched query for the sizes of a set of revisions.
1905 * MCR migration note: this replaces Revision::getParentLengths
1907 * @deprecated use RevisionStore::getRevisionSizes instead.
1909 * @param IDatabase $db
1910 * @param int[] $revIds
1911 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
1912 * of the corresponding revision.
1914 public function listRevisionSizes( IDatabase
$db, array $revIds ) {
1915 $this->checkDatabaseWikiId( $db );
1919 return $revLens; // empty
1924 [ 'rev_id', 'rev_len' ],
1925 [ 'rev_id' => $revIds ],
1929 foreach ( $res as $row ) {
1930 $revLens[$row->rev_id
] = intval( $row->rev_len
);
1937 * Get previous revision for this title
1939 * MCR migration note: this replaces Revision::getPrevious
1941 * @param RevisionRecord $rev
1942 * @param Title $title if known (optional)
1944 * @return RevisionRecord|null
1946 public function getPreviousRevision( RevisionRecord
$rev, Title
$title = null ) {
1947 if ( $title === null ) {
1948 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
1950 $prev = $title->getPreviousRevisionID( $rev->getId() );
1952 return $this->getRevisionByTitle( $title, $prev );
1958 * Get next revision for this title
1960 * MCR migration note: this replaces Revision::getNext
1962 * @param RevisionRecord $rev
1963 * @param Title $title if known (optional)
1965 * @return RevisionRecord|null
1967 public function getNextRevision( RevisionRecord
$rev, Title
$title = null ) {
1968 if ( $title === null ) {
1969 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
1971 $next = $title->getNextRevisionID( $rev->getId() );
1973 return $this->getRevisionByTitle( $title, $next );
1979 * Get previous revision Id for this page_id
1980 * This is used to populate rev_parent_id on save
1982 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
1984 * @param IDatabase $db
1985 * @param RevisionRecord $rev
1989 private function getPreviousRevisionId( IDatabase
$db, RevisionRecord
$rev ) {
1990 $this->checkDatabaseWikiId( $db );
1992 if ( $rev->getPageId() === null ) {
1995 # Use page_latest if ID is not given
1996 if ( !$rev->getId() ) {
1997 $prevId = $db->selectField(
1998 'page', 'page_latest',
1999 [ 'page_id' => $rev->getPageId() ],
2003 $prevId = $db->selectField(
2004 'revision', 'rev_id',
2005 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
2007 [ 'ORDER BY' => 'rev_id DESC' ]
2010 return intval( $prevId );
2014 * Get rev_timestamp from rev_id, without loading the rest of the row
2016 * MCR migration note: this replaces Revision::getTimestampFromId
2018 * @param Title $title
2021 * @return string|bool False if not found
2023 public function getTimestampFromId( $title, $id, $flags = 0 ) {
2024 $db = $this->getDBConnection(
2025 ( $flags & IDBAccessObject
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
2028 $conds = [ 'rev_id' => $id ];
2029 $conds['rev_page'] = $title->getArticleID();
2030 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
2032 $this->releaseDBConnection( $db );
2033 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
2037 * Get count of revisions per page...not very efficient
2039 * MCR migration note: this replaces Revision::countByPageId
2041 * @param IDatabase $db
2042 * @param int $id Page id
2045 public function countRevisionsByPageId( IDatabase
$db, $id ) {
2046 $this->checkDatabaseWikiId( $db );
2048 $row = $db->selectRow( 'revision',
2049 [ 'revCount' => 'COUNT(*)' ],
2050 [ 'rev_page' => $id ],
2054 return intval( $row->revCount
);
2060 * Get count of revisions per page...not very efficient
2062 * MCR migration note: this replaces Revision::countByTitle
2064 * @param IDatabase $db
2065 * @param Title $title
2068 public function countRevisionsByTitle( IDatabase
$db, $title ) {
2069 $id = $title->getArticleID();
2071 return $this->countRevisionsByPageId( $db, $id );
2077 * Check if no edits were made by other users since
2078 * the time a user started editing the page. Limit to
2079 * 50 revisions for the sake of performance.
2081 * MCR migration note: this replaces Revision::userWasLastToEdit
2083 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
2084 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
2085 * has been deprecated since 1.24.
2087 * @param IDatabase $db The Database to perform the check on.
2088 * @param int $pageId The ID of the page in question
2089 * @param int $userId The ID of the user in question
2090 * @param string $since Look at edits since this time
2092 * @return bool True if the given user was the only one to edit since the given timestamp
2094 public function userWasLastToEdit( IDatabase
$db, $pageId, $userId, $since ) {
2095 $this->checkDatabaseWikiId( $db );
2101 $revQuery = $this->getQueryInfo();
2103 $revQuery['tables'],
2105 'rev_user' => $revQuery['fields']['rev_user'],
2108 'rev_page' => $pageId,
2109 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
2112 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
2115 foreach ( $res as $row ) {
2116 if ( $row->rev_user
!= $userId ) {
2124 * Load a revision based on a known page ID and current revision ID from the DB
2126 * This method allows for the use of caching, though accessing anything that normally
2127 * requires permission checks (aside from the text) will trigger a small DB lookup.
2129 * MCR migration note: this replaces Revision::newKnownCurrent
2131 * @param Title $title the associated page title
2132 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
2134 * @return RevisionRecord|bool Returns false if missing
2136 public function getKnownCurrentRevision( Title
$title, $revId ) {
2137 $db = $this->getDBConnectionRef( DB_REPLICA
);
2139 $pageId = $title->getArticleID();
2146 $revId = $title->getLatestRevID();
2151 'No latest revision known for page ' . $title->getPrefixedDBkey()
2152 . ' even though it exists with page ID ' . $pageId
2157 $row = $this->cache
->getWithSetCallback(
2158 // Page/rev IDs passed in from DB to reflect history merges
2159 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2160 WANObjectCache
::TTL_WEEK
,
2161 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
2162 $setOpts +
= Database
::getCacheSetOptions( $db );
2165 'rev_page' => intval( $pageId ),
2166 'page_id' => intval( $pageId ),
2167 'rev_id' => intval( $revId ),
2170 $row = $this->fetchRevisionRowFromConds( $db, $conds );
2171 return $row ?
: false; // don't cache negatives
2175 // Reflect revision deletion and user renames
2177 return $this->newRevisionFromRow( $row, 0, $title );
2184 * Get a cache key for use with a row as selected with getQueryInfo( [ 'page', 'user' ] )
2185 * Caching rows without 'page' or 'user' could lead to issues.
2186 * If the format of the rows returned by the query provided by getQueryInfo changes the
2187 * cache key should be updated to avoid conflicts.
2189 * @param IDatabase $db
2190 * @param int $pageId
2194 private function getRevisionRowCacheKey( IDatabase
$db, $pageId, $revId ) {
2195 return $this->cache
->makeGlobalKey(
2196 self
::ROW_CACHE_KEY
,
2203 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.