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 use Wikimedia\Assert\Assert;
34 
46 
56  public static function newFromParentRevision( RevisionRecord $parent ) {
57  $rev = new MutableRevisionRecord( $parent->getPage(), $parent->getWikiId() );
58 
59  foreach ( $parent->getSlotRoles() as $role ) {
60  $slot = $parent->getSlot( $role, self::RAW );
61  $rev->inheritSlot( $slot );
62  }
63 
64  $rev->setPageId( $parent->getPageId() );
65  $rev->setParentId( $parent->getId() );
66 
67  return $rev;
68  }
69 
78  public static function newUpdatedRevisionRecord(
79  RevisionRecord $revision,
80  array $slots
82  $newRevisionRecord = new MutableRevisionRecord(
83  $revision->getPage(),
84  $revision->getWikiId()
85  );
86 
87  $newRevisionRecord->setId( $revision->getId( $revision->getWikiId() ) );
88  $newRevisionRecord->setPageId( $revision->getPageId( $revision->getWikiId() ) );
89  $newRevisionRecord->setParentId( $revision->getParentId( $revision->getWikiId() ) );
90  $newRevisionRecord->setUser( $revision->getUser() );
91 
92  foreach ( $revision->getSlots()->getSlots() as $role => $slot ) {
93  $newRevisionRecord->setSlot( $slot );
94  }
95 
96  foreach ( $slots as $role => $slot ) {
97  $newRevisionRecord->setSlot( $slot );
98  }
99 
100  return $newRevisionRecord;
101  }
102 
111  public function __construct( PageIdentity $page, $wikiId = self::LOCAL ) {
112  $slots = new MutableRevisionSlots( [], function () {
113  $this->resetAggregateValues();
114  } );
115 
116  parent::__construct( $page, $slots, $wikiId );
117  }
118 
123  public function setParentId( $parentId ) {
124  Assert::parameterType( 'integer', $parentId, '$parentId' );
125 
126  $this->mParentId = $parentId;
127 
128  return $this;
129  }
130 
147  public function setSlot( SlotRecord $slot ) {
148  if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
149  throw new InvalidArgumentException(
150  'The given slot must be an unsaved, unattached one. '
151  . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
152  . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
153  );
154  }
155 
156  $this->mSlots->setSlot( $slot );
157 
158  return $this;
159  }
160 
171  public function inheritSlot( SlotRecord $parentSlot ) {
172  $this->mSlots->inheritSlot( $parentSlot );
173 
174  return $this;
175  }
176 
192  public function setContent( $role, Content $content ) {
193  $this->mSlots->setContent( $role, $content );
194 
195  return $this;
196  }
197 
211  public function removeSlot( $role ) {
212  $this->mSlots->removeSlot( $role );
213 
214  return $this;
215  }
216 
223  public function applyUpdate( RevisionSlotsUpdate $update ) {
224  $update->apply( $this->mSlots );
225 
226  return $this;
227  }
228 
233  public function setComment( CommentStoreComment $comment ) {
234  $this->mComment = $comment;
235 
236  return $this;
237  }
238 
249  public function setSha1( $sha1 ) {
250  Assert::parameterType( 'string', $sha1, '$sha1' );
251 
252  $this->mSha1 = $sha1;
253 
254  return $this;
255  }
256 
267  public function setSize( $size ) {
268  Assert::parameterType( 'integer', $size, '$size' );
269 
270  $this->mSize = $size;
271 
272  return $this;
273  }
274 
279  public function setVisibility( $visibility ) {
280  Assert::parameterType( 'integer', $visibility, '$visibility' );
281 
282  $this->mDeleted = $visibility;
283 
284  return $this;
285  }
286 
291  public function setTimestamp( $timestamp ) {
292  Assert::parameterType( 'string', $timestamp, '$timestamp' );
293 
294  $this->mTimestamp = MWTimestamp::convert( TS_MW, $timestamp );
295 
296  return $this;
297  }
298 
303  public function setMinorEdit( $minorEdit ) {
304  Assert::parameterType( 'boolean', $minorEdit, '$minorEdit' );
305 
306  $this->mMinorEdit = $minorEdit;
307 
308  return $this;
309  }
310 
323  public function setId( $id ) {
324  Assert::parameterType( 'integer', $id, '$id' );
325 
326  $this->mId = $id;
327 
328  return $this;
329  }
330 
337  public function setUser( UserIdentity $user ) {
338  $this->mUser = $user;
339 
340  return $this;
341  }
342 
347  public function setPageId( $pageId ) {
348  Assert::parameterType( 'integer', $pageId, '$pageId' );
349 
350  $pageIdBasedOnPage = $this->getArticleId( $this->mPage );
351  if ( $pageIdBasedOnPage && $pageIdBasedOnPage !== $this->getArticleId( $this->mPage ) ) {
352  throw new InvalidArgumentException(
353  'The given page does not belong to page ID ' . $this->mPageId
354  );
355  }
356 
357  $this->mPageId = $pageId;
358 
359  return $this;
360  }
361 
369  public function getSize() {
370  // If not known, re-calculate and remember. Will be reset when slots change.
371  if ( $this->mSize === null ) {
372  $this->mSize = $this->mSlots->computeSize();
373  }
374 
375  return $this->mSize;
376  }
377 
385  public function getSha1() {
386  // If not known, re-calculate and remember. Will be reset when slots change.
387  if ( $this->mSha1 === null ) {
388  $this->mSha1 = $this->mSlots->computeSha1();
389  }
390 
391  return $this->mSha1;
392  }
393 
400  public function getSlots() {
401  // Overwritten just guarantee the more narrow return type.
402  return parent::getSlots();
403  }
404 
409  private function resetAggregateValues() {
410  $this->mSize = null;
411  $this->mSha1 = null;
412  }
413 
414 }
415 
420 class_alias( MutableRevisionRecord::class, 'MediaWiki\Storage\MutableRevisionRecord' );
Revision\MutableRevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision as a MutableRevisionSlots instance, which can be modified...
Definition: MutableRevisionRecord.php:400
Revision\MutableRevisionRecord\setMinorEdit
setMinorEdit( $minorEdit)
Definition: MutableRevisionRecord.php:303
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:65
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:37
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
Revision\RevisionRecord\getPageId
getPageId( $wikiId=self::LOCAL)
Get the page ID.
Definition: RevisionRecord.php:351
Revision\MutableRevisionRecord\setSha1
setSha1( $sha1)
Set revision hash, for optimization.
Definition: MutableRevisionRecord.php:249
Revision\MutableRevisionRecord\setParentId
setParentId( $parentId)
Definition: MutableRevisionRecord.php:123
Revision\RevisionRecord\getPage
getPage()
Returns the page this revision belongs to.
Definition: RevisionRecord.php:386
Revision\MutableRevisionRecord\inheritSlot
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
Definition: MutableRevisionRecord.php:171
Revision\MutableRevisionRecord\newFromParentRevision
static newFromParentRevision(RevisionRecord $parent)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision,...
Definition: MutableRevisionRecord.php:56
Revision\MutableRevisionRecord\setPageId
setPageId( $pageId)
Definition: MutableRevisionRecord.php:347
Revision\MutableRevisionRecord\setId
setId( $id)
Set the revision ID.
Definition: MutableRevisionRecord.php:323
Revision\MutableRevisionSlots
Mutable version of RevisionSlots, for constructing a new revision.
Definition: MutableRevisionSlots.php:33
Revision\SlotRecord\getRevision
getRevision()
Returns the ID of the revision this slot is associated with.
Definition: SlotRecord.php:413
Revision\MutableRevisionRecord\removeSlot
removeSlot( $role)
Removes the slot with the given role from this revision.
Definition: MutableRevisionRecord.php:211
Revision\RevisionRecord\getParentId
getParentId( $wikiId=self::LOCAL)
Get parent revision ID (the original previous page revision).
Definition: RevisionRecord.php:313
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Revision
Definition: ContributionsLookup.php:3
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:405
MWException
MediaWiki exception.
Definition: MWException.php:29
Revision\MutableRevisionRecord\setUser
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
Definition: MutableRevisionRecord.php:337
Revision\MutableRevisionRecord\applyUpdate
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
Definition: MutableRevisionRecord.php:223
Revision\MutableRevisionRecord\__construct
__construct(PageIdentity $page, $wikiId=self::LOCAL)
Definition: MutableRevisionRecord.php:111
Revision\RevisionRecord\$wikiId
string false $wikiId
Wiki ID; false means the current wiki.
Definition: RevisionRecord.php:67
Revision\MutableRevisionRecord\setVisibility
setVisibility( $visibility)
Definition: MutableRevisionRecord.php:279
Revision\MutableRevisionRecord\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionRecord.php:192
Revision\RevisionRecord\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionRecord.php:223
Revision\SlotRecord\hasRevision
hasRevision()
Whether this slot has revision ID associated.
Definition: SlotRecord.php:497
MediaWiki\Storage\RevisionSlotsUpdate
Value object representing a modification of revision slots.
Definition: RevisionSlotsUpdate.php:36
$content
$content
Definition: router.php:76
Revision\MutableRevisionRecord
Definition: MutableRevisionRecord.php:45
Revision\RevisionRecord\getId
getId( $wikiId=self::LOCAL)
Get revision ID.
Definition: RevisionRecord.php:295
Revision\RevisionRecord\$mSize
int null $mSize
Definition: RevisionRecord.php:81
Revision\RevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision.
Definition: RevisionRecord.php:238
Revision\MutableRevisionRecord\resetAggregateValues
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
Definition: MutableRevisionRecord.php:409
Content
Base interface for content objects.
Definition: Content.php:35
Revision\MutableRevisionRecord\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionRecord.php:147
Revision\MutableRevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Definition: MutableRevisionRecord.php:385
Revision\MutableRevisionRecord\setTimestamp
setTimestamp( $timestamp)
Definition: MutableRevisionRecord.php:291
Revision\MutableRevisionRecord\setSize
setSize( $size)
Set nominal revision size, for optimization.
Definition: MutableRevisionRecord.php:267
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:78
Revision\MutableRevisionRecord\getSize
getSize()
Returns the nominal size of this revision.
Definition: MutableRevisionRecord.php:369
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:83
Revision\RevisionRecord\getWikiId
getWikiId()
Get the ID of the wiki this revision belongs to.
Definition: RevisionRecord.php:361
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
Revision\MutableRevisionRecord\setComment
setComment(CommentStoreComment $comment)
Definition: MutableRevisionRecord.php:233
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:196