MediaWiki REL1_31
MutableRevisionRecord.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Storage;
24
26use Content;
27use InvalidArgumentException;
29use MWException;
30use Title;
31use Wikimedia\Assert\Assert;
32
40
53 public static function newFromParentRevision(
54 RevisionRecord $parent,
55 CommentStoreComment $comment,
56 UserIdentity $user,
57 $timestamp
58 ) {
59 // TODO: ideally, we wouldn't need a Title here
60 $title = Title::newFromLinkTarget( $parent->getPageAsLinkTarget() );
61 $rev = new MutableRevisionRecord( $title, $parent->getWikiId() );
62
63 $rev->setComment( $comment );
64 $rev->setUser( $user );
65 $rev->setTimestamp( $timestamp );
66
67 foreach ( $parent->getSlotRoles() as $role ) {
68 $slot = $parent->getSlot( $role, self::RAW );
69 $rev->inheritSlot( $slot );
70 }
71
72 $rev->setPageId( $parent->getPageId() );
73 $rev->setParentId( $parent->getId() );
74
75 return $rev;
76 }
77
88 function __construct( Title $title, $wikiId = false ) {
89 $slots = new MutableRevisionSlots();
90
91 parent::__construct( $title, $slots, $wikiId );
92
93 $this->mSlots = $slots; // redundant, but nice for static analysis
94 }
95
99 public function setParentId( $parentId ) {
100 Assert::parameterType( 'integer', $parentId, '$parentId' );
101
102 $this->mParentId = $parentId;
103 }
104
120 public function setSlot( SlotRecord $slot ) {
121 if ( $slot->hasRevision() && $slot->getRevision() !== $this->getId() ) {
122 throw new InvalidArgumentException(
123 'The given slot must be an unsaved, unattached one. '
124 . 'This slot is already attached to revision ' . $slot->getRevision() . '. '
125 . 'Use inheritSlot() instead to preserve a slot from a previous revision.'
126 );
127 }
128
129 $this->mSlots->setSlot( $slot );
130 $this->resetAggregateValues();
131 }
132
142 public function inheritSlot( SlotRecord $parentSlot ) {
143 $slot = SlotRecord::newInherited( $parentSlot );
144 $this->setSlot( $slot );
145 }
146
161 public function setContent( $role, Content $content ) {
162 $this->mSlots->setContent( $role, $content );
163 $this->resetAggregateValues();
164 }
165
178 public function removeSlot( $role ) {
179 $this->mSlots->removeSlot( $role );
180 $this->resetAggregateValues();
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 = wfTimestamp( 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
323 private function resetAggregateValues() {
324 $this->mSize = null;
325 $this->mSha1 = null;
326 }
327
328}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
CommentStoreComment represents a comment stored by CommentStore.
MediaWiki exception.
Mutable RevisionRecord implementation, for building new revision entries programmatically.
inheritSlot(SlotRecord $parentSlot)
"Inherits" the given slot's content.
getSha1()
Returns the base36 sha1 of this revision.
setUser(UserIdentity $user)
Sets the user identity associated with the revision.
getSize()
Returns the nominal size of this revision.
setContent( $role, Content $content)
Sets the content for the slot with the given role.
setSlot(SlotRecord $slot)
Sets the given slot.
setSize( $size)
Set nominal revision size, for optimization.
resetAggregateValues()
Invalidate cached aggregate values such as hash and size.
static newFromParentRevision(RevisionRecord $parent, CommentStoreComment $comment, UserIdentity $user, $timestamp)
Returns an incomplete MutableRevisionRecord which uses $parent as its parent revision,...
removeSlot( $role)
Removes the slot with the given role from this revision.
setSha1( $sha1)
Set revision hash, for optimization.
Mutable version of RevisionSlots, for constructing a new revision.
Page revision base class.
getSlot( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns meta-data for the given slot.
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
getWikiId()
Get the ID of the wiki this revision belongs to.
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.
static newInherited(SlotRecord $slot)
Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord of a p...
hasRevision()
Whether this slot has revision ID associated.
getRevision()
Returns the ID of the revision this slot is associated with.
Represents a title within MediaWiki.
Definition Title.php:39
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1777
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
Base interface for content objects.
Definition Content.php:34
Interface for objects representing user identity.