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