ba9780f66b4ee419ee71345db6fd4a65ca7e4468
3 * Value object representing the set of slots belonging to a revision.
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
;
27 use Wikimedia\Assert\Assert
;
30 * Value object representing the set of slots belonging to a revision.
36 /** @var SlotRecord[]|callable */
40 * @param SlotRecord[]|callable $slots SlotRecords,
41 * or a callback that returns such a structure.
43 public function __construct( $slots ) {
44 Assert
::parameterType( 'array|callable', $slots, '$slots' );
46 if ( is_callable( $slots ) ) {
47 $this->slots
= $slots;
49 $this->setSlotsInternal( $slots );
54 * @param SlotRecord[] $slots
56 private function setSlotsInternal( array $slots ) {
57 Assert
::parameterElementType( SlotRecord
::class, $slots, '$slots' );
61 // re-key the slot array
62 foreach ( $slots as $slot ) {
63 $role = $slot->getRole();
64 $this->slots
[$role] = $slot;
69 * Implemented to defy serialization.
71 * @throws LogicException always
73 public function __sleep() {
74 throw new LogicException( __CLASS__
. ' is not serializable.' );
78 * Returns the Content of the given slot.
79 * Call getSlotNames() to get a list of available slots.
81 * Note that for mutable Content objects, each call to this method will return a
84 * @param string $role The role name of the desired slot
86 * @throws RevisionAccessException if the slot does not exist or slot data
87 * could not be lazy-loaded.
90 public function getContent( $role ) {
91 // Return a copy to be safe. Immutable content objects return $this from copy().
92 return $this->getSlot( $role )->getContent()->copy();
96 * Returns the SlotRecord of the given slot.
97 * Call getSlotNames() to get a list of available slots.
99 * @param string $role The role name of the desired slot
101 * @throws RevisionAccessException if the slot does not exist or slot data
102 * could not be lazy-loaded.
105 public function getSlot( $role ) {
106 $slots = $this->getSlots();
108 if ( isset( $slots[$role] ) ) {
109 return $slots[$role];
111 throw new RevisionAccessException( 'No such slot: ' . $role );
116 * Returns whether the given slot is set.
118 * @param string $role The role name of the desired slot
122 public function hasSlot( $role ) {
123 $slots = $this->getSlots();
125 return isset( $slots[$role] );
129 * Returns the slot names (roles) of all slots present in this revision.
130 * getContent() will succeed only for the names returned by this method.
134 public function getSlotRoles() {
135 $slots = $this->getSlots();
136 return array_keys( $slots );
140 * Computes the total nominal size of the revision's slots, in bogo-bytes.
142 * @warn This is potentially expensive! It may cause all slot's content to be loaded
147 public function computeSize() {
148 return array_reduce( $this->getSlots(), function ( $accu, SlotRecord
$slot ) {
149 return $accu +
$slot->getSize();
154 * Returns an associative array that maps role names to SlotRecords. Each SlotRecord
155 * represents the content meta-data of a slot, together they define the content of
158 * @note This may cause the content meta-data for the revision to be lazy-loaded.
160 * @return SlotRecord[] revision slot/content rows, keyed by slot role name.
162 public function getSlots() {
163 if ( is_callable( $this->slots
) ) {
164 $slots = call_user_func( $this->slots
);
166 Assert
::postcondition(
168 'Slots info callback should return an array of objects'
171 $this->setSlotsInternal( $slots );
178 * Computes the combined hash of the revisions's slots.
180 * @note For backwards compatibility, the combined hash of a single slot
181 * is that slot's hash. For consistency, the combined hash of an empty set of slots
182 * is the hash of the empty string.
184 * @warn This is potentially expensive! It may cause all slot's content to be loaded
185 * and deserialized, then re-serialized and hashed.
189 public function computeSha1() {
190 $slots = $this->getSlots();
193 if ( empty( $slots ) ) {
194 return SlotRecord
::base36Sha1( '' );
197 return array_reduce( $slots, function ( $accu, SlotRecord
$slot ) {
198 return $accu === null
200 : SlotRecord
::base36Sha1( $accu . $slot->getSha1() );
205 * Return all slots that belong to the revision they originate from (that is,
206 * they are not inherited from some other revision).
208 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
210 * @return SlotRecord[]
212 public function getOriginalSlots() {
215 function ( SlotRecord
$slot ) {
216 return !$slot->isInherited();
222 * Return all slots that are not not originate in the revision they belong to (that is,
223 * they are inherited from some other revision).
225 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
227 * @return SlotRecord[]
229 public function getInheritedSlots() {
232 function ( SlotRecord
$slot ) {
233 return $slot->isInherited();
239 * Checks whether the other RevisionSlots instance has the same content
240 * as this instance. Note that this does not mean that the slots have to be the same:
241 * they could for instance belong to different revisions.
243 * @param RevisionSlots $other
247 public function hasSameContent( RevisionSlots
$other ) {
248 if ( $other === $this ) {
252 $aSlots = $this->getSlots();
253 $bSlots = $other->getSlots();
258 if ( array_keys( $aSlots ) !== array_keys( $bSlots ) ) {
262 foreach ( $aSlots as $role => $s ) {
265 if ( !$s->hasSameContent( $t ) ) {
274 * Find roles for which the $other RevisionSlots object has different content
275 * as this RevisionSlots object, including any roles that are present in one
278 * @param RevisionSlots $other
280 * @return string[] a list of slot roles that are different.
282 public function getRolesWithDifferentContent( RevisionSlots
$other ) {
283 if ( $other === $this ) {
287 $aSlots = $this->getSlots();
288 $bSlots = $other->getSlots();
293 $different = array_keys( array_merge(
294 array_diff_key( $aSlots, $bSlots ),
295 array_diff_key( $bSlots, $aSlots )
298 /** @var SlotRecord[] $common */
299 $common = array_intersect_key( $aSlots, $bSlots );
301 foreach ( $common as $role => $s ) {
304 if ( !$s->hasSameContent( $t ) ) {
305 $different[] = $role;