3 * Value object representing a modification of revision slots.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Storage
;
28 * Value object representing a modification of revision slots.
32 class RevisionSlotsUpdate
{
35 * @var SlotRecord[] modified slots, using the slot role as the key.
37 private $modifiedSlots = [];
40 * @var bool[] removed roles, stored in the keys of the array.
42 private $removedRoles = [];
45 * Constructs a RevisionSlotsUpdate representing the update that turned $parentSlots
46 * into $newSlots. If $parentSlots is not given, $newSlots is assumed to come from a
47 * page's first revision.
49 * @param RevisionSlots $newSlots
50 * @param RevisionSlots|null $parentSlots
52 * @return RevisionSlotsUpdate
54 public static function newFromRevisionSlots(
55 RevisionSlots
$newSlots,
56 RevisionSlots
$parentSlots = null
58 $modified = $newSlots->getSlots();
62 foreach ( $parentSlots->getSlots() as $role => $slot ) {
63 if ( !isset( $modified[$role] ) ) {
65 } elseif ( $slot->hasSameContent( $modified[$role] ) ) {
66 // Unset slots that had the same content in the parent revision from $modified.
67 unset( $modified[$role] );
72 return new RevisionSlotsUpdate( $modified, $removed );
76 * Constructs a RevisionSlotsUpdate representing the update of $parentSlots
77 * when changing $newContent. If a slot has the same content in $newContent
78 * as in $parentSlots, that slot is considered inherited and thus omitted from
79 * the resulting RevisionSlotsUpdate.
81 * In contrast to newFromRevisionSlots(), slots in $parentSlots that are not present
82 * in $newContent are not considered removed. They are instead assumed to be inherited.
84 * @param Content[] $newContent The new content, using slot roles as array keys.
86 * @return RevisionSlotsUpdate
88 public static function newFromContent( array $newContent, RevisionSlots
$parentSlots = null ) {
91 foreach ( $newContent as $role => $content ) {
92 $slot = SlotRecord
::newUnsaved( $role, $content );
95 && $parentSlots->hasSlot( $role )
96 && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
98 // Skip slots that had the same content in the parent revision from $modified.
102 $modified[$role] = $slot;
105 return new RevisionSlotsUpdate( $modified );
109 * @param SlotRecord[] $modifiedSlots
110 * @param string[] $removedRoles
112 public function __construct( array $modifiedSlots = [], array $removedRoles = [] ) {
113 foreach ( $modifiedSlots as $slot ) {
114 $this->modifySlot( $slot );
117 foreach ( $removedRoles as $role ) {
118 $this->removeSlot( $role );
123 * Returns a list of modified slot roles, that is, roles modified by calling modifySlot(),
124 * and not later removed by calling removeSlot().
126 * Note that slots in modified roles may still be inherited slots. This is for instance
127 * the case when the RevisionSlotsUpdate objects represents some kind of rollback
128 * operation, in which slots that existed in an earlier revision are restored in
133 public function getModifiedRoles() {
134 return array_keys( $this->modifiedSlots
);
138 * Returns a list of removed slot roles, that is, roles removed by calling removeSlot(),
139 * and not later re-introduced by calling modifySlot().
143 public function getRemovedRoles() {
144 return array_keys( $this->removedRoles
);
148 * Returns a list of all slot roles that modified or removed.
152 public function getTouchedRoles() {
153 return array_merge( $this->getModifiedRoles(), $this->getRemovedRoles() );
157 * Sets the given slot to be modified.
158 * If a slot with the same role is already present, it is replaced.
160 * The roles used with modifySlot() will be returned from getModifiedRoles(),
161 * unless overwritten with removeSlot().
163 * @param SlotRecord $slot
165 public function modifySlot( SlotRecord
$slot ) {
166 $role = $slot->getRole();
168 // XXX: We should perhaps require this to be an unsaved slot!
169 unset( $this->removedRoles
[$role] );
170 $this->modifiedSlots
[$role] = $slot;
174 * Sets the content for the slot with the given role to be modified.
175 * If a slot with the same role is already present, it is replaced.
177 * @param string $role
178 * @param Content $content
180 public function modifyContent( $role, Content
$content ) {
181 $slot = SlotRecord
::newUnsaved( $role, $content );
182 $this->modifySlot( $slot );
186 * Remove the slot for the given role, discontinue the corresponding stream.
188 * The roles used with removeSlot() will be returned from getRemovedSlots(),
189 * unless overwritten with modifySlot().
191 * @param string $role
193 public function removeSlot( $role ) {
194 unset( $this->modifiedSlots
[$role] );
195 $this->removedRoles
[$role] = true;
199 * Returns the SlotRecord associated with the given role, if the slot with that role
200 * was modified (and not again removed).
202 * @note If the SlotRecord returned by this method returns a non-inherited slot,
203 * the content of that slot may or may not already have PST applied. Methods
204 * that take a RevisionSlotsUpdate as a parameter should specify whether they
205 * expect PST to already have been applied to all slots. Inherited slots
206 * should never have PST applied again.
208 * @param string $role The role name of the desired slot
210 * @throws RevisionAccessException if the slot does not exist or was removed.
213 public function getModifiedSlot( $role ) {
214 if ( isset( $this->modifiedSlots
[$role] ) ) {
215 return $this->modifiedSlots
[$role];
217 throw new RevisionAccessException( 'No such slot: ' . $role );
222 * Returns whether getModifiedSlot() will return a SlotRecord for the given role.
224 * Will return true for the role names returned by getModifiedRoles(), false otherwise.
226 * @param string $role The role name of the desired slot
230 public function isModifiedSlot( $role ) {
231 return isset( $this->modifiedSlots
[$role] );
235 * Returns whether the given role is to be removed from the page.
237 * Will return true for the role names returned by getRemovedRoles(), false otherwise.
239 * @param string $role The role name of the desired slot
243 public function isRemovedSlot( $role ) {
244 return isset( $this->removedRoles
[$role] );
248 * Returns true if $other represents the same update - that is,
249 * if all methods defined by RevisionSlotsUpdate when called on $this or $other
250 * will yield the same result when called with the same parameters.
252 * SlotRecords for the same role are compared based on their model and content.
254 * @param RevisionSlotsUpdate $other
257 public function hasSameUpdates( RevisionSlotsUpdate
$other ) {
258 // NOTE: use != not !==, since the order of entries is not significant!
260 if ( $this->getModifiedRoles() != $other->getModifiedRoles() ) {
264 if ( $this->getRemovedRoles() != $other->getRemovedRoles() ) {
268 foreach ( $this->getModifiedRoles() as $role ) {
269 $s = $this->getModifiedSlot( $role );
270 $t = $other->getModifiedSlot( $role );
272 if ( !$s->hasSameContent( $t ) ) {
281 * Applies this update to the given MutableRevisionSlots, setting all modified slots,
282 * and removing all removed roles.
284 * @param MutableRevisionSlots $slots
286 public function apply( MutableRevisionSlots
$slots ) {
287 foreach ( $this->getModifiedRoles() as $role ) {
288 $slots->setSlot( $this->getModifiedSlot( $role ) );
291 foreach ( $this->getRemovedRoles() as $role ) {
292 $slots->removeSlot( $role );