Improve some parameter docs
[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 * @param RevisionRecord $rev
566 * @return int Rcid of the unpatrolled row, zero if there isn't one
567 */
568 public function isUnpatrolled( RevisionRecord $rev ) {
569 $rc = $this->getRecentChange( $rev );
570 if ( $rc && $rc->getAttribute( 'rc_patrolled' ) == 0 ) {
571 return $rc->getAttribute( 'rc_id' );
572 } else {
573 return 0;
574 }
575 }
576
577 /**
578 * Get the RC object belonging to the current revision, if there's one
579 *
580 * MCR migration note: this replaces Revision::getRecentChange
581 *
582 * @todo move this somewhere else?
583 *
584 * @param RevisionRecord $rev
585 * @param int $flags (optional) $flags include:
586 * IDBAccessObject::READ_LATEST: Select the data from the master
587 *
588 * @return null|RecentChange
589 */
590 public function getRecentChange( RevisionRecord $rev, $flags = 0 ) {
591 $dbr = $this->getDBConnection( DB_REPLICA );
592
593 list( $dbType, ) = DBAccessObjectUtils::getDBOptions( $flags );
594
595 $userIdentity = $rev->getUser( RevisionRecord::RAW );
596
597 if ( !$userIdentity ) {
598 // If the revision has no user identity, chances are it never went
599 // into the database, and doesn't have an RC entry.
600 return null;
601 }
602
603 // TODO: Select by rc_this_oldid alone - but as of Nov 2017, there is no index on that!
604 $rc = RecentChange::newFromConds(
605 [
606 'rc_user_text' => $userIdentity->getName(),
607 'rc_timestamp' => $dbr->timestamp( $rev->getTimestamp() ),
608 'rc_this_oldid' => $rev->getId()
609 ],
610 __METHOD__,
611 $dbType
612 );
613
614 $this->releaseDBConnection( $dbr );
615
616 // XXX: cache this locally? Glue it to the RevisionRecord?
617 return $rc;
618 }
619
620 /**
621 * Maps fields of the archive row to corresponding revision rows.
622 *
623 * @param object $archiveRow
624 *
625 * @return object a revision row object, corresponding to $archiveRow.
626 */
627 private static function mapArchiveFields( $archiveRow ) {
628 $fieldMap = [
629 // keep with ar prefix:
630 'ar_id' => 'ar_id',
631
632 // not the same suffix:
633 'ar_page_id' => 'rev_page',
634 'ar_rev_id' => 'rev_id',
635
636 // same suffix:
637 'ar_text_id' => 'rev_text_id',
638 'ar_timestamp' => 'rev_timestamp',
639 'ar_user_text' => 'rev_user_text',
640 'ar_user' => 'rev_user',
641 'ar_minor_edit' => 'rev_minor_edit',
642 'ar_deleted' => 'rev_deleted',
643 'ar_len' => 'rev_len',
644 'ar_parent_id' => 'rev_parent_id',
645 'ar_sha1' => 'rev_sha1',
646 'ar_comment' => 'rev_comment',
647 'ar_comment_cid' => 'rev_comment_cid',
648 'ar_comment_id' => 'rev_comment_id',
649 'ar_comment_text' => 'rev_comment_text',
650 'ar_comment_data' => 'rev_comment_data',
651 'ar_comment_old' => 'rev_comment_old',
652 'ar_content_format' => 'rev_content_format',
653 'ar_content_model' => 'rev_content_model',
654 ];
655
656 if ( empty( $archiveRow->ar_text_id ) ) {
657 $fieldMap['ar_text'] = 'old_text';
658 $fieldMap['ar_flags'] = 'old_flags';
659 }
660
661 $revRow = new stdClass();
662 foreach ( $fieldMap as $arKey => $revKey ) {
663 if ( property_exists( $archiveRow, $arKey ) ) {
664 $revRow->$revKey = $archiveRow->$arKey;
665 }
666 }
667
668 return $revRow;
669 }
670
671 /**
672 * Constructs a RevisionRecord for the revisions main slot, based on the MW1.29 schema.
673 *
674 * @param object|array $row Either a database row or an array
675 * @param int $queryFlags for callbacks
676 * @param Title $title
677 *
678 * @return SlotRecord The main slot, extracted from the MW 1.29 style row.
679 * @throws MWException
680 */
681 private function emulateMainSlot_1_29( $row, $queryFlags, Title $title ) {
682 $mainSlotRow = new stdClass();
683 $mainSlotRow->role_name = 'main';
684
685 $content = null;
686 $blobData = null;
687 $blobFlags = '';
688
689 if ( is_object( $row ) ) {
690 // archive row
691 if ( !isset( $row->rev_id ) && isset( $row->ar_user ) ) {
692 $row = $this->mapArchiveFields( $row );
693 }
694
695 if ( isset( $row->rev_text_id ) && $row->rev_text_id > 0 ) {
696 $mainSlotRow->cont_address = 'tt:' . $row->rev_text_id;
697 } elseif ( isset( $row->ar_id ) ) {
698 $mainSlotRow->cont_address = 'ar:' . $row->ar_id;
699 }
700
701 if ( isset( $row->old_text ) ) {
702 // this happens when the text-table gets joined directly, in the pre-1.30 schema
703 $blobData = isset( $row->old_text ) ? strval( $row->old_text ) : null;
704 $blobFlags = isset( $row->old_flags ) ? strval( $row->old_flags ) : '';
705 }
706
707 $mainSlotRow->slot_revision = intval( $row->rev_id );
708
709 $mainSlotRow->cont_size = isset( $row->rev_len ) ? intval( $row->rev_len ) : null;
710 $mainSlotRow->cont_sha1 = isset( $row->rev_sha1 ) ? strval( $row->rev_sha1 ) : null;
711 $mainSlotRow->model_name = isset( $row->rev_content_model )
712 ? strval( $row->rev_content_model )
713 : null;
714 // XXX: in the future, we'll probably always use the default format, and drop content_format
715 $mainSlotRow->format_name = isset( $row->rev_content_format )
716 ? strval( $row->rev_content_format )
717 : null;
718 } elseif ( is_array( $row ) ) {
719 $mainSlotRow->slot_revision = isset( $row['id'] ) ? intval( $row['id'] ) : null;
720
721 $mainSlotRow->cont_address = isset( $row['text_id'] )
722 ? 'tt:' . intval( $row['text_id'] )
723 : null;
724 $mainSlotRow->cont_size = isset( $row['len'] ) ? intval( $row['len'] ) : null;
725 $mainSlotRow->cont_sha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
726
727 $mainSlotRow->model_name = isset( $row['content_model'] )
728 ? strval( $row['content_model'] ) : null; // XXX: must be a string!
729 // XXX: in the future, we'll probably always use the default format, and drop content_format
730 $mainSlotRow->format_name = isset( $row['content_format'] )
731 ? strval( $row['content_format'] ) : null;
732 $blobData = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
733 $blobFlags = isset( $row['flags'] ) ? trim( strval( $row['flags'] ) ) : '';
734
735 // if we have a Content object, override mText and mContentModel
736 if ( !empty( $row['content'] ) ) {
737 if ( !( $row['content'] instanceof Content ) ) {
738 throw new MWException( 'content field must contain a Content object.' );
739 }
740
741 /** @var Content $content */
742 $content = $row['content'];
743 $handler = $content->getContentHandler();
744
745 $mainSlotRow->model_name = $content->getModel();
746
747 // XXX: in the future, we'll probably always use the default format.
748 if ( $mainSlotRow->format_name === null ) {
749 $mainSlotRow->format_name = $handler->getDefaultFormat();
750 }
751 }
752 } else {
753 throw new MWException( 'Revision constructor passed invalid row format.' );
754 }
755
756 // With the old schema, the content changes with every revision.
757 // ...except for null-revisions. Would be nice if we could detect them.
758 $mainSlotRow->slot_inherited = 0;
759
760 if ( $mainSlotRow->model_name === null ) {
761 $mainSlotRow->model_name = function ( SlotRecord $slot ) use ( $title ) {
762 // TODO: MCR: consider slot role in getDefaultModelFor()! Use LinkTarget!
763 // TODO: MCR: deprecate $title->getModel().
764 return ContentHandler::getDefaultModelFor( $title );
765 };
766 }
767
768 if ( !$content ) {
769 $content = function ( SlotRecord $slot )
770 use ( $blobData, $blobFlags, $queryFlags, $mainSlotRow )
771 {
772 return $this->loadSlotContent(
773 $slot,
774 $blobData,
775 $blobFlags,
776 $mainSlotRow->format_name,
777 $queryFlags
778 );
779 };
780 }
781
782 return new SlotRecord( $mainSlotRow, $content );
783 }
784
785 /**
786 * Loads a Content object based on a slot row.
787 *
788 * This method does not call $slot->getContent(), and may be used as a callback
789 * called by $slot->getContent().
790 *
791 * MCR migration note: this roughly corresponds to Revision::getContentInternal
792 *
793 * @param SlotRecord $slot The SlotRecord to load content for
794 * @param string|null $blobData The content blob, in the form indicated by $blobFlags
795 * @param string $blobFlags Flags indicating how $blobData needs to be processed
796 * @param string|null $blobFormat MIME type indicating how $dataBlob is encoded
797 * @param int $queryFlags
798 *
799 * @throw RevisionAccessException
800 * @return Content
801 */
802 private function loadSlotContent(
803 SlotRecord $slot,
804 $blobData = null,
805 $blobFlags = '',
806 $blobFormat = null,
807 $queryFlags = 0
808 ) {
809 if ( $blobData !== null ) {
810 Assert::parameterType( 'string', $blobData, '$blobData' );
811 Assert::parameterType( 'string', $blobFlags, '$blobFlags' );
812
813 $cacheKey = $slot->hasAddress() ? $slot->getAddress() : null;
814
815 $data = $this->blobStore->expandBlob( $blobData, $blobFlags, $cacheKey );
816
817 if ( $data === false ) {
818 throw new RevisionAccessException(
819 "Failed to expand blob data using flags $blobFlags (key: $cacheKey)"
820 );
821 }
822 } else {
823 $address = $slot->getAddress();
824 try {
825 $data = $this->blobStore->getBlob( $address, $queryFlags );
826 } catch ( BlobAccessException $e ) {
827 throw new RevisionAccessException(
828 "Failed to load data blob from $address: " . $e->getMessage(), 0, $e
829 );
830 }
831 }
832
833 // Unserialize content
834 $handler = ContentHandler::getForModelID( $slot->getModel() );
835
836 $content = $handler->unserializeContent( $data, $blobFormat );
837 return $content;
838 }
839
840 /**
841 * Load a page revision from a given revision ID number.
842 * Returns null if no such revision can be found.
843 *
844 * MCR migration note: this replaces Revision::newFromId
845 *
846 * $flags include:
847 * IDBAccessObject::READ_LATEST: Select the data from the master
848 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
849 *
850 * @param int $id
851 * @param int $flags (optional)
852 * @return RevisionRecord|null
853 */
854 public function getRevisionById( $id, $flags = 0 ) {
855 return $this->newRevisionFromConds( [ 'rev_id' => intval( $id ) ], $flags );
856 }
857
858 /**
859 * Load either the current, or a specified, revision
860 * that's attached to a given link target. If not attached
861 * to that link target, will return null.
862 *
863 * MCR migration note: this replaces Revision::newFromTitle
864 *
865 * $flags include:
866 * IDBAccessObject::READ_LATEST: Select the data from the master
867 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
868 *
869 * @param LinkTarget $linkTarget
870 * @param int $revId (optional)
871 * @param int $flags Bitfield (optional)
872 * @return RevisionRecord|null
873 */
874 public function getRevisionByTitle( LinkTarget $linkTarget, $revId = 0, $flags = 0 ) {
875 $conds = [
876 'page_namespace' => $linkTarget->getNamespace(),
877 'page_title' => $linkTarget->getDBkey()
878 ];
879 if ( $revId ) {
880 // Use the specified revision ID.
881 // Note that we use newRevisionFromConds here because we want to retry
882 // and fall back to master if the page is not found on a replica.
883 // Since the caller supplied a revision ID, we are pretty sure the revision is
884 // supposed to exist, so we should try hard to find it.
885 $conds['rev_id'] = $revId;
886 return $this->newRevisionFromConds( $conds, $flags );
887 } else {
888 // Use a join to get the latest revision.
889 // Note that we don't use newRevisionFromConds here because we don't want to retry
890 // and fall back to master. The assumption is that we only want to force the fallback
891 // if we are quite sure the revision exists because the caller supplied a revision ID.
892 // If the page isn't found at all on a replica, it probably simply does not exist.
893 $db = $this->getDBConnection( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
894
895 $conds[] = 'rev_id=page_latest';
896 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
897
898 $this->releaseDBConnection( $db );
899 return $rev;
900 }
901 }
902
903 /**
904 * Load either the current, or a specified, revision
905 * that's attached to a given page ID.
906 * Returns null if no such revision can be found.
907 *
908 * MCR migration note: this replaces Revision::newFromPageId
909 *
910 * $flags include:
911 * IDBAccessObject::READ_LATEST: Select the data from the master (since 1.20)
912 * IDBAccessObject::READ_LOCKING : Select & lock the data from the master
913 *
914 * @param int $pageId
915 * @param int $revId (optional)
916 * @param int $flags Bitfield (optional)
917 * @return RevisionRecord|null
918 */
919 public function getRevisionByPageId( $pageId, $revId = 0, $flags = 0 ) {
920 $conds = [ 'page_id' => $pageId ];
921 if ( $revId ) {
922 // Use the specified revision ID.
923 // Note that we use newRevisionFromConds here because we want to retry
924 // and fall back to master if the page is not found on a replica.
925 // Since the caller supplied a revision ID, we are pretty sure the revision is
926 // supposed to exist, so we should try hard to find it.
927 $conds['rev_id'] = $revId;
928 return $this->newRevisionFromConds( $conds, $flags );
929 } else {
930 // Use a join to get the latest revision.
931 // Note that we don't use newRevisionFromConds here because we don't want to retry
932 // and fall back to master. The assumption is that we only want to force the fallback
933 // if we are quite sure the revision exists because the caller supplied a revision ID.
934 // If the page isn't found at all on a replica, it probably simply does not exist.
935 $db = $this->getDBConnection( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
936
937 $conds[] = 'rev_id=page_latest';
938 $rev = $this->loadRevisionFromConds( $db, $conds, $flags );
939
940 $this->releaseDBConnection( $db );
941 return $rev;
942 }
943 }
944
945 /**
946 * Load the revision for the given title with the given timestamp.
947 * WARNING: Timestamps may in some circumstances not be unique,
948 * so this isn't the best key to use.
949 *
950 * MCR migration note: this replaces Revision::loadFromTimestamp
951 *
952 * @param Title $title
953 * @param string $timestamp
954 * @return RevisionRecord|null
955 */
956 public function getRevisionFromTimestamp( $title, $timestamp ) {
957 return $this->newRevisionFromConds(
958 [
959 'rev_timestamp' => $timestamp,
960 'page_namespace' => $title->getNamespace(),
961 'page_title' => $title->getDBkey()
962 ],
963 0,
964 $title
965 );
966 }
967
968 /**
969 * Make a fake revision object from an archive table row. This is queried
970 * for permissions or even inserted (as in Special:Undelete)
971 *
972 * MCR migration note: this replaces Revision::newFromArchiveRow
973 *
974 * @param object $row
975 * @param int $queryFlags
976 * @param Title|null $title
977 * @param array $overrides associative array with fields of $row to override. This may be
978 * used e.g. to force the parent revision ID or page ID. Keys in the array are fields
979 * names from the archive table without the 'ar_' prefix, i.e. use 'parent_id' to
980 * override ar_parent_id.
981 *
982 * @return RevisionRecord
983 * @throws MWException
984 */
985 public function newRevisionFromArchiveRow(
986 $row,
987 $queryFlags = 0,
988 Title $title = null,
989 array $overrides = []
990 ) {
991 Assert::parameterType( 'object', $row, '$row' );
992
993 // check second argument, since Revision::newFromArchiveRow had $overrides in that spot.
994 Assert::parameterType( 'integer', $queryFlags, '$queryFlags' );
995
996 if ( !$title && isset( $overrides['title'] ) ) {
997 if ( !( $overrides['title'] instanceof Title ) ) {
998 throw new MWException( 'title field override must contain a Title object.' );
999 }
1000
1001 $title = $overrides['title'];
1002 }
1003
1004 if ( !isset( $title ) ) {
1005 if ( isset( $row->ar_namespace ) && isset( $row->ar_title ) ) {
1006 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
1007 } else {
1008 throw new InvalidArgumentException(
1009 'A Title or ar_namespace and ar_title must be given'
1010 );
1011 }
1012 }
1013
1014 foreach ( $overrides as $key => $value ) {
1015 $field = "ar_$key";
1016 $row->$field = $value;
1017 }
1018
1019 $user = $this->getUserIdentityFromRowObject( $row, 'ar_' );
1020
1021 $comment = CommentStore::newKey( 'ar_comment' )
1022 // Legacy because $row may have come from self::selectFields()
1023 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), $row, true );
1024
1025 $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
1026 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1027
1028 return new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $this->wikiId );
1029 }
1030
1031 /**
1032 * @param object $row
1033 * @param string $prefix Field prefix, such as 'rev_' or 'ar_'.
1034 *
1035 * @return UserIdentityValue
1036 */
1037 private function getUserIdentityFromRowObject( $row, $prefix = 'rev_' ) {
1038 $idField = "{$prefix}user";
1039 $nameField = "{$prefix}user_text";
1040
1041 $userId = intval( $row->$idField );
1042
1043 if ( isset( $row->user_name ) ) {
1044 $userName = $row->user_name;
1045 } elseif ( isset( $row->$nameField ) ) {
1046 $userName = $row->$nameField;
1047 } else {
1048 $userName = User::whoIs( $userId );
1049 }
1050
1051 if ( $userName === false ) {
1052 wfWarn( __METHOD__ . ': Cannot determine user name for user ID ' . $userId );
1053 $userName = '';
1054 }
1055
1056 return new UserIdentityValue( $userId, $userName );
1057 }
1058
1059 /**
1060 * @see RevisionFactory::newRevisionFromRow_1_29
1061 *
1062 * MCR migration note: this replaces Revision::newFromRow
1063 *
1064 * @param object $row
1065 * @param int $queryFlags
1066 * @param Title|null $title
1067 *
1068 * @return RevisionRecord
1069 * @throws MWException
1070 * @throws RevisionAccessException
1071 */
1072 private function newRevisionFromRow_1_29( $row, $queryFlags = 0, Title $title = null ) {
1073 Assert::parameterType( 'object', $row, '$row' );
1074
1075 if ( !$title ) {
1076 $pageId = isset( $row->rev_page ) ? $row->rev_page : 0; // XXX: also check page_id?
1077 $revId = isset( $row->rev_id ) ? $row->rev_id : 0;
1078
1079 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1080 }
1081
1082 if ( !isset( $row->page_latest ) ) {
1083 $row->page_latest = $title->getLatestRevID();
1084 if ( $row->page_latest === 0 && $title->exists() ) {
1085 wfWarn( 'Encountered title object in limbo: ID ' . $title->getArticleID() );
1086 }
1087 }
1088
1089 $user = $this->getUserIdentityFromRowObject( $row );
1090
1091 $comment = CommentStore::newKey( 'rev_comment' )
1092 // Legacy because $row may have come from self::selectFields()
1093 ->getCommentLegacy( $this->getDBConnection( DB_REPLICA ), $row, true );
1094
1095 $mainSlot = $this->emulateMainSlot_1_29( $row, $queryFlags, $title );
1096 $slots = new RevisionSlots( [ 'main' => $mainSlot ] );
1097
1098 return new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $this->wikiId );
1099 }
1100
1101 /**
1102 * @see RevisionFactory::newRevisionFromRow
1103 *
1104 * MCR migration note: this replaces Revision::newFromRow
1105 *
1106 * @param object $row
1107 * @param int $queryFlags
1108 * @param Title|null $title
1109 *
1110 * @return RevisionRecord
1111 */
1112 public function newRevisionFromRow( $row, $queryFlags = 0, Title $title = null ) {
1113 return $this->newRevisionFromRow_1_29( $row, $queryFlags, $title );
1114 }
1115
1116 /**
1117 * Constructs a new MutableRevisionRecord based on the given associative array following
1118 * the MW1.29 convention for the Revision constructor.
1119 *
1120 * MCR migration note: this replaces Revision::newFromRow
1121 *
1122 * @param array $fields
1123 * @param int $queryFlags
1124 * @param Title|null $title
1125 *
1126 * @return MutableRevisionRecord
1127 * @throws MWException
1128 * @throws RevisionAccessException
1129 */
1130 public function newMutableRevisionFromArray(
1131 array $fields,
1132 $queryFlags = 0,
1133 Title $title = null
1134 ) {
1135 if ( !$title && isset( $fields['title'] ) ) {
1136 if ( !( $fields['title'] instanceof Title ) ) {
1137 throw new MWException( 'title field must contain a Title object.' );
1138 }
1139
1140 $title = $fields['title'];
1141 }
1142
1143 if ( !$title ) {
1144 $pageId = isset( $fields['page'] ) ? $fields['page'] : 0;
1145 $revId = isset( $fields['id'] ) ? $fields['id'] : 0;
1146
1147 $title = $this->getTitle( $pageId, $revId, $queryFlags );
1148 }
1149
1150 if ( !isset( $fields['page'] ) ) {
1151 $fields['page'] = $title->getArticleID( $queryFlags );
1152 }
1153
1154 // if we have a content object, use it to set the model and type
1155 if ( !empty( $fields['content'] ) ) {
1156 if ( !( $fields['content'] instanceof Content ) ) {
1157 throw new MWException( 'content field must contain a Content object.' );
1158 }
1159
1160 if ( !empty( $fields['text_id'] ) ) {
1161 throw new MWException(
1162 "Text already stored in external store (id {$fields['text_id']}), " .
1163 "can't serialize content object"
1164 );
1165 }
1166 }
1167
1168 // Replaces old lazy loading logic in Revision::getUserText.
1169 if ( !isset( $fields['user_text'] ) && isset( $fields['user'] ) ) {
1170 if ( $fields['user'] instanceof UserIdentity ) {
1171 /** @var User $user */
1172 $user = $fields['user'];
1173 $fields['user_text'] = $user->getName();
1174 $fields['user'] = $user->getId();
1175 } else {
1176 // TODO: wrap this in a callback to make it lazy again.
1177 $name = $fields['user'] === 0 ? false : User::whoIs( $fields['user'] );
1178
1179 if ( $name === false ) {
1180 throw new MWException(
1181 'user_text not given, and unknown user ID ' . $fields['user']
1182 );
1183 }
1184
1185 $fields['user_text'] = $name;
1186 }
1187 }
1188
1189 if (
1190 isset( $fields['comment'] )
1191 && !( $fields['comment'] instanceof CommentStoreComment )
1192 ) {
1193 $commentData = isset( $fields['comment_data'] ) ? $fields['comment_data'] : null;
1194
1195 if ( $fields['comment'] instanceof Message ) {
1196 $fields['comment'] = CommentStoreComment::newUnsavedComment(
1197 $fields['comment'],
1198 $commentData
1199 );
1200 } else {
1201 $commentText = trim( strval( $fields['comment'] ) );
1202 $fields['comment'] = CommentStoreComment::newUnsavedComment(
1203 $commentText,
1204 $commentData
1205 );
1206 }
1207 }
1208
1209 $mainSlot = $this->emulateMainSlot_1_29( $fields, $queryFlags, $title );
1210
1211 $revision = new MutableRevisionRecord( $title, $this->wikiId );
1212 $this->initializeMutableRevisionFromArray( $revision, $fields );
1213 $revision->setSlot( $mainSlot );
1214
1215 return $revision;
1216 }
1217
1218 /**
1219 * @param MutableRevisionRecord $record
1220 * @param array $fields
1221 */
1222 private function initializeMutableRevisionFromArray(
1223 MutableRevisionRecord $record,
1224 array $fields
1225 ) {
1226 /** @var UserIdentity $user */
1227 $user = null;
1228
1229 if ( isset( $fields['user'] ) && ( $fields['user'] instanceof UserIdentity ) ) {
1230 $user = $fields['user'];
1231 } elseif ( isset( $fields['user'] ) && isset( $fields['user_text'] ) ) {
1232 $user = new UserIdentityValue( intval( $fields['user'] ), $fields['user_text'] );
1233 } elseif ( isset( $fields['user'] ) ) {
1234 $user = User::newFromId( intval( $fields['user'] ) );
1235 } elseif ( isset( $fields['user_text'] ) ) {
1236 $user = User::newFromName( $fields['user_text'] );
1237
1238 // User::newFromName will return false for IP addresses (and invalid names)
1239 if ( $user == false ) {
1240 $user = new UserIdentityValue( 0, $fields['user_text'] );
1241 }
1242 }
1243
1244 if ( $user ) {
1245 $record->setUser( $user );
1246 }
1247
1248 $timestamp = isset( $fields['timestamp'] )
1249 ? strval( $fields['timestamp'] )
1250 : wfTimestampNow(); // TODO: use a callback, so we can override it for testing.
1251
1252 $record->setTimestamp( $timestamp );
1253
1254 if ( isset( $fields['page'] ) ) {
1255 $record->setPageId( intval( $fields['page'] ) );
1256 }
1257
1258 if ( isset( $fields['id'] ) ) {
1259 $record->setId( intval( $fields['id'] ) );
1260 }
1261 if ( isset( $fields['parent_id'] ) ) {
1262 $record->setParentId( intval( $fields['parent_id'] ) );
1263 }
1264
1265 if ( isset( $fields['sha1'] ) ) {
1266 $record->setSha1( $fields['sha1'] );
1267 }
1268 if ( isset( $fields['size'] ) ) {
1269 $record->setSize( intval( $fields['size'] ) );
1270 }
1271
1272 if ( isset( $fields['minor_edit'] ) ) {
1273 $record->setMinorEdit( intval( $fields['minor_edit'] ) !== 0 );
1274 }
1275 if ( isset( $fields['deleted'] ) ) {
1276 $record->setVisibility( intval( $fields['deleted'] ) );
1277 }
1278
1279 if ( isset( $fields['comment'] ) ) {
1280 Assert::parameterType(
1281 CommentStoreComment::class,
1282 $fields['comment'],
1283 '$row[\'comment\']'
1284 );
1285 $record->setComment( $fields['comment'] );
1286 }
1287 }
1288
1289 /**
1290 * Load a page revision from a given revision ID number.
1291 * Returns null if no such revision can be found.
1292 *
1293 * MCR migration note: this corresponds to Revision::loadFromId
1294 *
1295 * @note direct use is deprecated!
1296 * @todo remove when unused! there seem to be no callers of Revision::loadFromId
1297 *
1298 * @param IDatabase $db
1299 * @param int $id
1300 *
1301 * @return RevisionRecord|null
1302 */
1303 public function loadRevisionFromId( IDatabase $db, $id ) {
1304 return $this->loadRevisionFromConds( $db, [ 'rev_id' => intval( $id ) ] );
1305 }
1306
1307 /**
1308 * Load either the current, or a specified, revision
1309 * that's attached to a given page. If not attached
1310 * to that page, will return null.
1311 *
1312 * MCR migration note: this replaces Revision::loadFromPageId
1313 *
1314 * @note direct use is deprecated!
1315 * @todo remove when unused!
1316 *
1317 * @param IDatabase $db
1318 * @param int $pageid
1319 * @param int $id
1320 * @return RevisionRecord|null
1321 */
1322 public function loadRevisionFromPageId( IDatabase $db, $pageid, $id = 0 ) {
1323 $conds = [ 'rev_page' => intval( $pageid ), 'page_id' => intval( $pageid ) ];
1324 if ( $id ) {
1325 $conds['rev_id'] = intval( $id );
1326 } else {
1327 $conds[] = 'rev_id=page_latest';
1328 }
1329 return $this->loadRevisionFromConds( $db, $conds );
1330 }
1331
1332 /**
1333 * Load either the current, or a specified, revision
1334 * that's attached to a given page. If not attached
1335 * to that page, will return null.
1336 *
1337 * MCR migration note: this replaces Revision::loadFromTitle
1338 *
1339 * @note direct use is deprecated!
1340 * @todo remove when unused!
1341 *
1342 * @param IDatabase $db
1343 * @param Title $title
1344 * @param int $id
1345 *
1346 * @return RevisionRecord|null
1347 */
1348 public function loadRevisionFromTitle( IDatabase $db, $title, $id = 0 ) {
1349 if ( $id ) {
1350 $matchId = intval( $id );
1351 } else {
1352 $matchId = 'page_latest';
1353 }
1354
1355 return $this->loadRevisionFromConds(
1356 $db,
1357 [
1358 "rev_id=$matchId",
1359 'page_namespace' => $title->getNamespace(),
1360 'page_title' => $title->getDBkey()
1361 ],
1362 0,
1363 $title
1364 );
1365 }
1366
1367 /**
1368 * Load the revision for the given title with the given timestamp.
1369 * WARNING: Timestamps may in some circumstances not be unique,
1370 * so this isn't the best key to use.
1371 *
1372 * MCR migration note: this replaces Revision::loadFromTimestamp
1373 *
1374 * @note direct use is deprecated! Use getRevisionFromTimestamp instead!
1375 * @todo remove when unused!
1376 *
1377 * @param IDatabase $db
1378 * @param Title $title
1379 * @param string $timestamp
1380 * @return RevisionRecord|null
1381 */
1382 public function loadRevisionFromTimestamp( IDatabase $db, $title, $timestamp ) {
1383 return $this->loadRevisionFromConds( $db,
1384 [
1385 'rev_timestamp' => $db->timestamp( $timestamp ),
1386 'page_namespace' => $title->getNamespace(),
1387 'page_title' => $title->getDBkey()
1388 ],
1389 0,
1390 $title
1391 );
1392 }
1393
1394 /**
1395 * Given a set of conditions, fetch a revision
1396 *
1397 * This method should be used if we are pretty sure the revision exists.
1398 * Unless $flags has READ_LATEST set, this method will first try to find the revision
1399 * on a replica before hitting the master database.
1400 *
1401 * MCR migration note: this corresponds to Revision::newFromConds
1402 *
1403 * @param array $conditions
1404 * @param int $flags (optional)
1405 * @param Title $title
1406 *
1407 * @return RevisionRecord|null
1408 */
1409 private function newRevisionFromConds( $conditions, $flags = 0, Title $title = null ) {
1410 $db = $this->getDBConnection( ( $flags & self::READ_LATEST ) ? DB_MASTER : DB_REPLICA );
1411 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1412 $this->releaseDBConnection( $db );
1413
1414 $lb = $this->getDBLoadBalancer();
1415
1416 // Make sure new pending/committed revision are visibile later on
1417 // within web requests to certain avoid bugs like T93866 and T94407.
1418 if ( !$rev
1419 && !( $flags & self::READ_LATEST )
1420 && $lb->getServerCount() > 1
1421 && $lb->hasOrMadeRecentMasterChanges()
1422 ) {
1423 $flags = self::READ_LATEST;
1424 $db = $this->getDBConnection( DB_MASTER );
1425 $rev = $this->loadRevisionFromConds( $db, $conditions, $flags, $title );
1426 $this->releaseDBConnection( $db );
1427 }
1428
1429 return $rev;
1430 }
1431
1432 /**
1433 * Given a set of conditions, fetch a revision from
1434 * the given database connection.
1435 *
1436 * MCR migration note: this corresponds to Revision::loadFromConds
1437 *
1438 * @param IDatabase $db
1439 * @param array $conditions
1440 * @param int $flags (optional)
1441 * @param Title $title
1442 *
1443 * @return RevisionRecord|null
1444 */
1445 private function loadRevisionFromConds(
1446 IDatabase $db,
1447 $conditions,
1448 $flags = 0,
1449 Title $title = null
1450 ) {
1451 $row = $this->fetchRevisionRowFromConds( $db, $conditions, $flags );
1452 if ( $row ) {
1453 $rev = $this->newRevisionFromRow( $row, $flags, $title );
1454
1455 return $rev;
1456 }
1457
1458 return null;
1459 }
1460
1461 /**
1462 * Throws an exception if the given database connection does not belong to the wiki this
1463 * RevisionStore is bound to.
1464 *
1465 * @param IDatabase $db
1466 * @throws MWException
1467 */
1468 private function checkDatabaseWikiId( IDatabase $db ) {
1469 $storeWiki = $this->wikiId;
1470 $dbWiki = $db->getDomainID();
1471
1472 if ( $dbWiki === $storeWiki ) {
1473 return;
1474 }
1475
1476 // XXX: we really want the default database ID...
1477 $storeWiki = $storeWiki ?: wfWikiID();
1478 $dbWiki = $dbWiki ?: wfWikiID();
1479
1480 if ( $dbWiki === $storeWiki ) {
1481 return;
1482 }
1483
1484 // HACK: counteract encoding imposed by DatabaseDomain
1485 $storeWiki = str_replace( '?h', '-', $storeWiki );
1486 $dbWiki = str_replace( '?h', '-', $dbWiki );
1487
1488 if ( $dbWiki === $storeWiki ) {
1489 return;
1490 }
1491
1492 throw new MWException( "RevisionStore for $storeWiki "
1493 . "cannot be used with a DB connection for $dbWiki" );
1494 }
1495
1496 /**
1497 * Given a set of conditions, return a row with the
1498 * fields necessary to build RevisionRecord objects.
1499 *
1500 * MCR migration note: this corresponds to Revision::fetchFromConds
1501 *
1502 * @param IDatabase $db
1503 * @param array $conditions
1504 * @param int $flags (optional)
1505 *
1506 * @return object|false data row as a raw object
1507 */
1508 private function fetchRevisionRowFromConds( IDatabase $db, $conditions, $flags = 0 ) {
1509 $this->checkDatabaseWikiId( $db );
1510
1511 $revQuery = self::getQueryInfo( [ 'page', 'user' ] );
1512 $options = [];
1513 if ( ( $flags & self::READ_LOCKING ) == self::READ_LOCKING ) {
1514 $options[] = 'FOR UPDATE';
1515 }
1516 return $db->selectRow(
1517 $revQuery['tables'],
1518 $revQuery['fields'],
1519 $conditions,
1520 __METHOD__,
1521 $options,
1522 $revQuery['joins']
1523 );
1524 }
1525
1526 /**
1527 * Return the tables, fields, and join conditions to be selected to create
1528 * a new revision object.
1529 *
1530 * MCR migration note: this replaces Revision::getQueryInfo
1531 *
1532 * @since 1.31
1533 *
1534 * @param array $options Any combination of the following strings
1535 * - 'page': Join with the page table, and select fields to identify the page
1536 * - 'user': Join with the user table, and select the user name
1537 * - 'text': Join with the text table, and select fields to load page text
1538 *
1539 * @return array With three keys:
1540 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
1541 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
1542 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
1543 */
1544 public function getQueryInfo( $options = [] ) {
1545 $ret = [
1546 'tables' => [],
1547 'fields' => [],
1548 'joins' => [],
1549 ];
1550
1551 $ret['tables'][] = 'revision';
1552 $ret['fields'] = array_merge( $ret['fields'], [
1553 'rev_id',
1554 'rev_page',
1555 'rev_text_id',
1556 'rev_timestamp',
1557 'rev_user_text',
1558 'rev_user',
1559 'rev_minor_edit',
1560 'rev_deleted',
1561 'rev_len',
1562 'rev_parent_id',
1563 'rev_sha1',
1564 ] );
1565
1566 $commentQuery = CommentStore::newKey( 'rev_comment' )->getJoin();
1567 $ret['tables'] = array_merge( $ret['tables'], $commentQuery['tables'] );
1568 $ret['fields'] = array_merge( $ret['fields'], $commentQuery['fields'] );
1569 $ret['joins'] = array_merge( $ret['joins'], $commentQuery['joins'] );
1570
1571 if ( $this->contentHandlerUseDB ) {
1572 $ret['fields'][] = 'rev_content_format';
1573 $ret['fields'][] = 'rev_content_model';
1574 }
1575
1576 if ( in_array( 'page', $options, true ) ) {
1577 $ret['tables'][] = 'page';
1578 $ret['fields'] = array_merge( $ret['fields'], [
1579 'page_namespace',
1580 'page_title',
1581 'page_id',
1582 'page_latest',
1583 'page_is_redirect',
1584 'page_len',
1585 ] );
1586 $ret['joins']['page'] = [ 'INNER JOIN', [ 'page_id = rev_page' ] ];
1587 }
1588
1589 if ( in_array( 'user', $options, true ) ) {
1590 $ret['tables'][] = 'user';
1591 $ret['fields'] = array_merge( $ret['fields'], [
1592 'user_name',
1593 ] );
1594 $ret['joins']['user'] = [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ];
1595 }
1596
1597 if ( in_array( 'text', $options, true ) ) {
1598 $ret['tables'][] = 'text';
1599 $ret['fields'] = array_merge( $ret['fields'], [
1600 'old_text',
1601 'old_flags'
1602 ] );
1603 $ret['joins']['text'] = [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ];
1604 }
1605
1606 return $ret;
1607 }
1608
1609 /**
1610 * Return the tables, fields, and join conditions to be selected to create
1611 * a new archived revision object.
1612 *
1613 * MCR migration note: this replaces Revision::getArchiveQueryInfo
1614 *
1615 * @since 1.31
1616 *
1617 * @return array With three keys:
1618 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
1619 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
1620 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
1621 */
1622 public function getArchiveQueryInfo() {
1623 $commentQuery = CommentStore::newKey( 'ar_comment' )->getJoin();
1624 $ret = [
1625 'tables' => [ 'archive' ] + $commentQuery['tables'],
1626 'fields' => [
1627 'ar_id',
1628 'ar_page_id',
1629 'ar_namespace',
1630 'ar_title',
1631 'ar_rev_id',
1632 'ar_text',
1633 'ar_text_id',
1634 'ar_timestamp',
1635 'ar_user_text',
1636 'ar_user',
1637 'ar_minor_edit',
1638 'ar_deleted',
1639 'ar_len',
1640 'ar_parent_id',
1641 'ar_sha1',
1642 ] + $commentQuery['fields'],
1643 'joins' => $commentQuery['joins'],
1644 ];
1645
1646 if ( $this->contentHandlerUseDB ) {
1647 $ret['fields'][] = 'ar_content_format';
1648 $ret['fields'][] = 'ar_content_model';
1649 }
1650
1651 return $ret;
1652 }
1653
1654 /**
1655 * Do a batched query for the sizes of a set of revisions.
1656 *
1657 * MCR migration note: this replaces Revision::getParentLengths
1658 *
1659 * @param IDatabase $db
1660 * @param int[] $revIds
1661 * @return int[] associative array mapping revision IDs from $revIds to the nominal size
1662 * of the corresponding revision.
1663 */
1664 public function listRevisionSizes( IDatabase $db, array $revIds ) {
1665 $this->checkDatabaseWikiId( $db );
1666
1667 $revLens = [];
1668 if ( !$revIds ) {
1669 return $revLens; // empty
1670 }
1671
1672 $res = $db->select(
1673 'revision',
1674 [ 'rev_id', 'rev_len' ],
1675 [ 'rev_id' => $revIds ],
1676 __METHOD__
1677 );
1678
1679 foreach ( $res as $row ) {
1680 $revLens[$row->rev_id] = intval( $row->rev_len );
1681 }
1682
1683 return $revLens;
1684 }
1685
1686 /**
1687 * Get previous revision for this title
1688 *
1689 * MCR migration note: this replaces Revision::getPrevious
1690 *
1691 * @param RevisionRecord $rev
1692 *
1693 * @return RevisionRecord|null
1694 */
1695 public function getPreviousRevision( RevisionRecord $rev ) {
1696 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
1697 $prev = $title->getPreviousRevisionID( $rev->getId() );
1698 if ( $prev ) {
1699 return $this->getRevisionByTitle( $title, $prev );
1700 }
1701 return null;
1702 }
1703
1704 /**
1705 * Get next revision for this title
1706 *
1707 * MCR migration note: this replaces Revision::getNext
1708 *
1709 * @param RevisionRecord $rev
1710 *
1711 * @return RevisionRecord|null
1712 */
1713 public function getNextRevision( RevisionRecord $rev ) {
1714 $title = $this->getTitle( $rev->getPageId(), $rev->getId() );
1715 $next = $title->getNextRevisionID( $rev->getId() );
1716 if ( $next ) {
1717 return $this->getRevisionByTitle( $title, $next );
1718 }
1719 return null;
1720 }
1721
1722 /**
1723 * Get previous revision Id for this page_id
1724 * This is used to populate rev_parent_id on save
1725 *
1726 * MCR migration note: this corresponds to Revision::getPreviousRevisionId
1727 *
1728 * @param IDatabase $db
1729 * @param RevisionRecord $rev
1730 *
1731 * @return int
1732 */
1733 private function getPreviousRevisionId( IDatabase $db, RevisionRecord $rev ) {
1734 $this->checkDatabaseWikiId( $db );
1735
1736 if ( $rev->getPageId() === null ) {
1737 return 0;
1738 }
1739 # Use page_latest if ID is not given
1740 if ( !$rev->getId() ) {
1741 $prevId = $db->selectField(
1742 'page', 'page_latest',
1743 [ 'page_id' => $rev->getPageId() ],
1744 __METHOD__
1745 );
1746 } else {
1747 $prevId = $db->selectField(
1748 'revision', 'rev_id',
1749 [ 'rev_page' => $rev->getPageId(), 'rev_id < ' . $rev->getId() ],
1750 __METHOD__,
1751 [ 'ORDER BY' => 'rev_id DESC' ]
1752 );
1753 }
1754 return intval( $prevId );
1755 }
1756
1757 /**
1758 * Get rev_timestamp from rev_id, without loading the rest of the row
1759 *
1760 * MCR migration note: this replaces Revision::getTimestampFromId
1761 *
1762 * @param Title $title
1763 * @param int $id
1764 * @param int $flags
1765 * @return string|bool False if not found
1766 */
1767 public function getTimestampFromId( $title, $id, $flags = 0 ) {
1768 $db = $this->getDBConnection(
1769 ( $flags & IDBAccessObject::READ_LATEST ) ? DB_MASTER : DB_REPLICA
1770 );
1771
1772 $conds = [ 'rev_id' => $id ];
1773 $conds['rev_page'] = $title->getArticleID();
1774 $timestamp = $db->selectField( 'revision', 'rev_timestamp', $conds, __METHOD__ );
1775
1776 $this->releaseDBConnection( $db );
1777 return ( $timestamp !== false ) ? wfTimestamp( TS_MW, $timestamp ) : false;
1778 }
1779
1780 /**
1781 * Get count of revisions per page...not very efficient
1782 *
1783 * MCR migration note: this replaces Revision::countByPageId
1784 *
1785 * @param IDatabase $db
1786 * @param int $id Page id
1787 * @return int
1788 */
1789 public function countRevisionsByPageId( IDatabase $db, $id ) {
1790 $this->checkDatabaseWikiId( $db );
1791
1792 $row = $db->selectRow( 'revision',
1793 [ 'revCount' => 'COUNT(*)' ],
1794 [ 'rev_page' => $id ],
1795 __METHOD__
1796 );
1797 if ( $row ) {
1798 return intval( $row->revCount );
1799 }
1800 return 0;
1801 }
1802
1803 /**
1804 * Get count of revisions per page...not very efficient
1805 *
1806 * MCR migration note: this replaces Revision::countByTitle
1807 *
1808 * @param IDatabase $db
1809 * @param Title $title
1810 * @return int
1811 */
1812 public function countRevisionsByTitle( IDatabase $db, $title ) {
1813 $id = $title->getArticleID();
1814 if ( $id ) {
1815 return $this->countRevisionsByPageId( $db, $id );
1816 }
1817 return 0;
1818 }
1819
1820 /**
1821 * Check if no edits were made by other users since
1822 * the time a user started editing the page. Limit to
1823 * 50 revisions for the sake of performance.
1824 *
1825 * MCR migration note: this replaces Revision::userWasLastToEdit
1826 *
1827 * @deprecated since 1.31; Can possibly be removed, since the self-conflict suppression
1828 * logic in EditPage that uses this seems conceptually dubious. Revision::userWasLastToEdit
1829 * has been deprecated since 1.24.
1830 *
1831 * @param IDatabase $db The Database to perform the check on.
1832 * @param int $pageId The ID of the page in question
1833 * @param int $userId The ID of the user in question
1834 * @param string $since Look at edits since this time
1835 *
1836 * @return bool True if the given user was the only one to edit since the given timestamp
1837 */
1838 public function userWasLastToEdit( IDatabase $db, $pageId, $userId, $since ) {
1839 $this->checkDatabaseWikiId( $db );
1840
1841 if ( !$userId ) {
1842 return false;
1843 }
1844
1845 $res = $db->select(
1846 'revision',
1847 'rev_user',
1848 [
1849 'rev_page' => $pageId,
1850 'rev_timestamp > ' . $db->addQuotes( $db->timestamp( $since ) )
1851 ],
1852 __METHOD__,
1853 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ]
1854 );
1855 foreach ( $res as $row ) {
1856 if ( $row->rev_user != $userId ) {
1857 return false;
1858 }
1859 }
1860 return true;
1861 }
1862
1863 /**
1864 * Load a revision based on a known page ID and current revision ID from the DB
1865 *
1866 * This method allows for the use of caching, though accessing anything that normally
1867 * requires permission checks (aside from the text) will trigger a small DB lookup.
1868 *
1869 * MCR migration note: this replaces Revision::newKnownCurrent
1870 *
1871 * @param Title $title the associated page title
1872 * @param int $revId current revision of this page. Defaults to $title->getLatestRevID().
1873 *
1874 * @return RevisionRecord|bool Returns false if missing
1875 */
1876 public function getKnownCurrentRevision( Title $title, $revId ) {
1877 $db = $this->getDBConnectionRef( DB_REPLICA );
1878
1879 $pageId = $title->getArticleID();
1880
1881 if ( !$pageId ) {
1882 return false;
1883 }
1884
1885 if ( !$revId ) {
1886 $revId = $title->getLatestRevID();
1887 }
1888
1889 if ( !$revId ) {
1890 wfWarn(
1891 'No latest revision known for page ' . $title->getPrefixedDBkey()
1892 . ' even though it exists with page ID ' . $pageId
1893 );
1894 return false;
1895 }
1896
1897 $row = $this->cache->getWithSetCallback(
1898 // Page/rev IDs passed in from DB to reflect history merges
1899 $this->cache->makeGlobalKey( 'revision-row-1.29', $db->getDomainID(), $pageId, $revId ),
1900 WANObjectCache::TTL_WEEK,
1901 function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
1902 $setOpts += Database::getCacheSetOptions( $db );
1903
1904 $conds = [
1905 'rev_page' => intval( $pageId ),
1906 'page_id' => intval( $pageId ),
1907 'rev_id' => intval( $revId ),
1908 ];
1909
1910 $row = $this->fetchRevisionRowFromConds( $db, $conds );
1911 return $row ?: false; // don't cache negatives
1912 }
1913 );
1914
1915 // Reflect revision deletion and user renames
1916 if ( $row ) {
1917 return $this->newRevisionFromRow( $row, 0, $title );
1918 } else {
1919 return false;
1920 }
1921 }
1922
1923 // TODO: move relevant methods from Title here, e.g. getFirstRevision, isBigDeletion, etc.
1924
1925 }