MediaWiki  master
MutableRevisionRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use Content;
27 use InvalidArgumentException;
30 use MWException;
31 use MWTimestamp;
32 use Title;
33 use Wikimedia\Assert\Assert;
34 
46 
56  public static function newFromParentRevision( RevisionRecord $parent ) {
57  // TODO: ideally, we wouldn't need a Title here
59  $rev = new MutableRevisionRecord( $title, $parent->getWikiId() );
60 
61  foreach ( $parent->getSlotRoles() as $role ) {
62  $slot = $parent->getSlot( $role, self::RAW );
63  $rev->inheritSlot( $slot );
64  }
65 
66  $rev->setPageId( $parent->getPageId() );
67  $rev->setParentId( $parent->getId() );
68 
69  return $rev;
70  }
71 
83  public function __construct( Title $title, $dbDomain = false ) {
84  $slots = new MutableRevisionSlots( [], function () {
85  $this->resetAggregateValues();
86  } );
87 
88  parent::__construct( $title, $slots, $dbDomain );
89  }
90 
94  public function setParentId( $parentId ) {
95  Assert::parameterType( 'integer', $parentId, '$parentId' );
96 
97  $this->mParentId = $parentId;
98  }
99 
115  public function setSlot( SlotRecord $slot ) {
116  if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
117  throw new InvalidArgumentException(
118  'The given slot must be an unsaved, unattached one. '
119  . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
120  . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
121  );
122  }
123 
124  $this->mSlots->setSlot( $slot );
125  }
126 
136  public function inheritSlot( SlotRecord $parentSlot ) {
137  $this->mSlots->inheritSlot( $parentSlot );
138  }
139 
154  public function setContent( $role, Content $content ) {
155  $this->mSlots->setContent( $role, $content );
156  }
157 
170  public function removeSlot( $role ) {
171  $this->mSlots->removeSlot( $role );
172  }
173 
179  public function applyUpdate( RevisionSlotsUpdate $update ) {
180  $update->apply( $this->mSlots );
181  }
182 
186  public function setComment( CommentStoreComment $comment ) {
187  $this->mComment = $comment;
188  }
189 
199  public function setSha1( $sha1 ) {
200  Assert::parameterType( 'string', $sha1, '$sha1' );
201 
202  $this->mSha1 = $sha1;
203  }
204 
214  public function setSize( $size ) {
215  Assert::parameterType( 'integer', $size, '$size' );
216 
217  $this->mSize = $size;
218  }
219 
223  public function setVisibility( $visibility ) {
224  Assert::parameterType( 'integer', $visibility, '$visibility' );
225 
226  $this->mDeleted = $visibility;
227  }
228 
232  public function setTimestamp( $timestamp ) {
233  Assert::parameterType( 'string', $timestamp, '$timestamp' );
234 
235  $this->mTimestamp = MWTimestamp::convert( TS_MW, $timestamp );
236  }
237 
241  public function setMinorEdit( $minorEdit ) {
242  Assert::parameterType( 'boolean', $minorEdit, '$minorEdit' );
243 
244  $this->mMinorEdit = $minorEdit;
245  }
246 
258  public function setId( $id ) {
259  Assert::parameterType( 'integer', $id, '$id' );
260 
261  $this->mId = $id;
262  }
263 
269  public function setUser( UserIdentity $user ) {
270  $this->mUser = $user;
271  }
272 
276  public function setPageId( $pageId ) {
277  Assert::parameterType( 'integer', $pageId, '$pageId' );
278 
279  if ( $this->mTitle->exists() && $pageId !== $this->mTitle->getArticleID() ) {
280  throw new InvalidArgumentException(
281  'The given Title does not belong to page ID ' . $this->mPageId
282  );
283  }
284 
285  $this->mPageId = $pageId;
286  }
287 
295  public function getSize() {
296  // If not known, re-calculate and remember. Will be reset when slots change.
297  if ( $this->mSize === null ) {
298  $this->mSize = $this->mSlots->computeSize();
299  }
300 
301  return $this->mSize;
302  }
303 
311  public function getSha1() {
312  // If not known, re-calculate and remember. Will be reset when slots change.
313  if ( $this->mSha1 === null ) {
314  $this->mSha1 = $this->mSlots->computeSha1();
315  }
316 
317  return $this->mSha1;
318  }
319 
326  public function getSlots() {
327  // Overwritten just guarantee the more narrow return type.
328  return parent::getSlots();
329  }
330 
335  private function resetAggregateValues() {
336  $this->mSize = null;
337  $this->mSha1 = null;
338  }
339 
340 }
341 
346 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:326
Revision\MutableRevisionRecord\setMinorEdit
setMinorEdit( $minorEdit)
Definition: MutableRevisionRecord.php:241
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:34
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Revision\MutableRevisionRecord\setSha1
setSha1( $sha1)
Set revision hash, for optimization.
Definition: MutableRevisionRecord.php:199
Revision\MutableRevisionRecord\setParentId
setParentId( $parentId)
Definition: MutableRevisionRecord.php:94
Revision\MutableRevisionRecord\inheritSlot
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
Definition: MutableRevisionRecord.php:136
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:276
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:191
Revision\MutableRevisionRecord\setId
setId( $id)
Set the revision ID.
Definition: MutableRevisionRecord.php:258
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:396
Revision\MutableRevisionRecord\removeSlot
removeSlot( $role)
Removes the slot with the given role from this revision.
Definition: MutableRevisionRecord.php:170
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\Revision
Definition: ContributionsLookup.php:3
Revision\MutableRevisionRecord\__construct
__construct(Title $title, $dbDomain=false)
Definition: MutableRevisionRecord.php:83
MWException
MediaWiki exception.
Definition: MWException.php:29
Revision\MutableRevisionRecord\setUser
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
Definition: MutableRevisionRecord.php:269
Revision\MutableRevisionRecord\applyUpdate
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
Definition: MutableRevisionRecord.php:179
Revision\MutableRevisionRecord\setVisibility
setVisibility( $visibility)
Definition: MutableRevisionRecord.php:223
Revision\MutableRevisionRecord\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionRecord.php:154
$title
$title
Definition: testCompression.php:38
Revision\RevisionRecord\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionRecord.php:218
Revision\RevisionRecord\getPageId
getPageId()
Get the page ID.
Definition: RevisionRecord.php:331
Revision\SlotRecord\hasRevision
hasRevision()
Whether this slot has revision ID associated.
Definition: SlotRecord.php:480
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:279
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\$mSize
int null $mSize
Definition: RevisionRecord.php:77
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:281
Revision\MutableRevisionRecord\resetAggregateValues
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
Definition: MutableRevisionRecord.php:335
Content
Base interface for content objects.
Definition: Content.php:35
Revision\RevisionRecord\getPageAsLinkTarget
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
Definition: RevisionRecord.php:351
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Revision\MutableRevisionRecord\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionRecord.php:115
Revision\MutableRevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Definition: MutableRevisionRecord.php:311
Revision\MutableRevisionRecord\setTimestamp
setTimestamp( $timestamp)
Definition: MutableRevisionRecord.php:232
Revision\MutableRevisionRecord\setSize
setSize( $size)
Set nominal revision size, for optimization.
Definition: MutableRevisionRecord.php:214
Revision\MutableRevisionRecord\getSize
getSize()
Returns the nominal size of this revision.
Definition: MutableRevisionRecord.php:295
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:79
Revision\RevisionRecord\getWikiId
getWikiId()
Get the ID of the wiki this revision belongs to.
Definition: RevisionRecord.php:340
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
Revision\MutableRevisionRecord\setComment
setComment(CommentStoreComment $comment)
Definition: MutableRevisionRecord.php:186
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39