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
;
30 use CommentStoreComment
;
33 use DBAccessObjectUtils
;
36 use InvalidArgumentException
;
39 use MediaWiki\Linker\LinkTarget
;
40 use MediaWiki\User\UserIdentity
;
41 use MediaWiki\User\UserIdentityValue
;
44 use MWUnknownContentModelException
;
45 use Psr\Log\LoggerAwareInterface
;
46 use Psr\Log\LoggerInterface
;
47 use Psr\Log\NullLogger
;
53 use Wikimedia\Assert\Assert
;
54 use Wikimedia\Rdbms\Database
;
55 use Wikimedia\Rdbms\DBConnRef
;
56 use Wikimedia\Rdbms\IDatabase
;
57 use Wikimedia\Rdbms\LoadBalancer
;
60 * Service for looking up page revisions.
64 * @note This was written to act as a drop-in replacement for the corresponding
65 * static methods in Revision.
68 implements IDBAccessObject
, RevisionFactory
, RevisionLookup
, LoggerAwareInterface
{
83 private $contentHandlerUseDB = true;
88 private $loadBalancer;
96 * @var LoggerInterface
101 * @todo $blobStore should be allowed to be any BlobStore!
103 * @param LoadBalancer $loadBalancer
104 * @param SqlBlobStore $blobStore
105 * @param WANObjectCache $cache
106 * @param bool|string $wikiId
108 public function __construct(
109 LoadBalancer
$loadBalancer,
110 SqlBlobStore
$blobStore,
111 WANObjectCache
$cache,
114 Assert
::parameterType( 'string|boolean', $wikiId, '$wikiId' );
116 $this->loadBalancer
= $loadBalancer;
117 $this->blobStore
= $blobStore;
118 $this->cache
= $cache;
119 $this->wikiId
= $wikiId;
120 $this->logger
= new NullLogger();
123 public function setLogger( LoggerInterface
$logger ) {
124 $this->logger
= $logger;
130 public function getContentHandlerUseDB() {
131 return $this->contentHandlerUseDB
;
135 * @param bool $contentHandlerUseDB
137 public function setContentHandlerUseDB( $contentHandlerUseDB ) {
138 $this->contentHandlerUseDB
= $contentHandlerUseDB;
142 * @return LoadBalancer
144 private function getDBLoadBalancer() {
145 return $this->loadBalancer
;
149 * @param int $mode DB_MASTER or DB_REPLICA
153 private function getDBConnection( $mode ) {
154 $lb = $this->getDBLoadBalancer();
155 return $lb->getConnection( $mode, [], $this->wikiId
);
159 * @param IDatabase $connection
161 private function releaseDBConnection( IDatabase
$connection ) {
162 $lb = $this->getDBLoadBalancer();
163 $lb->reuseConnection( $connection );
167 * @param int $mode DB_MASTER or DB_REPLICA
171 private function getDBConnectionRef( $mode ) {
172 $lb = $this->getDBLoadBalancer();
173 return $lb->getConnectionRef( $mode, [], $this->wikiId
);
177 * Determines the page Title based on the available information.
179 * MCR migration note: this corresponds to Revision::getTitle
181 * @note this method should be private, external use should be avoided!
183 * @param int|null $pageId
184 * @param int|null $revId
185 * @param int $queryFlags
188 * @throws RevisionAccessException
190 public function getTitle( $pageId, $revId, $queryFlags = self
::READ_NORMAL
) {
191 if ( !$pageId && !$revId ) {
192 throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
195 // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
196 // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
197 if ( DBAccessObjectUtils
::hasFlags( $queryFlags, self
::READ_LATEST_IMMUTABLE
) ) {
198 $queryFlags = self
::READ_NORMAL
;
201 $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->wikiId
=== false );
202 list( $dbMode, $dbOptions ) = DBAccessObjectUtils
::getDBOptions( $queryFlags );
203 $titleFlags = ( $dbMode == DB_MASTER ? Title
::GAID_FOR_UPDATE
: 0 );
205 // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
206 if ( $canUseTitleNewFromId ) {
207 // TODO: better foreign title handling (introduce TitleFactory)
208 $title = Title
::newFromID( $pageId, $titleFlags );
214 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
215 $canUseRevId = ( $revId !== null && $revId > 0 );
217 if ( $canUseRevId ) {
218 $dbr = $this->getDBConnectionRef( $dbMode );
219 // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
220 $row = $dbr->selectRow(
221 [ 'revision', 'page' ],
230 [ 'rev_id' => $revId ],
233 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
236 // TODO: better foreign title handling (introduce TitleFactory)
237 return Title
::newFromRow( $row );
241 // If we still don't have a title, fallback to master if that wasn't already happening.
242 if ( $dbMode !== DB_MASTER
) {
243 $title = $this->getTitle( $pageId, $revId, self
::READ_LATEST
);
246 __METHOD__
. ' fell back to READ_LATEST and got a Title.',
247 [ 'trace' => wfDebugBacktrace() ]
253 throw new RevisionAccessException(
254 "Could not determine title for page ID $pageId and revision ID $revId"
259 * @param mixed $value
260 * @param string $name
262 * @throw IncompleteRevisionException if $value is null
263 * @return mixed $value, if $value is not null
265 private function failOnNull( $value, $name ) {
266 if ( $value === null ) {
267 throw new IncompleteRevisionException(
268 "$name must not be " . var_export( $value, true ) . "!"
276 * @param mixed $value
277 * @param string $name
279 * @throw IncompleteRevisionException if $value is empty
280 * @return mixed $value, if $value is not null
282 private function failOnEmpty( $value, $name ) {
283 if ( $value === null ||
$value === 0 ||
$value === '' ) {
284 throw new IncompleteRevisionException(
285 "$name must not be " . var_export( $value, true ) . "!"
293 * Insert a new revision into the database, returning the new revision ID
294 * number on success and dies horribly on failure.
296 * MCR migration note: this replaces Revision::insertOn
298 * @param RevisionRecord $rev
299 * @param IDatabase $dbw (master connection)
301 * @throws InvalidArgumentException
302 * @return RevisionRecord the new revision record.
304 public function insertRevisionOn( RevisionRecord
$rev, IDatabase
$dbw ) {
305 // TODO: pass in a DBTransactionContext instead of a database connection.
306 $this->checkDatabaseWikiId( $dbw );
308 if ( !$rev->getSlotRoles() ) {
309 throw new InvalidArgumentException( 'At least one slot needs to be defined!' );
312 if ( $rev->getSlotRoles() !== [ 'main' ] ) {
313 throw new InvalidArgumentException( 'Only the main slot is supported for now!' );
316 // TODO: we shouldn't need an actual Title here.
317 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
318 $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
320 $parentId = $rev->getParentId() === null
321 ?
$this->getPreviousRevisionId( $dbw, $rev )
322 : $rev->getParentId();
324 // Record the text (or external storage URL) to the blob store
325 $slot = $rev->getSlot( 'main', RevisionRecord
::RAW
);
327 $size = $this->failOnNull( $rev->getSize(), 'size field' );
328 $sha1 = $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
330 if ( !$slot->hasAddress() ) {
331 $content = $slot->getContent();
332 $format = $content->getDefaultFormat();
333 $model = $content->getModel();
335 $this->checkContentModel( $content, $title );
337 $data = $content->serialize( $format );
339 // Hints allow the blob store to optimize by "leaking" application level information to it.
340 // TODO: with the new MCR storage schema, we rev_id have this before storing the blobs.
341 // When we have it, add rev_id as a hint. Can be used with rev_parent_id for
342 // differential storage or compression of subsequent revisions.
344 BlobStore
::DESIGNATION_HINT
=> 'page-content', // BlobStore may be used for other things too.
345 BlobStore
::PAGE_HINT
=> $pageId,
346 BlobStore
::ROLE_HINT
=> $slot->getRole(),
347 BlobStore
::PARENT_HINT
=> $parentId,
348 BlobStore
::SHA1_HINT
=> $slot->getSha1(),
349 BlobStore
::MODEL_HINT
=> $model,
350 BlobStore
::FORMAT_HINT
=> $format,
353 $blobAddress = $this->blobStore
->storeBlob( $data, $blobHints );
355 $blobAddress = $slot->getAddress();
356 $model = $slot->getModel();
357 $format = $slot->getFormat();
360 $textId = $this->blobStore
->getTextIdFromAddress( $blobAddress );
363 throw new LogicException(
364 'Blob address not supported in 1.29 database schema: ' . $blobAddress
368 // getTextIdFromAddress() is free to insert something into the text table, so $textId
369 // may be a new value, not anything already contained in $blobAddress.
370 $blobAddress = 'tt:' . $textId;
372 $comment = $this->failOnNull( $rev->getComment( RevisionRecord
::RAW
), 'comment' );
373 $user = $this->failOnNull( $rev->getUser( RevisionRecord
::RAW
), 'user' );
374 $timestamp = $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
376 # Record the edit in revisions
378 'rev_page' => $pageId,
379 'rev_parent_id' => $parentId,
380 'rev_text_id' => $textId,
381 'rev_minor_edit' => $rev->isMinor() ?
1 : 0,
382 'rev_user' => $this->failOnNull( $user->getId(), 'user field' ),
383 'rev_user_text' => $this->failOnEmpty( $user->getName(), 'user_text field' ),
384 'rev_timestamp' => $dbw->timestamp( $timestamp ),
385 'rev_deleted' => $rev->getVisibility(),
390 if ( $rev->getId() !== null ) {
391 // Needed to restore revisions with their original ID
392 $row['rev_id'] = $rev->getId();
395 list( $commentFields, $commentCallback ) =
396 CommentStore
::getStore()->insertWithTempTable( $dbw, 'rev_comment', $comment );
397 $row +
= $commentFields;
399 if ( $this->contentHandlerUseDB
) {
400 // MCR migration note: rev_content_model and rev_content_format will go away
402 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
403 $defaultFormat = ContentHandler
::getForModelID( $defaultModel )->getDefaultFormat();
405 $row['rev_content_model'] = ( $model === $defaultModel ) ?
null : $model;
406 $row['rev_content_format'] = ( $format === $defaultFormat ) ?
null : $format;
409 $dbw->insert( 'revision', $row, __METHOD__
);
411 if ( !isset( $row['rev_id'] ) ) {
412 // only if auto-increment was used
413 $row['rev_id'] = intval( $dbw->insertId() );
415 $commentCallback( $row['rev_id'] );
417 // Insert IP revision into ip_changes for use when querying for a range.
418 if ( $row['rev_user'] === 0 && IP
::isValid( $row['rev_user_text'] ) ) {
420 'ipc_rev_id' => $row['rev_id'],
421 'ipc_rev_timestamp' => $row['rev_timestamp'],
422 'ipc_hex' => IP
::toHex( $row['rev_user_text'] ),
424 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__
);
427 $newSlot = SlotRecord
::newSaved( $row['rev_id'], $blobAddress, $slot );
428 $slots = new RevisionSlots( [ 'main' => $newSlot ] );
430 $user = new UserIdentityValue( intval( $row['rev_user'] ), $row['rev_user_text'] );
432 $rev = new RevisionStoreRecord(
441 $newSlot = $rev->getSlot( 'main', RevisionRecord
::RAW
);
444 Assert
::postcondition( $rev->getId() > 0, 'revision must have an ID' );
445 Assert
::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
446 Assert
::postcondition(
447 $rev->getComment( RevisionRecord
::RAW
) !== null,
448 'revision must have a comment'
450 Assert
::postcondition(
451 $rev->getUser( RevisionRecord
::RAW
) !== null,
452 'revision must have a user'
455 Assert
::postcondition( $newSlot !== null, 'revision must have a main slot' );
456 Assert
::postcondition(
457 $newSlot->getAddress() !== null,
458 'main slot must have an addess'
461 Hooks
::run( 'RevisionRecordInserted', [ $rev ] );
467 * MCR migration note: this corresponds to Revision::checkContentModel
469 * @param Content $content
470 * @param Title $title
472 * @throws MWException
473 * @throws MWUnknownContentModelException
475 private function checkContentModel( Content
$content, Title
$title ) {
476 // Note: may return null for revisions that have not yet been inserted
478 $model = $content->getModel();
479 $format = $content->getDefaultFormat();
480 $handler = $content->getContentHandler();
484 if ( !$handler->isSupportedFormat( $format ) ) {
485 throw new MWException( "Can't use format $format with content model $model on $name" );
488 if ( !$this->contentHandlerUseDB
) {
489 // if $wgContentHandlerUseDB is not set,
490 // all revisions must use the default content model and format.
492 $defaultModel = ContentHandler
::getDefaultModelFor( $title );
493 $defaultHandler = ContentHandler
::getForModelID( $defaultModel );
494 $defaultFormat = $defaultHandler->getDefaultFormat();
496 if ( $model != $defaultModel ) {
497 throw new MWException( "Can't save non-default content model with "
498 . "\$wgContentHandlerUseDB disabled: model is $model, "
499 . "default for $name is $defaultModel"
503 if ( $format != $defaultFormat ) {
504 throw new MWException( "Can't use non-default content format with "
505 . "\$wgContentHandlerUseDB disabled: format is $format, "
506 . "default for $name is $defaultFormat"
511 if ( !$content->isValid() ) {
512 throw new MWException(
513 "New content for $name is not valid! Content model is $model"
519 * Create a new null-revision for insertion into a page's
520 * history. This will not re-save the text, but simply refer
521 * to the text from the previous version.
523 * Such revisions can for instance identify page rename
524 * operations and other such meta-modifications.
526 * MCR migration note: this replaces Revision::newNullRevision
528 * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
531 * @param IDatabase $dbw
532 * @param Title $title Title of the page to read from
533 * @param CommentStoreComment $comment RevisionRecord's summary
534 * @param bool $minor Whether the revision should be considered as minor
535 * @param User $user The user to attribute the revision to
536 * @return RevisionRecord|null RevisionRecord or null on error
538 public function newNullRevision(
541 CommentStoreComment
$comment,
545 $this->checkDatabaseWikiId( $dbw );
547 $fields = [ 'page_latest', 'page_namespace', 'page_title',
548 'rev_id', 'rev_text_id', 'rev_len', 'rev_sha1' ];
550 if ( $this->contentHandlerUseDB
) {
551 $fields[] = 'rev_content_model';
552 $fields[] = 'rev_content_format';
555 $current = $dbw->selectRow(
556 [ 'page', 'revision' ],
559 'page_id' => $title->getArticleID(),
560 'page_latest=rev_id',
563 [ 'FOR UPDATE' ] // T51581
568 'page' => $title->getArticleID(),
569 'user_text' => $user->getName(),
570 'user' => $user->getId(),
571 'comment' => $comment,
572 'minor_edit' => $minor,
573 'text_id' => $current->rev_text_id
,
574 'parent_id' => $current->page_latest
,
575 'len' => $current->rev_len
,
576 'sha1' => $current->rev_sha1
579 if ( $this->contentHandlerUseDB
) {
580 $fields['content_model'] = $current->rev_content_model
;
581 $fields['content_format'] = $current->rev_content_format
;
584 $fields['title'] = Title
::makeTitle( $current->page_namespace
, $current->page_title
);
586 $mainSlot = $this->emulateMainSlot_1_29( $fields, 0, $title );
587 $revision = new MutableRevisionRecord( $title, $this->wikiId
);
588 $this->initializeMutableRevisionFromArray( $revision, $fields );
589 $revision->setSlot( $mainSlot );
598 * MCR migration note: this replaces Revision::isUnpatrolled
600 * @todo This is overly specific, so move or kill this method.
602 * @param RevisionRecord $rev
604 * @return int Rcid of the unpatrolled row, zero if there isn't one
606 public function getRcIdIfUnpatrolled( RevisionRecord
$rev ) {
607 $rc = $this->getRecentChange( $rev );
608 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
609 return $rc->getAttribute( 'rc_id' );
616 * Get the RC object belonging to the current revision, if there's one
618 * MCR migration note: this replaces Revision::getRecentChange
620 * @todo move this somewhere else?
622 * @param RevisionRecord $rev
623 * @param int $flags (optional) $flags include:
624 * IDBAccessObject::READ_LATEST: Select the data from the master
626 * @return null|RecentChange
628 public function getRecentChange( RevisionRecord
$rev, $flags = 0 ) {
629 $dbr = $this->getDBConnection( DB_REPLICA
);
631 list( $dbType, ) = DBAccessObjectUtils
::getDBOptions( $flags );
633 $userIdentity = $rev->getUser( RevisionRecord
::RAW
);
635 if ( !$userIdentity ) {
636 // If the revision has no user identity, chances are it never went
637 // into the database, and doesn't have an RC entry.
641 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
642 $rc = RecentChange
::newFromConds(
644 'rc_user_text' => $userIdentity->getName(),
645 'rc_timestamp' => $dbr->timestamp( $rev->getTimestamp() ),
646 'rc_this_oldid' => $rev->getId()
652 $this->releaseDBConnection( $dbr );
654 // XXX: cache this locally? Glue it to the RevisionRecord?
659 * Maps fields of the archive row to corresponding revision rows.
661 * @param object $archiveRow
663 * @return object a revision row object, corresponding to $archiveRow.
665 private static function mapArchiveFields( $archiveRow ) {
667 // keep with ar prefix:
670 // not the same suffix:
671 'ar_page_id' => 'rev_page',
672 'ar_rev_id' => 'rev_id',
675 'ar_text_id' => 'rev_text_id',
676 'ar_timestamp' => 'rev_timestamp',
677 'ar_user_text' => 'rev_user_text',
678 'ar_user' => 'rev_user',
679 'ar_minor_edit' => 'rev_minor_edit',
680 'ar_deleted' => 'rev_deleted',
681 'ar_len' => 'rev_len',
682 'ar_parent_id' => 'rev_parent_id',
683 'ar_sha1' => 'rev_sha1',
684 'ar_comment' => 'rev_comment',
685 'ar_comment_cid' => 'rev_comment_cid',
686 'ar_comment_id' => 'rev_comment_id',
687 'ar_comment_text' => 'rev_comment_text',
688 'ar_comment_data' => 'rev_comment_data',
689 'ar_comment_old' => 'rev_comment_old',
690 'ar_content_format' => 'rev_content_format',
691 'ar_content_model' => 'rev_content_model',
694 if ( empty( $archiveRow->ar_text_id
) ) {
695 $fieldMap['ar_text'] = 'old_text';
696 $fieldMap['ar_flags'] = 'old_flags';
699 $revRow = new stdClass();
700 foreach ( $fieldMap as $arKey => $revKey ) {
701 if ( property_exists( $archiveRow, $arKey ) ) {
702 $revRow->$revKey = $archiveRow->$arKey;
710 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
712 * @param object|array $row Either a database row or an array
713 * @param int $queryFlags for callbacks
714 * @param Title $title
716 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
717 * @throws MWException
719 private function emulateMainSlot_1_29( $row, $queryFlags, Title
$title ) {
720 $mainSlotRow = new stdClass();
721 $mainSlotRow->role_name
= 'main';
727 if ( is_object( $row ) ) {
729 if ( !isset( $row->rev_id
) && isset( $row->ar_user
) ) {
730 $row = $this->mapArchiveFields( $row );
733 if ( isset( $row->rev_text_id
) && $row->rev_text_id
> 0 ) {
734 $mainSlotRow->cont_address
= 'tt:' . $row->rev_text_id
;
735 } elseif ( isset( $row->ar_id
) ) {
736 $mainSlotRow->cont_address
= 'ar:' . $row->ar_id
;
739 if ( isset( $row->old_text
) ) {
740 // this happens when the text-table gets joined directly, in the pre-1.30 schema
741 $blobData = isset( $row->old_text
) ?
strval( $row->old_text
) : null;
742 // Check against selects that might have not included old_flags
743 if ( !property_exists( $row, 'old_flags' ) ) {
744 throw new InvalidArgumentException( 'old_flags was not set in $row' );
746 $blobFlags = ( $row->old_flags
=== null ) ?
'' : $row->old_flags
;
749 $mainSlotRow->slot_revision
= intval( $row->rev_id
);
751 $mainSlotRow->cont_size
= isset( $row->rev_len
) ?
intval( $row->rev_len
) : null;
752 $mainSlotRow->cont_sha1
= isset( $row->rev_sha1
) ?
strval( $row->rev_sha1
) : null;
753 $mainSlotRow->model_name
= isset( $row->rev_content_model
)
754 ?
strval( $row->rev_content_model
)
756 // XXX: in the future, we'll probably always use the default format, and drop content_format
757 $mainSlotRow->format_name
= isset( $row->rev_content_format
)
758 ?
strval( $row->rev_content_format
)
760 } elseif ( is_array( $row ) ) {
761 $mainSlotRow->slot_revision
= isset( $row['id'] ) ?
intval( $row['id'] ) : null;
763 $mainSlotRow->cont_address
= isset( $row['text_id'] )
764 ?
'tt:' . intval( $row['text_id'] )
766 $mainSlotRow->cont_size
= isset( $row['len'] ) ?
intval( $row['len'] ) : null;
767 $mainSlotRow->cont_sha1
= isset( $row['sha1'] ) ?
strval( $row['sha1'] ) : null;
769 $mainSlotRow->model_name
= isset( $row['content_model'] )
770 ?
strval( $row['content_model'] ) : null; // XXX: must be a string!
771 // XXX: in the future, we'll probably always use the default format, and drop content_format
772 $mainSlotRow->format_name
= isset( $row['content_format'] )
773 ?
strval( $row['content_format'] ) : null;
774 $blobData = isset( $row['text'] ) ?
rtrim( strval( $row['text'] ) ) : null;
775 // XXX: If the flags field is not set then $blobFlags should be null so that no
776 // decoding will happen. An empty string will result in default decodings.
777 $blobFlags = isset( $row['flags'] ) ?
trim( strval( $row['flags'] ) ) : null;
779 // if we have a Content object, override mText and mContentModel
780 if ( !empty( $row['content'] ) ) {
781 if ( !( $row['content'] instanceof Content
) ) {
782 throw new MWException( 'content field must contain a Content object.' );
785 /** @var Content $content */
786 $content = $row['content'];
787 $handler = $content->getContentHandler();
789 $mainSlotRow->model_name
= $content->getModel();
791 // XXX: in the future, we'll probably always use the default format.
792 if ( $mainSlotRow->format_name
=== null ) {
793 $mainSlotRow->format_name
= $handler->getDefaultFormat();
797 throw new MWException( 'Revision constructor passed invalid row format.' );
800 // With the old schema, the content changes with every revision.
801 // ...except for null-revisions. Would be nice if we could detect them.
802 $mainSlotRow->slot_inherited
= 0;
804 if ( $mainSlotRow->model_name
=== null ) {
805 $mainSlotRow->model_name
= function ( SlotRecord
$slot ) use ( $title ) {
806 // TODO: MCR: consider slot role in getDefaultModelFor()! Use LinkTarget!
807 // TODO: MCR: deprecate $title->getModel().
808 return ContentHandler
::getDefaultModelFor( $title );
813 $content = function ( SlotRecord
$slot )
814 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
816 return $this->loadSlotContent(
820 $mainSlotRow->format_name
,
826 return new SlotRecord( $mainSlotRow, $content );
830 * Loads a Content object based on a slot row.
832 * This method does not call $slot->getContent(), and may be used as a callback
833 * called by $slot->getContent().
835 * MCR migration note: this roughly corresponds to Revision::getContentInternal
837 * @param SlotRecord $slot The SlotRecord to load content for
838 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
839 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
840 * Use null if no processing should happen. That is in constrast to the empty string,
841 * which causes the blob to be decoded according to the configured legacy encoding.
842 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
843 * @param int $queryFlags
845 * @throw RevisionAccessException
848 private function loadSlotContent(
855 if ( $blobData !== null ) {
856 Assert
::parameterType( 'string', $blobData, '$blobData' );
857 Assert
::parameterType( 'string|null', $blobFlags, '$blobFlags' );
859 $cacheKey = $slot->hasAddress() ?
$slot->getAddress() : null;
861 if ( $blobFlags === null ) {
862 // No blob flags, so use the blob verbatim.
865 $data = $this->blobStore
->expandBlob( $blobData, $blobFlags, $cacheKey );
866 if ( $data === false ) {
867 throw new RevisionAccessException(
868 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
874 $address = $slot->getAddress();
876 $data = $this->blobStore
->getBlob( $address, $queryFlags );
877 } catch ( BlobAccessException
$e ) {
878 throw new RevisionAccessException(
879 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
884 // Unserialize content
885 $handler = ContentHandler
::getForModelID( $slot->getModel() );
887 $content = $handler->unserializeContent( $data, $blobFormat );
892 * Load a page revision from a given revision ID number.
893 * Returns null if no such revision can be found.
895 * MCR migration note: this replaces Revision::newFromId
898 * IDBAccessObject::READ_LATEST: Select the data from the master
899 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
902 * @param int $flags (optional)
903 * @return RevisionRecord|null
905 public function getRevisionById( $id, $flags = 0 ) {
906 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
910 * Load either the current, or a specified, revision
911 * that's attached to a given link target. If not attached
912 * to that link target, will return null.
914 * MCR migration note: this replaces Revision::newFromTitle
917 * IDBAccessObject::READ_LATEST: Select the data from the master
918 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
920 * @param LinkTarget $linkTarget
921 * @param int $revId (optional)
922 * @param int $flags Bitfield (optional)
923 * @return RevisionRecord|null
925 public function getRevisionByTitle( LinkTarget
$linkTarget, $revId = 0, $flags = 0 ) {
927 'page_namespace' => $linkTarget->getNamespace(),
928 'page_title' => $linkTarget->getDBkey()
931 // Use the specified revision ID.
932 // Note that we use newRevisionFromConds here because we want to retry
933 // and fall back to master if the page is not found on a replica.
934 // Since the caller supplied a revision ID, we are pretty sure the revision is
935 // supposed to exist, so we should try hard to find it.
936 $conds['rev_id'] = $revId;
937 return $this->newRevisionFromConds( $conds, $flags );
939 // Use a join to get the latest revision.
940 // Note that we don't use newRevisionFromConds here because we don't want to retry
941 // and fall back to master. The assumption is that we only want to force the fallback
942 // if we are quite sure the revision exists because the caller supplied a revision ID.
943 // If the page isn't found at all on a replica, it probably simply does not exist.
944 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
946 $conds[] = 'rev_id=page_latest';
947 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
949 $this->releaseDBConnection( $db );
955 * Load either the current, or a specified, revision
956 * that's attached to a given page ID.
957 * Returns null if no such revision can be found.
959 * MCR migration note: this replaces Revision::newFromPageId
962 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
963 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
966 * @param int $revId (optional)
967 * @param int $flags Bitfield (optional)
968 * @return RevisionRecord|null
970 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
971 $conds = [ 'page_id' => $pageId ];
973 // Use the specified revision ID.
974 // Note that we use newRevisionFromConds here because we want to retry
975 // and fall back to master if the page is not found on a replica.
976 // Since the caller supplied a revision ID, we are pretty sure the revision is
977 // supposed to exist, so we should try hard to find it.
978 $conds['rev_id'] = $revId;
979 return $this->newRevisionFromConds( $conds, $flags );
981 // Use a join to get the latest revision.
982 // Note that we don't use newRevisionFromConds here because we don't want to retry
983 // and fall back to master. The assumption is that we only want to force the fallback
984 // if we are quite sure the revision exists because the caller supplied a revision ID.
985 // If the page isn't found at all on a replica, it probably simply does not exist.
986 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
988 $conds[] = 'rev_id=page_latest';
989 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
991 $this->releaseDBConnection( $db );
997 * Load the revision for the given title with the given timestamp.
998 * WARNING: Timestamps may in some circumstances not be unique,
999 * so this isn't the best key to use.
1001 * MCR migration note: this replaces Revision::loadFromTimestamp
1003 * @param Title $title
1004 * @param string $timestamp
1005 * @return RevisionRecord|null
1007 public function getRevisionByTimestamp( $title, $timestamp ) {
1008 return $this->newRevisionFromConds(
1010 'rev_timestamp' => $timestamp,
1011 'page_namespace' => $title->getNamespace(),
1012 'page_title' => $title->getDBkey()
1020 * Make a fake revision object from an archive table row. This is queried
1021 * for permissions or even inserted (as in Special:Undelete)
1023 * MCR migration note: this replaces Revision::newFromArchiveRow
1025 * @param object $row
1026 * @param int $queryFlags
1027 * @param Title|null $title
1028 * @param array $overrides associative array with fields of $row to override. This may be
1029 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1030 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1031 * override ar_parent_id.
1033 * @return RevisionRecord
1034 * @throws MWException
1036 public function newRevisionFromArchiveRow(
1039 Title
$title = null,
1040 array $overrides = []
1042 Assert
::parameterType( 'object', $row, '$row' );
1044 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1045 Assert
::parameterType( 'integer', $queryFlags, '$queryFlags' );
1047 if ( !$title && isset( $overrides['title'] ) ) {
1048 if ( !( $overrides['title'] instanceof Title
) ) {
1049 throw new MWException( 'title field override must contain a Title object.' );
1052 $title = $overrides['title'];
1055 if ( !isset( $title ) ) {
1056 if ( isset( $row->ar_namespace
) && isset( $row->ar_title
) ) {
1057 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
1059 throw new InvalidArgumentException(
1060 'A Title or ar_namespace and ar_title must be given'
1065 foreach ( $overrides as $key => $value ) {
1067 $row->$field = $value;
1070 $user = $this->getUserIdentityFromRowObject( $row, 'ar_' );
1072 $comment = CommentStore
::getStore()
1073 // Legacy because $row may have come from self::selectFields()
1074 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA
), 'ar_comment', $row, true );
1076 $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
1077 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1079 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1083 * @param object $row
1084 * @param string $prefix Field prefix, such as 'rev_' or 'ar_'.
1086 * @return UserIdentityValue
1088 private function getUserIdentityFromRowObject( $row, $prefix = 'rev_' ) {
1089 $idField = "{$prefix}user";
1090 $nameField = "{$prefix}user_text";
1092 $userId = intval( $row->$idField );
1094 if ( isset( $row->user_name
) ) {
1095 $userName = $row->user_name
;
1096 } elseif ( isset( $row->$nameField ) ) {
1097 $userName = $row->$nameField;
1099 $userName = User
::whoIs( $userId );
1102 if ( $userName === false ) {
1103 wfWarn( __METHOD__
. ': Cannot determine user name for user ID ' . $userId );
1107 return new UserIdentityValue( $userId, $userName );
1111 * @see RevisionFactory::newRevisionFromRow_1_29
1113 * MCR migration note: this replaces Revision::newFromRow
1115 * @param object $row
1116 * @param int $queryFlags
1117 * @param Title|null $title
1119 * @return RevisionRecord
1120 * @throws MWException
1121 * @throws RevisionAccessException
1123 private function newRevisionFromRow_1_29( $row, $queryFlags = 0, Title
$title = null ) {
1124 Assert
::parameterType( 'object', $row, '$row' );
1127 $pageId = isset( $row->rev_page
) ?
$row->rev_page
: 0; // XXX: also check page_id?
1128 $revId = isset( $row->rev_id
) ?
$row->rev_id
: 0;
1130 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1133 if ( !isset( $row->page_latest
) ) {
1134 $row->page_latest
= $title->getLatestRevID();
1135 if ( $row->page_latest
=== 0 && $title->exists() ) {
1136 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1140 $user = $this->getUserIdentityFromRowObject( $row );
1142 $comment = CommentStore
::getStore()
1143 // Legacy because $row may have come from self::selectFields()
1144 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA
), 'rev_comment', $row, true );
1146 $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
1147 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1149 return new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $this->wikiId
);
1153 * @see RevisionFactory::newRevisionFromRow
1155 * MCR migration note: this replaces Revision::newFromRow
1157 * @param object $row
1158 * @param int $queryFlags
1159 * @param Title|null $title
1161 * @return RevisionRecord
1163 public function newRevisionFromRow( $row, $queryFlags = 0, Title
$title = null ) {
1164 return $this->newRevisionFromRow_1_29( $row, $queryFlags, $title );
1168 * Constructs a new MutableRevisionRecord based on the given associative array following
1169 * the MW1.29 convention for the Revision constructor.
1171 * MCR migration note: this replaces Revision::newFromRow
1173 * @param array $fields
1174 * @param int $queryFlags
1175 * @param Title|null $title
1177 * @return MutableRevisionRecord
1178 * @throws MWException
1179 * @throws RevisionAccessException
1181 public function newMutableRevisionFromArray(
1186 if ( !$title && isset( $fields['title'] ) ) {
1187 if ( !( $fields['title'] instanceof Title
) ) {
1188 throw new MWException( 'title field must contain a Title object.' );
1191 $title = $fields['title'];
1195 $pageId = isset( $fields['page'] ) ?
$fields['page'] : 0;
1196 $revId = isset( $fields['id'] ) ?
$fields['id'] : 0;
1198 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1201 if ( !isset( $fields['page'] ) ) {
1202 $fields['page'] = $title->getArticleID( $queryFlags );
1205 // if we have a content object, use it to set the model and type
1206 if ( !empty( $fields['content'] ) ) {
1207 if ( !( $fields['content'] instanceof Content
) ) {
1208 throw new MWException( 'content field must contain a Content object.' );
1211 if ( !empty( $fields['text_id'] ) ) {
1212 throw new MWException(
1213 "Text already stored in external store (id {$fields['text_id']}), " .
1214 "can't serialize content object"
1219 // Replaces old lazy loading logic in Revision::getUserText.
1220 if ( !isset( $fields['user_text'] ) && isset( $fields['user'] ) ) {
1221 if ( $fields['user'] instanceof UserIdentity
) {
1222 /** @var User $user */
1223 $user = $fields['user'];
1224 $fields['user_text'] = $user->getName();
1225 $fields['user'] = $user->getId();
1227 // TODO: wrap this in a callback to make it lazy again.
1228 $name = $fields['user'] === 0 ?
false : User
::whoIs( $fields['user'] );
1230 if ( $name === false ) {
1231 throw new MWException(
1232 'user_text not given, and unknown user ID ' . $fields['user']
1236 $fields['user_text'] = $name;
1241 isset( $fields['comment'] )
1242 && !( $fields['comment'] instanceof CommentStoreComment
)
1244 $commentData = isset( $fields['comment_data'] ) ?
$fields['comment_data'] : null;
1246 if ( $fields['comment'] instanceof Message
) {
1247 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1252 $commentText = trim( strval( $fields['comment'] ) );
1253 $fields['comment'] = CommentStoreComment
::newUnsavedComment(
1260 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1262 $revision = new MutableRevisionRecord( $title, $this->wikiId
);
1263 $this->initializeMutableRevisionFromArray( $revision, $fields );
1264 $revision->setSlot( $mainSlot );
1270 * @param MutableRevisionRecord $record
1271 * @param array $fields
1273 private function initializeMutableRevisionFromArray(
1274 MutableRevisionRecord
$record,
1277 /** @var UserIdentity $user */
1280 if ( isset( $fields['user'] ) && ( $fields['user'] instanceof UserIdentity
) ) {
1281 $user = $fields['user'];
1282 } elseif ( isset( $fields['user'] ) && isset( $fields['user_text'] ) ) {
1283 $user = new UserIdentityValue( intval( $fields['user'] ), $fields['user_text'] );
1284 } elseif ( isset( $fields['user'] ) ) {
1285 $user = User
::newFromId( intval( $fields['user'] ) );
1286 } elseif ( isset( $fields['user_text'] ) ) {
1287 $user = User
::newFromName( $fields['user_text'] );
1289 // User::newFromName will return false for IP addresses (and invalid names)
1290 if ( $user == false ) {
1291 $user = new UserIdentityValue( 0, $fields['user_text'] );
1296 $record->setUser( $user );
1299 $timestamp = isset( $fields['timestamp'] )
1300 ?
strval( $fields['timestamp'] )
1301 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1303 $record->setTimestamp( $timestamp );
1305 if ( isset( $fields['page'] ) ) {
1306 $record->setPageId( intval( $fields['page'] ) );
1309 if ( isset( $fields['id'] ) ) {
1310 $record->setId( intval( $fields['id'] ) );
1312 if ( isset( $fields['parent_id'] ) ) {
1313 $record->setParentId( intval( $fields['parent_id'] ) );
1316 if ( isset( $fields['sha1'] ) ) {
1317 $record->setSha1( $fields['sha1'] );
1319 if ( isset( $fields['size'] ) ) {
1320 $record->setSize( intval( $fields['size'] ) );
1323 if ( isset( $fields['minor_edit'] ) ) {
1324 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
1326 if ( isset( $fields['deleted'] ) ) {
1327 $record->setVisibility( intval( $fields['deleted'] ) );
1330 if ( isset( $fields['comment'] ) ) {
1331 Assert
::parameterType(
1332 CommentStoreComment
::class,
1336 $record->setComment( $fields['comment'] );
1341 * Load a page revision from a given revision ID number.
1342 * Returns null if no such revision can be found.
1344 * MCR migration note: this corresponds to Revision::loadFromId
1346 * @note direct use is deprecated!
1347 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
1349 * @param IDatabase $db
1352 * @return RevisionRecord|null
1354 public function loadRevisionFromId( IDatabase
$db, $id ) {
1355 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
1359 * Load either the current, or a specified, revision
1360 * that's attached to a given page. If not attached
1361 * to that page, will return null.
1363 * MCR migration note: this replaces Revision::loadFromPageId
1365 * @note direct use is deprecated!
1366 * @todo remove when unused!
1368 * @param IDatabase $db
1369 * @param int $pageid
1371 * @return RevisionRecord|null
1373 public function loadRevisionFromPageId( IDatabase
$db, $pageid, $id = 0 ) {
1374 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
1376 $conds['rev_id'] = intval( $id );
1378 $conds[] = 'rev_id=page_latest';
1380 return $this->loadRevisionFromConds( $db, $conds );
1384 * Load either the current, or a specified, revision
1385 * that's attached to a given page. If not attached
1386 * to that page, will return null.
1388 * MCR migration note: this replaces Revision::loadFromTitle
1390 * @note direct use is deprecated!
1391 * @todo remove when unused!
1393 * @param IDatabase $db
1394 * @param Title $title
1397 * @return RevisionRecord|null
1399 public function loadRevisionFromTitle( IDatabase
$db, $title, $id = 0 ) {
1401 $matchId = intval( $id );
1403 $matchId = 'page_latest';
1406 return $this->loadRevisionFromConds(
1410 'page_namespace' => $title->getNamespace(),
1411 'page_title' => $title->getDBkey()
1419 * Load the revision for the given title with the given timestamp.
1420 * WARNING: Timestamps may in some circumstances not be unique,
1421 * so this isn't the best key to use.
1423 * MCR migration note: this replaces Revision::loadFromTimestamp
1425 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
1426 * @todo remove when unused!
1428 * @param IDatabase $db
1429 * @param Title $title
1430 * @param string $timestamp
1431 * @return RevisionRecord|null
1433 public function loadRevisionFromTimestamp( IDatabase
$db, $title, $timestamp ) {
1434 return $this->loadRevisionFromConds( $db,
1436 'rev_timestamp' => $db->timestamp( $timestamp ),
1437 'page_namespace' => $title->getNamespace(),
1438 'page_title' => $title->getDBkey()
1446 * Given a set of conditions, fetch a revision
1448 * This method should be used if we are pretty sure the revision exists.
1449 * Unless $flags has READ_LATEST set, this method will first try to find the revision
1450 * on a replica before hitting the master database.
1452 * MCR migration note: this corresponds to Revision::newFromConds
1454 * @param array $conditions
1455 * @param int $flags (optional)
1456 * @param Title $title
1458 * @return RevisionRecord|null
1460 private function newRevisionFromConds( $conditions, $flags = 0, Title
$title = null ) {
1461 $db = $this->getDBConnection( ( $flags & self
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
);
1462 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1463 $this->releaseDBConnection( $db );
1465 $lb = $this->getDBLoadBalancer();
1467 // Make sure new pending/committed revision are visibile later on
1468 // within web requests to certain avoid bugs like T93866 and T94407.
1470 && !( $flags & self
::READ_LATEST
)
1471 && $lb->getServerCount() > 1
1472 && $lb->hasOrMadeRecentMasterChanges()
1474 $flags = self
::READ_LATEST
;
1475 $db = $this->getDBConnection( DB_MASTER
);
1476 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1477 $this->releaseDBConnection( $db );
1484 * Given a set of conditions, fetch a revision from
1485 * the given database connection.
1487 * MCR migration note: this corresponds to Revision::loadFromConds
1489 * @param IDatabase $db
1490 * @param array $conditions
1491 * @param int $flags (optional)
1492 * @param Title $title
1494 * @return RevisionRecord|null
1496 private function loadRevisionFromConds(
1502 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
1504 $rev = $this->newRevisionFromRow( $row, $flags, $title );
1513 * Throws an exception if the given database connection does not belong to the wiki this
1514 * RevisionStore is bound to.
1516 * @param IDatabase $db
1517 * @throws MWException
1519 private function checkDatabaseWikiId( IDatabase
$db ) {
1520 $storeWiki = $this->wikiId
;
1521 $dbWiki = $db->getDomainID();
1523 if ( $dbWiki === $storeWiki ) {
1527 // XXX: we really want the default database ID...
1528 $storeWiki = $storeWiki ?
: wfWikiID();
1529 $dbWiki = $dbWiki ?
: wfWikiID();
1531 if ( $dbWiki === $storeWiki ) {
1535 // HACK: counteract encoding imposed by DatabaseDomain
1536 $storeWiki = str_replace( '?h', '-', $storeWiki );
1537 $dbWiki = str_replace( '?h', '-', $dbWiki );
1539 if ( $dbWiki === $storeWiki ) {
1543 throw new MWException( "RevisionStore for $storeWiki "
1544 . "cannot be used with a DB connection for $dbWiki" );
1548 * Given a set of conditions, return a row with the
1549 * fields necessary to build RevisionRecord objects.
1551 * MCR migration note: this corresponds to Revision::fetchFromConds
1553 * @param IDatabase $db
1554 * @param array $conditions
1555 * @param int $flags (optional)
1557 * @return object|false data row as a raw object
1559 private function fetchRevisionRowFromConds( IDatabase
$db, $conditions, $flags = 0 ) {
1560 $this->checkDatabaseWikiId( $db );
1562 $revQuery = self
::getQueryInfo( [ 'page', 'user' ] );
1564 if ( ( $flags & self
::READ_LOCKING
) == self
::READ_LOCKING
) {
1565 $options[] = 'FOR UPDATE';
1567 return $db->selectRow(
1568 $revQuery['tables'],
1569 $revQuery['fields'],
1578 * Return the tables, fields, and join conditions to be selected to create
1579 * a new revision object.
1581 * MCR migration note: this replaces Revision::getQueryInfo
1585 * @param array $options Any combination of the following strings
1586 * - 'page': Join with the page table, and select fields to identify the page
1587 * - 'user': Join with the user table, and select the user name
1588 * - 'text': Join with the text table, and select fields to load page text
1590 * @return array With three keys:
1591 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
1592 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
1593 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
1595 public function getQueryInfo( $options = [] ) {
1602 $ret['tables'][] = 'revision';
1603 $ret['fields'] = array_merge( $ret['fields'], [
1617 $commentQuery = CommentStore
::getStore()->getJoin( 'rev_comment' );
1618 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
1619 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
1620 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
1622 if ( $this->contentHandlerUseDB
) {
1623 $ret['fields'][] = 'rev_content_format';
1624 $ret['fields'][] = 'rev_content_model';
1627 if ( in_array( 'page', $options, true ) ) {
1628 $ret['tables'][] = 'page';
1629 $ret['fields'] = array_merge( $ret['fields'], [
1637 $ret['joins']['page'] = [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
1640 if ( in_array( 'user', $options, true ) ) {
1641 $ret['tables'][] = 'user';
1642 $ret['fields'] = array_merge( $ret['fields'], [
1645 $ret['joins']['user'] = [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
1648 if ( in_array( 'text', $options, true ) ) {
1649 $ret['tables'][] = 'text';
1650 $ret['fields'] = array_merge( $ret['fields'], [
1654 $ret['joins']['text'] = [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ];
1661 * Return the tables, fields, and join conditions to be selected to create
1662 * a new archived revision object.
1664 * MCR migration note: this replaces Revision::getArchiveQueryInfo
1668 * @return array With three keys:
1669 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
1670 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
1671 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
1673 public function getArchiveQueryInfo() {
1674 $commentQuery = CommentStore
::getStore()->getJoin( 'ar_comment' );
1676 'tables' => [ 'archive' ] +
$commentQuery['tables'],
1693 ] +
$commentQuery['fields'],
1694 'joins' => $commentQuery['joins'],
1697 if ( $this->contentHandlerUseDB
) {
1698 $ret['fields'][] = 'ar_content_format';
1699 $ret['fields'][] = 'ar_content_model';
1706 * Do a batched query for the sizes of a set of revisions.
1708 * MCR migration note: this replaces Revision::getParentLengths
1710 * @param int[] $revIds
1711 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
1712 * of the corresponding revision.
1714 public function getRevisionSizes( array $revIds ) {
1715 return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA
), $revIds );
1719 * Do a batched query for the sizes of a set of revisions.
1721 * MCR migration note: this replaces Revision::getParentLengths
1723 * @deprecated use RevisionStore::getRevisionSizes instead.
1725 * @param IDatabase $db
1726 * @param int[] $revIds
1727 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
1728 * of the corresponding revision.
1730 public function listRevisionSizes( IDatabase
$db, array $revIds ) {
1731 $this->checkDatabaseWikiId( $db );
1735 return $revLens; // empty
1740 [ 'rev_id', 'rev_len' ],
1741 [ 'rev_id' => $revIds ],
1745 foreach ( $res as $row ) {
1746 $revLens[$row->rev_id
] = intval( $row->rev_len
);
1753 * Get previous revision for this title
1755 * MCR migration note: this replaces Revision::getPrevious
1757 * @param RevisionRecord $rev
1758 * @param Title $title if known (optional)
1760 * @return RevisionRecord|null
1762 public function getPreviousRevision( RevisionRecord
$rev, Title
$title = null ) {
1763 if ( $title === null ) {
1764 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
1766 $prev = $title->getPreviousRevisionID( $rev->getId() );
1768 return $this->getRevisionByTitle( $title, $prev );
1774 * Get next revision for this title
1776 * MCR migration note: this replaces Revision::getNext
1778 * @param RevisionRecord $rev
1779 * @param Title $title if known (optional)
1781 * @return RevisionRecord|null
1783 public function getNextRevision( RevisionRecord
$rev, Title
$title = null ) {
1784 if ( $title === null ) {
1785 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
1787 $next = $title->getNextRevisionID( $rev->getId() );
1789 return $this->getRevisionByTitle( $title, $next );
1795 * Get previous revision Id for this page_id
1796 * This is used to populate rev_parent_id on save
1798 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
1800 * @param IDatabase $db
1801 * @param RevisionRecord $rev
1805 private function getPreviousRevisionId( IDatabase
$db, RevisionRecord
$rev ) {
1806 $this->checkDatabaseWikiId( $db );
1808 if ( $rev->getPageId() === null ) {
1811 # Use page_latest if ID is not given
1812 if ( !$rev->getId() ) {
1813 $prevId = $db->selectField(
1814 'page', 'page_latest',
1815 [ 'page_id' => $rev->getPageId() ],
1819 $prevId = $db->selectField(
1820 'revision', 'rev_id',
1821 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
1823 [ 'ORDER BY' => 'rev_id DESC' ]
1826 return intval( $prevId );
1830 * Get rev_timestamp from rev_id, without loading the rest of the row
1832 * MCR migration note: this replaces Revision::getTimestampFromId
1834 * @param Title $title
1837 * @return string|bool False if not found
1839 public function getTimestampFromId( $title, $id, $flags = 0 ) {
1840 $db = $this->getDBConnection(
1841 ( $flags & IDBAccessObject
::READ_LATEST
) ? DB_MASTER
: DB_REPLICA
1844 $conds = [ 'rev_id' => $id ];
1845 $conds['rev_page'] = $title->getArticleID();
1846 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__
);
1848 $this->releaseDBConnection( $db );
1849 return ( $timestamp !== false ) ?
wfTimestamp( TS_MW
, $timestamp ) : false;
1853 * Get count of revisions per page...not very efficient
1855 * MCR migration note: this replaces Revision::countByPageId
1857 * @param IDatabase $db
1858 * @param int $id Page id
1861 public function countRevisionsByPageId( IDatabase
$db, $id ) {
1862 $this->checkDatabaseWikiId( $db );
1864 $row = $db->selectRow( 'revision',
1865 [ 'revCount' => 'COUNT(*)' ],
1866 [ 'rev_page' => $id ],
1870 return intval( $row->revCount
);
1876 * Get count of revisions per page...not very efficient
1878 * MCR migration note: this replaces Revision::countByTitle
1880 * @param IDatabase $db
1881 * @param Title $title
1884 public function countRevisionsByTitle( IDatabase
$db, $title ) {
1885 $id = $title->getArticleID();
1887 return $this->countRevisionsByPageId( $db, $id );
1893 * Check if no edits were made by other users since
1894 * the time a user started editing the page. Limit to
1895 * 50 revisions for the sake of performance.
1897 * MCR migration note: this replaces Revision::userWasLastToEdit
1899 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
1900 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
1901 * has been deprecated since 1.24.
1903 * @param IDatabase $db The Database to perform the check on.
1904 * @param int $pageId The ID of the page in question
1905 * @param int $userId The ID of the user in question
1906 * @param string $since Look at edits since this time
1908 * @return bool True if the given user was the only one to edit since the given timestamp
1910 public function userWasLastToEdit( IDatabase
$db, $pageId, $userId, $since ) {
1911 $this->checkDatabaseWikiId( $db );
1921 'rev_page' => $pageId,
1922 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1925 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ]
1927 foreach ( $res as $row ) {
1928 if ( $row->rev_user
!= $userId ) {
1936 * Load a revision based on a known page ID and current revision ID from the DB
1938 * This method allows for the use of caching, though accessing anything that normally
1939 * requires permission checks (aside from the text) will trigger a small DB lookup.
1941 * MCR migration note: this replaces Revision::newKnownCurrent
1943 * @param Title $title the associated page title
1944 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
1946 * @return RevisionRecord|bool Returns false if missing
1948 public function getKnownCurrentRevision( Title
$title, $revId ) {
1949 $db = $this->getDBConnectionRef( DB_REPLICA
);
1951 $pageId = $title->getArticleID();
1958 $revId = $title->getLatestRevID();
1963 'No latest revision known for page ' . $title->getPrefixedDBkey()
1964 . ' even though it exists with page ID ' . $pageId
1969 $row = $this->cache
->getWithSetCallback(
1970 // Page/rev IDs passed in from DB to reflect history merges
1971 $this->cache
->makeGlobalKey( 'revision-row-1.29', $db->getDomainID(), $pageId, $revId ),
1972 WANObjectCache
::TTL_WEEK
,
1973 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1974 $setOpts +
= Database
::getCacheSetOptions( $db );
1977 'rev_page' => intval( $pageId ),
1978 'page_id' => intval( $pageId ),
1979 'rev_id' => intval( $revId ),
1982 $row = $this->fetchRevisionRowFromConds( $db, $conds );
1983 return $row ?
: false; // don't cache negatives
1987 // Reflect revision deletion and user renames
1989 return $this->newRevisionFromRow( $row, 0, $title );
1995 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.