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 ) {
59 // re-key the slot array
60 foreach ( $slots as $slot ) {
61 $role = $slot->getRole();
62 $this->slots
[$role] = $slot;
67 * Implemented to defy serialization.
69 * @throws LogicException always
71 public function __sleep() {
72 throw new LogicException( __CLASS__
. ' is not serializable.' );
76 * Returns the Content of the given slot.
77 * Call getSlotNames() to get a list of available slots.
79 * Note that for mutable Content objects, each call to this method will return a
82 * @param string $role The role name of the desired slot
84 * @throws RevisionAccessException if the slot does not exist or slot data
85 * could not be lazy-loaded.
88 public function getContent( $role ) {
89 // Return a copy to be safe. Immutable content objects return $this from copy().
90 return $this->getSlot( $role )->getContent()->copy();
94 * Returns the SlotRecord of the given slot.
95 * Call getSlotNames() to get a list of available slots.
97 * @param string $role The role name of the desired slot
99 * @throws RevisionAccessException if the slot does not exist or slot data
100 * could not be lazy-loaded.
103 public function getSlot( $role ) {
104 $slots = $this->getSlots();
106 if ( isset( $slots[$role] ) ) {
107 return $slots[$role];
109 throw new RevisionAccessException( 'No such slot: ' . $role );
114 * Returns whether the given slot is set.
116 * @param string $role The role name of the desired slot
120 public function hasSlot( $role ) {
121 $slots = $this->getSlots();
123 return isset( $slots[$role] );
127 * Returns the slot names (roles) of all slots present in this revision.
128 * getContent() will succeed only for the names returned by this method.
132 public function getSlotRoles() {
133 $slots = $this->getSlots();
134 return array_keys( $slots );
138 * Computes the total nominal size of the revision's slots, in bogo-bytes.
140 * @warn This is potentially expensive! It may cause all slot's content to be loaded
145 public function computeSize() {
146 return array_reduce( $this->getSlots(), function ( $accu, SlotRecord
$slot ) {
147 return $accu +
$slot->getSize();
152 * Returns an associative array that maps role names to SlotRecords. Each SlotRecord
153 * represents the content meta-data of a slot, together they define the content of
156 * @note This may cause the content meta-data for the revision to be lazy-loaded.
158 * @return SlotRecord[] revision slot/content rows, keyed by slot role name.
160 public function getSlots() {
161 if ( is_callable( $this->slots
) ) {
162 $slots = call_user_func( $this->slots
);
164 Assert
::postcondition(
166 'Slots info callback should return an array of objects'
169 $this->setSlotsInternal( $slots );
176 * Computes the combined hash of the revisions's slots.
178 * @note For backwards compatibility, the combined hash of a single slot
179 * is that slot's hash. For consistency, the combined hash of an empty set of slots
180 * is the hash of the empty string.
182 * @warn This is potentially expensive! It may cause all slot's content to be loaded
183 * and deserialized, then re-serialized and hashed.
187 public function computeSha1() {
188 $slots = $this->getSlots();
191 if ( empty( $slots ) ) {
192 return SlotRecord
::base36Sha1( '' );
195 return array_reduce( $slots, function ( $accu, SlotRecord
$slot ) {
196 return $accu === null
198 : SlotRecord
::base36Sha1( $accu . $slot->getSha1() );