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