namespace MediaWiki\Storage;
use Content;
+use MediaWiki\Revision\MutableRevisionSlots;
+use MediaWiki\Revision\RevisionAccessException;
+use MediaWiki\Revision\RevisionSlots;
+use MediaWiki\Revision\SlotRecord;
/**
* Value object representing a modification of revision slots.
return new RevisionSlotsUpdate( $modified, $removed );
}
+ /**
+ * Constructs a RevisionSlotsUpdate representing the update of $parentSlots
+ * when changing $newContent. If a slot has the same content in $newContent
+ * as in $parentSlots, that slot is considered inherited and thus omitted from
+ * the resulting RevisionSlotsUpdate.
+ *
+ * In contrast to newFromRevisionSlots(), slots in $parentSlots that are not present
+ * in $newContent are not considered removed. They are instead assumed to be inherited.
+ *
+ * @param Content[] $newContent The new content, using slot roles as array keys.
+ *
+ * @return RevisionSlotsUpdate
+ */
+ public static function newFromContent( array $newContent, RevisionSlots $parentSlots = null ) {
+ $modified = [];
+
+ foreach ( $newContent as $role => $content ) {
+ $slot = SlotRecord::newUnsaved( $role, $content );
+
+ if ( $parentSlots
+ && $parentSlots->hasSlot( $role )
+ && $slot->hasSameContent( $parentSlots->getSlot( $role ) )
+ ) {
+ // Skip slots that had the same content in the parent revision from $modified.
+ continue;
+ }
+
+ $modified[$role] = $slot;
+ }
+
+ return new RevisionSlotsUpdate( $modified );
+ }
+
/**
* @param SlotRecord[] $modifiedSlots
* @param string[] $removedRoles
* Returns a list of modified slot roles, that is, roles modified by calling modifySlot(),
* and not later removed by calling removeSlot().
*
+ * Note that slots in modified roles may still be inherited slots. This is for instance
+ * the case when the RevisionSlotsUpdate objects represents some kind of rollback
+ * operation, in which slots that existed in an earlier revision are restored in
+ * a new revision.
+ *
* @return string[]
*/
public function getModifiedRoles() {
return true;
}
+ /**
+ * Applies this update to the given MutableRevisionSlots, setting all modified slots,
+ * and removing all removed roles.
+ *
+ * @param MutableRevisionSlots $slots
+ */
+ public function apply( MutableRevisionSlots $slots ) {
+ foreach ( $this->getModifiedRoles() as $role ) {
+ $slots->setSlot( $this->getModifiedSlot( $role ) );
+ }
+
+ foreach ( $this->getRemovedRoles() as $role ) {
+ $slots->removeSlot( $role );
+ }
+ }
+
}