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