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