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