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