MediaWiki  master
MutableRevisionRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use Content;
27 use InvalidArgumentException;
31 use MWException;
32 use MWTimestamp;
33 
45 
55  public static function newFromParentRevision( RevisionRecord $parent ) {
56  $rev = new MutableRevisionRecord( $parent->getPage(), $parent->getWikiId() );
57 
58  foreach ( $parent->getSlotRoles() as $role ) {
59  $slot = $parent->getSlot( $role, self::RAW );
60  $rev->inheritSlot( $slot );
61  }
62 
63  $rev->setPageId( $parent->getPageId() );
64  $rev->setParentId( $parent->getId() );
65 
66  return $rev;
67  }
68 
77  public static function newUpdatedRevisionRecord(
78  RevisionRecord $revision,
79  array $slots
81  $newRevisionRecord = new MutableRevisionRecord(
82  $revision->getPage(),
83  $revision->getWikiId()
84  );
85 
86  $newRevisionRecord->setId( $revision->getId( $revision->getWikiId() ) );
87  $newRevisionRecord->setPageId( $revision->getPageId( $revision->getWikiId() ) );
88  $newRevisionRecord->setParentId( $revision->getParentId( $revision->getWikiId() ) );
89  $newRevisionRecord->setUser( $revision->getUser() );
90 
91  foreach ( $revision->getSlots()->getSlots() as $role => $slot ) {
92  $newRevisionRecord->setSlot( $slot );
93  }
94 
95  foreach ( $slots as $role => $slot ) {
96  $newRevisionRecord->setSlot( $slot );
97  }
98 
99  return $newRevisionRecord;
100  }
101 
110  public function __construct( PageIdentity $page, $wikiId = self::LOCAL ) {
111  $slots = new MutableRevisionSlots( [], function () {
112  $this->resetAggregateValues();
113  } );
114 
115  parent::__construct( $page, $slots, $wikiId );
116  }
117 
122  public function setParentId( int $parentId ) {
123  $this->mParentId = $parentId;
124 
125  return $this;
126  }
127 
144  public function setSlot( SlotRecord $slot ) {
145  if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
146  throw new InvalidArgumentException(
147  'The given slot must be an unsaved, unattached one. '
148  . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
149  . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
150  );
151  }
152 
153  $this->mSlots->setSlot( $slot );
154 
155  return $this;
156  }
157 
168  public function inheritSlot( SlotRecord $parentSlot ) {
169  $this->mSlots->inheritSlot( $parentSlot );
170 
171  return $this;
172  }
173 
189  public function setContent( $role, Content $content ) {
190  $this->mSlots->setContent( $role, $content );
191 
192  return $this;
193  }
194 
208  public function removeSlot( $role ) {
209  $this->mSlots->removeSlot( $role );
210 
211  return $this;
212  }
213 
220  public function applyUpdate( RevisionSlotsUpdate $update ) {
221  $update->apply( $this->mSlots );
222 
223  return $this;
224  }
225 
230  public function setComment( CommentStoreComment $comment ) {
231  $this->mComment = $comment;
232 
233  return $this;
234  }
235 
246  public function setSha1( string $sha1 ) {
247  $this->mSha1 = $sha1;
248 
249  return $this;
250  }
251 
262  public function setSize( int $size ) {
263  $this->mSize = $size;
264 
265  return $this;
266  }
267 
272  public function setVisibility( int $visibility ) {
273  $this->mDeleted = $visibility;
274 
275  return $this;
276  }
277 
282  public function setTimestamp( string $timestamp ) {
283  $this->mTimestamp = MWTimestamp::convert( TS_MW, $timestamp );
284 
285  return $this;
286  }
287 
292  public function setMinorEdit( bool $minorEdit ) {
293  $this->mMinorEdit = $minorEdit;
294 
295  return $this;
296  }
297 
310  public function setId( int $id ) {
311  $this->mId = $id;
312 
313  return $this;
314  }
315 
322  public function setUser( UserIdentity $user ) {
323  $this->mUser = $user;
324 
325  return $this;
326  }
327 
332  public function setPageId( int $pageId ) {
333  $pageIdBasedOnPage = $this->getArticleId( $this->mPage );
334  if ( $pageIdBasedOnPage && $pageIdBasedOnPage !== $this->getArticleId( $this->mPage ) ) {
335  throw new InvalidArgumentException(
336  'The given page does not belong to page ID ' . $this->mPageId
337  );
338  }
339 
340  $this->mPageId = $pageId;
341 
342  return $this;
343  }
344 
352  public function getSize() {
353  // If not known, re-calculate and remember. Will be reset when slots change.
354  if ( $this->mSize === null ) {
355  $this->mSize = $this->mSlots->computeSize();
356  }
357 
358  return $this->mSize;
359  }
360 
368  public function getSha1() {
369  // If not known, re-calculate and remember. Will be reset when slots change.
370  if ( $this->mSha1 === null ) {
371  $this->mSha1 = $this->mSlots->computeSha1();
372  }
373 
374  return $this->mSha1;
375  }
376 
383  public function getSlots(): RevisionSlots {
384  // Overwritten just guarantee the more narrow return type.
385  // TODO Update return typehint once full return type covariance is allowed (PHP 7.4+, T278139)
386  // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
387  return parent::getSlots();
388  }
389 
394  private function resetAggregateValues() {
395  $this->mSize = null;
396  $this->mSha1 = null;
397  }
398 
399 }
400 
405 class_alias( MutableRevisionRecord::class, 'MediaWiki\Storage\MutableRevisionRecord' );
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:38
MediaWiki\Revision\MutableRevisionRecord\setTimestamp
setTimestamp(string $timestamp)
Definition: MutableRevisionRecord.php:282
MediaWiki\Revision\RevisionRecord\getUser
getUser( $audience=self::FOR_PUBLIC, Authority $performer=null)
Fetch revision's author's user identity, if it's available to the specified audience.
Definition: RevisionRecord.php:389
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\Revision\MutableRevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Definition: MutableRevisionRecord.php:368
MediaWiki\Revision\MutableRevisionRecord\setParentId
setParentId(int $parentId)
Definition: MutableRevisionRecord.php:122
MediaWiki\Revision\RevisionRecord\getPage
getPage()
Returns the page this revision belongs to.
Definition: RevisionRecord.php:370
MediaWiki\Revision\RevisionRecord\getPageId
getPageId( $wikiId=self::LOCAL)
Get the page ID.
Definition: RevisionRecord.php:335
MediaWiki\Revision\MutableRevisionSlots
Mutable version of RevisionSlots, for constructing a new revision.
Definition: MutableRevisionSlots.php:33
MediaWiki\Revision\MutableRevisionRecord\setId
setId(int $id)
Set the revision ID.
Definition: MutableRevisionRecord.php:310
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Revision\MutableRevisionRecord\setSize
setSize(int $size)
Set nominal revision size, for optimization.
Definition: MutableRevisionRecord.php:262
MediaWiki\Revision
Definition: ContributionsLookup.php:3
MWException
MediaWiki exception.
Definition: MWException.php:29
MediaWiki\Revision\MutableRevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision as a MutableRevisionSlots instance, which can be modified...
Definition: MutableRevisionRecord.php:383
MediaWiki\Revision\RevisionRecord\$wikiId
string false $wikiId
Wiki ID; false means the current wiki.
Definition: RevisionRecord.php:67
MediaWiki\Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:83
MediaWiki\Revision\MutableRevisionRecord\newFromParentRevision
static newFromParentRevision(RevisionRecord $parent)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision,...
Definition: MutableRevisionRecord.php:55
MediaWiki\Revision\RevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision.
Definition: RevisionRecord.php:222
MediaWiki\Revision\MutableRevisionRecord\__construct
__construct(PageIdentity $page, $wikiId=self::LOCAL)
Definition: MutableRevisionRecord.php:110
MediaWiki\Revision\RevisionRecord\getParentId
getParentId( $wikiId=self::LOCAL)
Get parent revision ID (the original previous page revision).
Definition: RevisionRecord.php:297
MediaWiki\Storage\RevisionSlotsUpdate
Value object representing a modification of revision slots.
Definition: RevisionSlotsUpdate.php:36
MediaWiki\Revision\SlotRecord\hasRevision
hasRevision()
Whether this slot has revision ID associated.
Definition: SlotRecord.php:498
$content
$content
Definition: router.php:76
MediaWiki\Revision\MutableRevisionRecord\removeSlot
removeSlot( $role)
Removes the slot with the given role from this revision.
Definition: MutableRevisionRecord.php:208
MediaWiki\Revision\MutableRevisionRecord\resetAggregateValues
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
Definition: MutableRevisionRecord.php:394
MediaWiki\Revision\MutableRevisionRecord
Definition: MutableRevisionRecord.php:44
MediaWiki\Revision\MutableRevisionRecord\setMinorEdit
setMinorEdit(bool $minorEdit)
Definition: MutableRevisionRecord.php:292
MediaWiki\Revision\MutableRevisionRecord\setSha1
setSha1(string $sha1)
Set revision hash, for optimization.
Definition: MutableRevisionRecord.php:246
MediaWiki\Revision\MutableRevisionRecord\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionRecord.php:189
MediaWiki\Revision\MutableRevisionRecord\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionRecord.php:144
MediaWiki\Storage\RevisionSlotsUpdate\apply
apply(MutableRevisionSlots $slots)
Applies this update to the given MutableRevisionSlots, setting all modified slots,...
Definition: RevisionSlotsUpdate.php:294
MediaWiki\Revision\MutableRevisionRecord\setUser
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
Definition: MutableRevisionRecord.php:322
Content
Base interface for content objects.
Definition: Content.php:35
MediaWiki\Revision\RevisionRecord\getWikiId
getWikiId()
Get the ID of the wiki this revision belongs to.
Definition: RevisionRecord.php:345
MediaWiki\Revision\MutableRevisionRecord\newUpdatedRevisionRecord
static newUpdatedRevisionRecord(RevisionRecord $revision, array $slots)
Returns a MutableRevisionRecord which is an updated version of $revision with $slots added.
Definition: MutableRevisionRecord.php:77
MediaWiki\Revision\RevisionRecord\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionRecord.php:207
MediaWiki\Revision\MutableRevisionRecord\getSize
getSize()
Returns the nominal size of this revision.
Definition: MutableRevisionRecord.php:352
MediaWiki\Revision\RevisionRecord\getId
getId( $wikiId=self::LOCAL)
Get revision ID.
Definition: RevisionRecord.php:279
MediaWiki\Revision\MutableRevisionRecord\setPageId
setPageId(int $pageId)
Definition: MutableRevisionRecord.php:332
MediaWiki\Revision\MutableRevisionRecord\applyUpdate
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
Definition: MutableRevisionRecord.php:220
MediaWiki\Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:41
MediaWiki\Revision\MutableRevisionRecord\inheritSlot
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
Definition: MutableRevisionRecord.php:168
MediaWiki\Revision\MutableRevisionRecord\setComment
setComment(CommentStoreComment $comment)
Definition: MutableRevisionRecord.php:230
MediaWiki\Revision\MutableRevisionRecord\setVisibility
setVisibility(int $visibility)
Definition: MutableRevisionRecord.php:272
MediaWiki\Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:81
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
MediaWiki\Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:180
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
MediaWiki\Revision\SlotRecord\getRevision
getRevision()
Returns the ID of the revision this slot is associated with.
Definition: SlotRecord.php:414