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 the slot names (roles) of all slots present in this revision.
115 * getContent() will succeed only for the names returned by this method.
119 public function getSlotRoles() {
120 $slots = $this->getSlots();
121 return array_keys( $slots );
125 * Computes the total nominal size of the revision's slots, in bogo-bytes.
127 * @warn This is potentially expensive! It may cause all slot's content to be loaded
132 public function computeSize() {
133 return array_reduce( $this->getSlots(), function ( $accu, SlotRecord
$slot ) {
134 return $accu +
$slot->getSize();
139 * Returns an associative array that maps role names to SlotRecords. Each SlotRecord
140 * represents the content meta-data of a slot, together they define the content of
143 * @note This may cause the content meta-data for the revision to be lazy-loaded.
145 * @return SlotRecord[] revision slot/content rows, keyed by slot role name.
147 public function getSlots() {
148 if ( is_callable( $this->slots
) ) {
149 $slots = call_user_func( $this->slots
);
151 Assert
::postcondition(
153 'Slots info callback should return an array of objects'
156 $this->setSlotsInternal( $slots );
163 * Computes the combined hash of the revisions's slots.
165 * @note For backwards compatibility, the combined hash of a single slot
166 * is that slot's hash. For consistency, the combined hash of an empty set of slots
167 * is the hash of the empty string.
169 * @warn This is potentially expensive! It may cause all slot's content to be loaded
170 * and deserialized, then re-serialized and hashed.
174 public function computeSha1() {
175 $slots = $this->getSlots();
178 if ( empty( $slots ) ) {
179 return SlotRecord
::base36Sha1( '' );
182 return array_reduce( $slots, function ( $accu, SlotRecord
$slot ) {
183 return $accu === null
185 : SlotRecord
::base36Sha1( $accu . $slot->getSha1() );