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