Merge "filebackend: avoid use of wfWikiId() in FileBackendGroup"
[lhc/web/wiklou.git] / includes / Revision / RevisionStore.php
1 <?php
2 /**
3 * Service for looking up page revisions.
4 *
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.
9 *
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.
14 *
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
19 *
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.
23 *
24 * @file
25 */
26
27 namespace MediaWiki\Revision;
28
29 use ActorMigration;
30 use CommentStore;
31 use CommentStoreComment;
32 use Content;
33 use ContentHandler;
34 use DBAccessObjectUtils;
35 use Hooks;
36 use IDBAccessObject;
37 use InvalidArgumentException;
38 use IP;
39 use LogicException;
40 use MediaWiki\Linker\LinkTarget;
41 use MediaWiki\Storage\BlobAccessException;
42 use MediaWiki\Storage\BlobStore;
43 use MediaWiki\Storage\NameTableAccessException;
44 use MediaWiki\Storage\NameTableStore;
45 use MediaWiki\Storage\SqlBlobStore;
46 use MediaWiki\User\UserIdentity;
47 use MediaWiki\User\UserIdentityValue;
48 use Message;
49 use MWException;
50 use MWUnknownContentModelException;
51 use Psr\Log\LoggerAwareInterface;
52 use Psr\Log\LoggerInterface;
53 use Psr\Log\NullLogger;
54 use RecentChange;
55 use Revision;
56 use RuntimeException;
57 use StatusValue;
58 use stdClass;
59 use Title;
60 use Traversable;
61 use User;
62 use WANObjectCache;
63 use Wikimedia\Assert\Assert;
64 use Wikimedia\Rdbms\Database;
65 use Wikimedia\Rdbms\DBConnRef;
66 use Wikimedia\Rdbms\IDatabase;
67 use Wikimedia\Rdbms\ILoadBalancer;
68 use Wikimedia\Rdbms\IResultWrapper;
69
70 /**
71 * Service for looking up page revisions.
72 *
73 * @since 1.31
74 * @since 1.32 Renamed from MediaWiki\Storage\RevisionStore
75 *
76 * @note This was written to act as a drop-in replacement for the corresponding
77 * static methods in Revision.
78 */
79 class RevisionStore
80 implements IDBAccessObject, RevisionFactory, RevisionLookup, LoggerAwareInterface {
81
82 const ROW_CACHE_KEY = 'revision-row-1.29';
83
84 /**
85 * @var SqlBlobStore
86 */
87 private $blobStore;
88
89 /**
90 * @var bool|string
91 */
92 private $dbDomain;
93
94 /**
95 * @var boolean
96 * @see $wgContentHandlerUseDB
97 */
98 private $contentHandlerUseDB = true;
99
100 /**
101 * @var ILoadBalancer
102 */
103 private $loadBalancer;
104
105 /**
106 * @var WANObjectCache
107 */
108 private $cache;
109
110 /**
111 * @var CommentStore
112 */
113 private $commentStore;
114
115 /**
116 * @var ActorMigration
117 */
118 private $actorMigration;
119
120 /**
121 * @var LoggerInterface
122 */
123 private $logger;
124
125 /**
126 * @var NameTableStore
127 */
128 private $contentModelStore;
129
130 /**
131 * @var NameTableStore
132 */
133 private $slotRoleStore;
134
135 /** @var int An appropriate combination of SCHEMA_COMPAT_XXX flags. */
136 private $mcrMigrationStage;
137
138 /** @var SlotRoleRegistry */
139 private $slotRoleRegistry;
140
141 /**
142 * @todo $blobStore should be allowed to be any BlobStore!
143 *
144 * @param ILoadBalancer $loadBalancer
145 * @param SqlBlobStore $blobStore
146 * @param WANObjectCache $cache A cache for caching revision rows. This can be the local
147 * wiki's default instance even if $dbDomain refers to a different wiki, since
148 * makeGlobalKey() is used to constructed a key that allows cached revision rows from
149 * the same database to be re-used between wikis. For example, enwiki and frwiki will
150 * use the same cache keys for revision rows from the wikidatawiki database, regardless
151 * of the cache's default key space.
152 * @param CommentStore $commentStore
153 * @param NameTableStore $contentModelStore
154 * @param NameTableStore $slotRoleStore
155 * @param SlotRoleRegistry $slotRoleRegistry
156 * @param int $mcrMigrationStage An appropriate combination of SCHEMA_COMPAT_XXX flags
157 * @param ActorMigration $actorMigration
158 * @param bool|string $dbDomain DB domain of the relevant wiki or false for the current one
159 */
160 public function __construct(
161 ILoadBalancer $loadBalancer,
162 SqlBlobStore $blobStore,
163 WANObjectCache $cache,
164 CommentStore $commentStore,
165 NameTableStore $contentModelStore,
166 NameTableStore $slotRoleStore,
167 SlotRoleRegistry $slotRoleRegistry,
168 $mcrMigrationStage,
169 ActorMigration $actorMigration,
170 $dbDomain = false
171 ) {
172 Assert::parameterType( 'string|boolean', $dbDomain, '$dbDomain' );
173 Assert::parameterType( 'integer', $mcrMigrationStage, '$mcrMigrationStage' );
174 Assert::parameter(
175 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH ) !== SCHEMA_COMPAT_READ_BOTH,
176 '$mcrMigrationStage',
177 'Reading from the old and the new schema at the same time is not supported.'
178 );
179 Assert::parameter(
180 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_BOTH ) !== 0,
181 '$mcrMigrationStage',
182 'Reading needs to be enabled for the old or the new schema.'
183 );
184 Assert::parameter(
185 ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_BOTH ) !== 0,
186 '$mcrMigrationStage',
187 'Writing needs to be enabled for the old or the new schema.'
188 );
189 Assert::parameter(
190 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_OLD ) === 0
191 || ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) !== 0,
192 '$mcrMigrationStage',
193 'Cannot read the old schema when not also writing it.'
194 );
195 Assert::parameter(
196 ( $mcrMigrationStage & SCHEMA_COMPAT_READ_NEW ) === 0
197 || ( $mcrMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) !== 0,
198 '$mcrMigrationStage',
199 'Cannot read the new schema when not also writing it.'
200 );
201
202 $this->loadBalancer = $loadBalancer;
203 $this->blobStore = $blobStore;
204 $this->cache = $cache;
205 $this->commentStore = $commentStore;
206 $this->contentModelStore = $contentModelStore;
207 $this->slotRoleStore = $slotRoleStore;
208 $this->slotRoleRegistry = $slotRoleRegistry;
209 $this->mcrMigrationStage = $mcrMigrationStage;
210 $this->actorMigration = $actorMigration;
211 $this->dbDomain = $dbDomain;
212 $this->logger = new NullLogger();
213 }
214
215 /**
216 * @param int $flags A combination of SCHEMA_COMPAT_XXX flags.
217 * @return bool True if all the given flags were set in the $mcrMigrationStage
218 * parameter passed to the constructor.
219 */
220 private function hasMcrSchemaFlags( $flags ) {
221 return ( $this->mcrMigrationStage & $flags ) === $flags;
222 }
223
224 /**
225 * Throws a RevisionAccessException if this RevisionStore is configured for cross-wiki loading
226 * and still reading from the old DB schema.
227 *
228 * @throws RevisionAccessException
229 */
230 private function assertCrossWikiContentLoadingIsSafe() {
231 if ( $this->dbDomain !== false && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
232 throw new RevisionAccessException(
233 "Cross-wiki content loading is not supported by the pre-MCR schema"
234 );
235 }
236 }
237
238 public function setLogger( LoggerInterface $logger ) {
239 $this->logger = $logger;
240 }
241
242 /**
243 * @return bool Whether the store is read-only
244 */
245 public function isReadOnly() {
246 return $this->blobStore->isReadOnly();
247 }
248
249 /**
250 * @return bool
251 */
252 public function getContentHandlerUseDB() {
253 return $this->contentHandlerUseDB;
254 }
255
256 /**
257 * @see $wgContentHandlerUseDB
258 * @param bool $contentHandlerUseDB
259 * @throws MWException
260 */
261 public function setContentHandlerUseDB( $contentHandlerUseDB ) {
262 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW )
263 || $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW )
264 ) {
265 if ( !$contentHandlerUseDB ) {
266 throw new MWException(
267 'Content model must be stored in the database for multi content revision migration.'
268 );
269 }
270 }
271 $this->contentHandlerUseDB = $contentHandlerUseDB;
272 }
273
274 /**
275 * @return ILoadBalancer
276 */
277 private function getDBLoadBalancer() {
278 return $this->loadBalancer;
279 }
280
281 /**
282 * @param int $queryFlags a bit field composed of READ_XXX flags
283 *
284 * @return DBConnRef
285 */
286 private function getDBConnectionRefForQueryFlags( $queryFlags ) {
287 list( $mode, ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
288 return $this->getDBConnectionRef( $mode );
289 }
290
291 /**
292 * @param int $mode DB_MASTER or DB_REPLICA
293 *
294 * @param array $groups
295 * @return DBConnRef
296 */
297 private function getDBConnectionRef( $mode, $groups = [] ) {
298 $lb = $this->getDBLoadBalancer();
299 return $lb->getConnectionRef( $mode, $groups, $this->dbDomain );
300 }
301
302 /**
303 * Determines the page Title based on the available information.
304 *
305 * MCR migration note: this corresponds to Revision::getTitle
306 *
307 * @note this method should be private, external use should be avoided!
308 *
309 * @param int|null $pageId
310 * @param int|null $revId
311 * @param int $queryFlags
312 *
313 * @return Title
314 * @throws RevisionAccessException
315 */
316 public function getTitle( $pageId, $revId, $queryFlags = self::READ_NORMAL ) {
317 if ( !$pageId && !$revId ) {
318 throw new InvalidArgumentException( '$pageId and $revId cannot both be 0 or null' );
319 }
320
321 // This method recalls itself with READ_LATEST if READ_NORMAL doesn't get us a Title
322 // So ignore READ_LATEST_IMMUTABLE flags and handle the fallback logic in this method
323 if ( DBAccessObjectUtils::hasFlags( $queryFlags, self::READ_LATEST_IMMUTABLE ) ) {
324 $queryFlags = self::READ_NORMAL;
325 }
326
327 $canUseTitleNewFromId = ( $pageId !== null && $pageId > 0 && $this->dbDomain === false );
328 list( $dbMode, $dbOptions ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
329
330 // Loading by ID is best, but Title::newFromID does not support that for foreign IDs.
331 if ( $canUseTitleNewFromId ) {
332 $titleFlags = ( $dbMode == DB_MASTER ? Title::READ_LATEST : 0 );
333 // TODO: better foreign title handling (introduce TitleFactory)
334 $title = Title::newFromID( $pageId, $titleFlags );
335 if ( $title ) {
336 return $title;
337 }
338 }
339
340 // rev_id is defined as NOT NULL, but this revision may not yet have been inserted.
341 $canUseRevId = ( $revId !== null && $revId > 0 );
342
343 if ( $canUseRevId ) {
344 $dbr = $this->getDBConnectionRef( $dbMode );
345 // @todo: Title::getSelectFields(), or Title::getQueryInfo(), or something like that
346 $row = $dbr->selectRow(
347 [ 'revision', 'page' ],
348 [
349 'page_namespace',
350 'page_title',
351 'page_id',
352 'page_latest',
353 'page_is_redirect',
354 'page_len',
355 ],
356 [ 'rev_id' => $revId ],
357 __METHOD__,
358 $dbOptions,
359 [ 'page' => [ 'JOIN', 'page_id=rev_page' ] ]
360 );
361 if ( $row ) {
362 // TODO: better foreign title handling (introduce TitleFactory)
363 return Title::newFromRow( $row );
364 }
365 }
366
367 // If we still don't have a title, fallback to master if that wasn't already happening.
368 if ( $dbMode !== DB_MASTER ) {
369 $title = $this->getTitle( $pageId, $revId, self::READ_LATEST );
370 if ( $title ) {
371 $this->logger->info(
372 __METHOD__ . ' fell back to READ_LATEST and got a Title.',
373 [ 'trace' => wfBacktrace() ]
374 );
375 return $title;
376 }
377 }
378
379 throw new RevisionAccessException(
380 "Could not determine title for page ID $pageId and revision ID $revId"
381 );
382 }
383
384 /**
385 * @param mixed $value
386 * @param string $name
387 *
388 * @throws IncompleteRevisionException if $value is null
389 * @return mixed $value, if $value is not null
390 */
391 private function failOnNull( $value, $name ) {
392 if ( $value === null ) {
393 throw new IncompleteRevisionException(
394 "$name must not be " . var_export( $value, true ) . "!"
395 );
396 }
397
398 return $value;
399 }
400
401 /**
402 * @param mixed $value
403 * @param string $name
404 *
405 * @throws IncompleteRevisionException if $value is empty
406 * @return mixed $value, if $value is not null
407 */
408 private function failOnEmpty( $value, $name ) {
409 if ( $value === null || $value === 0 || $value === '' ) {
410 throw new IncompleteRevisionException(
411 "$name must not be " . var_export( $value, true ) . "!"
412 );
413 }
414
415 return $value;
416 }
417
418 /**
419 * Insert a new revision into the database, returning the new revision record
420 * on success and dies horribly on failure.
421 *
422 * MCR migration note: this replaces Revision::insertOn
423 *
424 * @param RevisionRecord $rev
425 * @param IDatabase $dbw (master connection)
426 *
427 * @throws InvalidArgumentException
428 * @return RevisionRecord the new revision record.
429 */
430 public function insertRevisionOn( RevisionRecord $rev, IDatabase $dbw ) {
431 // TODO: pass in a DBTransactionContext instead of a database connection.
432 $this->checkDatabaseDomain( $dbw );
433
434 $slotRoles = $rev->getSlotRoles();
435
436 // Make sure the main slot is always provided throughout migration
437 if ( !in_array( SlotRecord::MAIN, $slotRoles ) ) {
438 throw new InvalidArgumentException(
439 'main slot must be provided'
440 );
441 }
442
443 // If we are not writing into the new schema, we can't support extra slots.
444 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW )
445 && $slotRoles !== [ SlotRecord::MAIN ]
446 ) {
447 throw new InvalidArgumentException(
448 'Only the main slot is supported when not writing to the MCR enabled schema!'
449 );
450 }
451
452 // As long as we are not reading from the new schema, we don't want to write extra slots.
453 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW )
454 && $slotRoles !== [ SlotRecord::MAIN ]
455 ) {
456 throw new InvalidArgumentException(
457 'Only the main slot is supported when not reading from the MCR enabled schema!'
458 );
459 }
460
461 // Checks
462 $this->failOnNull( $rev->getSize(), 'size field' );
463 $this->failOnEmpty( $rev->getSha1(), 'sha1 field' );
464 $this->failOnEmpty( $rev->getTimestamp(), 'timestamp field' );
465 $comment = $this->failOnNull( $rev->getComment( RevisionRecord::RAW ), 'comment' );
466 $user = $this->failOnNull( $rev->getUser( RevisionRecord::RAW ), 'user' );
467 $this->failOnNull( $user->getId(), 'user field' );
468 $this->failOnEmpty( $user->getName(), 'user_text field' );
469
470 if ( !$rev->isReadyForInsertion() ) {
471 // This is here for future-proofing. At the time this check being added, it
472 // was redundant to the individual checks above.
473 throw new IncompleteRevisionException( 'Revision is incomplete' );
474 }
475
476 // TODO: we shouldn't need an actual Title here.
477 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
478 $pageId = $this->failOnEmpty( $rev->getPageId(), 'rev_page field' ); // check this early
479
480 $parentId = $rev->getParentId() === null
481 ? $this->getPreviousRevisionId( $dbw, $rev )
482 : $rev->getParentId();
483
484 /** @var RevisionRecord $rev */
485 $rev = $dbw->doAtomicSection(
486 __METHOD__,
487 function ( IDatabase $dbw, $fname ) use (
488 $rev,
489 $user,
490 $comment,
491 $title,
492 $pageId,
493 $parentId
494 ) {
495 return $this->insertRevisionInternal(
496 $rev,
497 $dbw,
498 $user,
499 $comment,
500 $title,
501 $pageId,
502 $parentId
503 );
504 }
505 );
506
507 // sanity checks
508 Assert::postcondition( $rev->getId() > 0, 'revision must have an ID' );
509 Assert::postcondition( $rev->getPageId() > 0, 'revision must have a page ID' );
510 Assert::postcondition(
511 $rev->getComment( RevisionRecord::RAW ) !== null,
512 'revision must have a comment'
513 );
514 Assert::postcondition(
515 $rev->getUser( RevisionRecord::RAW ) !== null,
516 'revision must have a user'
517 );
518
519 // Trigger exception if the main slot is missing.
520 // Technically, this could go away after MCR migration: while
521 // calling code may require a main slot to exist, RevisionStore
522 // really should not know or care about that requirement.
523 $rev->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
524
525 foreach ( $slotRoles as $role ) {
526 $slot = $rev->getSlot( $role, RevisionRecord::RAW );
527 Assert::postcondition(
528 $slot->getContent() !== null,
529 $role . ' slot must have content'
530 );
531 Assert::postcondition(
532 $slot->hasRevision(),
533 $role . ' slot must have a revision associated'
534 );
535 }
536
537 Hooks::run( 'RevisionRecordInserted', [ $rev ] );
538
539 // TODO: deprecate in 1.32!
540 $legacyRevision = new Revision( $rev );
541 Hooks::run( 'RevisionInsertComplete', [ &$legacyRevision, null, null ] );
542
543 return $rev;
544 }
545
546 private function insertRevisionInternal(
547 RevisionRecord $rev,
548 IDatabase $dbw,
549 User $user,
550 CommentStoreComment $comment,
551 Title $title,
552 $pageId,
553 $parentId
554 ) {
555 $slotRoles = $rev->getSlotRoles();
556
557 $revisionRow = $this->insertRevisionRowOn(
558 $dbw,
559 $rev,
560 $title,
561 $parentId
562 );
563
564 $revisionId = $revisionRow['rev_id'];
565
566 $blobHints = [
567 BlobStore::PAGE_HINT => $pageId,
568 BlobStore::REVISION_HINT => $revisionId,
569 BlobStore::PARENT_HINT => $parentId,
570 ];
571
572 $newSlots = [];
573 foreach ( $slotRoles as $role ) {
574 $slot = $rev->getSlot( $role, RevisionRecord::RAW );
575
576 // If the SlotRecord already has a revision ID set, this means it already exists
577 // in the database, and should already belong to the current revision.
578 // However, a slot may already have a revision, but no content ID, if the slot
579 // is emulated based on the archive table, because we are in SCHEMA_COMPAT_READ_OLD
580 // mode, and the respective archive row was not yet migrated to the new schema.
581 // In that case, a new slot row (and content row) must be inserted even during
582 // undeletion.
583 if ( $slot->hasRevision() && $slot->hasContentId() ) {
584 // TODO: properly abort transaction if the assertion fails!
585 Assert::parameter(
586 $slot->getRevision() === $revisionId,
587 'slot role ' . $slot->getRole(),
588 'Existing slot should belong to revision '
589 . $revisionId . ', but belongs to revision ' . $slot->getRevision() . '!'
590 );
591
592 // Slot exists, nothing to do, move along.
593 // This happens when restoring archived revisions.
594
595 $newSlots[$role] = $slot;
596
597 // Write the main slot's text ID to the revision table for backwards compatibility
598 if ( $slot->getRole() === SlotRecord::MAIN
599 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD )
600 ) {
601 $blobAddress = $slot->getAddress();
602 $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
603 }
604 } else {
605 $newSlots[$role] = $this->insertSlotOn( $dbw, $revisionId, $slot, $title, $blobHints );
606 }
607 }
608
609 $this->insertIpChangesRow( $dbw, $user, $rev, $revisionId );
610
611 $rev = new RevisionStoreRecord(
612 $title,
613 $user,
614 $comment,
615 (object)$revisionRow,
616 new RevisionSlots( $newSlots ),
617 $this->dbDomain
618 );
619
620 return $rev;
621 }
622
623 /**
624 * @param IDatabase $dbw
625 * @param int $revisionId
626 * @param string &$blobAddress (may change!)
627 *
628 * @return int the text row id
629 */
630 private function updateRevisionTextId( IDatabase $dbw, $revisionId, &$blobAddress ) {
631 $textId = $this->blobStore->getTextIdFromAddress( $blobAddress );
632 if ( !$textId ) {
633 throw new LogicException(
634 'Blob address not supported in 1.29 database schema: ' . $blobAddress
635 );
636 }
637
638 // getTextIdFromAddress() is free to insert something into the text table, so $textId
639 // may be a new value, not anything already contained in $blobAddress.
640 $blobAddress = SqlBlobStore::makeAddressFromTextId( $textId );
641
642 $dbw->update(
643 'revision',
644 [ 'rev_text_id' => $textId ],
645 [ 'rev_id' => $revisionId ],
646 __METHOD__
647 );
648
649 return $textId;
650 }
651
652 /**
653 * @param IDatabase $dbw
654 * @param int $revisionId
655 * @param SlotRecord $protoSlot
656 * @param Title $title
657 * @param array $blobHints See the BlobStore::XXX_HINT constants
658 * @return SlotRecord
659 */
660 private function insertSlotOn(
661 IDatabase $dbw,
662 $revisionId,
663 SlotRecord $protoSlot,
664 Title $title,
665 array $blobHints = []
666 ) {
667 if ( $protoSlot->hasAddress() ) {
668 $blobAddress = $protoSlot->getAddress();
669 } else {
670 $blobAddress = $this->storeContentBlob( $protoSlot, $title, $blobHints );
671 }
672
673 $contentId = null;
674
675 // Write the main slot's text ID to the revision table for backwards compatibility
676 if ( $protoSlot->getRole() === SlotRecord::MAIN
677 && $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD )
678 ) {
679 // If SCHEMA_COMPAT_WRITE_NEW is also set, the fake content ID is overwritten
680 // with the real content ID below.
681 $textId = $this->updateRevisionTextId( $dbw, $revisionId, $blobAddress );
682 $contentId = $this->emulateContentId( $textId );
683 }
684
685 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
686 if ( $protoSlot->hasContentId() ) {
687 $contentId = $protoSlot->getContentId();
688 } else {
689 $contentId = $this->insertContentRowOn( $protoSlot, $dbw, $blobAddress );
690 }
691
692 $this->insertSlotRowOn( $protoSlot, $dbw, $revisionId, $contentId );
693 }
694
695 $savedSlot = SlotRecord::newSaved(
696 $revisionId,
697 $contentId,
698 $blobAddress,
699 $protoSlot
700 );
701
702 return $savedSlot;
703 }
704
705 /**
706 * Insert IP revision into ip_changes for use when querying for a range.
707 * @param IDatabase $dbw
708 * @param User $user
709 * @param RevisionRecord $rev
710 * @param int $revisionId
711 */
712 private function insertIpChangesRow(
713 IDatabase $dbw,
714 User $user,
715 RevisionRecord $rev,
716 $revisionId
717 ) {
718 if ( $user->getId() === 0 && IP::isValid( $user->getName() ) ) {
719 $ipcRow = [
720 'ipc_rev_id' => $revisionId,
721 'ipc_rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
722 'ipc_hex' => IP::toHex( $user->getName() ),
723 ];
724 $dbw->insert( 'ip_changes', $ipcRow, __METHOD__ );
725 }
726 }
727
728 /**
729 * @param IDatabase $dbw
730 * @param RevisionRecord $rev
731 * @param Title $title
732 * @param int $parentId
733 *
734 * @return array a revision table row
735 *
736 * @throws MWException
737 * @throws MWUnknownContentModelException
738 */
739 private function insertRevisionRowOn(
740 IDatabase $dbw,
741 RevisionRecord $rev,
742 Title $title,
743 $parentId
744 ) {
745 $revisionRow = $this->getBaseRevisionRow( $dbw, $rev, $title, $parentId );
746
747 list( $commentFields, $commentCallback ) =
748 $this->commentStore->insertWithTempTable(
749 $dbw,
750 'rev_comment',
751 $rev->getComment( RevisionRecord::RAW )
752 );
753 $revisionRow += $commentFields;
754
755 list( $actorFields, $actorCallback ) =
756 $this->actorMigration->getInsertValuesWithTempTable(
757 $dbw,
758 'rev_user',
759 $rev->getUser( RevisionRecord::RAW )
760 );
761 $revisionRow += $actorFields;
762
763 $dbw->insert( 'revision', $revisionRow, __METHOD__ );
764
765 if ( !isset( $revisionRow['rev_id'] ) ) {
766 // only if auto-increment was used
767 $revisionRow['rev_id'] = intval( $dbw->insertId() );
768
769 if ( $dbw->getType() === 'mysql' ) {
770 // (T202032) MySQL until 8.0 and MariaDB until some version after 10.1.34 don't save the
771 // auto-increment value to disk, so on server restart it might reuse IDs from deleted
772 // revisions. We can fix that with an insert with an explicit rev_id value, if necessary.
773
774 $maxRevId = intval( $dbw->selectField( 'archive', 'MAX(ar_rev_id)', '', __METHOD__ ) );
775 $table = 'archive';
776 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
777 $maxRevId2 = intval( $dbw->selectField( 'slots', 'MAX(slot_revision_id)', '', __METHOD__ ) );
778 if ( $maxRevId2 >= $maxRevId ) {
779 $maxRevId = $maxRevId2;
780 $table = 'slots';
781 }
782 }
783
784 if ( $maxRevId >= $revisionRow['rev_id'] ) {
785 $this->logger->debug(
786 '__METHOD__: Inserted revision {revid} but {table} has revisions up to {maxrevid}.'
787 . ' Trying to fix it.',
788 [
789 'revid' => $revisionRow['rev_id'],
790 'table' => $table,
791 'maxrevid' => $maxRevId,
792 ]
793 );
794
795 if ( !$dbw->lock( 'fix-for-T202032', __METHOD__ ) ) {
796 throw new MWException( 'Failed to get database lock for T202032' );
797 }
798 $fname = __METHOD__;
799 $dbw->onTransactionResolution(
800 function ( $trigger, IDatabase $dbw ) use ( $fname ) {
801 $dbw->unlock( 'fix-for-T202032', $fname );
802 }
803 );
804
805 $dbw->delete( 'revision', [ 'rev_id' => $revisionRow['rev_id'] ], __METHOD__ );
806
807 // The locking here is mostly to make MySQL bypass the REPEATABLE-READ transaction
808 // isolation (weird MySQL "feature"). It does seem to block concurrent auto-incrementing
809 // inserts too, though, at least on MariaDB 10.1.29.
810 //
811 // Don't try to lock `revision` in this way, it'll deadlock if there are concurrent
812 // transactions in this code path thanks to the row lock from the original ->insert() above.
813 //
814 // And we have to use raw SQL to bypass the "aggregation used with a locking SELECT" warning
815 // that's for non-MySQL DBs.
816 $row1 = $dbw->query(
817 $dbw->selectSQLText( 'archive', [ 'v' => "MAX(ar_rev_id)" ], '', __METHOD__ ) . ' FOR UPDATE'
818 )->fetchObject();
819 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
820 $row2 = $dbw->query(
821 $dbw->selectSQLText( 'slots', [ 'v' => "MAX(slot_revision_id)" ], '', __METHOD__ )
822 . ' FOR UPDATE'
823 )->fetchObject();
824 } else {
825 $row2 = null;
826 }
827 $maxRevId = max(
828 $maxRevId,
829 $row1 ? intval( $row1->v ) : 0,
830 $row2 ? intval( $row2->v ) : 0
831 );
832
833 // If we don't have SCHEMA_COMPAT_WRITE_NEW, all except the first of any concurrent
834 // transactions will throw a duplicate key error here. It doesn't seem worth trying
835 // to avoid that.
836 $revisionRow['rev_id'] = $maxRevId + 1;
837 $dbw->insert( 'revision', $revisionRow, __METHOD__ );
838 }
839 }
840 }
841
842 $commentCallback( $revisionRow['rev_id'] );
843 $actorCallback( $revisionRow['rev_id'], $revisionRow );
844
845 return $revisionRow;
846 }
847
848 /**
849 * @param IDatabase $dbw
850 * @param RevisionRecord $rev
851 * @param Title $title
852 * @param int $parentId
853 *
854 * @return array [ 0 => array $revisionRow, 1 => callable ]
855 * @throws MWException
856 * @throws MWUnknownContentModelException
857 */
858 private function getBaseRevisionRow(
859 IDatabase $dbw,
860 RevisionRecord $rev,
861 Title $title,
862 $parentId
863 ) {
864 // Record the edit in revisions
865 $revisionRow = [
866 'rev_page' => $rev->getPageId(),
867 'rev_parent_id' => $parentId,
868 'rev_minor_edit' => $rev->isMinor() ? 1 : 0,
869 'rev_timestamp' => $dbw->timestamp( $rev->getTimestamp() ),
870 'rev_deleted' => $rev->getVisibility(),
871 'rev_len' => $rev->getSize(),
872 'rev_sha1' => $rev->getSha1(),
873 ];
874
875 if ( $rev->getId() !== null ) {
876 // Needed to restore revisions with their original ID
877 $revisionRow['rev_id'] = $rev->getId();
878 }
879
880 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD ) ) {
881 // In non MCR mode this IF section will relate to the main slot
882 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
883 $model = $mainSlot->getModel();
884 $format = $mainSlot->getFormat();
885
886 // MCR migration note: rev_content_model and rev_content_format will go away
887 if ( $this->contentHandlerUseDB ) {
888 $this->assertCrossWikiContentLoadingIsSafe();
889
890 $defaultModel = ContentHandler::getDefaultModelFor( $title );
891 $defaultFormat = ContentHandler::getForModelID( $defaultModel )->getDefaultFormat();
892
893 $revisionRow['rev_content_model'] = ( $model === $defaultModel ) ? null : $model;
894 $revisionRow['rev_content_format'] = ( $format === $defaultFormat ) ? null : $format;
895 }
896 }
897
898 return $revisionRow;
899 }
900
901 /**
902 * @param SlotRecord $slot
903 * @param Title $title
904 * @param array $blobHints See the BlobStore::XXX_HINT constants
905 *
906 * @throws MWException
907 * @return string the blob address
908 */
909 private function storeContentBlob(
910 SlotRecord $slot,
911 Title $title,
912 array $blobHints = []
913 ) {
914 $content = $slot->getContent();
915 $format = $content->getDefaultFormat();
916 $model = $content->getModel();
917
918 $this->checkContent( $content, $title, $slot->getRole() );
919
920 return $this->blobStore->storeBlob(
921 $content->serialize( $format ),
922 // These hints "leak" some information from the higher abstraction layer to
923 // low level storage to allow for optimization.
924 array_merge(
925 $blobHints,
926 [
927 BlobStore::DESIGNATION_HINT => 'page-content',
928 BlobStore::ROLE_HINT => $slot->getRole(),
929 BlobStore::SHA1_HINT => $slot->getSha1(),
930 BlobStore::MODEL_HINT => $model,
931 BlobStore::FORMAT_HINT => $format,
932 ]
933 )
934 );
935 }
936
937 /**
938 * @param SlotRecord $slot
939 * @param IDatabase $dbw
940 * @param int $revisionId
941 * @param int $contentId
942 */
943 private function insertSlotRowOn( SlotRecord $slot, IDatabase $dbw, $revisionId, $contentId ) {
944 $slotRow = [
945 'slot_revision_id' => $revisionId,
946 'slot_role_id' => $this->slotRoleStore->acquireId( $slot->getRole() ),
947 'slot_content_id' => $contentId,
948 // If the slot has a specific origin use that ID, otherwise use the ID of the revision
949 // that we just inserted.
950 'slot_origin' => $slot->hasOrigin() ? $slot->getOrigin() : $revisionId,
951 ];
952 $dbw->insert( 'slots', $slotRow, __METHOD__ );
953 }
954
955 /**
956 * @param SlotRecord $slot
957 * @param IDatabase $dbw
958 * @param string $blobAddress
959 * @return int content row ID
960 */
961 private function insertContentRowOn( SlotRecord $slot, IDatabase $dbw, $blobAddress ) {
962 $contentRow = [
963 'content_size' => $slot->getSize(),
964 'content_sha1' => $slot->getSha1(),
965 'content_model' => $this->contentModelStore->acquireId( $slot->getModel() ),
966 'content_address' => $blobAddress,
967 ];
968 $dbw->insert( 'content', $contentRow, __METHOD__ );
969 return intval( $dbw->insertId() );
970 }
971
972 /**
973 * MCR migration note: this corresponds to Revision::checkContentModel
974 *
975 * @param Content $content
976 * @param Title $title
977 * @param string $role
978 *
979 * @throws MWException
980 * @throws MWUnknownContentModelException
981 */
982 private function checkContent( Content $content, Title $title, $role ) {
983 // Note: may return null for revisions that have not yet been inserted
984
985 $model = $content->getModel();
986 $format = $content->getDefaultFormat();
987 $handler = $content->getContentHandler();
988
989 $name = "$title";
990
991 if ( !$handler->isSupportedFormat( $format ) ) {
992 throw new MWException( "Can't use format $format with content model $model on $name" );
993 }
994
995 if ( !$this->contentHandlerUseDB ) {
996 // if $wgContentHandlerUseDB is not set,
997 // all revisions must use the default content model and format.
998
999 $this->assertCrossWikiContentLoadingIsSafe();
1000
1001 $roleHandler = $this->slotRoleRegistry->getRoleHandler( $role );
1002 $defaultModel = $roleHandler->getDefaultModel( $title );
1003 $defaultHandler = ContentHandler::getForModelID( $defaultModel );
1004 $defaultFormat = $defaultHandler->getDefaultFormat();
1005
1006 if ( $model != $defaultModel ) {
1007 throw new MWException( "Can't save non-default content model with "
1008 . "\$wgContentHandlerUseDB disabled: model is $model, "
1009 . "default for $name is $defaultModel"
1010 );
1011 }
1012
1013 if ( $format != $defaultFormat ) {
1014 throw new MWException( "Can't use non-default content format with "
1015 . "\$wgContentHandlerUseDB disabled: format is $format, "
1016 . "default for $name is $defaultFormat"
1017 );
1018 }
1019 }
1020
1021 if ( !$content->isValid() ) {
1022 throw new MWException(
1023 "New content for $name is not valid! Content model is $model"
1024 );
1025 }
1026 }
1027
1028 /**
1029 * Create a new null-revision for insertion into a page's
1030 * history. This will not re-save the text, but simply refer
1031 * to the text from the previous version.
1032 *
1033 * Such revisions can for instance identify page rename
1034 * operations and other such meta-modifications.
1035 *
1036 * @note This method grabs a FOR UPDATE lock on the relevant row of the page table,
1037 * to prevent a new revision from being inserted before the null revision has been written
1038 * to the database.
1039 *
1040 * MCR migration note: this replaces Revision::newNullRevision
1041 *
1042 * @todo Introduce newFromParentRevision(). newNullRevision can then be based on that
1043 * (or go away).
1044 *
1045 * @param IDatabase $dbw used for obtaining the lock on the page table row
1046 * @param Title $title Title of the page to read from
1047 * @param CommentStoreComment $comment RevisionRecord's summary
1048 * @param bool $minor Whether the revision should be considered as minor
1049 * @param User $user The user to attribute the revision to
1050 *
1051 * @return RevisionRecord|null RevisionRecord or null on error
1052 */
1053 public function newNullRevision(
1054 IDatabase $dbw,
1055 Title $title,
1056 CommentStoreComment $comment,
1057 $minor,
1058 User $user
1059 ) {
1060 $this->checkDatabaseDomain( $dbw );
1061
1062 $pageId = $title->getArticleID();
1063
1064 // T51581: Lock the page table row to ensure no other process
1065 // is adding a revision to the page at the same time.
1066 // Avoid locking extra tables, compare T191892.
1067 $pageLatest = $dbw->selectField(
1068 'page',
1069 'page_latest',
1070 [ 'page_id' => $pageId ],
1071 __METHOD__,
1072 [ 'FOR UPDATE' ]
1073 );
1074
1075 if ( !$pageLatest ) {
1076 return null;
1077 }
1078
1079 // Fetch the actual revision row from master, without locking all extra tables.
1080 $oldRevision = $this->loadRevisionFromConds(
1081 $dbw,
1082 [ 'rev_id' => intval( $pageLatest ) ],
1083 self::READ_LATEST,
1084 $title
1085 );
1086
1087 if ( !$oldRevision ) {
1088 $msg = "Failed to load latest revision ID $pageLatest of page ID $pageId.";
1089 $this->logger->error(
1090 $msg,
1091 [ 'exception' => new RuntimeException( $msg ) ]
1092 );
1093 return null;
1094 }
1095
1096 // Construct the new revision
1097 $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1098 $newRevision = MutableRevisionRecord::newFromParentRevision( $oldRevision );
1099
1100 $newRevision->setComment( $comment );
1101 $newRevision->setUser( $user );
1102 $newRevision->setTimestamp( $timestamp );
1103 $newRevision->setMinorEdit( $minor );
1104
1105 return $newRevision;
1106 }
1107
1108 /**
1109 * MCR migration note: this replaces Revision::isUnpatrolled
1110 *
1111 * @todo This is overly specific, so move or kill this method.
1112 *
1113 * @param RevisionRecord $rev
1114 *
1115 * @return int Rcid of the unpatrolled row, zero if there isn't one
1116 */
1117 public function getRcIdIfUnpatrolled( RevisionRecord $rev ) {
1118 $rc = $this->getRecentChange( $rev );
1119 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_UNPATROLLED ) {
1120 return $rc->getAttribute( 'rc_id' );
1121 } else {
1122 return 0;
1123 }
1124 }
1125
1126 /**
1127 * Get the RC object belonging to the current revision, if there's one
1128 *
1129 * MCR migration note: this replaces Revision::getRecentChange
1130 *
1131 * @todo move this somewhere else?
1132 *
1133 * @param RevisionRecord $rev
1134 * @param int $flags (optional) $flags include:
1135 * IDBAccessObject::READ_LATEST: Select the data from the master
1136 *
1137 * @return null|RecentChange
1138 */
1139 public function getRecentChange( RevisionRecord $rev, $flags = 0 ) {
1140 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
1141 $db = $this->getDBConnectionRef( $dbType );
1142
1143 $userIdentity = $rev->getUser( RevisionRecord::RAW );
1144
1145 if ( !$userIdentity ) {
1146 // If the revision has no user identity, chances are it never went
1147 // into the database, and doesn't have an RC entry.
1148 return null;
1149 }
1150
1151 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
1152 $actorWhere = $this->actorMigration->getWhere( $db, 'rc_user', $rev->getUser(), false );
1153 $rc = RecentChange::newFromConds(
1154 [
1155 $actorWhere['conds'],
1156 'rc_timestamp' => $db->timestamp( $rev->getTimestamp() ),
1157 'rc_this_oldid' => $rev->getId()
1158 ],
1159 __METHOD__,
1160 $dbType
1161 );
1162
1163 // XXX: cache this locally? Glue it to the RevisionRecord?
1164 return $rc;
1165 }
1166
1167 /**
1168 * Maps fields of the archive row to corresponding revision rows.
1169 *
1170 * @param object $archiveRow
1171 *
1172 * @return object a revision row object, corresponding to $archiveRow.
1173 */
1174 private static function mapArchiveFields( $archiveRow ) {
1175 $fieldMap = [
1176 // keep with ar prefix:
1177 'ar_id' => 'ar_id',
1178
1179 // not the same suffix:
1180 'ar_page_id' => 'rev_page',
1181 'ar_rev_id' => 'rev_id',
1182
1183 // same suffix:
1184 'ar_text_id' => 'rev_text_id',
1185 'ar_timestamp' => 'rev_timestamp',
1186 'ar_user_text' => 'rev_user_text',
1187 'ar_user' => 'rev_user',
1188 'ar_actor' => 'rev_actor',
1189 'ar_minor_edit' => 'rev_minor_edit',
1190 'ar_deleted' => 'rev_deleted',
1191 'ar_len' => 'rev_len',
1192 'ar_parent_id' => 'rev_parent_id',
1193 'ar_sha1' => 'rev_sha1',
1194 'ar_comment' => 'rev_comment',
1195 'ar_comment_cid' => 'rev_comment_cid',
1196 'ar_comment_id' => 'rev_comment_id',
1197 'ar_comment_text' => 'rev_comment_text',
1198 'ar_comment_data' => 'rev_comment_data',
1199 'ar_comment_old' => 'rev_comment_old',
1200 'ar_content_format' => 'rev_content_format',
1201 'ar_content_model' => 'rev_content_model',
1202 ];
1203
1204 $revRow = new stdClass();
1205 foreach ( $fieldMap as $arKey => $revKey ) {
1206 if ( property_exists( $archiveRow, $arKey ) ) {
1207 $revRow->$revKey = $archiveRow->$arKey;
1208 }
1209 }
1210
1211 return $revRow;
1212 }
1213
1214 /**
1215 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
1216 *
1217 * @param object|array $row Either a database row or an array
1218 * @param int $queryFlags for callbacks
1219 * @param Title $title
1220 *
1221 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
1222 * @throws MWException
1223 */
1224 private function emulateMainSlot_1_29( $row, $queryFlags, Title $title ) {
1225 $mainSlotRow = new stdClass();
1226 $mainSlotRow->role_name = SlotRecord::MAIN;
1227 $mainSlotRow->model_name = null;
1228 $mainSlotRow->slot_revision_id = null;
1229 $mainSlotRow->slot_content_id = null;
1230 $mainSlotRow->content_address = null;
1231
1232 $content = null;
1233 $blobData = null;
1234 $blobFlags = null;
1235
1236 if ( is_object( $row ) ) {
1237 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW ) ) {
1238 // Don't emulate from a row when using the new schema.
1239 // Emulating from an array is still OK.
1240 throw new LogicException( 'Can\'t emulate the main slot when using MCR schema.' );
1241 }
1242
1243 // archive row
1244 if ( !isset( $row->rev_id ) && ( isset( $row->ar_user ) || isset( $row->ar_actor ) ) ) {
1245 $row = $this->mapArchiveFields( $row );
1246 }
1247
1248 if ( isset( $row->rev_text_id ) && $row->rev_text_id > 0 ) {
1249 $mainSlotRow->content_address = SqlBlobStore::makeAddressFromTextId(
1250 $row->rev_text_id
1251 );
1252 }
1253
1254 // This is used by null-revisions
1255 $mainSlotRow->slot_origin = isset( $row->slot_origin )
1256 ? intval( $row->slot_origin )
1257 : null;
1258
1259 if ( isset( $row->old_text ) ) {
1260 // this happens when the text-table gets joined directly, in the pre-1.30 schema
1261 $blobData = isset( $row->old_text ) ? strval( $row->old_text ) : null;
1262 // Check against selects that might have not included old_flags
1263 if ( !property_exists( $row, 'old_flags' ) ) {
1264 throw new InvalidArgumentException( 'old_flags was not set in $row' );
1265 }
1266 $blobFlags = $row->old_flags ?? '';
1267 }
1268
1269 $mainSlotRow->slot_revision_id = intval( $row->rev_id );
1270
1271 $mainSlotRow->content_size = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
1272 $mainSlotRow->content_sha1 = isset( $row->rev_sha1 ) ? strval( $row->rev_sha1 ) : null;
1273 $mainSlotRow->model_name = isset( $row->rev_content_model )
1274 ? strval( $row->rev_content_model )
1275 : null;
1276 // XXX: in the future, we'll probably always use the default format, and drop content_format
1277 $mainSlotRow->format_name = isset( $row->rev_content_format )
1278 ? strval( $row->rev_content_format )
1279 : null;
1280
1281 if ( isset( $row->rev_text_id ) && intval( $row->rev_text_id ) > 0 ) {
1282 // Overwritten below for SCHEMA_COMPAT_WRITE_NEW
1283 $mainSlotRow->slot_content_id
1284 = $this->emulateContentId( intval( $row->rev_text_id ) );
1285 }
1286 } elseif ( is_array( $row ) ) {
1287 $mainSlotRow->slot_revision_id = isset( $row['id'] ) ? intval( $row['id'] ) : null;
1288
1289 $mainSlotRow->slot_origin = isset( $row['slot_origin'] )
1290 ? intval( $row['slot_origin'] )
1291 : null;
1292 $mainSlotRow->content_address = isset( $row['text_id'] )
1293 ? SqlBlobStore::makeAddressFromTextId( intval( $row['text_id'] ) )
1294 : null;
1295 $mainSlotRow->content_size = isset( $row['len'] ) ? intval( $row['len'] ) : null;
1296 $mainSlotRow->content_sha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
1297
1298 $mainSlotRow->model_name = isset( $row['content_model'] )
1299 ? strval( $row['content_model'] ) : null; // XXX: must be a string!
1300 // XXX: in the future, we'll probably always use the default format, and drop content_format
1301 $mainSlotRow->format_name = isset( $row['content_format'] )
1302 ? strval( $row['content_format'] ) : null;
1303 $blobData = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
1304 // XXX: If the flags field is not set then $blobFlags should be null so that no
1305 // decoding will happen. An empty string will result in default decodings.
1306 $blobFlags = isset( $row['flags'] ) ? trim( strval( $row['flags'] ) ) : null;
1307
1308 // if we have a Content object, override mText and mContentModel
1309 if ( !empty( $row['content'] ) ) {
1310 if ( !( $row['content'] instanceof Content ) ) {
1311 throw new MWException( 'content field must contain a Content object.' );
1312 }
1313
1314 /** @var Content $content */
1315 $content = $row['content'];
1316 $handler = $content->getContentHandler();
1317
1318 $mainSlotRow->model_name = $content->getModel();
1319
1320 // XXX: in the future, we'll probably always use the default format.
1321 if ( $mainSlotRow->format_name === null ) {
1322 $mainSlotRow->format_name = $handler->getDefaultFormat();
1323 }
1324 }
1325
1326 if ( isset( $row['text_id'] ) && intval( $row['text_id'] ) > 0 ) {
1327 // Overwritten below for SCHEMA_COMPAT_WRITE_NEW
1328 $mainSlotRow->slot_content_id
1329 = $this->emulateContentId( intval( $row['text_id'] ) );
1330 }
1331 } else {
1332 throw new MWException( 'Revision constructor passed invalid row format.' );
1333 }
1334
1335 // With the old schema, the content changes with every revision,
1336 // except for null-revisions.
1337 if ( !isset( $mainSlotRow->slot_origin ) ) {
1338 $mainSlotRow->slot_origin = $mainSlotRow->slot_revision_id;
1339 }
1340
1341 if ( $mainSlotRow->model_name === null ) {
1342 $mainSlotRow->model_name = function ( SlotRecord $slot ) use ( $title ) {
1343 $this->assertCrossWikiContentLoadingIsSafe();
1344
1345 return $this->slotRoleRegistry->getRoleHandler( $slot->getRole() )
1346 ->getDefaultModel( $title );
1347 };
1348 }
1349
1350 if ( !$content ) {
1351 // XXX: We should perhaps fail if $blobData is null and $mainSlotRow->content_address
1352 // is missing, but "empty revisions" with no content are used in some edge cases.
1353
1354 $content = function ( SlotRecord $slot )
1355 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
1356 {
1357 return $this->loadSlotContent(
1358 $slot,
1359 $blobData,
1360 $blobFlags,
1361 $mainSlotRow->format_name,
1362 $queryFlags
1363 );
1364 };
1365 }
1366
1367 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
1368 // NOTE: this callback will be looped through RevisionSlot::newInherited(), allowing
1369 // the inherited slot to have the same content_id as the original slot. In that case,
1370 // $slot will be the inherited slot, while $mainSlotRow still refers to the original slot.
1371 $mainSlotRow->slot_content_id =
1372 function ( SlotRecord $slot ) use ( $queryFlags, $mainSlotRow ) {
1373 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1374 return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id, SlotRecord::MAIN );
1375 };
1376 }
1377
1378 return new SlotRecord( $mainSlotRow, $content );
1379 }
1380
1381 /**
1382 * Provides a content ID to use with emulated SlotRecords in SCHEMA_COMPAT_OLD mode,
1383 * based on the revision's text ID (rev_text_id or ar_text_id, respectively).
1384 * Note that in SCHEMA_COMPAT_WRITE_BOTH, a callback to findSlotContentId() should be used
1385 * instead, since in that mode, some revision rows may already have a real content ID,
1386 * while other's don't - and for the ones that don't, we should indicate that it
1387 * is missing and cause SlotRecords::hasContentId() to return false.
1388 *
1389 * @param int $textId
1390 * @return int The emulated content ID
1391 */
1392 private function emulateContentId( $textId ) {
1393 // Return a negative number to ensure the ID is distinct from any real content IDs
1394 // that will be assigned in SCHEMA_COMPAT_WRITE_NEW mode and read in SCHEMA_COMPAT_READ_NEW
1395 // mode.
1396 return -$textId;
1397 }
1398
1399 /**
1400 * Loads a Content object based on a slot row.
1401 *
1402 * This method does not call $slot->getContent(), and may be used as a callback
1403 * called by $slot->getContent().
1404 *
1405 * MCR migration note: this roughly corresponds to Revision::getContentInternal
1406 *
1407 * @param SlotRecord $slot The SlotRecord to load content for
1408 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
1409 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
1410 * Use null if no processing should happen. That is in constrast to the empty string,
1411 * which causes the blob to be decoded according to the configured legacy encoding.
1412 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
1413 * @param int $queryFlags
1414 *
1415 * @throws RevisionAccessException
1416 * @return Content
1417 */
1418 private function loadSlotContent(
1419 SlotRecord $slot,
1420 $blobData = null,
1421 $blobFlags = null,
1422 $blobFormat = null,
1423 $queryFlags = 0
1424 ) {
1425 if ( $blobData !== null ) {
1426 Assert::parameterType( 'string', $blobData, '$blobData' );
1427 Assert::parameterType( 'string|null', $blobFlags, '$blobFlags' );
1428
1429 $cacheKey = $slot->hasAddress() ? $slot->getAddress() : null;
1430
1431 if ( $blobFlags === null ) {
1432 // No blob flags, so use the blob verbatim.
1433 $data = $blobData;
1434 } else {
1435 $data = $this->blobStore->expandBlob( $blobData, $blobFlags, $cacheKey );
1436 if ( $data === false ) {
1437 throw new RevisionAccessException(
1438 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
1439 );
1440 }
1441 }
1442
1443 } else {
1444 $address = $slot->getAddress();
1445 try {
1446 $data = $this->blobStore->getBlob( $address, $queryFlags );
1447 } catch ( BlobAccessException $e ) {
1448 throw new RevisionAccessException(
1449 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
1450 );
1451 }
1452 }
1453
1454 // Unserialize content
1455 $handler = ContentHandler::getForModelID( $slot->getModel() );
1456
1457 $content = $handler->unserializeContent( $data, $blobFormat );
1458 return $content;
1459 }
1460
1461 /**
1462 * Load a page revision from a given revision ID number.
1463 * Returns null if no such revision can be found.
1464 *
1465 * MCR migration note: this replaces Revision::newFromId
1466 *
1467 * $flags include:
1468 * IDBAccessObject::READ_LATEST: Select the data from the master
1469 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1470 *
1471 * @param int $id
1472 * @param int $flags (optional)
1473 * @return RevisionRecord|null
1474 */
1475 public function getRevisionById( $id, $flags = 0 ) {
1476 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
1477 }
1478
1479 /**
1480 * Load either the current, or a specified, revision
1481 * that's attached to a given link target. If not attached
1482 * to that link target, will return null.
1483 *
1484 * MCR migration note: this replaces Revision::newFromTitle
1485 *
1486 * $flags include:
1487 * IDBAccessObject::READ_LATEST: Select the data from the master
1488 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1489 *
1490 * @param LinkTarget $linkTarget
1491 * @param int $revId (optional)
1492 * @param int $flags Bitfield (optional)
1493 * @return RevisionRecord|null
1494 */
1495 public function getRevisionByTitle( LinkTarget $linkTarget, $revId = 0, $flags = 0 ) {
1496 // TODO should not require Title in future (T206498)
1497 $title = Title::newFromLinkTarget( $linkTarget );
1498 $conds = [
1499 'page_namespace' => $title->getNamespace(),
1500 'page_title' => $title->getDBkey()
1501 ];
1502 if ( $revId ) {
1503 // Use the specified revision ID.
1504 // Note that we use newRevisionFromConds here because we want to retry
1505 // and fall back to master if the page is not found on a replica.
1506 // Since the caller supplied a revision ID, we are pretty sure the revision is
1507 // supposed to exist, so we should try hard to find it.
1508 $conds['rev_id'] = $revId;
1509 return $this->newRevisionFromConds( $conds, $flags, $title );
1510 } else {
1511 // Use a join to get the latest revision.
1512 // Note that we don't use newRevisionFromConds here because we don't want to retry
1513 // and fall back to master. The assumption is that we only want to force the fallback
1514 // if we are quite sure the revision exists because the caller supplied a revision ID.
1515 // If the page isn't found at all on a replica, it probably simply does not exist.
1516 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1517
1518 $conds[] = 'rev_id=page_latest';
1519 $rev = $this->loadRevisionFromConds( $db, $conds, $flags, $title );
1520
1521 return $rev;
1522 }
1523 }
1524
1525 /**
1526 * Load either the current, or a specified, revision
1527 * that's attached to a given page ID.
1528 * Returns null if no such revision can be found.
1529 *
1530 * MCR migration note: this replaces Revision::newFromPageId
1531 *
1532 * $flags include:
1533 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
1534 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1535 *
1536 * @param int $pageId
1537 * @param int $revId (optional)
1538 * @param int $flags Bitfield (optional)
1539 * @return RevisionRecord|null
1540 */
1541 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
1542 $conds = [ 'page_id' => $pageId ];
1543 if ( $revId ) {
1544 // Use the specified revision ID.
1545 // Note that we use newRevisionFromConds here because we want to retry
1546 // and fall back to master if the page is not found on a replica.
1547 // Since the caller supplied a revision ID, we are pretty sure the revision is
1548 // supposed to exist, so we should try hard to find it.
1549 $conds['rev_id'] = $revId;
1550 return $this->newRevisionFromConds( $conds, $flags );
1551 } else {
1552 // Use a join to get the latest revision.
1553 // Note that we don't use newRevisionFromConds here because we don't want to retry
1554 // and fall back to master. The assumption is that we only want to force the fallback
1555 // if we are quite sure the revision exists because the caller supplied a revision ID.
1556 // If the page isn't found at all on a replica, it probably simply does not exist.
1557 $db = $this->getDBConnectionRefForQueryFlags( $flags );
1558
1559 $conds[] = 'rev_id=page_latest';
1560 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1561
1562 return $rev;
1563 }
1564 }
1565
1566 /**
1567 * Load the revision for the given title with the given timestamp.
1568 * WARNING: Timestamps may in some circumstances not be unique,
1569 * so this isn't the best key to use.
1570 *
1571 * MCR migration note: this replaces Revision::loadFromTimestamp
1572 *
1573 * @param Title $title
1574 * @param string $timestamp
1575 * @return RevisionRecord|null
1576 */
1577 public function getRevisionByTimestamp( $title, $timestamp ) {
1578 $db = $this->getDBConnectionRef( DB_REPLICA );
1579 return $this->newRevisionFromConds(
1580 [
1581 'rev_timestamp' => $db->timestamp( $timestamp ),
1582 'page_namespace' => $title->getNamespace(),
1583 'page_title' => $title->getDBkey()
1584 ],
1585 0,
1586 $title
1587 );
1588 }
1589
1590 /**
1591 * @param int $revId The revision to load slots for.
1592 * @param int $queryFlags
1593 * @param Title $title
1594 *
1595 * @return SlotRecord[]
1596 */
1597 private function loadSlotRecords( $revId, $queryFlags, Title $title ) {
1598 $revQuery = self::getSlotsQueryInfo( [ 'content' ] );
1599
1600 list( $dbMode, $dbOptions ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
1601 $db = $this->getDBConnectionRef( $dbMode );
1602
1603 $res = $db->select(
1604 $revQuery['tables'],
1605 $revQuery['fields'],
1606 [
1607 'slot_revision_id' => $revId,
1608 ],
1609 __METHOD__,
1610 $dbOptions,
1611 $revQuery['joins']
1612 );
1613
1614 $slots = $this->constructSlotRecords( $revId, $res, $queryFlags, $title );
1615
1616 return $slots;
1617 }
1618
1619 /**
1620 * Factory method for SlotRecords based on known slot rows.
1621 *
1622 * @param int $revId The revision to load slots for.
1623 * @param object[]|IResultWrapper $slotRows
1624 * @param int $queryFlags
1625 * @param Title $title
1626 * @param array|null $slotContents a map from blobAddress to slot
1627 * content blob or Content object.
1628 *
1629 * @return SlotRecord[]
1630 */
1631 private function constructSlotRecords(
1632 $revId,
1633 $slotRows,
1634 $queryFlags,
1635 Title $title,
1636 $slotContents = null
1637 ) {
1638 $slots = [];
1639
1640 foreach ( $slotRows as $row ) {
1641 // Resolve role names and model names from in-memory cache, if they were not joined in.
1642 if ( !isset( $row->role_name ) ) {
1643 $row->role_name = $this->slotRoleStore->getName( (int)$row->slot_role_id );
1644 }
1645
1646 if ( !isset( $row->model_name ) ) {
1647 if ( isset( $row->content_model ) ) {
1648 $row->model_name = $this->contentModelStore->getName( (int)$row->content_model );
1649 } else {
1650 // We may get here if $row->model_name is set but null, perhaps because it
1651 // came from rev_content_model, which is NULL for the default model.
1652 $slotRoleHandler = $this->slotRoleRegistry->getRoleHandler( $row->role_name );
1653 $row->model_name = $slotRoleHandler->getDefaultModel( $title );
1654 }
1655 }
1656
1657 if ( !isset( $row->content_id ) && isset( $row->rev_text_id ) ) {
1658 $row->slot_content_id
1659 = $this->emulateContentId( intval( $row->rev_text_id ) );
1660 }
1661
1662 $contentCallback = function ( SlotRecord $slot ) use ( $slotContents, $queryFlags ) {
1663 $blob = null;
1664 if ( isset( $slotContents[$slot->getAddress()] ) ) {
1665 $blob = $slotContents[$slot->getAddress()];
1666 if ( $blob instanceof Content ) {
1667 return $blob;
1668 }
1669 }
1670 return $this->loadSlotContent( $slot, $blob, null, null, $queryFlags );
1671 };
1672
1673 $slots[$row->role_name] = new SlotRecord( $row, $contentCallback );
1674 }
1675
1676 if ( !isset( $slots[SlotRecord::MAIN] ) ) {
1677 throw new RevisionAccessException(
1678 'Main slot of revision ' . $revId . ' not found in database!'
1679 );
1680 }
1681
1682 return $slots;
1683 }
1684
1685 /**
1686 * Factory method for RevisionSlots based on a revision ID.
1687 *
1688 * @note If other code has a need to construct RevisionSlots objects, this should be made
1689 * public, since RevisionSlots instances should not be constructed directly.
1690 *
1691 * @param int $revId
1692 * @param object $revisionRow
1693 * @param object[]|null $slotRows
1694 * @param int $queryFlags
1695 * @param Title $title
1696 *
1697 * @return RevisionSlots
1698 * @throws MWException
1699 */
1700 private function newRevisionSlots(
1701 $revId,
1702 $revisionRow,
1703 $slotRows,
1704 $queryFlags,
1705 Title $title
1706 ) {
1707 if ( $slotRows ) {
1708 $slots = new RevisionSlots(
1709 $this->constructSlotRecords( $revId, $slotRows, $queryFlags, $title )
1710 );
1711 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_NEW ) ) {
1712 $mainSlot = $this->emulateMainSlot_1_29( $revisionRow, $queryFlags, $title );
1713 // @phan-suppress-next-line PhanTypeInvalidCallableArraySize false positive
1714 $slots = new RevisionSlots( [ SlotRecord::MAIN => $mainSlot ] );
1715 } else {
1716 // XXX: do we need the same kind of caching here
1717 // that getKnownCurrentRevision uses (if $revId == page_latest?)
1718
1719 $slots = new RevisionSlots( function () use( $revId, $queryFlags, $title ) {
1720 return $this->loadSlotRecords( $revId, $queryFlags, $title );
1721 } );
1722 }
1723
1724 return $slots;
1725 }
1726
1727 /**
1728 * Make a fake revision object from an archive table row. This is queried
1729 * for permissions or even inserted (as in Special:Undelete)
1730 *
1731 * MCR migration note: this replaces Revision::newFromArchiveRow
1732 *
1733 * @param object $row
1734 * @param int $queryFlags
1735 * @param Title|null $title
1736 * @param array $overrides associative array with fields of $row to override. This may be
1737 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1738 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1739 * override ar_parent_id.
1740 *
1741 * @return RevisionRecord
1742 * @throws MWException
1743 */
1744 public function newRevisionFromArchiveRow(
1745 $row,
1746 $queryFlags = 0,
1747 Title $title = null,
1748 array $overrides = []
1749 ) {
1750 Assert::parameterType( 'object', $row, '$row' );
1751
1752 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1753 Assert::parameterType( 'integer', $queryFlags, '$queryFlags' );
1754
1755 if ( !$title && isset( $overrides['title'] ) ) {
1756 if ( !( $overrides['title'] instanceof Title ) ) {
1757 throw new MWException( 'title field override must contain a Title object.' );
1758 }
1759
1760 $title = $overrides['title'];
1761 }
1762
1763 if ( !isset( $title ) ) {
1764 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
1765 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
1766 } else {
1767 throw new InvalidArgumentException(
1768 'A Title or ar_namespace and ar_title must be given'
1769 );
1770 }
1771 }
1772
1773 foreach ( $overrides as $key => $value ) {
1774 $field = "ar_$key";
1775 $row->$field = $value;
1776 }
1777
1778 try {
1779 $user = User::newFromAnyId(
1780 $row->ar_user ?? null,
1781 $row->ar_user_text ?? null,
1782 $row->ar_actor ?? null,
1783 $this->dbDomain
1784 );
1785 } catch ( InvalidArgumentException $ex ) {
1786 wfWarn( __METHOD__ . ': ' . $title->getPrefixedDBkey() . ': ' . $ex->getMessage() );
1787 $user = new UserIdentityValue( 0, 'Unknown user', 0 );
1788 }
1789
1790 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1791 // Legacy because $row may have come from self::selectFields()
1792 $comment = $this->commentStore->getCommentLegacy( $db, 'ar_comment', $row, true );
1793
1794 $slots = $this->newRevisionSlots( $row->ar_rev_id, $row, null, $queryFlags, $title );
1795
1796 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->dbDomain );
1797 }
1798
1799 /**
1800 * @see RevisionFactory::newRevisionFromRow
1801 *
1802 * MCR migration note: this replaces Revision::newFromRow
1803 *
1804 * @param object $row A database row generated from a query based on getQueryInfo()
1805 * @param int $queryFlags
1806 * @param Title|null $title
1807 * @param bool $fromCache if true, the returned RevisionRecord will ensure that no stale
1808 * data is returned from getters, by querying the database as needed
1809 * @return RevisionRecord
1810 */
1811 public function newRevisionFromRow(
1812 $row,
1813 $queryFlags = 0,
1814 Title $title = null,
1815 $fromCache = false
1816 ) {
1817 return $this->newRevisionFromRowAndSlots( $row, null, $queryFlags, $title, $fromCache );
1818 }
1819
1820 /**
1821 * @param object $row A database row generated from a query based on getQueryInfo()
1822 * @param null|object[]|RevisionSlots $slots
1823 * - Database rows generated from a query based on getSlotsQueryInfo
1824 * with the 'content' flag set. Or
1825 * - RevisionSlots instance
1826 * @param int $queryFlags
1827 * @param Title|null $title
1828 * @param bool $fromCache if true, the returned RevisionRecord will ensure that no stale
1829 * data is returned from getters, by querying the database as needed
1830 *
1831 * @return RevisionRecord
1832 * @throws MWException
1833 * @see RevisionFactory::newRevisionFromRow
1834 *
1835 * MCR migration note: this replaces Revision::newFromRow
1836 */
1837 public function newRevisionFromRowAndSlots(
1838 $row,
1839 $slots,
1840 $queryFlags = 0,
1841 Title $title = null,
1842 $fromCache = false
1843 ) {
1844 Assert::parameterType( 'object', $row, '$row' );
1845
1846 if ( !$title ) {
1847 $pageId = $row->rev_page ?? 0; // XXX: also check page_id?
1848 $revId = $row->rev_id ?? 0;
1849
1850 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1851 }
1852
1853 if ( !isset( $row->page_latest ) ) {
1854 $row->page_latest = $title->getLatestRevID();
1855 if ( $row->page_latest === 0 && $title->exists() ) {
1856 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1857 }
1858 }
1859
1860 try {
1861 $user = User::newFromAnyId(
1862 $row->rev_user ?? null,
1863 $row->rev_user_text ?? null,
1864 $row->rev_actor ?? null,
1865 $this->dbDomain
1866 );
1867 } catch ( InvalidArgumentException $ex ) {
1868 wfWarn( __METHOD__ . ': ' . $title->getPrefixedDBkey() . ': ' . $ex->getMessage() );
1869 $user = new UserIdentityValue( 0, 'Unknown user', 0 );
1870 }
1871
1872 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1873 // Legacy because $row may have come from self::selectFields()
1874 $comment = $this->commentStore->getCommentLegacy( $db, 'rev_comment', $row, true );
1875
1876 if ( !( $slots instanceof RevisionSlots ) ) {
1877 $slots = $this->newRevisionSlots( $row->rev_id, $row, $slots, $queryFlags, $title );
1878 }
1879
1880 // If this is a cached row, instantiate a cache-aware revision class to avoid stale data.
1881 if ( $fromCache ) {
1882 $rev = new RevisionStoreCacheRecord(
1883 function ( $revId ) use ( $queryFlags ) {
1884 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1885 return $this->fetchRevisionRowFromConds(
1886 $db,
1887 [ 'rev_id' => intval( $revId ) ]
1888 );
1889 },
1890 $title, $user, $comment, $row, $slots, $this->dbDomain
1891 );
1892 } else {
1893 $rev = new RevisionStoreRecord(
1894 $title, $user, $comment, $row, $slots, $this->dbDomain );
1895 }
1896 return $rev;
1897 }
1898
1899 /**
1900 * Construct a RevisionRecord instance for each row in $rows,
1901 * and return them as an associative array indexed by revision ID.
1902 * @param Traversable|array $rows the rows to construct revision records from
1903 * @param array $options Supports the following options:
1904 * 'slots' - whether metadata about revision slots should be
1905 * loaded immediately. Supports falsy or truthy value as well
1906 * as an explicit list of slot role names.
1907 * 'content'- whether the actual content of the slots should be
1908 * preloaded.
1909 * @param int $queryFlags
1910 * @param Title|null $title The title to which all the revision rows belong, if there
1911 * is such a title and the caller has it handy, so we don't have to look it up again.
1912 * If this parameter is given and any of the rows has a rev_page_id that is different
1913 * from $title->getArticleID(), an InvalidArgumentException is thrown.
1914 *
1915 * @return StatusValue a status with a RevisionRecord[] of successfully fetched revisions
1916 * and an array of errors for the revisions failed to fetch.
1917 */
1918 public function newRevisionsFromBatch(
1919 $rows,
1920 array $options = [],
1921 $queryFlags = 0,
1922 Title $title = null
1923 ) {
1924 $result = new StatusValue();
1925
1926 $rowsByRevId = [];
1927 $pageIds = [];
1928 $titlesByPageId = [];
1929 foreach ( $rows as $row ) {
1930 if ( isset( $rowsByRevId[$row->rev_id] ) ) {
1931 $result->warning(
1932 'internalerror',
1933 "Duplicate rows in newRevisionsFromBatch, rev_id {$row->rev_id}"
1934 );
1935 }
1936 if ( $title && $row->rev_page != $title->getArticleID() ) {
1937 throw new InvalidArgumentException(
1938 "Revision {$row->rev_id} doesn't belong to page {$title->getArticleID()}"
1939 );
1940 }
1941 $pageIds[] = $row->rev_page;
1942 $rowsByRevId[$row->rev_id] = $row;
1943 }
1944
1945 if ( empty( $rowsByRevId ) ) {
1946 $result->setResult( true, [] );
1947 return $result;
1948 }
1949
1950 // If the title is not supplied, batch-fetch Title objects.
1951 if ( $title ) {
1952 $titlesByPageId[$title->getArticleID()] = $title;
1953 } else {
1954 $pageIds = array_unique( $pageIds );
1955 foreach ( Title::newFromIDs( $pageIds ) as $t ) {
1956 $titlesByPageId[$t->getArticleID()] = $t;
1957 }
1958 }
1959
1960 if ( !isset( $options['slots'] ) || $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
1961 $result->setResult( true,
1962 array_map( function ( $row ) use ( $queryFlags, $titlesByPageId, $result ) {
1963 try {
1964 return $this->newRevisionFromRow(
1965 $row,
1966 $queryFlags,
1967 $titlesByPageId[$row->rev_page]
1968 );
1969 } catch ( MWException $e ) {
1970 $result->warning( 'internalerror', $e->getMessage() );
1971 return null;
1972 }
1973 }, $rowsByRevId )
1974 );
1975 return $result;
1976 }
1977
1978 $slotQueryConds = [ 'slot_revision_id' => array_keys( $rowsByRevId ) ];
1979 if ( is_array( $options['slots'] ) ) {
1980 $slotQueryConds['slot_role_id'] = array_map( function ( $slot_name ) {
1981 return $this->slotRoleStore->getId( $slot_name );
1982 }, $options['slots'] );
1983 }
1984
1985 // We need to set the `content` flag because newRevisionFromRowAndSlots requires content
1986 // metadata to be loaded.
1987 $slotQueryInfo = self::getSlotsQueryInfo( [ 'content' ] );
1988 $db = $this->getDBConnectionRefForQueryFlags( $queryFlags );
1989 $slotRows = $db->select(
1990 $slotQueryInfo['tables'],
1991 $slotQueryInfo['fields'],
1992 $slotQueryConds,
1993 __METHOD__,
1994 [],
1995 $slotQueryInfo['joins']
1996 );
1997
1998 $slotRowsByRevId = [];
1999 foreach ( $slotRows as $slotRow ) {
2000 $slotRowsByRevId[$slotRow->slot_revision_id][] = $slotRow;
2001 }
2002
2003 $slotContents = null;
2004 if ( $options['content'] ?? false ) {
2005 $blobAddresses = [];
2006 foreach ( $slotRows as $slotRow ) {
2007 $blobAddresses[] = $slotRow->content_address;
2008 }
2009 $slotContentFetchStatus = $this->blobStore
2010 ->getBlobBatch( $blobAddresses, $queryFlags );
2011 foreach ( $slotContentFetchStatus->getErrors() as $error ) {
2012 $result->warning( $error['message'], ...$error['params'] );
2013 }
2014 $slotContents = $slotContentFetchStatus->getValue();
2015 }
2016
2017 $result->setResult( true, array_map( function ( $row ) use
2018 ( $slotRowsByRevId, $queryFlags, $titlesByPageId, $slotContents, $result ) {
2019 if ( !isset( $slotRowsByRevId[$row->rev_id] ) ) {
2020 $result->warning(
2021 'internalerror',
2022 "Couldn't find slots for rev {$row->rev_id}"
2023 );
2024 return null;
2025 }
2026 try {
2027 return $this->newRevisionFromRowAndSlots(
2028 $row,
2029 new RevisionSlots(
2030 $this->constructSlotRecords(
2031 $row->rev_id,
2032 $slotRowsByRevId[$row->rev_id],
2033 $queryFlags,
2034 $titlesByPageId[$row->rev_page],
2035 $slotContents
2036 )
2037 ),
2038 $queryFlags,
2039 $titlesByPageId[$row->rev_page]
2040 );
2041 } catch ( MWException $e ) {
2042 $result->warning( 'internalerror', $e->getMessage() );
2043 return null;
2044 }
2045 }, $rowsByRevId ) );
2046 return $result;
2047 }
2048
2049 /**
2050 * Constructs a new MutableRevisionRecord based on the given associative array following
2051 * the MW1.29 convention for the Revision constructor.
2052 *
2053 * MCR migration note: this replaces Revision::newFromRow
2054 *
2055 * @param array $fields
2056 * @param int $queryFlags
2057 * @param Title|null $title
2058 *
2059 * @return MutableRevisionRecord
2060 * @throws MWException
2061 * @throws RevisionAccessException
2062 */
2063 public function newMutableRevisionFromArray(
2064 array $fields,
2065 $queryFlags = 0,
2066 Title $title = null
2067 ) {
2068 if ( !$title && isset( $fields['title'] ) ) {
2069 if ( !( $fields['title'] instanceof Title ) ) {
2070 throw new MWException( 'title field must contain a Title object.' );
2071 }
2072
2073 $title = $fields['title'];
2074 }
2075
2076 if ( !$title ) {
2077 $pageId = $fields['page'] ?? 0;
2078 $revId = $fields['id'] ?? 0;
2079
2080 $title = $this->getTitle( $pageId, $revId, $queryFlags );
2081 }
2082
2083 if ( !isset( $fields['page'] ) ) {
2084 $fields['page'] = $title->getArticleID( $queryFlags );
2085 }
2086
2087 // if we have a content object, use it to set the model and type
2088 if ( !empty( $fields['content'] ) && !( $fields['content'] instanceof Content )
2089 && !is_array( $fields['content'] )
2090 ) {
2091 throw new MWException(
2092 'content field must contain a Content object or an array of Content objects.'
2093 );
2094 }
2095
2096 if ( !empty( $fields['text_id'] ) ) {
2097 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
2098 throw new MWException( "The text_id field is only available in the pre-MCR schema" );
2099 }
2100
2101 if ( !empty( $fields['content'] ) ) {
2102 throw new MWException(
2103 "Text already stored in external store (id {$fields['text_id']}), " .
2104 "can't specify content object"
2105 );
2106 }
2107 }
2108
2109 if (
2110 isset( $fields['comment'] )
2111 && !( $fields['comment'] instanceof CommentStoreComment )
2112 ) {
2113 $commentData = $fields['comment_data'] ?? null;
2114
2115 if ( $fields['comment'] instanceof Message ) {
2116 $fields['comment'] = CommentStoreComment::newUnsavedComment(
2117 $fields['comment'],
2118 $commentData
2119 );
2120 } else {
2121 $commentText = trim( strval( $fields['comment'] ) );
2122 $fields['comment'] = CommentStoreComment::newUnsavedComment(
2123 $commentText,
2124 $commentData
2125 );
2126 }
2127 }
2128
2129 $revision = new MutableRevisionRecord( $title, $this->dbDomain );
2130 $this->initializeMutableRevisionFromArray( $revision, $fields );
2131
2132 if ( isset( $fields['content'] ) && is_array( $fields['content'] ) ) {
2133 // @phan-suppress-next-line PhanTypeNoPropertiesForeach
2134 foreach ( $fields['content'] as $role => $content ) {
2135 $revision->setContent( $role, $content );
2136 }
2137 } else {
2138 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
2139 $revision->setSlot( $mainSlot );
2140 }
2141
2142 return $revision;
2143 }
2144
2145 /**
2146 * @param MutableRevisionRecord $record
2147 * @param array $fields
2148 */
2149 private function initializeMutableRevisionFromArray(
2150 MutableRevisionRecord $record,
2151 array $fields
2152 ) {
2153 /** @var UserIdentity $user */
2154 $user = null;
2155
2156 // If a user is passed in, use it if possible. We cannot use a user from a
2157 // remote wiki with unsuppressed ids, due to issues described in T222212.
2158 if ( isset( $fields['user'] ) &&
2159 ( $fields['user'] instanceof UserIdentity ) &&
2160 ( $this->dbDomain === false ||
2161 ( !$fields['user']->getId() && !$fields['user']->getActorId() ) )
2162 ) {
2163 $user = $fields['user'];
2164 } else {
2165 try {
2166 $user = User::newFromAnyId(
2167 $fields['user'] ?? null,
2168 $fields['user_text'] ?? null,
2169 $fields['actor'] ?? null,
2170 $this->dbDomain
2171 );
2172 } catch ( InvalidArgumentException $ex ) {
2173 $user = null;
2174 }
2175 }
2176
2177 if ( $user ) {
2178 $record->setUser( $user );
2179 }
2180
2181 $timestamp = isset( $fields['timestamp'] )
2182 ? strval( $fields['timestamp'] )
2183 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
2184
2185 $record->setTimestamp( $timestamp );
2186
2187 if ( isset( $fields['page'] ) ) {
2188 $record->setPageId( intval( $fields['page'] ) );
2189 }
2190
2191 if ( isset( $fields['id'] ) ) {
2192 $record->setId( intval( $fields['id'] ) );
2193 }
2194 if ( isset( $fields['parent_id'] ) ) {
2195 $record->setParentId( intval( $fields['parent_id'] ) );
2196 }
2197
2198 if ( isset( $fields['sha1'] ) ) {
2199 $record->setSha1( $fields['sha1'] );
2200 }
2201 if ( isset( $fields['size'] ) ) {
2202 $record->setSize( intval( $fields['size'] ) );
2203 }
2204
2205 if ( isset( $fields['minor_edit'] ) ) {
2206 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
2207 }
2208 if ( isset( $fields['deleted'] ) ) {
2209 $record->setVisibility( intval( $fields['deleted'] ) );
2210 }
2211
2212 if ( isset( $fields['comment'] ) ) {
2213 Assert::parameterType(
2214 CommentStoreComment::class,
2215 $fields['comment'],
2216 '$row[\'comment\']'
2217 );
2218 $record->setComment( $fields['comment'] );
2219 }
2220 }
2221
2222 /**
2223 * Load a page revision from a given revision ID number.
2224 * Returns null if no such revision can be found.
2225 *
2226 * MCR migration note: this corresponds to Revision::loadFromId
2227 *
2228 * @note direct use is deprecated!
2229 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
2230 *
2231 * @param IDatabase $db
2232 * @param int $id
2233 *
2234 * @return RevisionRecord|null
2235 */
2236 public function loadRevisionFromId( IDatabase $db, $id ) {
2237 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
2238 }
2239
2240 /**
2241 * Load either the current, or a specified, revision
2242 * that's attached to a given page. If not attached
2243 * to that page, will return null.
2244 *
2245 * MCR migration note: this replaces Revision::loadFromPageId
2246 *
2247 * @note direct use is deprecated!
2248 * @todo remove when unused!
2249 *
2250 * @param IDatabase $db
2251 * @param int $pageid
2252 * @param int $id
2253 * @return RevisionRecord|null
2254 */
2255 public function loadRevisionFromPageId( IDatabase $db, $pageid, $id = 0 ) {
2256 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
2257 if ( $id ) {
2258 $conds['rev_id'] = intval( $id );
2259 } else {
2260 $conds[] = 'rev_id=page_latest';
2261 }
2262 return $this->loadRevisionFromConds( $db, $conds );
2263 }
2264
2265 /**
2266 * Load either the current, or a specified, revision
2267 * that's attached to a given page. If not attached
2268 * to that page, will return null.
2269 *
2270 * MCR migration note: this replaces Revision::loadFromTitle
2271 *
2272 * @note direct use is deprecated!
2273 * @todo remove when unused!
2274 *
2275 * @param IDatabase $db
2276 * @param Title $title
2277 * @param int $id
2278 *
2279 * @return RevisionRecord|null
2280 */
2281 public function loadRevisionFromTitle( IDatabase $db, $title, $id = 0 ) {
2282 if ( $id ) {
2283 $matchId = intval( $id );
2284 } else {
2285 $matchId = 'page_latest';
2286 }
2287
2288 return $this->loadRevisionFromConds(
2289 $db,
2290 [
2291 "rev_id=$matchId",
2292 'page_namespace' => $title->getNamespace(),
2293 'page_title' => $title->getDBkey()
2294 ],
2295 0,
2296 $title
2297 );
2298 }
2299
2300 /**
2301 * Load the revision for the given title with the given timestamp.
2302 * WARNING: Timestamps may in some circumstances not be unique,
2303 * so this isn't the best key to use.
2304 *
2305 * MCR migration note: this replaces Revision::loadFromTimestamp
2306 *
2307 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
2308 * @todo remove when unused!
2309 *
2310 * @param IDatabase $db
2311 * @param Title $title
2312 * @param string $timestamp
2313 * @return RevisionRecord|null
2314 */
2315 public function loadRevisionFromTimestamp( IDatabase $db, $title, $timestamp ) {
2316 return $this->loadRevisionFromConds( $db,
2317 [
2318 'rev_timestamp' => $db->timestamp( $timestamp ),
2319 'page_namespace' => $title->getNamespace(),
2320 'page_title' => $title->getDBkey()
2321 ],
2322 0,
2323 $title
2324 );
2325 }
2326
2327 /**
2328 * Given a set of conditions, fetch a revision
2329 *
2330 * This method should be used if we are pretty sure the revision exists.
2331 * Unless $flags has READ_LATEST set, this method will first try to find the revision
2332 * on a replica before hitting the master database.
2333 *
2334 * MCR migration note: this corresponds to Revision::newFromConds
2335 *
2336 * @param array $conditions
2337 * @param int $flags (optional)
2338 * @param Title|null $title
2339 *
2340 * @return RevisionRecord|null
2341 */
2342 private function newRevisionFromConds( $conditions, $flags = 0, Title $title = null ) {
2343 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2344 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
2345
2346 $lb = $this->getDBLoadBalancer();
2347
2348 // Make sure new pending/committed revision are visibile later on
2349 // within web requests to certain avoid bugs like T93866 and T94407.
2350 if ( !$rev
2351 && !( $flags & self::READ_LATEST )
2352 && $lb->hasStreamingReplicaServers()
2353 && $lb->hasOrMadeRecentMasterChanges()
2354 ) {
2355 $flags = self::READ_LATEST;
2356 $dbw = $this->getDBConnectionRef( DB_MASTER );
2357 $rev = $this->loadRevisionFromConds( $dbw, $conditions, $flags, $title );
2358 }
2359
2360 return $rev;
2361 }
2362
2363 /**
2364 * Given a set of conditions, fetch a revision from
2365 * the given database connection.
2366 *
2367 * MCR migration note: this corresponds to Revision::loadFromConds
2368 *
2369 * @param IDatabase $db
2370 * @param array $conditions
2371 * @param int $flags (optional)
2372 * @param Title|null $title
2373 *
2374 * @return RevisionRecord|null
2375 */
2376 private function loadRevisionFromConds(
2377 IDatabase $db,
2378 $conditions,
2379 $flags = 0,
2380 Title $title = null
2381 ) {
2382 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
2383 if ( $row ) {
2384 $rev = $this->newRevisionFromRow( $row, $flags, $title );
2385
2386 return $rev;
2387 }
2388
2389 return null;
2390 }
2391
2392 /**
2393 * Throws an exception if the given database connection does not belong to the wiki this
2394 * RevisionStore is bound to.
2395 *
2396 * @param IDatabase $db
2397 * @throws MWException
2398 */
2399 private function checkDatabaseDomain( IDatabase $db ) {
2400 $dbDomain = $db->getDomainID();
2401 $storeDomain = $this->loadBalancer->resolveDomainID( $this->dbDomain );
2402 if ( $dbDomain === $storeDomain ) {
2403 return;
2404 }
2405
2406 throw new MWException( "DB connection domain '$dbDomain' does not match '$storeDomain'" );
2407 }
2408
2409 /**
2410 * Given a set of conditions, return a row with the
2411 * fields necessary to build RevisionRecord objects.
2412 *
2413 * MCR migration note: this corresponds to Revision::fetchFromConds
2414 *
2415 * @param IDatabase $db
2416 * @param array $conditions
2417 * @param int $flags (optional)
2418 *
2419 * @return object|false data row as a raw object
2420 */
2421 private function fetchRevisionRowFromConds( IDatabase $db, $conditions, $flags = 0 ) {
2422 $this->checkDatabaseDomain( $db );
2423
2424 $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
2425 $options = [];
2426 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
2427 $options[] = 'FOR UPDATE';
2428 }
2429 return $db->selectRow(
2430 $revQuery['tables'],
2431 $revQuery['fields'],
2432 $conditions,
2433 __METHOD__,
2434 $options,
2435 $revQuery['joins']
2436 );
2437 }
2438
2439 /**
2440 * Finds the ID of a content row for a given revision and slot role.
2441 * This can be used to re-use content rows even while the content ID
2442 * is still missing from SlotRecords, when writing to both the old and
2443 * the new schema during MCR schema migration.
2444 *
2445 * @todo remove after MCR schema migration is complete.
2446 *
2447 * @param IDatabase $db
2448 * @param int $revId
2449 * @param string $role
2450 *
2451 * @return int|null
2452 */
2453 private function findSlotContentId( IDatabase $db, $revId, $role ) {
2454 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_NEW ) ) {
2455 return null;
2456 }
2457
2458 try {
2459 $roleId = $this->slotRoleStore->getId( $role );
2460 $conditions = [
2461 'slot_revision_id' => $revId,
2462 'slot_role_id' => $roleId,
2463 ];
2464
2465 $contentId = $db->selectField( 'slots', 'slot_content_id', $conditions, __METHOD__ );
2466
2467 return $contentId ?: null;
2468 } catch ( NameTableAccessException $ex ) {
2469 // If the role is missing from the slot_roles table,
2470 // the corresponding row in slots cannot exist.
2471 return null;
2472 }
2473 }
2474
2475 /**
2476 * Return the tables, fields, and join conditions to be selected to create
2477 * a new RevisionStoreRecord object.
2478 *
2479 * MCR migration note: this replaces Revision::getQueryInfo
2480 *
2481 * If the format of fields returned changes in any way then the cache key provided by
2482 * self::getRevisionRowCacheKey should be updated.
2483 *
2484 * @since 1.31
2485 *
2486 * @param array $options Any combination of the following strings
2487 * - 'page': Join with the page table, and select fields to identify the page
2488 * - 'user': Join with the user table, and select the user name
2489 * - 'text': Join with the text table, and select fields to load page text. This
2490 * option is deprecated in MW 1.32 when the MCR migration flag SCHEMA_COMPAT_WRITE_NEW
2491 * is set, and disallowed when SCHEMA_COMPAT_READ_OLD is not set.
2492 *
2493 * @return array With three keys:
2494 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2495 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2496 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2497 * @phan-return array{tables:string[],fields:string[],joins:array}
2498 */
2499 public function getQueryInfo( $options = [] ) {
2500 $ret = [
2501 'tables' => [],
2502 'fields' => [],
2503 'joins' => [],
2504 ];
2505
2506 $ret['tables'][] = 'revision';
2507 $ret['fields'] = array_merge( $ret['fields'], [
2508 'rev_id',
2509 'rev_page',
2510 'rev_timestamp',
2511 'rev_minor_edit',
2512 'rev_deleted',
2513 'rev_len',
2514 'rev_parent_id',
2515 'rev_sha1',
2516 ] );
2517
2518 $commentQuery = $this->commentStore->getJoin( 'rev_comment' );
2519 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
2520 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
2521 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
2522
2523 $actorQuery = $this->actorMigration->getJoin( 'rev_user' );
2524 $ret['tables'] = array_merge( $ret['tables'], $actorQuery['tables'] );
2525 $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
2526 $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
2527
2528 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
2529 $ret['fields'][] = 'rev_text_id';
2530
2531 if ( $this->contentHandlerUseDB ) {
2532 $ret['fields'][] = 'rev_content_format';
2533 $ret['fields'][] = 'rev_content_model';
2534 }
2535 }
2536
2537 if ( in_array( 'page', $options, true ) ) {
2538 $ret['tables'][] = 'page';
2539 $ret['fields'] = array_merge( $ret['fields'], [
2540 'page_namespace',
2541 'page_title',
2542 'page_id',
2543 'page_latest',
2544 'page_is_redirect',
2545 'page_len',
2546 ] );
2547 $ret['joins']['page'] = [ 'JOIN', [ 'page_id = rev_page' ] ];
2548 }
2549
2550 if ( in_array( 'user', $options, true ) ) {
2551 $ret['tables'][] = 'user';
2552 $ret['fields'] = array_merge( $ret['fields'], [
2553 'user_name',
2554 ] );
2555 $u = $actorQuery['fields']['rev_user'];
2556 $ret['joins']['user'] = [ 'LEFT JOIN', [ "$u != 0", "user_id = $u" ] ];
2557 }
2558
2559 if ( in_array( 'text', $options, true ) ) {
2560 if ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_WRITE_OLD ) ) {
2561 throw new InvalidArgumentException( 'text table can no longer be joined directly' );
2562 } elseif ( !$this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
2563 // NOTE: even when this class is set to not read from the old schema, callers
2564 // should still be able to join against the text table, as long as we are still
2565 // writing the old schema for compatibility.
2566 // TODO: This should trigger a deprecation warning eventually (T200918), but not
2567 // before all known usages are removed (see T198341 and T201164).
2568 // wfDeprecated( __METHOD__ . ' with `text` option', '1.32' );
2569 }
2570
2571 $ret['tables'][] = 'text';
2572 $ret['fields'] = array_merge( $ret['fields'], [
2573 'old_text',
2574 'old_flags'
2575 ] );
2576 $ret['joins']['text'] = [ 'JOIN', [ 'rev_text_id=old_id' ] ];
2577 }
2578
2579 return $ret;
2580 }
2581
2582 /**
2583 * Return the tables, fields, and join conditions to be selected to create
2584 * a new SlotRecord.
2585 *
2586 * @since 1.32
2587 *
2588 * @param array $options Any combination of the following strings
2589 * - 'content': Join with the content table, and select content meta-data fields
2590 * - 'model': Join with the content_models table, and select the model_name field.
2591 * Only applicable if 'content' is also set.
2592 * - 'role': Join with the slot_roles table, and select the role_name field
2593 *
2594 * @return array With three keys:
2595 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2596 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2597 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2598 */
2599 public function getSlotsQueryInfo( $options = [] ) {
2600 $ret = [
2601 'tables' => [],
2602 'fields' => [],
2603 'joins' => [],
2604 ];
2605
2606 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
2607 $db = $this->getDBConnectionRef( DB_REPLICA );
2608 $ret['tables'][] = 'revision';
2609
2610 $ret['fields']['slot_revision_id'] = 'rev_id';
2611 $ret['fields']['slot_content_id'] = 'NULL';
2612 $ret['fields']['slot_origin'] = 'rev_id';
2613 $ret['fields']['role_name'] = $db->addQuotes( SlotRecord::MAIN );
2614
2615 if ( in_array( 'content', $options, true ) ) {
2616 $ret['fields']['content_size'] = 'rev_len';
2617 $ret['fields']['content_sha1'] = 'rev_sha1';
2618 $ret['fields']['content_address']
2619 = $db->buildConcat( [ $db->addQuotes( 'tt:' ), 'rev_text_id' ] );
2620
2621 // Allow the content_id field to be emulated later
2622 $ret['fields']['rev_text_id'] = 'rev_text_id';
2623
2624 if ( $this->contentHandlerUseDB ) {
2625 $ret['fields']['model_name'] = 'rev_content_model';
2626 } else {
2627 $ret['fields']['model_name'] = 'NULL';
2628 }
2629 }
2630 } else {
2631 $ret['tables'][] = 'slots';
2632 $ret['fields'] = array_merge( $ret['fields'], [
2633 'slot_revision_id',
2634 'slot_content_id',
2635 'slot_origin',
2636 'slot_role_id',
2637 ] );
2638
2639 if ( in_array( 'role', $options, true ) ) {
2640 // Use left join to attach role name, so we still find the revision row even
2641 // if the role name is missing. This triggers a more obvious failure mode.
2642 $ret['tables'][] = 'slot_roles';
2643 $ret['joins']['slot_roles'] = [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ];
2644 $ret['fields'][] = 'role_name';
2645 }
2646
2647 if ( in_array( 'content', $options, true ) ) {
2648 $ret['tables'][] = 'content';
2649 $ret['fields'] = array_merge( $ret['fields'], [
2650 'content_size',
2651 'content_sha1',
2652 'content_address',
2653 'content_model',
2654 ] );
2655 $ret['joins']['content'] = [ 'JOIN', [ 'slot_content_id = content_id' ] ];
2656
2657 if ( in_array( 'model', $options, true ) ) {
2658 // Use left join to attach model name, so we still find the revision row even
2659 // if the model name is missing. This triggers a more obvious failure mode.
2660 $ret['tables'][] = 'content_models';
2661 $ret['joins']['content_models'] = [ 'LEFT JOIN', [ 'content_model = model_id' ] ];
2662 $ret['fields'][] = 'model_name';
2663 }
2664
2665 }
2666 }
2667
2668 return $ret;
2669 }
2670
2671 /**
2672 * Return the tables, fields, and join conditions to be selected to create
2673 * a new RevisionArchiveRecord object.
2674 *
2675 * MCR migration note: this replaces Revision::getArchiveQueryInfo
2676 *
2677 * @since 1.31
2678 *
2679 * @return array With three keys:
2680 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2681 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2682 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2683 */
2684 public function getArchiveQueryInfo() {
2685 $commentQuery = $this->commentStore->getJoin( 'ar_comment' );
2686 $actorQuery = $this->actorMigration->getJoin( 'ar_user' );
2687 $ret = [
2688 'tables' => [ 'archive' ] + $commentQuery['tables'] + $actorQuery['tables'],
2689 'fields' => [
2690 'ar_id',
2691 'ar_page_id',
2692 'ar_namespace',
2693 'ar_title',
2694 'ar_rev_id',
2695 'ar_timestamp',
2696 'ar_minor_edit',
2697 'ar_deleted',
2698 'ar_len',
2699 'ar_parent_id',
2700 'ar_sha1',
2701 ] + $commentQuery['fields'] + $actorQuery['fields'],
2702 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
2703 ];
2704
2705 if ( $this->hasMcrSchemaFlags( SCHEMA_COMPAT_READ_OLD ) ) {
2706 $ret['fields'][] = 'ar_text_id';
2707
2708 if ( $this->contentHandlerUseDB ) {
2709 $ret['fields'][] = 'ar_content_format';
2710 $ret['fields'][] = 'ar_content_model';
2711 }
2712 }
2713
2714 return $ret;
2715 }
2716
2717 /**
2718 * Do a batched query for the sizes of a set of revisions.
2719 *
2720 * MCR migration note: this replaces Revision::getParentLengths
2721 *
2722 * @param int[] $revIds
2723 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2724 * of the corresponding revision.
2725 */
2726 public function getRevisionSizes( array $revIds ) {
2727 return $this->listRevisionSizes( $this->getDBConnectionRef( DB_REPLICA ), $revIds );
2728 }
2729
2730 /**
2731 * Do a batched query for the sizes of a set of revisions.
2732 *
2733 * MCR migration note: this replaces Revision::getParentLengths
2734 *
2735 * @deprecated use RevisionStore::getRevisionSizes instead.
2736 *
2737 * @param IDatabase $db
2738 * @param int[] $revIds
2739 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2740 * of the corresponding revision.
2741 */
2742 public function listRevisionSizes( IDatabase $db, array $revIds ) {
2743 $this->checkDatabaseDomain( $db );
2744
2745 $revLens = [];
2746 if ( !$revIds ) {
2747 return $revLens; // empty
2748 }
2749
2750 $res = $db->select(
2751 'revision',
2752 [ 'rev_id', 'rev_len' ],
2753 [ 'rev_id' => $revIds ],
2754 __METHOD__
2755 );
2756
2757 foreach ( $res as $row ) {
2758 $revLens[$row->rev_id] = intval( $row->rev_len );
2759 }
2760
2761 return $revLens;
2762 }
2763
2764 /**
2765 * Implementation of getPreviousRevision and getNextRevision.
2766 *
2767 * @param RevisionRecord $rev
2768 * @param int $flags
2769 * @param string $dir 'next' or 'prev'
2770 * @return RevisionRecord|null
2771 */
2772 private function getRelativeRevision( RevisionRecord $rev, $flags, $dir ) {
2773 $op = $dir === 'next' ? '>' : '<';
2774 $sort = $dir === 'next' ? 'ASC' : 'DESC';
2775
2776 if ( !$rev->getId() || !$rev->getPageId() ) {
2777 // revision is unsaved or otherwise incomplete
2778 return null;
2779 }
2780
2781 if ( $rev instanceof RevisionArchiveRecord ) {
2782 // revision is deleted, so it's not part of the page history
2783 return null;
2784 }
2785
2786 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
2787 $db = $this->getDBConnectionRef( $dbType, [ 'contributions' ] );
2788
2789 $ts = $this->getTimestampFromId( $rev->getId(), $flags );
2790 if ( $ts === false ) {
2791 // XXX Should this be moved into getTimestampFromId?
2792 $ts = $db->selectField( 'archive', 'ar_timestamp',
2793 [ 'ar_rev_id' => $rev->getId() ], __METHOD__ );
2794 if ( $ts === false ) {
2795 // XXX Is this reachable? How can we have a page id but no timestamp?
2796 return null;
2797 }
2798 }
2799 $ts = $db->addQuotes( $db->timestamp( $ts ) );
2800
2801 $revId = $db->selectField( 'revision', 'rev_id',
2802 [
2803 'rev_page' => $rev->getPageId(),
2804 "rev_timestamp $op $ts OR (rev_timestamp = $ts AND rev_id $op {$rev->getId()})"
2805 ],
2806 __METHOD__,
2807 [
2808 'ORDER BY' => "rev_timestamp $sort, rev_id $sort",
2809 'IGNORE INDEX' => 'rev_timestamp', // Probably needed for T159319
2810 ]
2811 );
2812
2813 if ( $revId === false ) {
2814 return null;
2815 }
2816
2817 return $this->getRevisionById( intval( $revId ) );
2818 }
2819
2820 /**
2821 * Get the revision before $rev in the page's history, if any.
2822 * Will return null for the first revision but also for deleted or unsaved revisions.
2823 *
2824 * MCR migration note: this replaces Revision::getPrevious
2825 *
2826 * @see Title::getPreviousRevisionID
2827 * @see PageArchive::getPreviousRevision
2828 *
2829 * @param RevisionRecord $rev
2830 * @param int $flags (optional) $flags include:
2831 * IDBAccessObject::READ_LATEST: Select the data from the master
2832 *
2833 * @return RevisionRecord|null
2834 */
2835 public function getPreviousRevision( RevisionRecord $rev, $flags = 0 ) {
2836 if ( $flags instanceof Title ) {
2837 // Old calling convention, we don't use Title here anymore
2838 wfDeprecated( __METHOD__ . ' with Title', '1.34' );
2839 $flags = 0;
2840 }
2841
2842 return $this->getRelativeRevision( $rev, $flags, 'prev' );
2843 }
2844
2845 /**
2846 * Get the revision after $rev in the page's history, if any.
2847 * Will return null for the latest revision but also for deleted or unsaved revisions.
2848 *
2849 * MCR migration note: this replaces Revision::getNext
2850 *
2851 * @see Title::getNextRevisionID
2852 *
2853 * @param RevisionRecord $rev
2854 * @param int $flags (optional) $flags include:
2855 * IDBAccessObject::READ_LATEST: Select the data from the master
2856 * @return RevisionRecord|null
2857 */
2858 public function getNextRevision( RevisionRecord $rev, $flags = 0 ) {
2859 if ( $flags instanceof Title ) {
2860 // Old calling convention, we don't use Title here anymore
2861 wfDeprecated( __METHOD__ . ' with Title', '1.34' );
2862 $flags = 0;
2863 }
2864
2865 return $this->getRelativeRevision( $rev, $flags, 'next' );
2866 }
2867
2868 /**
2869 * Get previous revision Id for this page_id
2870 * This is used to populate rev_parent_id on save
2871 *
2872 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
2873 *
2874 * @param IDatabase $db
2875 * @param RevisionRecord $rev
2876 *
2877 * @return int
2878 */
2879 private function getPreviousRevisionId( IDatabase $db, RevisionRecord $rev ) {
2880 $this->checkDatabaseDomain( $db );
2881
2882 if ( $rev->getPageId() === null ) {
2883 return 0;
2884 }
2885 # Use page_latest if ID is not given
2886 if ( !$rev->getId() ) {
2887 $prevId = $db->selectField(
2888 'page', 'page_latest',
2889 [ 'page_id' => $rev->getPageId() ],
2890 __METHOD__
2891 );
2892 } else {
2893 $prevId = $db->selectField(
2894 'revision', 'rev_id',
2895 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
2896 __METHOD__,
2897 [ 'ORDER BY' => 'rev_id DESC' ]
2898 );
2899 }
2900 return intval( $prevId );
2901 }
2902
2903 /**
2904 * Get rev_timestamp from rev_id, without loading the rest of the row.
2905 *
2906 * Historically, there was an extra Title parameter that was passed before $id. This is no
2907 * longer needed and is deprecated in 1.34.
2908 *
2909 * MCR migration note: this replaces Revision::getTimestampFromId
2910 *
2911 * @param int $id
2912 * @param int $flags
2913 * @return string|bool False if not found
2914 */
2915 public function getTimestampFromId( $id, $flags = 0 ) {
2916 if ( $id instanceof Title ) {
2917 // Old deprecated calling convention supported for backwards compatibility
2918 $id = $flags;
2919 $flags = func_num_args() > 2 ? func_get_arg( 2 ) : 0;
2920 }
2921 $db = $this->getDBConnectionRefForQueryFlags( $flags );
2922
2923 $timestamp =
2924 $db->selectField( 'revision', 'rev_timestamp', [ 'rev_id' => $id ], __METHOD__ );
2925
2926 return ( $timestamp !== false ) ? wfTimestamp( TS_MW, $timestamp ) : false;
2927 }
2928
2929 /**
2930 * Get count of revisions per page...not very efficient
2931 *
2932 * MCR migration note: this replaces Revision::countByPageId
2933 *
2934 * @param IDatabase $db
2935 * @param int $id Page id
2936 * @return int
2937 */
2938 public function countRevisionsByPageId( IDatabase $db, $id ) {
2939 $this->checkDatabaseDomain( $db );
2940
2941 $row = $db->selectRow( 'revision',
2942 [ 'revCount' => 'COUNT(*)' ],
2943 [ 'rev_page' => $id ],
2944 __METHOD__
2945 );
2946 if ( $row ) {
2947 return intval( $row->revCount );
2948 }
2949 return 0;
2950 }
2951
2952 /**
2953 * Get count of revisions per page...not very efficient
2954 *
2955 * MCR migration note: this replaces Revision::countByTitle
2956 *
2957 * @param IDatabase $db
2958 * @param Title $title
2959 * @return int
2960 */
2961 public function countRevisionsByTitle( IDatabase $db, $title ) {
2962 $id = $title->getArticleID();
2963 if ( $id ) {
2964 return $this->countRevisionsByPageId( $db, $id );
2965 }
2966 return 0;
2967 }
2968
2969 /**
2970 * Check if no edits were made by other users since
2971 * the time a user started editing the page. Limit to
2972 * 50 revisions for the sake of performance.
2973 *
2974 * MCR migration note: this replaces Revision::userWasLastToEdit
2975 *
2976 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
2977 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
2978 * has been deprecated since 1.24.
2979 *
2980 * @param IDatabase $db The Database to perform the check on.
2981 * @param int $pageId The ID of the page in question
2982 * @param int $userId The ID of the user in question
2983 * @param string $since Look at edits since this time
2984 *
2985 * @return bool True if the given user was the only one to edit since the given timestamp
2986 */
2987 public function userWasLastToEdit( IDatabase $db, $pageId, $userId, $since ) {
2988 $this->checkDatabaseDomain( $db );
2989
2990 if ( !$userId ) {
2991 return false;
2992 }
2993
2994 $revQuery = $this->getQueryInfo();
2995 $res = $db->select(
2996 $revQuery['tables'],
2997 [
2998 'rev_user' => $revQuery['fields']['rev_user'],
2999 ],
3000 [
3001 'rev_page' => $pageId,
3002 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
3003 ],
3004 __METHOD__,
3005 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
3006 $revQuery['joins']
3007 );
3008 foreach ( $res as $row ) {
3009 if ( $row->rev_user != $userId ) {
3010 return false;
3011 }
3012 }
3013 return true;
3014 }
3015
3016 /**
3017 * Load a revision based on a known page ID and current revision ID from the DB
3018 *
3019 * This method allows for the use of caching, though accessing anything that normally
3020 * requires permission checks (aside from the text) will trigger a small DB lookup.
3021 *
3022 * MCR migration note: this replaces Revision::newKnownCurrent
3023 *
3024 * @param Title $title the associated page title
3025 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
3026 *
3027 * @return RevisionRecord|bool Returns false if missing
3028 */
3029 public function getKnownCurrentRevision( Title $title, $revId ) {
3030 $db = $this->getDBConnectionRef( DB_REPLICA );
3031
3032 $pageId = $title->getArticleID();
3033
3034 if ( !$pageId ) {
3035 return false;
3036 }
3037
3038 if ( !$revId ) {
3039 $revId = $title->getLatestRevID();
3040 }
3041
3042 if ( !$revId ) {
3043 wfWarn(
3044 'No latest revision known for page ' . $title->getPrefixedDBkey()
3045 . ' even though it exists with page ID ' . $pageId
3046 );
3047 return false;
3048 }
3049
3050 // Load the row from cache if possible. If not possible, populate the cache.
3051 // As a minor optimization, remember if this was a cache hit or miss.
3052 // We can sometimes avoid a database query later if this is a cache miss.
3053 $fromCache = true;
3054 $row = $this->cache->getWithSetCallback(
3055 // Page/rev IDs passed in from DB to reflect history merges
3056 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
3057 WANObjectCache::TTL_WEEK,
3058 function ( $curValue, &$ttl, array &$setOpts ) use (
3059 $db, $pageId, $revId, &$fromCache
3060 ) {
3061 $setOpts += Database::getCacheSetOptions( $db );
3062 $row = $this->fetchRevisionRowFromConds( $db, [ 'rev_id' => intval( $revId ) ] );
3063 if ( $row ) {
3064 $fromCache = false;
3065 }
3066 return $row; // don't cache negatives
3067 }
3068 );
3069
3070 // Reflect revision deletion and user renames.
3071 if ( $row ) {
3072 return $this->newRevisionFromRow( $row, 0, $title, $fromCache );
3073 } else {
3074 return false;
3075 }
3076 }
3077
3078 /**
3079 * Get a cache key for use with a row as selected with getQueryInfo( [ 'page', 'user' ] )
3080 * Caching rows without 'page' or 'user' could lead to issues.
3081 * If the format of the rows returned by the query provided by getQueryInfo changes the
3082 * cache key should be updated to avoid conflicts.
3083 *
3084 * @param IDatabase $db
3085 * @param int $pageId
3086 * @param int $revId
3087 * @return string
3088 */
3089 private function getRevisionRowCacheKey( IDatabase $db, $pageId, $revId ) {
3090 return $this->cache->makeGlobalKey(
3091 self::ROW_CACHE_KEY,
3092 $db->getDomainID(),
3093 $pageId,
3094 $revId
3095 );
3096 }
3097
3098 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.
3099
3100 }
3101
3102 /**
3103 * Retain the old class name for backwards compatibility.
3104 * @deprecated since 1.32
3105 */
3106 class_alias( RevisionStore::class, 'MediaWiki\Storage\RevisionStore' );