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 Title;
32 use Wikimedia\Assert\Assert;
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  public 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' );
Revision\MutableRevisionRecord\getSlots
getSlots()
Returns the slots defined for this revision as a MutableRevisionSlots instance, which can be modified...
Definition: MutableRevisionRecord.php:323
Revision\MutableRevisionRecord\setMinorEdit
setMinorEdit( $minorEdit)
Definition: MutableRevisionRecord.php:238
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
Revision\MutableRevisionRecord\setSha1
setSha1( $sha1)
Set revision hash, for optimization.
Definition: MutableRevisionRecord.php:196
Revision\MutableRevisionRecord\setParentId
setParentId( $parentId)
Definition: MutableRevisionRecord.php:87
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1871
Revision\MutableRevisionRecord\inheritSlot
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
Definition: MutableRevisionRecord.php:130
Revision\MutableRevisionRecord\newFromParentRevision
static newFromParentRevision(RevisionRecord $parent)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision,...
Definition: MutableRevisionRecord.php:53
Revision\MutableRevisionRecord\setPageId
setPageId( $pageId)
Definition: MutableRevisionRecord.php:273
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:255
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
Created by PhpStorm.
Definition: FallbackSlotRoleHandler.php:23
Revision\MutableRevisionRecord\__construct
__construct(Title $title, $dbDomain=false)
Definition: MutableRevisionRecord.php:78
MWException
MediaWiki exception.
Definition: MWException.php:26
Revision\MutableRevisionRecord\setUser
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
Definition: MutableRevisionRecord.php:266
Revision\MutableRevisionRecord\applyUpdate
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
Definition: MutableRevisionRecord.php:176
Revision\MutableRevisionRecord\setVisibility
setVisibility( $visibility)
Definition: MutableRevisionRecord.php:220
Revision\MutableRevisionRecord\setContent
setContent( $role, Content $content)
Sets the content for the slot with the given role.
Definition: MutableRevisionRecord.php:149
$title
$title
Definition: testCompression.php:36
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:78
Revision\MutableRevisionRecord
Definition: MutableRevisionRecord.php:42
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:269
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:108
Revision\MutableRevisionRecord\getSha1
getSha1()
Returns the base36 sha1 of this revision.
Definition: MutableRevisionRecord.php:308
Revision\MutableRevisionRecord\setTimestamp
setTimestamp( $timestamp)
Definition: MutableRevisionRecord.php:229
Revision\MutableRevisionRecord\setSize
setSize( $size)
Set nominal revision size, for optimization.
Definition: MutableRevisionRecord.php:211
Revision\MutableRevisionRecord\getSize
getSize()
Returns the nominal size of this revision.
Definition: MutableRevisionRecord.php:292
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:183
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39