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->getSlots(), 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->getSlots();
194  ksort( $slots );
195 
196  if ( empty( $slots ) ) {
197  return SlotRecord::base36Sha1( '' );
198  }
199 
200  return array_reduce( $slots, 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  function ( SlotRecord $slot ) {
219  return !$slot->isInherited();
220  }
221  );
222  }
223 
232  public function getInheritedSlots() {
233  return array_filter(
234  $this->getSlots(),
235  function ( SlotRecord $slot ) {
236  return $slot->isInherited();
237  }
238  );
239  }
240 
250  public function hasSameContent( RevisionSlots $other ) {
251  if ( $other === $this ) {
252  return true;
253  }
254 
255  $aSlots = $this->getSlots();
256  $bSlots = $other->getSlots();
257 
258  ksort( $aSlots );
259  ksort( $bSlots );
260 
261  if ( array_keys( $aSlots ) !== array_keys( $bSlots ) ) {
262  return false;
263  }
264 
265  foreach ( $aSlots as $role => $s ) {
266  $t = $bSlots[$role];
267 
268  if ( !$s->hasSameContent( $t ) ) {
269  return false;
270  }
271  }
272 
273  return true;
274  }
275 
285  public function getRolesWithDifferentContent( RevisionSlots $other ) {
286  if ( $other === $this ) {
287  return [];
288  }
289 
290  $aSlots = $this->getSlots();
291  $bSlots = $other->getSlots();
292 
293  ksort( $aSlots );
294  ksort( $bSlots );
295 
296  $different = array_keys( array_merge(
297  array_diff_key( $aSlots, $bSlots ),
298  array_diff_key( $bSlots, $aSlots )
299  ) );
300 
302  $common = array_intersect_key( $aSlots, $bSlots );
303 
304  foreach ( $common as $role => $s ) {
305  $t = $bSlots[$role];
306 
307  if ( !$s->hasSameContent( $t ) ) {
308  $different[] = $role;
309  }
310  }
311 
312  return $different;
313  }
314 
315 }
316 
321 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)
Stable to call.
Definition: RevisionSlots.php:53
Revision\RevisionSlots\getRolesWithDifferentContent
getRolesWithDifferentContent(RevisionSlots $other)
Find roles for which the $other RevisionSlots object has different content as this RevisionSlots obje...
Definition: RevisionSlots.php:285
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:413
$s
$s
Definition: mergeMessageFileList.php:186
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:250
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:605
Revision\SlotRecord\getSha1
getSha1()
Returns the content size.
Definition: SlotRecord.php:531
Revision\SlotRecord\getSize
getSize()
Returns the content size.
Definition: SlotRecord.php:515
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\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40