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