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