MediaWiki REL1_34
MutableRevisionRecord.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
26use Content;
27use InvalidArgumentException;
30use MWException;
31use Title;
32use Wikimedia\Assert\Assert;
33
43
53 public static function newFromParentRevision( RevisionRecord $parent ) {
54 // TODO: ideally, we wouldn't need a Title here
55 $title = Title::newFromLinkTarget( $parent->getPageAsLinkTarget() );
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
342class_alias( MutableRevisionRecord::class, 'MediaWiki\Storage\MutableRevisionRecord' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
CommentStoreComment represents a comment stored by CommentStore.
MediaWiki exception.
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:34
Interface for objects representing user identity.
$content
Definition router.php:78