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