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