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
;
26 use MediaWiki\Revision\MutableRevisionSlots
;
27 use MediaWiki\Revision\RevisionAccessException
;
28 use MediaWiki\Revision\RevisionSlots
;
29 use MediaWiki\Revision\SlotRecord
;
32 * Value object representing a modification of revision slots.
36 class RevisionSlotsUpdate
{
39 * @var SlotRecord[] modified slots, using the slot role as the key.
41 private $modifiedSlots = [];
44 * @var bool[] removed roles, stored in the keys of the array.
46 private $removedRoles = [];
49 * Constructs a RevisionSlotsUpdate representing the update that turned $parentSlots
50 * into $newSlots. If $parentSlots is not given, $newSlots is assumed to come from a
51 * page's first revision.
53 * @param RevisionSlots $newSlots
54 * @param RevisionSlots|null $parentSlots
56 * @return RevisionSlotsUpdate
58 public static function newFromRevisionSlots(
59 RevisionSlots
$newSlots,
60 RevisionSlots
$parentSlots = null
62 $modified = $newSlots->getSlots();
66 foreach ( $parentSlots->getSlots() as $role => $slot ) {
67 if ( !isset( $modified[$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] );
76 return new RevisionSlotsUpdate( $modified, $removed );
80 * Constructs a RevisionSlotsUpdate representing the update of $parentSlots
81 * when changing $newContent. If a slot has the same content in $newContent
82 * as in $parentSlots, that slot is considered inherited and thus omitted from
83 * the resulting RevisionSlotsUpdate.
85 * In contrast to newFromRevisionSlots(), slots in $parentSlots that are not present
86 * in $newContent are not considered removed. They are instead assumed to be inherited.
88 * @param Content[] $newContent The new content, using slot roles as array keys.
90 * @return RevisionSlotsUpdate
92 public static function newFromContent( array $newContent, RevisionSlots
$parentSlots = null ) {
95 foreach ( $newContent as $role => $content ) {
96 $slot = SlotRecord
::newUnsaved( $role, $content );
99 && $parentSlots->hasSlot( $role )
100 && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
102 // Skip slots that had the same content in the parent revision from $modified.
106 $modified[$role] = $slot;
109 return new RevisionSlotsUpdate( $modified );
113 * @param SlotRecord[] $modifiedSlots
114 * @param string[] $removedRoles
116 public function __construct( array $modifiedSlots = [], array $removedRoles = [] ) {
117 foreach ( $modifiedSlots as $slot ) {
118 $this->modifySlot( $slot );
121 foreach ( $removedRoles as $role ) {
122 $this->removeSlot( $role );
127 * Returns a list of modified slot roles, that is, roles modified by calling modifySlot(),
128 * and not later removed by calling removeSlot().
130 * Note that slots in modified roles may still be inherited slots. This is for instance
131 * the case when the RevisionSlotsUpdate objects represents some kind of rollback
132 * operation, in which slots that existed in an earlier revision are restored in
137 public function getModifiedRoles() {
138 return array_keys( $this->modifiedSlots
);
142 * Returns a list of removed slot roles, that is, roles removed by calling removeSlot(),
143 * and not later re-introduced by calling modifySlot().
147 public function getRemovedRoles() {
148 return array_keys( $this->removedRoles
);
152 * Returns a list of all slot roles that modified or removed.
156 public function getTouchedRoles() {
157 return array_merge( $this->getModifiedRoles(), $this->getRemovedRoles() );
161 * Sets the given slot to be modified.
162 * If a slot with the same role is already present, it is replaced.
164 * The roles used with modifySlot() will be returned from getModifiedRoles(),
165 * unless overwritten with removeSlot().
167 * @param SlotRecord $slot
169 public function modifySlot( SlotRecord
$slot ) {
170 $role = $slot->getRole();
172 // XXX: We should perhaps require this to be an unsaved slot!
173 unset( $this->removedRoles
[$role] );
174 $this->modifiedSlots
[$role] = $slot;
178 * Sets the content for the slot with the given role to be modified.
179 * If a slot with the same role is already present, it is replaced.
181 * @param string $role
182 * @param Content $content
184 public function modifyContent( $role, Content
$content ) {
185 $slot = SlotRecord
::newUnsaved( $role, $content );
186 $this->modifySlot( $slot );
190 * Remove the slot for the given role, discontinue the corresponding stream.
192 * The roles used with removeSlot() will be returned from getRemovedSlots(),
193 * unless overwritten with modifySlot().
195 * @param string $role
197 public function removeSlot( $role ) {
198 unset( $this->modifiedSlots
[$role] );
199 $this->removedRoles
[$role] = true;
203 * Returns the SlotRecord associated with the given role, if the slot with that role
204 * was modified (and not again removed).
206 * @note If the SlotRecord returned by this method returns a non-inherited slot,
207 * the content of that slot may or may not already have PST applied. Methods
208 * that take a RevisionSlotsUpdate as a parameter should specify whether they
209 * expect PST to already have been applied to all slots. Inherited slots
210 * should never have PST applied again.
212 * @param string $role The role name of the desired slot
214 * @throws RevisionAccessException if the slot does not exist or was removed.
217 public function getModifiedSlot( $role ) {
218 if ( isset( $this->modifiedSlots
[$role] ) ) {
219 return $this->modifiedSlots
[$role];
221 throw new RevisionAccessException( 'No such slot: ' . $role );
226 * Returns whether getModifiedSlot() will return a SlotRecord for the given role.
228 * Will return true for the role names returned by getModifiedRoles(), false otherwise.
230 * @param string $role The role name of the desired slot
234 public function isModifiedSlot( $role ) {
235 return isset( $this->modifiedSlots
[$role] );
239 * Returns whether the given role is to be removed from the page.
241 * Will return true for the role names returned by getRemovedRoles(), false otherwise.
243 * @param string $role The role name of the desired slot
247 public function isRemovedSlot( $role ) {
248 return isset( $this->removedRoles
[$role] );
252 * Returns true if $other represents the same update - that is,
253 * if all methods defined by RevisionSlotsUpdate when called on $this or $other
254 * will yield the same result when called with the same parameters.
256 * SlotRecords for the same role are compared based on their model and content.
258 * @param RevisionSlotsUpdate $other
261 public function hasSameUpdates( RevisionSlotsUpdate
$other ) {
262 // NOTE: use != not !==, since the order of entries is not significant!
264 if ( $this->getModifiedRoles() != $other->getModifiedRoles() ) {
268 if ( $this->getRemovedRoles() != $other->getRemovedRoles() ) {
272 foreach ( $this->getModifiedRoles() as $role ) {
273 $s = $this->getModifiedSlot( $role );
274 $t = $other->getModifiedSlot( $role );
276 if ( !$s->hasSameContent( $t ) ) {
285 * Applies this update to the given MutableRevisionSlots, setting all modified slots,
286 * and removing all removed roles.
288 * @param MutableRevisionSlots $slots
290 public function apply( MutableRevisionSlots
$slots ) {
291 foreach ( $this->getModifiedRoles() as $role ) {
292 $slots->setSlot( $this->getModifiedSlot( $role ) );
295 foreach ( $this->getRemovedRoles() as $role ) {
296 $slots->removeSlot( $role );