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