MediaWiki  master
RevisionSlotsUpdate.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Storage;
24 
25 use Content;
30 
37 
41  private $modifiedSlots = [];
42 
46  private $removedRoles = [];
47 
58  public static function newFromRevisionSlots(
59  RevisionSlots $newSlots,
60  RevisionSlots $parentSlots = null
61  ) {
62  $modified = $newSlots->getSlots();
63  $removed = [];
64 
65  if ( $parentSlots ) {
66  foreach ( $parentSlots->getSlots() as $role => $slot ) {
67  if ( !isset( $modified[$role] ) ) {
68  $removed[] = $role;
69  } elseif ( $slot->hasSameContent( $modified[$role] ) ) {
70  // Unset slots that had the same content in the parent revision from $modified.
71  unset( $modified[$role] );
72  }
73  }
74  }
75 
76  return new RevisionSlotsUpdate( $modified, $removed );
77  }
78 
93  public static function newFromContent( array $newContent, RevisionSlots $parentSlots = null ) {
94  $modified = [];
95 
96  foreach ( $newContent as $role => $content ) {
97  $slot = SlotRecord::newUnsaved( $role, $content );
98 
99  if ( $parentSlots
100  && $parentSlots->hasSlot( $role )
101  && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
102  ) {
103  // Skip slots that had the same content in the parent revision from $modified.
104  continue;
105  }
106 
107  $modified[$role] = $slot;
108  }
109 
110  return new RevisionSlotsUpdate( $modified );
111  }
112 
117  public function __construct( array $modifiedSlots = [], array $removedRoles = [] ) {
118  foreach ( $modifiedSlots as $slot ) {
119  $this->modifySlot( $slot );
120  }
121 
122  foreach ( $removedRoles as $role ) {
123  $this->removeSlot( $role );
124  }
125  }
126 
138  public function getModifiedRoles() {
139  return array_keys( $this->modifiedSlots );
140  }
141 
148  public function getRemovedRoles() {
149  return array_keys( $this->removedRoles );
150  }
151 
157  public function getTouchedRoles() {
158  return array_merge( $this->getModifiedRoles(), $this->getRemovedRoles() );
159  }
160 
170  public function modifySlot( SlotRecord $slot ) {
171  $role = $slot->getRole();
172 
173  // XXX: We should perhaps require this to be an unsaved slot!
174  unset( $this->removedRoles[$role] );
175  $this->modifiedSlots[$role] = $slot;
176  }
177 
185  public function modifyContent( $role, Content $content ) {
186  $slot = SlotRecord::newUnsaved( $role, $content );
187  $this->modifySlot( $slot );
188  }
189 
198  public function removeSlot( $role ) {
199  unset( $this->modifiedSlots[$role] );
200  $this->removedRoles[$role] = true;
201  }
202 
218  public function getModifiedSlot( $role ) {
219  if ( isset( $this->modifiedSlots[$role] ) ) {
220  return $this->modifiedSlots[$role];
221  } else {
222  throw new RevisionAccessException( 'No such slot: ' . $role );
223  }
224  }
225 
235  public function isModifiedSlot( $role ) {
236  return isset( $this->modifiedSlots[$role] );
237  }
238 
248  public function isRemovedSlot( $role ) {
249  return isset( $this->removedRoles[$role] );
250  }
251 
262  public function hasSameUpdates( RevisionSlotsUpdate $other ) {
263  // NOTE: use != not !==, since the order of entries is not significant!
264 
265  if ( $this->getModifiedRoles() != $other->getModifiedRoles() ) {
266  return false;
267  }
268 
269  if ( $this->getRemovedRoles() != $other->getRemovedRoles() ) {
270  return false;
271  }
272 
273  foreach ( $this->getModifiedRoles() as $role ) {
274  $s = $this->getModifiedSlot( $role );
275  $t = $other->getModifiedSlot( $role );
276 
277  if ( !$s->hasSameContent( $t ) ) {
278  return false;
279  }
280  }
281 
282  return true;
283  }
284 
291  public function apply( MutableRevisionSlots $slots ) {
292  foreach ( $this->getModifiedRoles() as $role ) {
293  $slots->setSlot( $this->getModifiedSlot( $role ) );
294  }
295 
296  foreach ( $this->getRemovedRoles() as $role ) {
297  $slots->removeSlot( $role );
298  }
299  }
300 
301 }
MediaWiki\Storage\RevisionSlotsUpdate\getModifiedRoles
getModifiedRoles()
Returns a list of modified slot roles, that is, roles modified by calling modifySlot(),...
Definition: RevisionSlotsUpdate.php:138
MediaWiki\Storage\RevisionSlotsUpdate\getModifiedSlot
getModifiedSlot( $role)
Returns the SlotRecord associated with the given role, if the slot with that role was modified (and n...
Definition: RevisionSlotsUpdate.php:218
MediaWiki\Storage\RevisionSlotsUpdate\isModifiedSlot
isModifiedSlot( $role)
Returns whether getModifiedSlot() will return a SlotRecord for the given role.
Definition: RevisionSlotsUpdate.php:235
Revision\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:33
MediaWiki\Storage\RevisionSlotsUpdate\$modifiedSlots
SlotRecord[] $modifiedSlots
modified slots, using the slot role as the key.
Definition: RevisionSlotsUpdate.php:41
MediaWiki\Storage\RevisionSlotsUpdate\isRemovedSlot
isRemovedSlot( $role)
Returns whether the given role is to be removed from the page.
Definition: RevisionSlotsUpdate.php:248
$s
$s
Definition: mergeMessageFileList.php:185
Revision\MutableRevisionSlots
Mutable version of RevisionSlots, for constructing a new revision.
Definition: MutableRevisionSlots.php:33
MediaWiki\Storage\RevisionSlotsUpdate\$removedRoles
bool[] $removedRoles
removed roles, stored in the keys of the array.
Definition: RevisionSlotsUpdate.php:46
MediaWiki\Storage\RevisionSlotsUpdate\removeSlot
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Definition: RevisionSlotsUpdate.php:198
MediaWiki\Storage\RevisionSlotsUpdate\modifyContent
modifyContent( $role, Content $content)
Sets the content for the slot with the given role to be modified.
Definition: RevisionSlotsUpdate.php:185
Revision\MutableRevisionSlots\setSlot
setSlot(SlotRecord $slot)
Sets the given slot.
Definition: MutableRevisionSlots.php:66
Revision\SlotRecord\getRole
getRole()
Returns the role of the slot.
Definition: SlotRecord.php:489
MediaWiki\Storage\RevisionSlotsUpdate\hasSameUpdates
hasSameUpdates(RevisionSlotsUpdate $other)
Returns true if $other represents the same update - that is, if all methods defined by RevisionSlotsU...
Definition: RevisionSlotsUpdate.php:262
MediaWiki\Storage\RevisionSlotsUpdate\modifySlot
modifySlot(SlotRecord $slot)
Sets the given slot to be modified.
Definition: RevisionSlotsUpdate.php:170
MediaWiki\Storage\RevisionSlotsUpdate\__construct
__construct(array $modifiedSlots=[], array $removedRoles=[])
Definition: RevisionSlotsUpdate.php:117
Revision\SlotRecord\newUnsaved
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:129
MediaWiki\Storage\RevisionSlotsUpdate
Value object representing a modification of revision slots.
Definition: RevisionSlotsUpdate.php:36
$content
$content
Definition: router.php:78
MediaWiki\Storage\RevisionSlotsUpdate\getTouchedRoles
getTouchedRoles()
Returns a list of all slot roles that modified or removed.
Definition: RevisionSlotsUpdate.php:157
Revision\RevisionSlots\getSlots
getSlots()
Returns an associative array that maps role names to SlotRecords.
Definition: RevisionSlots.php:169
MediaWiki\Storage
Definition: BlobAccessException.php:23
MediaWiki\Storage\RevisionSlotsUpdate\apply
apply(MutableRevisionSlots $slots)
Applies this update to the given MutableRevisionSlots, setting all modified slots,...
Definition: RevisionSlotsUpdate.php:291
Content
Base interface for content objects.
Definition: Content.php:34
MediaWiki\Storage\RevisionSlotsUpdate\newFromRevisionSlots
static newFromRevisionSlots(RevisionSlots $newSlots, RevisionSlots $parentSlots=null)
Constructs a RevisionSlotsUpdate representing the update that turned $parentSlots into $newSlots.
Definition: RevisionSlotsUpdate.php:58
MediaWiki\Storage\RevisionSlotsUpdate\getRemovedRoles
getRemovedRoles()
Returns a list of removed slot roles, that is, roles removed by calling removeSlot(),...
Definition: RevisionSlotsUpdate.php:148
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:39
$t
$t
Definition: testCompression.php:71
Revision\MutableRevisionSlots\removeSlot
removeSlot( $role)
Remove the slot for the given role, discontinue the corresponding stream.
Definition: MutableRevisionSlots.php:102
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
MediaWiki\Storage\RevisionSlotsUpdate\newFromContent
static newFromContent(array $newContent, RevisionSlots $parentSlots=null)
Constructs a RevisionSlotsUpdate representing the update of $parentSlots when changing $newContent.
Definition: RevisionSlotsUpdate.php:93