MediaWiki master
RevisionSlotsUpdate.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Storage;
8
14
21
25 private $modifiedSlots = [];
26
30 private $removedRoles = [];
31
42 public static function newFromRevisionSlots(
43 RevisionSlots $newSlots,
44 ?RevisionSlots $parentSlots = null
45 ) {
46 $modified = $newSlots->getSlots();
47 $removed = [];
48
49 if ( $parentSlots ) {
50 foreach ( $parentSlots->getSlots() as $role => $slot ) {
51 if ( !isset( $modified[$role] ) ) {
52 $removed[] = $role;
53 } elseif ( $slot->hasSameContent( $modified[$role] ) ) {
54 // Unset slots that had the same content in the parent revision from $modified.
55 unset( $modified[$role] );
56 }
57 }
58 }
59
60 return new RevisionSlotsUpdate( $modified, $removed );
61 }
62
77 public static function newFromContent( array $newContent, ?RevisionSlots $parentSlots = null ) {
78 $modified = [];
79
80 foreach ( $newContent as $role => $content ) {
81 $slot = SlotRecord::newUnsaved( $role, $content );
82
83 if ( $parentSlots
84 && $parentSlots->hasSlot( $role )
85 && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
86 ) {
87 // Skip slots that had the same content in the parent revision from $modified.
88 continue;
89 }
90
91 $modified[$role] = $slot;
92 }
93
94 return new RevisionSlotsUpdate( $modified );
95 }
96
101 public function __construct( array $modifiedSlots = [], array $removedRoles = [] ) {
102 foreach ( $modifiedSlots as $slot ) {
103 $this->modifySlot( $slot );
104 }
105
106 foreach ( $removedRoles as $role ) {
107 $this->removeSlot( $role );
108 }
109 }
110
122 public function getModifiedRoles() {
123 return array_keys( $this->modifiedSlots );
124 }
125
132 public function getRemovedRoles() {
133 return array_keys( $this->removedRoles );
134 }
135
141 public function getTouchedRoles() {
142 return array_merge( $this->getModifiedRoles(), $this->getRemovedRoles() );
143 }
144
152 public function modifySlot( SlotRecord $slot ) {
153 $role = $slot->getRole();
154
155 // XXX: We should perhaps require this to be an unsaved slot!
156 unset( $this->removedRoles[$role] );
157 $this->modifiedSlots[$role] = $slot;
158 }
159
167 public function modifyContent( $role, Content $content ) {
168 $slot = SlotRecord::newUnsaved( $role, $content );
169 $this->modifySlot( $slot );
170 }
171
180 public function removeSlot( $role ) {
181 unset( $this->modifiedSlots[$role] );
182 $this->removedRoles[$role] = true;
183 }
184
200 public function getModifiedSlot( $role ) {
201 if ( isset( $this->modifiedSlots[$role] ) ) {
202 return $this->modifiedSlots[$role];
203 } else {
204 throw new RevisionAccessException(
205 'No such slot: {role}',
206 [ 'role' => $role ]
207 );
208 }
209 }
210
220 public function isModifiedSlot( $role ) {
221 return isset( $this->modifiedSlots[$role] );
222 }
223
233 public function isRemovedSlot( $role ) {
234 return isset( $this->removedRoles[$role] );
235 }
236
247 public function hasSameUpdates( RevisionSlotsUpdate $other ) {
248 // NOTE: use != not !==, since the order of entries is not significant!
249
250 if ( $this->getModifiedRoles() != $other->getModifiedRoles() ) {
251 return false;
252 }
253
254 if ( $this->getRemovedRoles() != $other->getRemovedRoles() ) {
255 return false;
256 }
257
258 foreach ( $this->getModifiedRoles() as $role ) {
259 $s = $this->getModifiedSlot( $role );
260 $t = $other->getModifiedSlot( $role );
261
262 if ( !$s->hasSameContent( $t ) ) {
263 return false;
264 }
265 }
266
267 return true;
268 }
269
274 public function apply( MutableRevisionSlots $slots ) {
275 foreach ( $this->getModifiedRoles() as $role ) {
276 $slots->setSlot( $this->getModifiedSlot( $role ) );
277 }
278
279 foreach ( $this->getRemovedRoles() as $role ) {
280 $slots->removeSlot( $role );
281 }
282 }
283
284}
Mutable version of RevisionSlots, for constructing a new revision.
setSlot(SlotRecord $slot)
Sets the given slot.
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Exception representing a failure to look up a revision.
Value object representing the set of slots belonging to a revision.
getSlots()
Returns an associative array that maps role names to SlotRecords.
Value object representing a content slot associated with a page revision.
getRole()
Returns the role of the slot.
Value object representing a modification of revision slots.
getRemovedRoles()
Returns a list of removed slot roles, that is, roles removed by calling removeSlot(),...
static newFromContent(array $newContent, ?RevisionSlots $parentSlots=null)
Constructs a RevisionSlotsUpdate representing the update of $parentSlots when changing $newContent.
hasSameUpdates(RevisionSlotsUpdate $other)
Returns true if $other represents the same update - that is, if all methods defined by RevisionSlotsU...
isRemovedSlot( $role)
Returns whether the given role is to be removed from the page.
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
getModifiedRoles()
Returns a list of modified slot roles, that is, roles modified by calling modifySlot(),...
__construct(array $modifiedSlots=[], array $removedRoles=[])
modifyContent( $role, Content $content)
Sets the content for the slot with the given role to be modified.
getModifiedSlot( $role)
Returns the SlotRecord associated with the given role, if the slot with that role was modified (and n...
apply(MutableRevisionSlots $slots)
Applies this update to the given MutableRevisionSlots, setting all modified slots,...
static newFromRevisionSlots(RevisionSlots $newSlots, ?RevisionSlots $parentSlots=null)
Constructs a RevisionSlotsUpdate representing the update that turned $parentSlots into $newSlots.
modifySlot(SlotRecord $slot)
Sets the given slot to be modified.
isModifiedSlot( $role)
Returns whether getModifiedSlot() will return a SlotRecord for the given role.
getTouchedRoles()
Returns a list of all slot roles that modified or removed.
Content objects represent page content, e.g.
Definition Content.php:28