MediaWiki REL1_31
RevisionSlots.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Storage;
24
25use Content;
26use LogicException;
27use Wikimedia\Assert\Assert;
28
35
37 protected $slots;
38
43 public function __construct( $slots ) {
44 Assert::parameterType( 'array|callable', $slots, '$slots' );
45
46 if ( is_callable( $slots ) ) {
47 $this->slots = $slots;
48 } else {
49 $this->setSlotsInternal( $slots );
50 }
51 }
52
56 private function setSlotsInternal( array $slots ) {
57 $this->slots = [];
58
59 // re-key the slot array
60 foreach ( $slots as $slot ) {
61 $role = $slot->getRole();
62 $this->slots[$role] = $slot;
63 }
64 }
65
71 public function __sleep() {
72 throw new LogicException( __CLASS__ . ' is not serializable.' );
73 }
74
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();
91 }
92
103 public function getSlot( $role ) {
104 $slots = $this->getSlots();
105
106 if ( isset( $slots[$role] ) ) {
107 return $slots[$role];
108 } else {
109 throw new RevisionAccessException( 'No such slot: ' . $role );
110 }
111 }
112
120 public function hasSlot( $role ) {
121 $slots = $this->getSlots();
122
123 return isset( $slots[$role] );
124 }
125
132 public function getSlotRoles() {
133 $slots = $this->getSlots();
134 return array_keys( $slots );
135 }
136
145 public function computeSize() {
146 return array_reduce( $this->getSlots(), function ( $accu, SlotRecord $slot ) {
147 return $accu + $slot->getSize();
148 }, 0 );
149 }
150
160 public function getSlots() {
161 if ( is_callable( $this->slots ) ) {
162 $slots = call_user_func( $this->slots );
163
164 Assert::postcondition(
165 is_array( $slots ),
166 'Slots info callback should return an array of objects'
167 );
168
169 $this->setSlotsInternal( $slots );
170 }
171
172 return $this->slots;
173 }
174
187 public function computeSha1() {
188 $slots = $this->getSlots();
189 ksort( $slots );
190
191 if ( empty( $slots ) ) {
192 return SlotRecord::base36Sha1( '' );
193 }
194
195 return array_reduce( $slots, function ( $accu, SlotRecord $slot ) {
196 return $accu === null
197 ? $slot->getSha1()
198 : SlotRecord::base36Sha1( $accu . $slot->getSha1() );
199 }, null );
200 }
201
202}
Exception representing a failure to look up a revision.
Value object representing the set of slots belonging to a revision.
getSlot( $role)
Returns the SlotRecord of the given slot.
getSlots()
Returns an associative array that maps role names to SlotRecords.
getContent( $role)
Returns the Content of the given slot.
__sleep()
Implemented to defy serialization.
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
hasSlot( $role)
Returns whether the given slot is set.
computeSize()
Computes the total nominal size of the revision's slots, in bogo-bytes.
computeSha1()
Computes the combined hash of the revisions's slots.
SlotRecord[] callable $slots
Value object representing a content slot associated with a page revision.
getSha1()
Returns the content size.
getSize()
Returns the content size.
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Base interface for content objects.
Definition Content.php:34