MediaWiki  master
RevisionSlots.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use Content;
26 use Wikimedia\Assert\Assert;
27 use Wikimedia\NonSerializable\NonSerializableTrait;
28 
42  use NonSerializableTrait;
43 
45  protected $slots;
46 
53  public function __construct( $slots ) {
54  Assert::parameterType( 'array|callable', $slots, '$slots' );
55 
56  if ( is_callable( $slots ) ) {
57  $this->slots = $slots;
58  } else {
59  $this->setSlotsInternal( $slots );
60  }
61  }
62 
66  private function setSlotsInternal( array $slots ) {
67  Assert::parameterElementType( SlotRecord::class, $slots, '$slots' );
68 
69  $this->slots = [];
70 
71  // re-key the slot array
72  foreach ( $slots as $slot ) {
73  $role = $slot->getRole();
74  $this->slots[$role] = $slot;
75  }
76  }
77 
93  public function getContent( $role ) {
94  // Return a copy to be safe. Immutable content objects return $this from copy().
95  return $this->getSlot( $role )->getContent()->copy();
96  }
97 
108  public function getSlot( $role ) {
109  $slots = $this->getSlots();
110 
111  if ( isset( $slots[$role] ) ) {
112  return $slots[$role];
113  } else {
114  throw new RevisionAccessException( 'No such slot: ' . $role );
115  }
116  }
117 
125  public function hasSlot( $role ) {
126  $slots = $this->getSlots();
127 
128  return isset( $slots[$role] );
129  }
130 
137  public function getSlotRoles() {
138  $slots = $this->getSlots();
139  return array_keys( $slots );
140  }
141 
150  public function computeSize() {
151  return array_reduce( $this->getPrimarySlots(), static function ( $accu, SlotRecord $slot ) {
152  return $accu + $slot->getSize();
153  }, 0 );
154  }
155 
165  public function getSlots() {
166  if ( is_callable( $this->slots ) ) {
167  $slots = call_user_func( $this->slots );
168 
169  Assert::postcondition(
170  is_array( $slots ),
171  'Slots info callback should return an array of objects'
172  );
173 
174  $this->setSlotsInternal( $slots );
175  }
176 
177  return $this->slots;
178  }
179 
192  public function computeSha1() {
193  $slots = $this->getPrimarySlots();
194  ksort( $slots );
195 
196  if ( empty( $slots ) ) {
197  return SlotRecord::base36Sha1( '' );
198  }
199 
200  return array_reduce( $slots, static function ( $accu, SlotRecord $slot ) {
201  return $accu === null
202  ? $slot->getSha1()
203  : SlotRecord::base36Sha1( $accu . $slot->getSha1() );
204  }, null );
205  }
206 
215  public function getOriginalSlots() {
216  return array_filter(
217  $this->getSlots(),
218  static function ( SlotRecord $slot ) {
219  return !$slot->isInherited();
220  }
221  );
222  }
223 
232  public function getInheritedSlots() {
233  return array_filter(
234  $this->getSlots(),
235  static function ( SlotRecord $slot ) {
236  return $slot->isInherited();
237  }
238  );
239  }
240 
247  public function getPrimarySlots() : array {
248  return array_filter(
249  $this->getSlots(),
250  static function ( SlotRecord $slot ) {
251  return !$slot->isDerived();
252  }
253  );
254  }
255 
265  public function hasSameContent( RevisionSlots $other ) {
266  if ( $other === $this ) {
267  return true;
268  }
269 
270  $aSlots = $this->getSlots();
271  $bSlots = $other->getSlots();
272 
273  ksort( $aSlots );
274  ksort( $bSlots );
275 
276  if ( array_keys( $aSlots ) !== array_keys( $bSlots ) ) {
277  return false;
278  }
279 
280  foreach ( $aSlots as $role => $s ) {
281  $t = $bSlots[$role];
282 
283  if ( !$s->hasSameContent( $t ) ) {
284  return false;
285  }
286  }
287 
288  return true;
289  }
290 
300  public function getRolesWithDifferentContent( RevisionSlots $other ) {
301  if ( $other === $this ) {
302  return [];
303  }
304 
305  $aSlots = $this->getSlots();
306  $bSlots = $other->getSlots();
307 
308  ksort( $aSlots );
309  ksort( $bSlots );
310 
311  $different = array_keys( array_merge(
312  array_diff_key( $aSlots, $bSlots ),
313  array_diff_key( $bSlots, $aSlots )
314  ) );
315 
317  $common = array_intersect_key( $aSlots, $bSlots );
318 
319  foreach ( $common as $role => $s ) {
320  $t = $bSlots[$role];
321 
322  if ( !$s->hasSameContent( $t ) ) {
323  $different[] = $role;
324  }
325  }
326 
327  return $different;
328  }
329 
330 }
331 
336 class_alias( RevisionSlots::class, 'MediaWiki\Storage\RevisionSlots' );
Revision\RevisionSlots\$slots
SlotRecord[] callable $slots
Definition: RevisionSlots.php:45
Revision\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:34
Revision\RevisionSlots\__construct
__construct( $slots)
Definition: RevisionSlots.php:53
Revision\SlotRecord\isDerived
isDerived()
Definition: SlotRecord.php:683
Revision\RevisionSlots\getRolesWithDifferentContent
getRolesWithDifferentContent(RevisionSlots $other)
Find roles for which the $other RevisionSlots object has different content as this RevisionSlots obje...
Definition: RevisionSlots.php:300
Revision\RevisionSlots\computeSha1
computeSha1()
Computes the combined hash of the revisions's slots.
Definition: RevisionSlots.php:192
Revision\RevisionSlots\hasSlot
hasSlot( $role)
Returns whether the given slot is set.
Definition: RevisionSlots.php:125
Revision\SlotRecord\isInherited
isInherited()
Whether this slot was inherited from an older revision.
Definition: SlotRecord.php:437
Revision\RevisionSlots\setSlotsInternal
setSlotsInternal(array $slots)
Definition: RevisionSlots.php:66
Revision\RevisionSlots\hasSameContent
hasSameContent(RevisionSlots $other)
Checks whether the other RevisionSlots instance has the same content as this instance.
Definition: RevisionSlots.php:265
MediaWiki\Revision
Definition: ContributionsLookup.php:3
Revision\RevisionSlots\getOriginalSlots
getOriginalSlots()
Return all slots that belong to the revision they originate from (that is, they are not inherited fro...
Definition: RevisionSlots.php:215
Revision\SlotRecord\base36Sha1
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Definition: SlotRecord.php:629
Revision\SlotRecord\getSha1
getSha1()
Returns the content size.
Definition: SlotRecord.php:555
Revision\SlotRecord\getSize
getSize()
Returns the content size.
Definition: SlotRecord.php:539
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
Revision\RevisionSlots\getContent
getContent( $role)
Returns the Content of the given slot.
Definition: RevisionSlots.php:93
Revision\RevisionSlots\getInheritedSlots
getInheritedSlots()
Return all slots that are not originate in the revision they belong to (that is, they are inherited f...
Definition: RevisionSlots.php:232
Revision\RevisionSlots\getSlots
getSlots()
Returns an associative array that maps role names to SlotRecords.
Definition: RevisionSlots.php:165
Content
Base interface for content objects.
Definition: Content.php:35
Revision\RevisionSlots\getSlot
getSlot( $role)
Returns the SlotRecord of the given slot.
Definition: RevisionSlots.php:108
Revision\RevisionSlots\computeSize
computeSize()
Computes the total nominal size of the revision's slots, in bogo-bytes.
Definition: RevisionSlots.php:150
Revision\RevisionSlots\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionSlots.php:137
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:41
$t
$t
Definition: testCompression.php:74
Revision\RevisionSlots\getPrimarySlots
getPrimarySlots()
Return all primary slots (those that are not derived).
Definition: RevisionSlots.php:247
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40