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