MediaWiki REL1_35
MutableRevisionRecord.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
26use Content;
27use InvalidArgumentException;
30use MWException;
31use MWTimestamp;
32use Title;
33use Wikimedia\Assert\Assert;
34
46
56 public static function newFromParentRevision( RevisionRecord $parent ) {
57 // TODO: ideally, we wouldn't need a Title here
58 $title = Title::newFromLinkTarget( $parent->getPageAsLinkTarget() );
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
346class_alias( MutableRevisionRecord::class, 'MediaWiki\Storage\MutableRevisionRecord' );
CommentStoreComment represents a comment stored by CommentStore.
MediaWiki exception.
Library for creating and parsing MW-style timestamps.
setSlot(SlotRecord $slot)
Sets the given slot.
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
removeSlot( $role)
Removes the slot with the given role from this revision.
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
applyUpdate(RevisionSlotsUpdate $update)
Applies the given update to the slots of this revision.
setSize( $size)
Set nominal revision size, for optimization.
getSha1()
Returns the base36 sha1 of this revision.
setContent( $role, Content $content)
Sets the content for the slot with the given role.
setSha1( $sha1)
Set revision hash, for optimization.
getSize()
Returns the nominal size of this revision.
static newFromParentRevision(RevisionRecord $parent)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision,...
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
getSlots()
Returns the slots defined for this revision as a MutableRevisionSlots instance, which can be modified...
Mutable version of RevisionSlots, for constructing a new revision.
Page revision base class.
getWikiId()
Get the ID of the wiki this revision belongs to.
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
Value object representing a content slot associated with a page revision.
hasRevision()
Whether this slot has revision ID associated.
getRevision()
Returns the ID of the revision this slot is associated with.
Value object representing a modification of revision slots.
apply(MutableRevisionSlots $slots)
Applies this update to the given MutableRevisionSlots, setting all modified slots,...
Represents a title within MediaWiki.
Definition Title.php:42
Base interface for content objects.
Definition Content.php:35
Interface for objects representing user identity.
$content
Definition router.php:76