MediaWiki master
RevisionSlotsUpdate.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Storage;
22
28
35
39 private $modifiedSlots = [];
40
44 private $removedRoles = [];
45
56 public static function newFromRevisionSlots(
57 RevisionSlots $newSlots,
58 RevisionSlots $parentSlots = null
59 ) {
60 $modified = $newSlots->getSlots();
61 $removed = [];
62
63 if ( $parentSlots ) {
64 foreach ( $parentSlots->getSlots() as $role => $slot ) {
65 if ( !isset( $modified[$role] ) ) {
66 $removed[] = $role;
67 } elseif ( $slot->hasSameContent( $modified[$role] ) ) {
68 // Unset slots that had the same content in the parent revision from $modified.
69 unset( $modified[$role] );
70 }
71 }
72 }
73
74 return new RevisionSlotsUpdate( $modified, $removed );
75 }
76
91 public static function newFromContent( array $newContent, RevisionSlots $parentSlots = null ) {
92 $modified = [];
93
94 foreach ( $newContent as $role => $content ) {
95 $slot = SlotRecord::newUnsaved( $role, $content );
96
97 if ( $parentSlots
98 && $parentSlots->hasSlot( $role )
99 && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
100 ) {
101 // Skip slots that had the same content in the parent revision from $modified.
102 continue;
103 }
104
105 $modified[$role] = $slot;
106 }
107
108 return new RevisionSlotsUpdate( $modified );
109 }
110
115 public function __construct( array $modifiedSlots = [], array $removedRoles = [] ) {
116 foreach ( $modifiedSlots as $slot ) {
117 $this->modifySlot( $slot );
118 }
119
120 foreach ( $removedRoles as $role ) {
121 $this->removeSlot( $role );
122 }
123 }
124
136 public function getModifiedRoles() {
137 return array_keys( $this->modifiedSlots );
138 }
139
146 public function getRemovedRoles() {
147 return array_keys( $this->removedRoles );
148 }
149
155 public function getTouchedRoles() {
156 return array_merge( $this->getModifiedRoles(), $this->getRemovedRoles() );
157 }
158
168 public function modifySlot( SlotRecord $slot ) {
169 $role = $slot->getRole();
170
171 // XXX: We should perhaps require this to be an unsaved slot!
172 unset( $this->removedRoles[$role] );
173 $this->modifiedSlots[$role] = $slot;
174 }
175
183 public function modifyContent( $role, Content $content ) {
184 $slot = SlotRecord::newUnsaved( $role, $content );
185 $this->modifySlot( $slot );
186 }
187
196 public function removeSlot( $role ) {
197 unset( $this->modifiedSlots[$role] );
198 $this->removedRoles[$role] = true;
199 }
200
216 public function getModifiedSlot( $role ) {
217 if ( isset( $this->modifiedSlots[$role] ) ) {
218 return $this->modifiedSlots[$role];
219 } else {
220 throw new RevisionAccessException(
221 'No such slot: {role}',
222 [ 'role' => $role ]
223 );
224 }
225 }
226
236 public function isModifiedSlot( $role ) {
237 return isset( $this->modifiedSlots[$role] );
238 }
239
249 public function isRemovedSlot( $role ) {
250 return isset( $this->removedRoles[$role] );
251 }
252
263 public function hasSameUpdates( RevisionSlotsUpdate $other ) {
264 // NOTE: use != not !==, since the order of entries is not significant!
265
266 if ( $this->getModifiedRoles() != $other->getModifiedRoles() ) {
267 return false;
268 }
269
270 if ( $this->getRemovedRoles() != $other->getRemovedRoles() ) {
271 return false;
272 }
273
274 foreach ( $this->getModifiedRoles() as $role ) {
275 $s = $this->getModifiedSlot( $role );
276 $t = $other->getModifiedSlot( $role );
277
278 if ( !$s->hasSameContent( $t ) ) {
279 return false;
280 }
281 }
282
283 return true;
284 }
285
292 public function apply( MutableRevisionSlots $slots ) {
293 foreach ( $this->getModifiedRoles() as $role ) {
294 $slots->setSlot( $this->getModifiedSlot( $role ) );
295 }
296
297 foreach ( $this->getRemovedRoles() as $role ) {
298 $slots->removeSlot( $role );
299 }
300 }
301
302}
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.
Base interface for representing page content.
Definition Content.php:39