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