X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FStorage%2FRevisionSlotsUpdate.php;h=a863ad5a51c68f11a81d85817b930177e6194e17;hb=d1a497a9f4c2a5c50914e19aee1e97f849cef6ca;hp=0eef90f2bc5732728201c6a1dfc5e7bb5ce06b7b;hpb=e85682ed109e13432f8ee376e16eb89325f05373;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Storage/RevisionSlotsUpdate.php b/includes/Storage/RevisionSlotsUpdate.php index 0eef90f2bc..a863ad5a51 100644 --- a/includes/Storage/RevisionSlotsUpdate.php +++ b/includes/Storage/RevisionSlotsUpdate.php @@ -23,6 +23,10 @@ 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. @@ -72,6 +76,39 @@ class RevisionSlotsUpdate { 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 @@ -90,6 +127,11 @@ class RevisionSlotsUpdate { * 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() { @@ -239,4 +281,20 @@ class RevisionSlotsUpdate { 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 ); + } + } + }