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