Merge "Document return of void in Maintenance::execute"
[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, without locking all extra tables.
900 $oldRevision = $this->loadRevisionFromId( $dbw, $pageLatest );
901
902 // Construct the new revision
903 $timestamp = wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
904 $newRevision = MutableRevisionRecord::newFromParentRevision( $oldRevision );
905
906 $newRevision->setComment( $comment );
907 $newRevision->setUser( $user );
908 $newRevision->setTimestamp( $timestamp );
909 $newRevision->setMinorEdit( $minor );
910
911 return $newRevision;
912 }
913
914 /**
915 * MCR migration note: this replaces Revision::isUnpatrolled
916 *
917 * @todo This is overly specific, so move or kill this method.
918 *
919 * @param RevisionRecord $rev
920 *
921 * @return int Rcid of the unpatrolled row, zero if there isn't one
922 */
923 public function getRcIdIfUnpatrolled( RevisionRecord $rev ) {
924 $rc = $this->getRecentChange( $rev );
925 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == RecentChange::PRC_UNPATROLLED ) {
926 return $rc->getAttribute( 'rc_id' );
927 } else {
928 return 0;
929 }
930 }
931
932 /**
933 * Get the RC object belonging to the current revision, if there's one
934 *
935 * MCR migration note: this replaces Revision::getRecentChange
936 *
937 * @todo move this somewhere else?
938 *
939 * @param RevisionRecord $rev
940 * @param int $flags (optional) $flags include:
941 * IDBAccessObject::READ_LATEST: Select the data from the master
942 *
943 * @return null|RecentChange
944 */
945 public function getRecentChange( RevisionRecord $rev, $flags = 0 ) {
946 $dbr = $this->getDBConnection( DB_REPLICA );
947
948 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
949
950 $userIdentity = $rev->getUser( RevisionRecord::RAW );
951
952 if ( !$userIdentity ) {
953 // If the revision has no user identity, chances are it never went
954 // into the database, and doesn't have an RC entry.
955 return null;
956 }
957
958 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
959 $actorWhere = $this->actorMigration->getWhere( $dbr, 'rc_user', $rev->getUser(), false );
960 $rc = RecentChange::newFromConds(
961 [
962 $actorWhere['conds'],
963 'rc_timestamp' => $dbr->timestamp( $rev->getTimestamp() ),
964 'rc_this_oldid' => $rev->getId()
965 ],
966 __METHOD__,
967 $dbType
968 );
969
970 $this->releaseDBConnection( $dbr );
971
972 // XXX: cache this locally? Glue it to the RevisionRecord?
973 return $rc;
974 }
975
976 /**
977 * Maps fields of the archive row to corresponding revision rows.
978 *
979 * @param object $archiveRow
980 *
981 * @return object a revision row object, corresponding to $archiveRow.
982 */
983 private static function mapArchiveFields( $archiveRow ) {
984 $fieldMap = [
985 // keep with ar prefix:
986 'ar_id' => 'ar_id',
987
988 // not the same suffix:
989 'ar_page_id' => 'rev_page',
990 'ar_rev_id' => 'rev_id',
991
992 // same suffix:
993 'ar_text_id' => 'rev_text_id',
994 'ar_timestamp' => 'rev_timestamp',
995 'ar_user_text' => 'rev_user_text',
996 'ar_user' => 'rev_user',
997 'ar_actor' => 'rev_actor',
998 'ar_minor_edit' => 'rev_minor_edit',
999 'ar_deleted' => 'rev_deleted',
1000 'ar_len' => 'rev_len',
1001 'ar_parent_id' => 'rev_parent_id',
1002 'ar_sha1' => 'rev_sha1',
1003 'ar_comment' => 'rev_comment',
1004 'ar_comment_cid' => 'rev_comment_cid',
1005 'ar_comment_id' => 'rev_comment_id',
1006 'ar_comment_text' => 'rev_comment_text',
1007 'ar_comment_data' => 'rev_comment_data',
1008 'ar_comment_old' => 'rev_comment_old',
1009 'ar_content_format' => 'rev_content_format',
1010 'ar_content_model' => 'rev_content_model',
1011 ];
1012
1013 $revRow = new stdClass();
1014 foreach ( $fieldMap as $arKey => $revKey ) {
1015 if ( property_exists( $archiveRow, $arKey ) ) {
1016 $revRow->$revKey = $archiveRow->$arKey;
1017 }
1018 }
1019
1020 return $revRow;
1021 }
1022
1023 /**
1024 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
1025 *
1026 * @param object|array $row Either a database row or an array
1027 * @param int $queryFlags for callbacks
1028 * @param Title $title
1029 *
1030 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
1031 * @throws MWException
1032 */
1033 private function emulateMainSlot_1_29( $row, $queryFlags, Title $title ) {
1034 $mainSlotRow = new stdClass();
1035 $mainSlotRow->role_name = 'main';
1036 $mainSlotRow->model_name = null;
1037 $mainSlotRow->slot_revision_id = null;
1038 $mainSlotRow->content_address = null;
1039
1040 $content = null;
1041 $blobData = null;
1042 $blobFlags = null;
1043
1044 if ( is_object( $row ) ) {
1045 if ( $this->mcrMigrationStage >= MIGRATION_NEW ) {
1046 // Don't emulate from a row when using the new schema.
1047 // Emulating from an array is still OK.
1048 throw new LogicException( 'Can\'t emulate the main slot when using MCR schema.' );
1049 }
1050
1051 // archive row
1052 if ( !isset( $row->rev_id ) && ( isset( $row->ar_user ) || isset( $row->ar_actor ) ) ) {
1053 $row = $this->mapArchiveFields( $row );
1054 }
1055
1056 if ( isset( $row->rev_text_id ) && $row->rev_text_id > 0 ) {
1057 $mainSlotRow->content_address = SqlBlobStore::makeAddressFromTextId(
1058 $row->rev_text_id
1059 );
1060 }
1061
1062 // This is used by null-revisions
1063 $mainSlotRow->slot_origin = isset( $row->slot_origin )
1064 ? intval( $row->slot_origin )
1065 : null;
1066
1067 if ( isset( $row->old_text ) ) {
1068 // this happens when the text-table gets joined directly, in the pre-1.30 schema
1069 $blobData = isset( $row->old_text ) ? strval( $row->old_text ) : null;
1070 // Check against selects that might have not included old_flags
1071 if ( !property_exists( $row, 'old_flags' ) ) {
1072 throw new InvalidArgumentException( 'old_flags was not set in $row' );
1073 }
1074 $blobFlags = ( $row->old_flags === null ) ? '' : $row->old_flags;
1075 }
1076
1077 $mainSlotRow->slot_revision_id = intval( $row->rev_id );
1078
1079 $mainSlotRow->content_size = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
1080 $mainSlotRow->content_sha1 = isset( $row->rev_sha1 ) ? strval( $row->rev_sha1 ) : null;
1081 $mainSlotRow->model_name = isset( $row->rev_content_model )
1082 ? strval( $row->rev_content_model )
1083 : null;
1084 // XXX: in the future, we'll probably always use the default format, and drop content_format
1085 $mainSlotRow->format_name = isset( $row->rev_content_format )
1086 ? strval( $row->rev_content_format )
1087 : null;
1088 } elseif ( is_array( $row ) ) {
1089 $mainSlotRow->slot_revision_id = isset( $row['id'] ) ? intval( $row['id'] ) : null;
1090
1091 $mainSlotRow->slot_origin = isset( $row['slot_origin'] )
1092 ? intval( $row['slot_origin'] )
1093 : null;
1094 $mainSlotRow->content_address = isset( $row['text_id'] )
1095 ? SqlBlobStore::makeAddressFromTextId( intval( $row['text_id'] ) )
1096 : null;
1097 $mainSlotRow->content_size = isset( $row['len'] ) ? intval( $row['len'] ) : null;
1098 $mainSlotRow->content_sha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
1099
1100 $mainSlotRow->model_name = isset( $row['content_model'] )
1101 ? strval( $row['content_model'] ) : null; // XXX: must be a string!
1102 // XXX: in the future, we'll probably always use the default format, and drop content_format
1103 $mainSlotRow->format_name = isset( $row['content_format'] )
1104 ? strval( $row['content_format'] ) : null;
1105 $blobData = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
1106 // XXX: If the flags field is not set then $blobFlags should be null so that no
1107 // decoding will happen. An empty string will result in default decodings.
1108 $blobFlags = isset( $row['flags'] ) ? trim( strval( $row['flags'] ) ) : null;
1109
1110 // if we have a Content object, override mText and mContentModel
1111 if ( !empty( $row['content'] ) ) {
1112 if ( !( $row['content'] instanceof Content ) ) {
1113 throw new MWException( 'content field must contain a Content object.' );
1114 }
1115
1116 /** @var Content $content */
1117 $content = $row['content'];
1118 $handler = $content->getContentHandler();
1119
1120 $mainSlotRow->model_name = $content->getModel();
1121
1122 // XXX: in the future, we'll probably always use the default format.
1123 if ( $mainSlotRow->format_name === null ) {
1124 $mainSlotRow->format_name = $handler->getDefaultFormat();
1125 }
1126 }
1127 } else {
1128 throw new MWException( 'Revision constructor passed invalid row format.' );
1129 }
1130
1131 // With the old schema, the content changes with every revision,
1132 // except for null-revisions.
1133 if ( !isset( $mainSlotRow->slot_origin ) ) {
1134 $mainSlotRow->slot_origin = $mainSlotRow->slot_revision_id;
1135 }
1136
1137 if ( $mainSlotRow->model_name === null ) {
1138 $mainSlotRow->model_name = function ( SlotRecord $slot ) use ( $title ) {
1139 // TODO: MCR: consider slot role in getDefaultModelFor()! Use LinkTarget!
1140 // TODO: MCR: deprecate $title->getModel().
1141 return ContentHandler::getDefaultModelFor( $title );
1142 };
1143 }
1144
1145 if ( !$content ) {
1146 // XXX: We should perhaps fail if $blobData is null and $mainSlotRow->content_address
1147 // is missing, but "empty revisions" with no content are used in some edge cases.
1148
1149 $content = function ( SlotRecord $slot )
1150 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
1151 {
1152 return $this->loadSlotContent(
1153 $slot,
1154 $blobData,
1155 $blobFlags,
1156 $mainSlotRow->format_name,
1157 $queryFlags
1158 );
1159 };
1160 }
1161
1162 // NOTE: this callback will be looped through RevisionSlot::newInherited(), allowing
1163 // the inherited slot to have the same content_id as the original slot. In that case,
1164 // $slot will be the inherited slot, while $mainSlotRow still refers to the original slot.
1165 $mainSlotRow->slot_content_id =
1166 function ( SlotRecord $slot ) use ( $queryFlags, $mainSlotRow ) {
1167 list( $dbMode, ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
1168 $db = $this->getDBConnectionRef( $dbMode );
1169 return $this->findSlotContentId( $db, $mainSlotRow->slot_revision_id, 'main' );
1170 };
1171
1172 return new SlotRecord( $mainSlotRow, $content );
1173 }
1174
1175 /**
1176 * Loads a Content object based on a slot row.
1177 *
1178 * This method does not call $slot->getContent(), and may be used as a callback
1179 * called by $slot->getContent().
1180 *
1181 * MCR migration note: this roughly corresponds to Revision::getContentInternal
1182 *
1183 * @param SlotRecord $slot The SlotRecord to load content for
1184 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
1185 * @param string|null $blobFlags Flags indicating how $blobData needs to be processed.
1186 * Use null if no processing should happen. That is in constrast to the empty string,
1187 * which causes the blob to be decoded according to the configured legacy encoding.
1188 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
1189 * @param int $queryFlags
1190 *
1191 * @throws RevisionAccessException
1192 * @return Content
1193 */
1194 private function loadSlotContent(
1195 SlotRecord $slot,
1196 $blobData = null,
1197 $blobFlags = null,
1198 $blobFormat = null,
1199 $queryFlags = 0
1200 ) {
1201 if ( $blobData !== null ) {
1202 Assert::parameterType( 'string', $blobData, '$blobData' );
1203 Assert::parameterType( 'string|null', $blobFlags, '$blobFlags' );
1204
1205 $cacheKey = $slot->hasAddress() ? $slot->getAddress() : null;
1206
1207 if ( $blobFlags === null ) {
1208 // No blob flags, so use the blob verbatim.
1209 $data = $blobData;
1210 } else {
1211 $data = $this->blobStore->expandBlob( $blobData, $blobFlags, $cacheKey );
1212 if ( $data === false ) {
1213 throw new RevisionAccessException(
1214 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
1215 );
1216 }
1217 }
1218
1219 } else {
1220 $address = $slot->getAddress();
1221 try {
1222 $data = $this->blobStore->getBlob( $address, $queryFlags );
1223 } catch ( BlobAccessException $e ) {
1224 throw new RevisionAccessException(
1225 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
1226 );
1227 }
1228 }
1229
1230 // Unserialize content
1231 $handler = ContentHandler::getForModelID( $slot->getModel() );
1232
1233 $content = $handler->unserializeContent( $data, $blobFormat );
1234 return $content;
1235 }
1236
1237 /**
1238 * Load a page revision from a given revision ID number.
1239 * Returns null if no such revision can be found.
1240 *
1241 * MCR migration note: this replaces Revision::newFromId
1242 *
1243 * $flags include:
1244 * IDBAccessObject::READ_LATEST: Select the data from the master
1245 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1246 *
1247 * @param int $id
1248 * @param int $flags (optional)
1249 * @return RevisionRecord|null
1250 */
1251 public function getRevisionById( $id, $flags = 0 ) {
1252 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
1253 }
1254
1255 /**
1256 * Load either the current, or a specified, revision
1257 * that's attached to a given link target. If not attached
1258 * to that link target, will return null.
1259 *
1260 * MCR migration note: this replaces Revision::newFromTitle
1261 *
1262 * $flags include:
1263 * IDBAccessObject::READ_LATEST: Select the data from the master
1264 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1265 *
1266 * @param LinkTarget $linkTarget
1267 * @param int $revId (optional)
1268 * @param int $flags Bitfield (optional)
1269 * @return RevisionRecord|null
1270 */
1271 public function getRevisionByTitle( LinkTarget $linkTarget, $revId = 0, $flags = 0 ) {
1272 $conds = [
1273 'page_namespace' => $linkTarget->getNamespace(),
1274 'page_title' => $linkTarget->getDBkey()
1275 ];
1276 if ( $revId ) {
1277 // Use the specified revision ID.
1278 // Note that we use newRevisionFromConds here because we want to retry
1279 // and fall back to master if the page is not found on a replica.
1280 // Since the caller supplied a revision ID, we are pretty sure the revision is
1281 // supposed to exist, so we should try hard to find it.
1282 $conds['rev_id'] = $revId;
1283 return $this->newRevisionFromConds( $conds, $flags );
1284 } else {
1285 // Use a join to get the latest revision.
1286 // Note that we don't use newRevisionFromConds here because we don't want to retry
1287 // and fall back to master. The assumption is that we only want to force the fallback
1288 // if we are quite sure the revision exists because the caller supplied a revision ID.
1289 // If the page isn't found at all on a replica, it probably simply does not exist.
1290 $db = $this->getDBConnection( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
1291
1292 $conds[] = 'rev_id=page_latest';
1293 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1294
1295 $this->releaseDBConnection( $db );
1296 return $rev;
1297 }
1298 }
1299
1300 /**
1301 * Load either the current, or a specified, revision
1302 * that's attached to a given page ID.
1303 * Returns null if no such revision can be found.
1304 *
1305 * MCR migration note: this replaces Revision::newFromPageId
1306 *
1307 * $flags include:
1308 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
1309 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
1310 *
1311 * @param int $pageId
1312 * @param int $revId (optional)
1313 * @param int $flags Bitfield (optional)
1314 * @return RevisionRecord|null
1315 */
1316 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
1317 $conds = [ 'page_id' => $pageId ];
1318 if ( $revId ) {
1319 // Use the specified revision ID.
1320 // Note that we use newRevisionFromConds here because we want to retry
1321 // and fall back to master if the page is not found on a replica.
1322 // Since the caller supplied a revision ID, we are pretty sure the revision is
1323 // supposed to exist, so we should try hard to find it.
1324 $conds['rev_id'] = $revId;
1325 return $this->newRevisionFromConds( $conds, $flags );
1326 } else {
1327 // Use a join to get the latest revision.
1328 // Note that we don't use newRevisionFromConds here because we don't want to retry
1329 // and fall back to master. The assumption is that we only want to force the fallback
1330 // if we are quite sure the revision exists because the caller supplied a revision ID.
1331 // If the page isn't found at all on a replica, it probably simply does not exist.
1332 $db = $this->getDBConnection( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
1333
1334 $conds[] = 'rev_id=page_latest';
1335 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
1336
1337 $this->releaseDBConnection( $db );
1338 return $rev;
1339 }
1340 }
1341
1342 /**
1343 * Load the revision for the given title with the given timestamp.
1344 * WARNING: Timestamps may in some circumstances not be unique,
1345 * so this isn't the best key to use.
1346 *
1347 * MCR migration note: this replaces Revision::loadFromTimestamp
1348 *
1349 * @param Title $title
1350 * @param string $timestamp
1351 * @return RevisionRecord|null
1352 */
1353 public function getRevisionByTimestamp( $title, $timestamp ) {
1354 $db = $this->getDBConnection( DB_REPLICA );
1355 return $this->newRevisionFromConds(
1356 [
1357 'rev_timestamp' => $db->timestamp( $timestamp ),
1358 'page_namespace' => $title->getNamespace(),
1359 'page_title' => $title->getDBkey()
1360 ],
1361 0,
1362 $title
1363 );
1364 }
1365
1366 /**
1367 * @param int $revId The revision to load slots for.
1368 * @param int $queryFlags
1369 *
1370 * @return SlotRecord[]
1371 */
1372 private function loadSlotRecords( $revId, $queryFlags ) {
1373 $revQuery = self::getSlotsQueryInfo( [ 'content' ] );
1374
1375 list( $dbMode, $dbOptions ) = DBAccessObjectUtils::getDBOptions( $queryFlags );
1376 $db = $this->getDBConnectionRef( $dbMode );
1377
1378 $res = $db->select(
1379 $revQuery['tables'],
1380 $revQuery['fields'],
1381 [
1382 'slot_revision_id' => $revId,
1383 ],
1384 __METHOD__,
1385 $dbOptions,
1386 $revQuery['joins']
1387 );
1388
1389 $slots = [];
1390
1391 foreach ( $res as $row ) {
1392 $contentCallback = function ( SlotRecord $slot ) use ( $queryFlags, $row ) {
1393 return $this->loadSlotContent( $slot, null, null, null, $queryFlags );
1394 };
1395
1396 $slots[$row->role_name] = new SlotRecord( $row, $contentCallback );
1397 }
1398
1399 if ( !isset( $slots['main'] ) ) {
1400 throw new RevisionAccessException(
1401 'Main slot of revision ' . $revId . ' not found in database!'
1402 );
1403 };
1404
1405 return $slots;
1406 }
1407
1408 /**
1409 * Factory method for RevisionSlots.
1410 *
1411 * @note If other code has a need to construct RevisionSlots objects, this should be made
1412 * public, since RevisionSlots instances should not be constructed directly.
1413 *
1414 * @param int $revId
1415 * @param object $revisionRow
1416 * @param int $queryFlags
1417 * @param Title $title
1418 *
1419 * @return RevisionSlots
1420 * @throws MWException
1421 */
1422 private function newRevisionSlots(
1423 $revId,
1424 $revisionRow,
1425 $queryFlags,
1426 Title $title
1427 ) {
1428 if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
1429 // TODO: in MIGRATION_WRITE_BOTH, we could use the old and the new method:
1430 // e.g. call emulateMainSlot_1_29() if loadSlotRecords() fails.
1431
1432 $mainSlot = $this->emulateMainSlot_1_29( $revisionRow, $queryFlags, $title );
1433 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1434 } else {
1435 // XXX: do we need the same kind of caching here
1436 // that getKnownCurrentRevision uses (if $revId == page_latest?)
1437
1438 $slots = new RevisionSlots( function () use( $revId, $queryFlags ) {
1439 return $this->loadSlotRecords( $revId, $queryFlags );
1440 } );
1441 }
1442
1443 return $slots;
1444 }
1445
1446 /**
1447 * Make a fake revision object from an archive table row. This is queried
1448 * for permissions or even inserted (as in Special:Undelete)
1449 *
1450 * MCR migration note: this replaces Revision::newFromArchiveRow
1451 *
1452 * @param object $row
1453 * @param int $queryFlags
1454 * @param Title|null $title
1455 * @param array $overrides associative array with fields of $row to override. This may be
1456 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
1457 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
1458 * override ar_parent_id.
1459 *
1460 * @return RevisionRecord
1461 * @throws MWException
1462 */
1463 public function newRevisionFromArchiveRow(
1464 $row,
1465 $queryFlags = 0,
1466 Title $title = null,
1467 array $overrides = []
1468 ) {
1469 Assert::parameterType( 'object', $row, '$row' );
1470
1471 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
1472 Assert::parameterType( 'integer', $queryFlags, '$queryFlags' );
1473
1474 if ( !$title && isset( $overrides['title'] ) ) {
1475 if ( !( $overrides['title'] instanceof Title ) ) {
1476 throw new MWException( 'title field override must contain a Title object.' );
1477 }
1478
1479 $title = $overrides['title'];
1480 }
1481
1482 if ( !isset( $title ) ) {
1483 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
1484 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
1485 } else {
1486 throw new InvalidArgumentException(
1487 'A Title or ar_namespace and ar_title must be given'
1488 );
1489 }
1490 }
1491
1492 foreach ( $overrides as $key => $value ) {
1493 $field = "ar_$key";
1494 $row->$field = $value;
1495 }
1496
1497 try {
1498 $user = User::newFromAnyId(
1499 $row->ar_user ?? null,
1500 $row->ar_user_text ?? null,
1501 $row->ar_actor ?? null
1502 );
1503 } catch ( InvalidArgumentException $ex ) {
1504 wfWarn( __METHOD__ . ': ' . $ex->getMessage() );
1505 $user = new UserIdentityValue( 0, '', 0 );
1506 }
1507
1508 $comment = $this->commentStore
1509 // Legacy because $row may have come from self::selectFields()
1510 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), 'ar_comment', $row, true );
1511
1512 $slots = $this->newRevisionSlots( $row->ar_rev_id, $row, $queryFlags, $title );
1513
1514 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId );
1515 }
1516
1517 /**
1518 * @see RevisionFactory::newRevisionFromRow
1519 *
1520 * MCR migration note: this replaces Revision::newFromRow
1521 *
1522 * @param object $row
1523 * @param int $queryFlags
1524 * @param Title|null $title
1525 *
1526 * @return RevisionRecord
1527 */
1528 public function newRevisionFromRow( $row, $queryFlags = 0, Title $title = null ) {
1529 Assert::parameterType( 'object', $row, '$row' );
1530
1531 if ( !$title ) {
1532 $pageId = $row->rev_page ?? 0; // XXX: also check page_id?
1533 $revId = $row->rev_id ?? 0;
1534
1535 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1536 }
1537
1538 if ( !isset( $row->page_latest ) ) {
1539 $row->page_latest = $title->getLatestRevID();
1540 if ( $row->page_latest === 0 && $title->exists() ) {
1541 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1542 }
1543 }
1544
1545 try {
1546 $user = User::newFromAnyId(
1547 $row->rev_user ?? null,
1548 $row->rev_user_text ?? null,
1549 $row->rev_actor ?? null
1550 );
1551 } catch ( InvalidArgumentException $ex ) {
1552 wfWarn( __METHOD__ . ': ' . $ex->getMessage() );
1553 $user = new UserIdentityValue( 0, '', 0 );
1554 }
1555
1556 $comment = $this->commentStore
1557 // Legacy because $row may have come from self::selectFields()
1558 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), 'rev_comment', $row, true );
1559
1560 $slots = $this->newRevisionSlots( $row->rev_id, $row, $queryFlags, $title );
1561
1562 return new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $this->wikiId );
1563 }
1564
1565 /**
1566 * Constructs a new MutableRevisionRecord based on the given associative array following
1567 * the MW1.29 convention for the Revision constructor.
1568 *
1569 * MCR migration note: this replaces Revision::newFromRow
1570 *
1571 * @param array $fields
1572 * @param int $queryFlags
1573 * @param Title|null $title
1574 *
1575 * @return MutableRevisionRecord
1576 * @throws MWException
1577 * @throws RevisionAccessException
1578 */
1579 public function newMutableRevisionFromArray(
1580 array $fields,
1581 $queryFlags = 0,
1582 Title $title = null
1583 ) {
1584 if ( !$title && isset( $fields['title'] ) ) {
1585 if ( !( $fields['title'] instanceof Title ) ) {
1586 throw new MWException( 'title field must contain a Title object.' );
1587 }
1588
1589 $title = $fields['title'];
1590 }
1591
1592 if ( !$title ) {
1593 $pageId = $fields['page'] ?? 0;
1594 $revId = $fields['id'] ?? 0;
1595
1596 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1597 }
1598
1599 if ( !isset( $fields['page'] ) ) {
1600 $fields['page'] = $title->getArticleID( $queryFlags );
1601 }
1602
1603 // if we have a content object, use it to set the model and type
1604 if ( !empty( $fields['content'] ) ) {
1605 if ( !( $fields['content'] instanceof Content ) && !is_array( $fields['content'] ) ) {
1606 throw new MWException(
1607 'content field must contain a Content object or an array of Content objects.'
1608 );
1609 }
1610 }
1611
1612 if ( !empty( $fields['text_id'] ) ) {
1613 if ( $this->mcrMigrationStage >= MIGRATION_NEW ) {
1614 throw new MWException( "Cannot use text_id field with MCR schema" );
1615 }
1616
1617 if ( !empty( $fields['content'] ) ) {
1618 throw new MWException(
1619 "Text already stored in external store (id {$fields['text_id']}), " .
1620 "can't specify content object"
1621 );
1622 }
1623 }
1624
1625 if (
1626 isset( $fields['comment'] )
1627 && !( $fields['comment'] instanceof CommentStoreComment )
1628 ) {
1629 $commentData = $fields['comment_data'] ?? null;
1630
1631 if ( $fields['comment'] instanceof Message ) {
1632 $fields['comment'] = CommentStoreComment::newUnsavedComment(
1633 $fields['comment'],
1634 $commentData
1635 );
1636 } else {
1637 $commentText = trim( strval( $fields['comment'] ) );
1638 $fields['comment'] = CommentStoreComment::newUnsavedComment(
1639 $commentText,
1640 $commentData
1641 );
1642 }
1643 }
1644
1645 $revision = new MutableRevisionRecord( $title, $this->wikiId );
1646 $this->initializeMutableRevisionFromArray( $revision, $fields );
1647
1648 if ( isset( $fields['content'] ) && is_array( $fields['content'] ) ) {
1649 foreach ( $fields['content'] as $role => $content ) {
1650 $revision->setContent( $role, $content );
1651 }
1652 } else {
1653 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1654 $revision->setSlot( $mainSlot );
1655 }
1656
1657 return $revision;
1658 }
1659
1660 /**
1661 * @param MutableRevisionRecord $record
1662 * @param array $fields
1663 */
1664 private function initializeMutableRevisionFromArray(
1665 MutableRevisionRecord $record,
1666 array $fields
1667 ) {
1668 /** @var UserIdentity $user */
1669 $user = null;
1670
1671 if ( isset( $fields['user'] ) && ( $fields['user'] instanceof UserIdentity ) ) {
1672 $user = $fields['user'];
1673 } else {
1674 try {
1675 $user = User::newFromAnyId(
1676 $fields['user'] ?? null,
1677 $fields['user_text'] ?? null,
1678 $fields['actor'] ?? null
1679 );
1680 } catch ( InvalidArgumentException $ex ) {
1681 $user = null;
1682 }
1683 }
1684
1685 if ( $user ) {
1686 $record->setUser( $user );
1687 }
1688
1689 $timestamp = isset( $fields['timestamp'] )
1690 ? strval( $fields['timestamp'] )
1691 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1692
1693 $record->setTimestamp( $timestamp );
1694
1695 if ( isset( $fields['page'] ) ) {
1696 $record->setPageId( intval( $fields['page'] ) );
1697 }
1698
1699 if ( isset( $fields['id'] ) ) {
1700 $record->setId( intval( $fields['id'] ) );
1701 }
1702 if ( isset( $fields['parent_id'] ) ) {
1703 $record->setParentId( intval( $fields['parent_id'] ) );
1704 }
1705
1706 if ( isset( $fields['sha1'] ) ) {
1707 $record->setSha1( $fields['sha1'] );
1708 }
1709 if ( isset( $fields['size'] ) ) {
1710 $record->setSize( intval( $fields['size'] ) );
1711 }
1712
1713 if ( isset( $fields['minor_edit'] ) ) {
1714 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
1715 }
1716 if ( isset( $fields['deleted'] ) ) {
1717 $record->setVisibility( intval( $fields['deleted'] ) );
1718 }
1719
1720 if ( isset( $fields['comment'] ) ) {
1721 Assert::parameterType(
1722 CommentStoreComment::class,
1723 $fields['comment'],
1724 '$row[\'comment\']'
1725 );
1726 $record->setComment( $fields['comment'] );
1727 }
1728 }
1729
1730 /**
1731 * Load a page revision from a given revision ID number.
1732 * Returns null if no such revision can be found.
1733 *
1734 * MCR migration note: this corresponds to Revision::loadFromId
1735 *
1736 * @note direct use is deprecated!
1737 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
1738 *
1739 * @param IDatabase $db
1740 * @param int $id
1741 *
1742 * @return RevisionRecord|null
1743 */
1744 public function loadRevisionFromId( IDatabase $db, $id ) {
1745 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
1746 }
1747
1748 /**
1749 * Load either the current, or a specified, revision
1750 * that's attached to a given page. If not attached
1751 * to that page, will return null.
1752 *
1753 * MCR migration note: this replaces Revision::loadFromPageId
1754 *
1755 * @note direct use is deprecated!
1756 * @todo remove when unused!
1757 *
1758 * @param IDatabase $db
1759 * @param int $pageid
1760 * @param int $id
1761 * @return RevisionRecord|null
1762 */
1763 public function loadRevisionFromPageId( IDatabase $db, $pageid, $id = 0 ) {
1764 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
1765 if ( $id ) {
1766 $conds['rev_id'] = intval( $id );
1767 } else {
1768 $conds[] = 'rev_id=page_latest';
1769 }
1770 return $this->loadRevisionFromConds( $db, $conds );
1771 }
1772
1773 /**
1774 * Load either the current, or a specified, revision
1775 * that's attached to a given page. If not attached
1776 * to that page, will return null.
1777 *
1778 * MCR migration note: this replaces Revision::loadFromTitle
1779 *
1780 * @note direct use is deprecated!
1781 * @todo remove when unused!
1782 *
1783 * @param IDatabase $db
1784 * @param Title $title
1785 * @param int $id
1786 *
1787 * @return RevisionRecord|null
1788 */
1789 public function loadRevisionFromTitle( IDatabase $db, $title, $id = 0 ) {
1790 if ( $id ) {
1791 $matchId = intval( $id );
1792 } else {
1793 $matchId = 'page_latest';
1794 }
1795
1796 return $this->loadRevisionFromConds(
1797 $db,
1798 [
1799 "rev_id=$matchId",
1800 'page_namespace' => $title->getNamespace(),
1801 'page_title' => $title->getDBkey()
1802 ],
1803 0,
1804 $title
1805 );
1806 }
1807
1808 /**
1809 * Load the revision for the given title with the given timestamp.
1810 * WARNING: Timestamps may in some circumstances not be unique,
1811 * so this isn't the best key to use.
1812 *
1813 * MCR migration note: this replaces Revision::loadFromTimestamp
1814 *
1815 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
1816 * @todo remove when unused!
1817 *
1818 * @param IDatabase $db
1819 * @param Title $title
1820 * @param string $timestamp
1821 * @return RevisionRecord|null
1822 */
1823 public function loadRevisionFromTimestamp( IDatabase $db, $title, $timestamp ) {
1824 return $this->loadRevisionFromConds( $db,
1825 [
1826 'rev_timestamp' => $db->timestamp( $timestamp ),
1827 'page_namespace' => $title->getNamespace(),
1828 'page_title' => $title->getDBkey()
1829 ],
1830 0,
1831 $title
1832 );
1833 }
1834
1835 /**
1836 * Given a set of conditions, fetch a revision
1837 *
1838 * This method should be used if we are pretty sure the revision exists.
1839 * Unless $flags has READ_LATEST set, this method will first try to find the revision
1840 * on a replica before hitting the master database.
1841 *
1842 * MCR migration note: this corresponds to Revision::newFromConds
1843 *
1844 * @param array $conditions
1845 * @param int $flags (optional)
1846 * @param Title $title
1847 *
1848 * @return RevisionRecord|null
1849 */
1850 private function newRevisionFromConds( $conditions, $flags = 0, Title $title = null ) {
1851 $db = $this->getDBConnection( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
1852 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1853 $this->releaseDBConnection( $db );
1854
1855 $lb = $this->getDBLoadBalancer();
1856
1857 // Make sure new pending/committed revision are visibile later on
1858 // within web requests to certain avoid bugs like T93866 and T94407.
1859 if ( !$rev
1860 && !( $flags & self::READ_LATEST )
1861 && $lb->getServerCount() > 1
1862 && $lb->hasOrMadeRecentMasterChanges()
1863 ) {
1864 $flags = self::READ_LATEST;
1865 $db = $this->getDBConnection( DB_MASTER );
1866 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1867 $this->releaseDBConnection( $db );
1868 }
1869
1870 return $rev;
1871 }
1872
1873 /**
1874 * Given a set of conditions, fetch a revision from
1875 * the given database connection.
1876 *
1877 * MCR migration note: this corresponds to Revision::loadFromConds
1878 *
1879 * @param IDatabase $db
1880 * @param array $conditions
1881 * @param int $flags (optional)
1882 * @param Title $title
1883 *
1884 * @return RevisionRecord|null
1885 */
1886 private function loadRevisionFromConds(
1887 IDatabase $db,
1888 $conditions,
1889 $flags = 0,
1890 Title $title = null
1891 ) {
1892 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
1893 if ( $row ) {
1894 $rev = $this->newRevisionFromRow( $row, $flags, $title );
1895
1896 return $rev;
1897 }
1898
1899 return null;
1900 }
1901
1902 /**
1903 * Throws an exception if the given database connection does not belong to the wiki this
1904 * RevisionStore is bound to.
1905 *
1906 * @param IDatabase $db
1907 * @throws MWException
1908 */
1909 private function checkDatabaseWikiId( IDatabase $db ) {
1910 $storeWiki = $this->wikiId;
1911 $dbWiki = $db->getDomainID();
1912
1913 if ( $dbWiki === $storeWiki ) {
1914 return;
1915 }
1916
1917 // XXX: we really want the default database ID...
1918 $storeWiki = $storeWiki ?: wfWikiID();
1919 $dbWiki = $dbWiki ?: wfWikiID();
1920
1921 if ( $dbWiki === $storeWiki ) {
1922 return;
1923 }
1924
1925 // HACK: counteract encoding imposed by DatabaseDomain
1926 $storeWiki = str_replace( '?h', '-', $storeWiki );
1927 $dbWiki = str_replace( '?h', '-', $dbWiki );
1928
1929 if ( $dbWiki === $storeWiki ) {
1930 return;
1931 }
1932
1933 throw new MWException( "RevisionStore for $storeWiki "
1934 . "cannot be used with a DB connection for $dbWiki" );
1935 }
1936
1937 /**
1938 * Given a set of conditions, return a row with the
1939 * fields necessary to build RevisionRecord objects.
1940 *
1941 * MCR migration note: this corresponds to Revision::fetchFromConds
1942 *
1943 * @param IDatabase $db
1944 * @param array $conditions
1945 * @param int $flags (optional)
1946 *
1947 * @return object|false data row as a raw object
1948 */
1949 private function fetchRevisionRowFromConds( IDatabase $db, $conditions, $flags = 0 ) {
1950 $this->checkDatabaseWikiId( $db );
1951
1952 $revQuery = $this->getQueryInfo( [ 'page', 'user' ] );
1953 $options = [];
1954 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
1955 $options[] = 'FOR UPDATE';
1956 }
1957 return $db->selectRow(
1958 $revQuery['tables'],
1959 $revQuery['fields'],
1960 $conditions,
1961 __METHOD__,
1962 $options,
1963 $revQuery['joins']
1964 );
1965 }
1966
1967 /**
1968 * Finds the ID of a content row for a given revision and slot role.
1969 * This can be used to re-use content rows even while the content ID
1970 * is still missing from SlotRecords, in MIGRATION_WRITE_BOTH mode.
1971 *
1972 * @todo remove after MCR schema migration is complete.
1973 *
1974 * @param IDatabase $db
1975 * @param int $revId
1976 * @param string $role
1977 *
1978 * @return int|null
1979 */
1980 private function findSlotContentId( IDatabase $db, $revId, $role ) {
1981 if ( $this->mcrMigrationStage < MIGRATION_WRITE_BOTH ) {
1982 return null;
1983 }
1984
1985 try {
1986 $roleId = $this->slotRoleStore->getId( $role );
1987 $conditions = [
1988 'slot_revision_id' => $revId,
1989 'slot_role_id' => $roleId,
1990 ];
1991
1992 $contentId = $db->selectField( 'slots', 'slot_content_id', $conditions, __METHOD__ );
1993
1994 return $contentId ?: null;
1995 } catch ( NameTableAccessException $ex ) {
1996 // If the role is missing from the slot_roles table,
1997 // the corresponding row in slots cannot exist.
1998 return null;
1999 }
2000 }
2001
2002 /**
2003 * Return the tables, fields, and join conditions to be selected to create
2004 * a new RevisionStoreRecord object.
2005 *
2006 * MCR migration note: this replaces Revision::getQueryInfo
2007 *
2008 * If the format of fields returned changes in any way then the cache key provided by
2009 * self::getRevisionRowCacheKey should be updated.
2010 *
2011 * @since 1.31
2012 *
2013 * @param array $options Any combination of the following strings
2014 * - 'page': Join with the page table, and select fields to identify the page
2015 * - 'user': Join with the user table, and select the user name
2016 * - 'text': Join with the text table, and select fields to load page text. This
2017 * option is deprecated in MW 1.32 with MCR migration stage MIGRATION_WRITE_BOTH,
2018 * and disallowed with MIGRATION_MEW.
2019 *
2020 * @return array With three keys:
2021 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2022 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2023 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2024 */
2025 public function getQueryInfo( $options = [] ) {
2026 $ret = [
2027 'tables' => [],
2028 'fields' => [],
2029 'joins' => [],
2030 ];
2031
2032 $ret['tables'][] = 'revision';
2033 $ret['fields'] = array_merge( $ret['fields'], [
2034 'rev_id',
2035 'rev_page',
2036 'rev_timestamp',
2037 'rev_minor_edit',
2038 'rev_deleted',
2039 'rev_len',
2040 'rev_parent_id',
2041 'rev_sha1',
2042 ] );
2043
2044 $commentQuery = $this->commentStore->getJoin( 'rev_comment' );
2045 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
2046 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
2047 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
2048
2049 $actorQuery = $this->actorMigration->getJoin( 'rev_user' );
2050 $ret['tables'] = array_merge( $ret['tables'], $actorQuery['tables'] );
2051 $ret['fields'] = array_merge( $ret['fields'], $actorQuery['fields'] );
2052 $ret['joins'] = array_merge( $ret['joins'], $actorQuery['joins'] );
2053
2054 if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
2055 $ret['fields'][] = 'rev_text_id';
2056
2057 if ( $this->contentHandlerUseDB ) {
2058 $ret['fields'][] = 'rev_content_format';
2059 $ret['fields'][] = 'rev_content_model';
2060 }
2061 }
2062
2063 if ( in_array( 'page', $options, true ) ) {
2064 $ret['tables'][] = 'page';
2065 $ret['fields'] = array_merge( $ret['fields'], [
2066 'page_namespace',
2067 'page_title',
2068 'page_id',
2069 'page_latest',
2070 'page_is_redirect',
2071 'page_len',
2072 ] );
2073 $ret['joins']['page'] = [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
2074 }
2075
2076 if ( in_array( 'user', $options, true ) ) {
2077 $ret['tables'][] = 'user';
2078 $ret['fields'] = array_merge( $ret['fields'], [
2079 'user_name',
2080 ] );
2081 $u = $actorQuery['fields']['rev_user'];
2082 $ret['joins']['user'] = [ 'LEFT JOIN', [ "$u != 0", "user_id = $u" ] ];
2083 }
2084
2085 if ( in_array( 'text', $options, true ) ) {
2086 if ( $this->mcrMigrationStage === MIGRATION_NEW ) {
2087 throw new InvalidArgumentException( 'text table can no longer be joined directly' );
2088 } elseif ( $this->mcrMigrationStage >= MIGRATION_WRITE_BOTH ) {
2089 wfDeprecated( __METHOD__ . ' with `text` option', '1.32' );
2090 }
2091
2092 $ret['tables'][] = 'text';
2093 $ret['fields'] = array_merge( $ret['fields'], [
2094 'old_text',
2095 'old_flags'
2096 ] );
2097 $ret['joins']['text'] = [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ];
2098 }
2099
2100 return $ret;
2101 }
2102
2103 /**
2104 * Return the tables, fields, and join conditions to be selected to create
2105 * a new SlotRecord.
2106 *
2107 * @since 1.32
2108 *
2109 * @param array $options Any combination of the following strings
2110 * - 'content': Join with the content table, and select content meta-data fields
2111 *
2112 * @return array With three keys:
2113 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2114 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2115 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2116 */
2117 public function getSlotsQueryInfo( $options = [] ) {
2118 $ret = [
2119 'tables' => [],
2120 'fields' => [],
2121 'joins' => [],
2122 ];
2123
2124 if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
2125 $db = $this->getDBConnectionRef( DB_REPLICA );
2126 $ret['tables']['slots'] = 'revision';
2127
2128 $ret['fields']['slot_revision_id'] = 'slots.rev_id';
2129 $ret['fields']['slot_content_id'] = 'NULL';
2130 $ret['fields']['slot_origin'] = 'slots.rev_id';
2131 $ret['fields']['role_name'] = $db->addQuotes( 'main' );
2132
2133 if ( in_array( 'content', $options, true ) ) {
2134 $ret['fields']['content_size'] = 'slots.rev_len';
2135 $ret['fields']['content_sha1'] = 'slots.rev_sha1';
2136 $ret['fields']['content_address']
2137 = $db->buildConcat( [ $db->addQuotes( 'tt:' ), 'slots.rev_text_id' ] );
2138
2139 if ( $this->contentHandlerUseDB ) {
2140 $ret['fields']['model_name'] = 'slots.rev_content_model';
2141 } else {
2142 $ret['fields']['model_name'] = 'NULL';
2143 }
2144 }
2145
2146 // XXX: in MIGRATION_WRITE_BOTH mode, emulate *and* select - using a UNION?
2147 // See Anomie's idea at <https://gerrit.wikimedia.org/r/c/416465/
2148 // 8..10/includes/Storage/RevisionStore.php#2113>
2149 } else {
2150 $ret['tables'][] = 'slots';
2151 $ret['tables'][] = 'slot_roles';
2152 $ret['fields'] = array_merge( $ret['fields'], [
2153 'slot_revision_id',
2154 'slot_content_id',
2155 'slot_origin',
2156 'role_name'
2157 ] );
2158 $ret['joins']['slot_roles'] = [ 'INNER JOIN', [ 'slot_role_id = role_id' ] ];
2159
2160 if ( in_array( 'content', $options, true ) ) {
2161 $ret['tables'][] = 'content';
2162 $ret['tables'][] = 'content_models';
2163 $ret['fields'] = array_merge( $ret['fields'], [
2164 'content_size',
2165 'content_sha1',
2166 'content_address',
2167 'model_name'
2168 ] );
2169 $ret['joins']['content'] = [ 'INNER JOIN', [ 'slot_content_id = content_id' ] ];
2170 $ret['joins']['content_models'] = [ 'INNER JOIN', [ 'content_model = model_id' ] ];
2171 }
2172 }
2173
2174 return $ret;
2175 }
2176
2177 /**
2178 * Return the tables, fields, and join conditions to be selected to create
2179 * a new RevisionArchiveRecord object.
2180 *
2181 * MCR migration note: this replaces Revision::getArchiveQueryInfo
2182 *
2183 * @since 1.31
2184 *
2185 * @return array With three keys:
2186 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
2187 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
2188 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
2189 */
2190 public function getArchiveQueryInfo() {
2191 $commentQuery = $this->commentStore->getJoin( 'ar_comment' );
2192 $actorQuery = $this->actorMigration->getJoin( 'ar_user' );
2193 $ret = [
2194 'tables' => [ 'archive' ] + $commentQuery['tables'] + $actorQuery['tables'],
2195 'fields' => [
2196 'ar_id',
2197 'ar_page_id',
2198 'ar_namespace',
2199 'ar_title',
2200 'ar_rev_id',
2201 'ar_timestamp',
2202 'ar_minor_edit',
2203 'ar_deleted',
2204 'ar_len',
2205 'ar_parent_id',
2206 'ar_sha1',
2207 ] + $commentQuery['fields'] + $actorQuery['fields'],
2208 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
2209 ];
2210
2211 if ( $this->mcrMigrationStage < MIGRATION_NEW ) {
2212 $ret['fields'][] = 'ar_text_id';
2213
2214 if ( $this->contentHandlerUseDB ) {
2215 $ret['fields'][] = 'ar_content_format';
2216 $ret['fields'][] = 'ar_content_model';
2217 }
2218 }
2219
2220 return $ret;
2221 }
2222
2223 /**
2224 * Do a batched query for the sizes of a set of revisions.
2225 *
2226 * MCR migration note: this replaces Revision::getParentLengths
2227 *
2228 * @param int[] $revIds
2229 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2230 * of the corresponding revision.
2231 */
2232 public function getRevisionSizes( array $revIds ) {
2233 return $this->listRevisionSizes( $this->getDBConnection( DB_REPLICA ), $revIds );
2234 }
2235
2236 /**
2237 * Do a batched query for the sizes of a set of revisions.
2238 *
2239 * MCR migration note: this replaces Revision::getParentLengths
2240 *
2241 * @deprecated use RevisionStore::getRevisionSizes instead.
2242 *
2243 * @param IDatabase $db
2244 * @param int[] $revIds
2245 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
2246 * of the corresponding revision.
2247 */
2248 public function listRevisionSizes( IDatabase $db, array $revIds ) {
2249 $this->checkDatabaseWikiId( $db );
2250
2251 $revLens = [];
2252 if ( !$revIds ) {
2253 return $revLens; // empty
2254 }
2255
2256 $res = $db->select(
2257 'revision',
2258 [ 'rev_id', 'rev_len' ],
2259 [ 'rev_id' => $revIds ],
2260 __METHOD__
2261 );
2262
2263 foreach ( $res as $row ) {
2264 $revLens[$row->rev_id] = intval( $row->rev_len );
2265 }
2266
2267 return $revLens;
2268 }
2269
2270 /**
2271 * Get previous revision for this title
2272 *
2273 * MCR migration note: this replaces Revision::getPrevious
2274 *
2275 * @param RevisionRecord $rev
2276 * @param Title $title if known (optional)
2277 *
2278 * @return RevisionRecord|null
2279 */
2280 public function getPreviousRevision( RevisionRecord $rev, Title $title = null ) {
2281 if ( $title === null ) {
2282 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
2283 }
2284 $prev = $title->getPreviousRevisionID( $rev->getId() );
2285 if ( $prev ) {
2286 return $this->getRevisionByTitle( $title, $prev );
2287 }
2288 return null;
2289 }
2290
2291 /**
2292 * Get next revision for this title
2293 *
2294 * MCR migration note: this replaces Revision::getNext
2295 *
2296 * @param RevisionRecord $rev
2297 * @param Title $title if known (optional)
2298 *
2299 * @return RevisionRecord|null
2300 */
2301 public function getNextRevision( RevisionRecord $rev, Title $title = null ) {
2302 if ( $title === null ) {
2303 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
2304 }
2305 $next = $title->getNextRevisionID( $rev->getId() );
2306 if ( $next ) {
2307 return $this->getRevisionByTitle( $title, $next );
2308 }
2309 return null;
2310 }
2311
2312 /**
2313 * Get previous revision Id for this page_id
2314 * This is used to populate rev_parent_id on save
2315 *
2316 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
2317 *
2318 * @param IDatabase $db
2319 * @param RevisionRecord $rev
2320 *
2321 * @return int
2322 */
2323 private function getPreviousRevisionId( IDatabase $db, RevisionRecord $rev ) {
2324 $this->checkDatabaseWikiId( $db );
2325
2326 if ( $rev->getPageId() === null ) {
2327 return 0;
2328 }
2329 # Use page_latest if ID is not given
2330 if ( !$rev->getId() ) {
2331 $prevId = $db->selectField(
2332 'page', 'page_latest',
2333 [ 'page_id' => $rev->getPageId() ],
2334 __METHOD__
2335 );
2336 } else {
2337 $prevId = $db->selectField(
2338 'revision', 'rev_id',
2339 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
2340 __METHOD__,
2341 [ 'ORDER BY' => 'rev_id DESC' ]
2342 );
2343 }
2344 return intval( $prevId );
2345 }
2346
2347 /**
2348 * Get rev_timestamp from rev_id, without loading the rest of the row
2349 *
2350 * MCR migration note: this replaces Revision::getTimestampFromId
2351 *
2352 * @param Title $title
2353 * @param int $id
2354 * @param int $flags
2355 * @return string|bool False if not found
2356 */
2357 public function getTimestampFromId( $title, $id, $flags = 0 ) {
2358 $db = $this->getDBConnection(
2359 ( $flags & IDBAccessObject::READ_LATEST ) ? DB_MASTER : DB_REPLICA
2360 );
2361
2362 $conds = [ 'rev_id' => $id ];
2363 $conds['rev_page'] = $title->getArticleID();
2364 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
2365
2366 $this->releaseDBConnection( $db );
2367 return ( $timestamp !== false ) ? wfTimestamp( TS_MW, $timestamp ) : false;
2368 }
2369
2370 /**
2371 * Get count of revisions per page...not very efficient
2372 *
2373 * MCR migration note: this replaces Revision::countByPageId
2374 *
2375 * @param IDatabase $db
2376 * @param int $id Page id
2377 * @return int
2378 */
2379 public function countRevisionsByPageId( IDatabase $db, $id ) {
2380 $this->checkDatabaseWikiId( $db );
2381
2382 $row = $db->selectRow( 'revision',
2383 [ 'revCount' => 'COUNT(*)' ],
2384 [ 'rev_page' => $id ],
2385 __METHOD__
2386 );
2387 if ( $row ) {
2388 return intval( $row->revCount );
2389 }
2390 return 0;
2391 }
2392
2393 /**
2394 * Get count of revisions per page...not very efficient
2395 *
2396 * MCR migration note: this replaces Revision::countByTitle
2397 *
2398 * @param IDatabase $db
2399 * @param Title $title
2400 * @return int
2401 */
2402 public function countRevisionsByTitle( IDatabase $db, $title ) {
2403 $id = $title->getArticleID();
2404 if ( $id ) {
2405 return $this->countRevisionsByPageId( $db, $id );
2406 }
2407 return 0;
2408 }
2409
2410 /**
2411 * Check if no edits were made by other users since
2412 * the time a user started editing the page. Limit to
2413 * 50 revisions for the sake of performance.
2414 *
2415 * MCR migration note: this replaces Revision::userWasLastToEdit
2416 *
2417 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
2418 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
2419 * has been deprecated since 1.24.
2420 *
2421 * @param IDatabase $db The Database to perform the check on.
2422 * @param int $pageId The ID of the page in question
2423 * @param int $userId The ID of the user in question
2424 * @param string $since Look at edits since this time
2425 *
2426 * @return bool True if the given user was the only one to edit since the given timestamp
2427 */
2428 public function userWasLastToEdit( IDatabase $db, $pageId, $userId, $since ) {
2429 $this->checkDatabaseWikiId( $db );
2430
2431 if ( !$userId ) {
2432 return false;
2433 }
2434
2435 $revQuery = $this->getQueryInfo();
2436 $res = $db->select(
2437 $revQuery['tables'],
2438 [
2439 'rev_user' => $revQuery['fields']['rev_user'],
2440 ],
2441 [
2442 'rev_page' => $pageId,
2443 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
2444 ],
2445 __METHOD__,
2446 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
2447 $revQuery['joins']
2448 );
2449 foreach ( $res as $row ) {
2450 if ( $row->rev_user != $userId ) {
2451 return false;
2452 }
2453 }
2454 return true;
2455 }
2456
2457 /**
2458 * Load a revision based on a known page ID and current revision ID from the DB
2459 *
2460 * This method allows for the use of caching, though accessing anything that normally
2461 * requires permission checks (aside from the text) will trigger a small DB lookup.
2462 *
2463 * MCR migration note: this replaces Revision::newKnownCurrent
2464 *
2465 * @param Title $title the associated page title
2466 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
2467 *
2468 * @return RevisionRecord|bool Returns false if missing
2469 */
2470 public function getKnownCurrentRevision( Title $title, $revId ) {
2471 $db = $this->getDBConnectionRef( DB_REPLICA );
2472
2473 $pageId = $title->getArticleID();
2474
2475 if ( !$pageId ) {
2476 return false;
2477 }
2478
2479 if ( !$revId ) {
2480 $revId = $title->getLatestRevID();
2481 }
2482
2483 if ( !$revId ) {
2484 wfWarn(
2485 'No latest revision known for page ' . $title->getPrefixedDBkey()
2486 . ' even though it exists with page ID ' . $pageId
2487 );
2488 return false;
2489 }
2490
2491 $row = $this->cache->getWithSetCallback(
2492 // Page/rev IDs passed in from DB to reflect history merges
2493 $this->getRevisionRowCacheKey( $db, $pageId, $revId ),
2494 WANObjectCache::TTL_WEEK,
2495 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
2496 $setOpts += Database::getCacheSetOptions( $db );
2497
2498 $conds = [
2499 'rev_page' => intval( $pageId ),
2500 'page_id' => intval( $pageId ),
2501 'rev_id' => intval( $revId ),
2502 ];
2503
2504 $row = $this->fetchRevisionRowFromConds( $db, $conds );
2505 return $row ?: false; // don't cache negatives
2506 }
2507 );
2508
2509 // Reflect revision deletion and user renames
2510 if ( $row ) {
2511 return $this->newRevisionFromRow( $row, 0, $title );
2512 } else {
2513 return false;
2514 }
2515 }
2516
2517 /**
2518 * Get a cache key for use with a row as selected with getQueryInfo( [ 'page', 'user' ] )
2519 * Caching rows without 'page' or 'user' could lead to issues.
2520 * If the format of the rows returned by the query provided by getQueryInfo changes the
2521 * cache key should be updated to avoid conflicts.
2522 *
2523 * @param IDatabase $db
2524 * @param int $pageId
2525 * @param int $revId
2526 * @return string
2527 */
2528 private function getRevisionRowCacheKey( IDatabase $db, $pageId, $revId ) {
2529 return $this->cache->makeGlobalKey(
2530 self::ROW_CACHE_KEY,
2531 $db->getDomainID(),
2532 $pageId,
2533 $revId
2534 );
2535 }
2536
2537 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.
2538
2539 }