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\Revision
;
27 use Wikimedia\Assert\Assert
;
30 * Value object representing the set of slots belonging to a revision.
33 * @since 1.32 Renamed from MediaWiki\Storage\RevisionSlots
37 /** @var SlotRecord[]|callable */
41 * @param SlotRecord[]|callable $slots SlotRecords,
42 * or a callback that returns such a structure.
44 public function __construct( $slots ) {
45 Assert
::parameterType( 'array|callable', $slots, '$slots' );
47 if ( is_callable( $slots ) ) {
48 $this->slots
= $slots;
50 $this->setSlotsInternal( $slots );
55 * @param SlotRecord[] $slots
57 private function setSlotsInternal( array $slots ) {
58 Assert
::parameterElementType( SlotRecord
::class, $slots, '$slots' );
62 // re-key the slot array
63 foreach ( $slots as $slot ) {
64 $role = $slot->getRole();
65 $this->slots
[$role] = $slot;
70 * Implemented to defy serialization.
72 * @throws LogicException always
74 public function __sleep() {
75 throw new LogicException( __CLASS__
. ' is not serializable.' );
79 * Returns the Content of the given slot.
80 * Call getSlotNames() to get a list of available slots.
82 * Note that for mutable Content objects, each call to this method will return a
85 * @param string $role The role name of the desired slot
87 * @throws RevisionAccessException if the slot does not exist or slot data
88 * could not be lazy-loaded.
91 public function getContent( $role ) {
92 // Return a copy to be safe. Immutable content objects return $this from copy().
93 return $this->getSlot( $role )->getContent()->copy();
97 * Returns the SlotRecord of the given slot.
98 * Call getSlotNames() to get a list of available slots.
100 * @param string $role The role name of the desired slot
102 * @throws RevisionAccessException if the slot does not exist or slot data
103 * could not be lazy-loaded.
106 public function getSlot( $role ) {
107 $slots = $this->getSlots();
109 if ( isset( $slots[$role] ) ) {
110 return $slots[$role];
112 throw new RevisionAccessException( 'No such slot: ' . $role );
117 * Returns whether the given slot is set.
119 * @param string $role The role name of the desired slot
123 public function hasSlot( $role ) {
124 $slots = $this->getSlots();
126 return isset( $slots[$role] );
130 * Returns the slot names (roles) of all slots present in this revision.
131 * getContent() will succeed only for the names returned by this method.
135 public function getSlotRoles() {
136 $slots = $this->getSlots();
137 return array_keys( $slots );
141 * Computes the total nominal size of the revision's slots, in bogo-bytes.
143 * @warning This is potentially expensive! It may cause all slot's content to be loaded
148 public function computeSize() {
149 return array_reduce( $this->getSlots(), function ( $accu, SlotRecord
$slot ) {
150 return $accu +
$slot->getSize();
155 * Returns an associative array that maps role names to SlotRecords. Each SlotRecord
156 * represents the content meta-data of a slot, together they define the content of
159 * @note This may cause the content meta-data for the revision to be lazy-loaded.
161 * @return SlotRecord[] revision slot/content rows, keyed by slot role name.
163 public function getSlots() {
164 if ( is_callable( $this->slots
) ) {
165 $slots = call_user_func( $this->slots
);
167 Assert
::postcondition(
169 'Slots info callback should return an array of objects'
172 $this->setSlotsInternal( $slots );
179 * Computes the combined hash of the revisions's slots.
181 * @note For backwards compatibility, the combined hash of a single slot
182 * is that slot's hash. For consistency, the combined hash of an empty set of slots
183 * is the hash of the empty string.
185 * @warning This is potentially expensive! It may cause all slot's content to be loaded
186 * and deserialized, then re-serialized and hashed.
190 public function computeSha1() {
191 $slots = $this->getSlots();
194 if ( empty( $slots ) ) {
195 return SlotRecord
::base36Sha1( '' );
198 return array_reduce( $slots, function ( $accu, SlotRecord
$slot ) {
199 return $accu === null
201 : SlotRecord
::base36Sha1( $accu . $slot->getSha1() );
206 * Return all slots that belong to the revision they originate from (that is,
207 * they are not inherited from some other revision).
209 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
211 * @return SlotRecord[]
213 public function getOriginalSlots() {
216 function ( SlotRecord
$slot ) {
217 return !$slot->isInherited();
223 * Return all slots that are not not originate in the revision they belong to (that is,
224 * they are inherited from some other revision).
226 * @note This may cause the slot meta-data for the revision to be lazy-loaded.
228 * @return SlotRecord[]
230 public function getInheritedSlots() {
233 function ( SlotRecord
$slot ) {
234 return $slot->isInherited();
240 * Checks whether the other RevisionSlots instance has the same content
241 * as this instance. Note that this does not mean that the slots have to be the same:
242 * they could for instance belong to different revisions.
244 * @param RevisionSlots $other
248 public function hasSameContent( RevisionSlots
$other ) {
249 if ( $other === $this ) {
253 $aSlots = $this->getSlots();
254 $bSlots = $other->getSlots();
259 if ( array_keys( $aSlots ) !== array_keys( $bSlots ) ) {
263 foreach ( $aSlots as $role => $s ) {
266 if ( !$s->hasSameContent( $t ) ) {
275 * Find roles for which the $other RevisionSlots object has different content
276 * as this RevisionSlots object, including any roles that are present in one
279 * @param RevisionSlots $other
281 * @return string[] a list of slot roles that are different.
283 public function getRolesWithDifferentContent( RevisionSlots
$other ) {
284 if ( $other === $this ) {
288 $aSlots = $this->getSlots();
289 $bSlots = $other->getSlots();
294 $different = array_keys( array_merge(
295 array_diff_key( $aSlots, $bSlots ),
296 array_diff_key( $bSlots, $aSlots )
299 /** @var SlotRecord[] $common */
300 $common = array_intersect_key( $aSlots, $bSlots );
302 foreach ( $common as $role => $s ) {
305 if ( !$s->hasSameContent( $t ) ) {
306 $different[] = $role;
316 * Retain the old class name for backwards compatibility.
317 * @deprecated since 1.32
319 class_alias( RevisionSlots
::class, 'MediaWiki\Storage\RevisionSlots' );