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 
95  public function setParentId( $parentId ) {
96  Assert::parameterType( 'integer', $parentId, '$parentId' );
97 
98  $this->mParentId = $parentId;
99 
100  return $this;
101  }
102 
119  public function setSlot( SlotRecord $slot ) {
120  if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
121  throw new InvalidArgumentException(
122  'The given slot must be an unsaved, unattached one. '
123  . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
124  . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
125  );
126  }
127 
128  $this->mSlots->setSlot( $slot );
129 
130  return $this;
131  }
132 
143  public function inheritSlot( SlotRecord $parentSlot ) {
144  $this->mSlots->inheritSlot( $parentSlot );
145 
146  return $this;
147  }
148 
164  public function setContent( $role, Content $content ) {
165  $this->mSlots->setContent( $role, $content );
166 
167  return $this;
168  }
169 
183  public function removeSlot( $role ) {
184  $this->mSlots->removeSlot( $role );
185 
186  return $this;
187  }
188 
195  public function applyUpdate( RevisionSlotsUpdate $update ) {
196  $update->apply( $this->mSlots );
197 
198  return $this;
199  }
200 
205  public function setComment( CommentStoreComment $comment ) {
206  $this->mComment = $comment;
207 
208  return $this;
209  }
210 
221  public function setSha1( $sha1 ) {
222  Assert::parameterType( 'string', $sha1, '$sha1' );
223 
224  $this->mSha1 = $sha1;
225 
226  return $this;
227  }
228 
239  public function setSize( $size ) {
240  Assert::parameterType( 'integer', $size, '$size' );
241 
242  $this->mSize = $size;
243 
244  return $this;
245  }
246 
251  public function setVisibility( $visibility ) {
252  Assert::parameterType( 'integer', $visibility, '$visibility' );
253 
254  $this->mDeleted = $visibility;
255 
256  return $this;
257  }
258 
263  public function setTimestamp( $timestamp ) {
264  Assert::parameterType( 'string', $timestamp, '$timestamp' );
265 
266  $this->mTimestamp = MWTimestamp::convert( TS_MW, $timestamp );
267 
268  return $this;
269  }
270 
275  public function setMinorEdit( $minorEdit ) {
276  Assert::parameterType( 'boolean', $minorEdit, '$minorEdit' );
277 
278  $this->mMinorEdit = $minorEdit;
279 
280  return $this;
281  }
282 
295  public function setId( $id ) {
296  Assert::parameterType( 'integer', $id, '$id' );
297 
298  $this->mId = $id;
299 
300  return $this;
301  }
302 
309  public function setUser( UserIdentity $user ) {
310  $this->mUser = $user;
311 
312  return $this;
313  }
314 
319  public function setPageId( $pageId ) {
320  Assert::parameterType( 'integer', $pageId, '$pageId' );
321 
322  if ( $this->mTitle->exists() && $pageId !== $this->mTitle->getArticleID() ) {
323  throw new InvalidArgumentException(
324  'The given Title does not belong to page ID ' . $this->mPageId
325  );
326  }
327 
328  $this->mPageId = $pageId;
329 
330  return $this;
331  }
332 
340  public function getSize() {
341  // If not known, re-calculate and remember. Will be reset when slots change.
342  if ( $this->mSize === null ) {
343  $this->mSize = $this->mSlots->computeSize();
344  }
345 
346  return $this->mSize;
347  }
348 
356  public function getSha1() {
357  // If not known, re-calculate and remember. Will be reset when slots change.
358  if ( $this->mSha1 === null ) {
359  $this->mSha1 = $this->mSlots->computeSha1();
360  }
361 
362  return $this->mSha1;
363  }
364 
371  public function getSlots() {
372  // Overwritten just guarantee the more narrow return type.
373  return parent::getSlots();
374  }
375 
380  private function resetAggregateValues() {
381  $this->mSize = null;
382  $this->mSha1 = null;
383  }
384 
385 }
386 
391 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:371
Revision\MutableRevisionRecord\setMinorEdit
setMinorEdit( $minorEdit)
Definition: MutableRevisionRecord.php:275
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:221
Revision\MutableRevisionRecord\setParentId
setParentId( $parentId)
Definition: MutableRevisionRecord.php:95
Revision\MutableRevisionRecord\inheritSlot
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
Definition: MutableRevisionRecord.php:143
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:319
Revision\RevisionRecord\getSlot
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
Definition: RevisionRecord.php:183
Revision\MutableRevisionRecord\setId
setId( $id)
Set the revision ID.
Definition: MutableRevisionRecord.php:295
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:389
Revision\MutableRevisionRecord\removeSlot
removeSlot( $role)
Removes the slot with the given role from this revision.
Definition: MutableRevisionRecord.php:183
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:309
Revision\MutableRevisionRecord\applyUpdate
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
Definition: MutableRevisionRecord.php:195
Revision\MutableRevisionRecord\setVisibility
setVisibility( $visibility)
Definition: MutableRevisionRecord.php:251
Revision\MutableRevisionRecord\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionRecord.php:164
$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:210
Revision\RevisionRecord\getPageId
getPageId()
Get the page ID.
Definition: RevisionRecord.php:323
Revision\SlotRecord\hasRevision
hasRevision()
Whether this slot has revision ID associated.
Definition: SlotRecord.php:473
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:271
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:78
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:289
Revision\MutableRevisionRecord\resetAggregateValues
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
Definition: MutableRevisionRecord.php:380
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:343
Title
Represents a title within MediaWiki.
Definition: Title.php:46
Revision\MutableRevisionRecord\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionRecord.php:119
Revision\MutableRevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Definition: MutableRevisionRecord.php:356
Revision\MutableRevisionRecord\setTimestamp
setTimestamp( $timestamp)
Definition: MutableRevisionRecord.php:263
Revision\MutableRevisionRecord\setSize
setSize( $size)
Set nominal revision size, for optimization.
Definition: MutableRevisionRecord.php:239
Revision\MutableRevisionRecord\getSize
getSize()
Returns the nominal size of this revision.
Definition: MutableRevisionRecord.php:340
Revision\RevisionRecord\$mSha1
string null $mSha1
Definition: RevisionRecord.php:80
Revision\RevisionRecord\getWikiId
getWikiId()
Get the ID of the wiki this revision belongs to.
Definition: RevisionRecord.php:332
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
Revision\MutableRevisionRecord\setComment
setComment(CommentStoreComment $comment)
Definition: MutableRevisionRecord.php:205
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40