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