MediaWiki  master
MutableRevisionRecord.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
26 use Content;
30 use MWException;
31 use Title;
33 
43 
53  public static function newFromParentRevision( RevisionRecord $parent ) {
54  // TODO: ideally, we wouldn't need a Title here
56  $rev = new MutableRevisionRecord( $title, $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 
78  function __construct( Title $title, $dbDomain = false ) {
79  $slots = new MutableRevisionSlots();
80 
81  parent::__construct( $title, $slots, $dbDomain );
82  }
83 
87  public function setParentId( $parentId ) {
88  Assert::parameterType( 'integer', $parentId, '$parentId' );
89 
90  $this->mParentId = $parentId;
91  }
92 
108  public function setSlot( SlotRecord $slot ) {
109  if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
110  throw new InvalidArgumentException(
111  'The given slot must be an unsaved, unattached one. '
112  . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
113  . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
114  );
115  }
116 
117  $this->mSlots->setSlot( $slot );
118  $this->resetAggregateValues();
119  }
120 
130  public function inheritSlot( SlotRecord $parentSlot ) {
131  $this->mSlots->inheritSlot( $parentSlot );
132  $this->resetAggregateValues();
133  }
134 
149  public function setContent( $role, Content $content ) {
150  $this->mSlots->setContent( $role, $content );
151  $this->resetAggregateValues();
152  }
153 
166  public function removeSlot( $role ) {
167  $this->mSlots->removeSlot( $role );
168  $this->resetAggregateValues();
169  }
170 
176  public function applyUpdate( RevisionSlotsUpdate $update ) {
177  $update->apply( $this->mSlots );
178  }
179 
183  public function setComment( CommentStoreComment $comment ) {
184  $this->mComment = $comment;
185  }
186 
196  public function setSha1( $sha1 ) {
197  Assert::parameterType( 'string', $sha1, '$sha1' );
198 
199  $this->mSha1 = $sha1;
200  }
201 
211  public function setSize( $size ) {
212  Assert::parameterType( 'integer', $size, '$size' );
213 
214  $this->mSize = $size;
215  }
216 
220  public function setVisibility( $visibility ) {
221  Assert::parameterType( 'integer', $visibility, '$visibility' );
222 
223  $this->mDeleted = $visibility;
224  }
225 
229  public function setTimestamp( $timestamp ) {
230  Assert::parameterType( 'string', $timestamp, '$timestamp' );
231 
232  $this->mTimestamp = wfTimestamp( TS_MW, $timestamp );
233  }
234 
238  public function setMinorEdit( $minorEdit ) {
239  Assert::parameterType( 'boolean', $minorEdit, '$minorEdit' );
240 
241  $this->mMinorEdit = $minorEdit;
242  }
243 
255  public function setId( $id ) {
256  Assert::parameterType( 'integer', $id, '$id' );
257 
258  $this->mId = $id;
259  }
260 
266  public function setUser( UserIdentity $user ) {
267  $this->mUser = $user;
268  }
269 
273  public function setPageId( $pageId ) {
274  Assert::parameterType( 'integer', $pageId, '$pageId' );
275 
276  if ( $this->mTitle->exists() && $pageId !== $this->mTitle->getArticleID() ) {
277  throw new InvalidArgumentException(
278  'The given Title does not belong to page ID ' . $this->mPageId
279  );
280  }
281 
282  $this->mPageId = $pageId;
283  }
284 
292  public function getSize() {
293  // If not known, re-calculate and remember. Will be reset when slots change.
294  if ( $this->mSize === null ) {
295  $this->mSize = $this->mSlots->computeSize();
296  }
297 
298  return $this->mSize;
299  }
300 
308  public function getSha1() {
309  // If not known, re-calculate and remember. Will be reset when slots change.
310  if ( $this->mSha1 === null ) {
311  $this->mSha1 = $this->mSlots->computeSha1();
312  }
313 
314  return $this->mSha1;
315  }
316 
323  public function getSlots() {
324  // Overwritten just guarantee the more narrow return type.
325  return parent::getSlots();
326  }
327 
331  private function resetAggregateValues() {
332  $this->mSize = null;
333  $this->mSha1 = null;
334  }
335 
336 }
337 
342 class_alias( MutableRevisionRecord::class, 'MediaWiki\Storage\MutableRevisionRecord' );
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
Created by PhpStorm.
getSize()
Returns the nominal size of this revision.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
setSlot(SlotRecord $slot)
Sets the given slot.
setSize( $size)
Set nominal revision size, for optimization.
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
setComment(CommentStoreComment $comment)
getSlots()
Returns the slots defined for this revision as a MutableRevisionSlots instance, which can be modified...
getSha1()
Returns the base36 sha1 of this revision.
getRevision()
Returns the ID of the revision this slot is associated with.
Definition: SlotRecord.php:396
static newFromParentRevision(RevisionRecord $parent)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision, and inherits all slots form it.
setSha1( $sha1)
Set revision hash, for optimization.
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:271
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot&#39;s content.
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
getId()
Get revision ID.
setId( $id)
Set the revision ID.
hasRevision()
Whether this slot has revision ID associated.
Definition: SlotRecord.php:480
removeSlot( $role)
Removes the slot with the given role from this revision.
getWikiId()
Get the ID of the wiki this revision belongs to.
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
Page revision base class.
$content
Definition: router.php:78
Mutable version of RevisionSlots, for constructing a new revision.
__construct(Title $title, $dbDomain=false)
setContent( $role, Content $content)
Sets the content for the slot with the given role.
getPageId()
Get the page ID.