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 
44 
54  public static function newFromParentRevision( RevisionRecord $parent ) {
55  // TODO: ideally, we wouldn't need a Title here
57  $rev = new MutableRevisionRecord( $title, $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 
79  public function __construct( Title $title, $dbDomain = false ) {
80  $slots = new MutableRevisionSlots( [], function () {
81  $this->resetAggregateValues();
82  } );
83 
84  parent::__construct( $title, $slots, $dbDomain );
85  }
86 
90  public function setParentId( $parentId ) {
91  Assert::parameterType( 'integer', $parentId, '$parentId' );
92 
93  $this->mParentId = $parentId;
94  }
95 
111  public function setSlot( SlotRecord $slot ) {
112  if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
113  throw new InvalidArgumentException(
114  'The given slot must be an unsaved, unattached one. '
115  . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
116  . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
117  );
118  }
119 
120  $this->mSlots->setSlot( $slot );
121  }
122 
132  public function inheritSlot( SlotRecord $parentSlot ) {
133  $this->mSlots->inheritSlot( $parentSlot );
134  }
135 
150  public function setContent( $role, Content $content ) {
151  $this->mSlots->setContent( $role, $content );
152  }
153 
166  public function removeSlot( $role ) {
167  $this->mSlots->removeSlot( $role );
168  }
169 
175  public function applyUpdate( RevisionSlotsUpdate $update ) {
176  $update->apply( $this->mSlots );
177  }
178 
182  public function setComment( CommentStoreComment $comment ) {
183  $this->mComment = $comment;
184  }
185 
195  public function setSha1( $sha1 ) {
196  Assert::parameterType( 'string', $sha1, '$sha1' );
197 
198  $this->mSha1 = $sha1;
199  }
200 
210  public function setSize( $size ) {
211  Assert::parameterType( 'integer', $size, '$size' );
212 
213  $this->mSize = $size;
214  }
215 
219  public function setVisibility( $visibility ) {
220  Assert::parameterType( 'integer', $visibility, '$visibility' );
221 
222  $this->mDeleted = $visibility;
223  }
224 
228  public function setTimestamp( $timestamp ) {
229  Assert::parameterType( 'string', $timestamp, '$timestamp' );
230 
231  $this->mTimestamp = MWTimestamp::convert( TS_MW, $timestamp );
232  }
233 
237  public function setMinorEdit( $minorEdit ) {
238  Assert::parameterType( 'boolean', $minorEdit, '$minorEdit' );
239 
240  $this->mMinorEdit = $minorEdit;
241  }
242 
254  public function setId( $id ) {
255  Assert::parameterType( 'integer', $id, '$id' );
256 
257  $this->mId = $id;
258  }
259 
265  public function setUser( UserIdentity $user ) {
266  $this->mUser = $user;
267  }
268 
272  public function setPageId( $pageId ) {
273  Assert::parameterType( 'integer', $pageId, '$pageId' );
274 
275  if ( $this->mTitle->exists() && $pageId !== $this->mTitle->getArticleID() ) {
276  throw new InvalidArgumentException(
277  'The given Title does not belong to page ID ' . $this->mPageId
278  );
279  }
280 
281  $this->mPageId = $pageId;
282  }
283 
291  public function getSize() {
292  // If not known, re-calculate and remember. Will be reset when slots change.
293  if ( $this->mSize === null ) {
294  $this->mSize = $this->mSlots->computeSize();
295  }
296 
297  return $this->mSize;
298  }
299 
307  public function getSha1() {
308  // If not known, re-calculate and remember. Will be reset when slots change.
309  if ( $this->mSha1 === null ) {
310  $this->mSha1 = $this->mSlots->computeSha1();
311  }
312 
313  return $this->mSha1;
314  }
315 
322  public function getSlots() {
323  // Overwritten just guarantee the more narrow return type.
324  return parent::getSlots();
325  }
326 
331  private function resetAggregateValues() {
332  $this->mSize = null;
333  $this->mSha1 = null;
334  }
335 
336 }
337 
342 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:322
Revision\MutableRevisionRecord\setMinorEdit
setMinorEdit( $minorEdit)
Definition: MutableRevisionRecord.php:237
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:32
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Revision\MutableRevisionRecord\setSha1
setSha1( $sha1)
Set revision hash, for optimization.
Definition: MutableRevisionRecord.php:195
Revision\MutableRevisionRecord\setParentId
setParentId( $parentId)
Definition: MutableRevisionRecord.php:90
Revision\MutableRevisionRecord\inheritSlot
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
Definition: MutableRevisionRecord.php:132
Revision\MutableRevisionRecord\newFromParentRevision
static newFromParentRevision(RevisionRecord $parent)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision,...
Definition: MutableRevisionRecord.php:54
Revision\MutableRevisionRecord\setPageId
setPageId( $pageId)
Definition: MutableRevisionRecord.php:272
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:254
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:166
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\Revision
Definition: FallbackSlotRoleHandler.php:23
Revision\MutableRevisionRecord\__construct
__construct(Title $title, $dbDomain=false)
Definition: MutableRevisionRecord.php:79
MWException
MediaWiki exception.
Definition: MWException.php:26
Revision\MutableRevisionRecord\setUser
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
Definition: MutableRevisionRecord.php:265
Revision\MutableRevisionRecord\applyUpdate
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
Definition: MutableRevisionRecord.php:175
Revision\MutableRevisionRecord\setVisibility
setVisibility( $visibility)
Definition: MutableRevisionRecord.php:219
Revision\MutableRevisionRecord\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionRecord.php:150
$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:43
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:284
Revision\MutableRevisionRecord\resetAggregateValues
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
Definition: MutableRevisionRecord.php:331
Content
Base interface for content objects.
Definition: Content.php:34
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:111
Revision\MutableRevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Definition: MutableRevisionRecord.php:307
Revision\MutableRevisionRecord\setTimestamp
setTimestamp( $timestamp)
Definition: MutableRevisionRecord.php:228
Revision\MutableRevisionRecord\setSize
setSize( $size)
Set nominal revision size, for optimization.
Definition: MutableRevisionRecord.php:210
Revision\MutableRevisionRecord\getSize
getSize()
Returns the nominal size of this revision.
Definition: MutableRevisionRecord.php:291
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
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:29
Revision\MutableRevisionRecord\setComment
setComment(CommentStoreComment $comment)
Definition: MutableRevisionRecord.php:182
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39