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