ParsoidParser: Record ParserOptions watcher on ParserOutput object
[mediawiki.git] / includes / Storage / RevisionSlotsUpdate.php
blob8c632f05c8d440d4631fd41e342dada2a5141c19
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Storage;
23 use Content;
24 use MediaWiki\Revision\MutableRevisionSlots;
25 use MediaWiki\Revision\RevisionAccessException;
26 use MediaWiki\Revision\RevisionSlots;
27 use MediaWiki\Revision\SlotRecord;
29 /**
30 * Value object representing a modification of revision slots.
32 * @since 1.32
34 class RevisionSlotsUpdate {
36 /**
37 * @var SlotRecord[] modified slots, using the slot role as the key.
39 private $modifiedSlots = [];
41 /**
42 * @var bool[] removed roles, stored in the keys of the array.
44 private $removedRoles = [];
46 /**
47 * Constructs a RevisionSlotsUpdate representing the update that turned $parentSlots
48 * into $newSlots. If $parentSlots is not given, $newSlots is assumed to come from a
49 * page's first revision.
51 * @param RevisionSlots $newSlots
52 * @param RevisionSlots|null $parentSlots
54 * @return RevisionSlotsUpdate
56 public static function newFromRevisionSlots(
57 RevisionSlots $newSlots,
58 RevisionSlots $parentSlots = null
59 ) {
60 $modified = $newSlots->getSlots();
61 $removed = [];
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] );
74 return new RevisionSlotsUpdate( $modified, $removed );
77 /**
78 * Constructs a RevisionSlotsUpdate representing the update of $parentSlots
79 * when changing $newContent. If a slot has the same content in $newContent
80 * as in $parentSlots, that slot is considered inherited and thus omitted from
81 * the resulting RevisionSlotsUpdate.
83 * In contrast to newFromRevisionSlots(), slots in $parentSlots that are not present
84 * in $newContent are not considered removed. They are instead assumed to be inherited.
86 * @param Content[] $newContent The new content, using slot roles as array keys.
87 * @param RevisionSlots|null $parentSlots
89 * @return RevisionSlotsUpdate
91 public static function newFromContent( array $newContent, RevisionSlots $parentSlots = null ) {
92 $modified = [];
94 foreach ( $newContent as $role => $content ) {
95 $slot = SlotRecord::newUnsaved( $role, $content );
97 if ( $parentSlots
98 && $parentSlots->hasSlot( $role )
99 && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
101 // Skip slots that had the same content in the parent revision from $modified.
102 continue;
105 $modified[$role] = $slot;
108 return new RevisionSlotsUpdate( $modified );
112 * @param SlotRecord[] $modifiedSlots
113 * @param string[] $removedRoles
115 public function __construct( array $modifiedSlots = [], array $removedRoles = [] ) {
116 foreach ( $modifiedSlots as $slot ) {
117 $this->modifySlot( $slot );
120 foreach ( $removedRoles as $role ) {
121 $this->removeSlot( $role );
126 * Returns a list of modified slot roles, that is, roles modified by calling modifySlot(),
127 * and not later removed by calling removeSlot().
129 * Note that slots in modified roles may still be inherited slots. This is for instance
130 * the case when the RevisionSlotsUpdate objects represents some kind of rollback
131 * operation, in which slots that existed in an earlier revision are restored in
132 * a new revision.
134 * @return string[]
136 public function getModifiedRoles() {
137 return array_keys( $this->modifiedSlots );
141 * Returns a list of removed slot roles, that is, roles removed by calling removeSlot(),
142 * and not later re-introduced by calling modifySlot().
144 * @return string[]
146 public function getRemovedRoles() {
147 return array_keys( $this->removedRoles );
151 * Returns a list of all slot roles that modified or removed.
153 * @return string[]
155 public function getTouchedRoles() {
156 return array_merge( $this->getModifiedRoles(), $this->getRemovedRoles() );
160 * Sets the given slot to be modified.
161 * If a slot with the same role is already present, it is replaced.
163 * The roles used with modifySlot() will be returned from getModifiedRoles(),
164 * unless overwritten with removeSlot().
166 * @param SlotRecord $slot
168 public function modifySlot( SlotRecord $slot ) {
169 $role = $slot->getRole();
171 // XXX: We should perhaps require this to be an unsaved slot!
172 unset( $this->removedRoles[$role] );
173 $this->modifiedSlots[$role] = $slot;
177 * Sets the content for the slot with the given role to be modified.
178 * If a slot with the same role is already present, it is replaced.
180 * @param string $role
181 * @param Content $content
183 public function modifyContent( $role, Content $content ) {
184 $slot = SlotRecord::newUnsaved( $role, $content );
185 $this->modifySlot( $slot );
189 * Remove the slot for the given role, discontinue the corresponding stream.
191 * The roles used with removeSlot() will be returned from getRemovedSlots(),
192 * unless overwritten with modifySlot().
194 * @param string $role
196 public function removeSlot( $role ) {
197 unset( $this->modifiedSlots[$role] );
198 $this->removedRoles[$role] = true;
202 * Returns the SlotRecord associated with the given role, if the slot with that role
203 * was modified (and not again removed).
205 * @note If the SlotRecord returned by this method returns a non-inherited slot,
206 * the content of that slot may or may not already have PST applied. Methods
207 * that take a RevisionSlotsUpdate as a parameter should specify whether they
208 * expect PST to already have been applied to all slots. Inherited slots
209 * should never have PST applied again.
211 * @param string $role The role name of the desired slot
213 * @throws RevisionAccessException if the slot does not exist or was removed.
214 * @return SlotRecord
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 ]
228 * Returns whether getModifiedSlot() will return a SlotRecord for the given role.
230 * Will return true for the role names returned by getModifiedRoles(), false otherwise.
232 * @param string $role The role name of the desired slot
234 * @return bool
236 public function isModifiedSlot( $role ) {
237 return isset( $this->modifiedSlots[$role] );
241 * Returns whether the given role is to be removed from the page.
243 * Will return true for the role names returned by getRemovedRoles(), false otherwise.
245 * @param string $role The role name of the desired slot
247 * @return bool
249 public function isRemovedSlot( $role ) {
250 return isset( $this->removedRoles[$role] );
254 * Returns true if $other represents the same update - that is,
255 * if all methods defined by RevisionSlotsUpdate when called on $this or $other
256 * will yield the same result when called with the same parameters.
258 * SlotRecords for the same role are compared based on their model and content.
260 * @param RevisionSlotsUpdate $other
261 * @return bool
263 public function hasSameUpdates( RevisionSlotsUpdate $other ) {
264 // NOTE: use != not !==, since the order of entries is not significant!
266 if ( $this->getModifiedRoles() != $other->getModifiedRoles() ) {
267 return false;
270 if ( $this->getRemovedRoles() != $other->getRemovedRoles() ) {
271 return false;
274 foreach ( $this->getModifiedRoles() as $role ) {
275 $s = $this->getModifiedSlot( $role );
276 $t = $other->getModifiedSlot( $role );
278 if ( !$s->hasSameContent( $t ) ) {
279 return false;
283 return true;
287 * Applies this update to the given MutableRevisionSlots, setting all modified slots,
288 * and removing all removed roles.
290 * @param MutableRevisionSlots $slots
292 public function apply( MutableRevisionSlots $slots ) {
293 foreach ( $this->getModifiedRoles() as $role ) {
294 $slots->setSlot( $this->getModifiedSlot( $role ) );
297 foreach ( $this->getRemovedRoles() as $role ) {
298 $slots->removeSlot( $role );