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