MediaWiki  1.33.0
RevisionSlots.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use Content;
26 use LogicException;
27 use Wikimedia\Assert\Assert;
28 
36 
38  protected $slots;
39 
44  public function __construct( $slots ) {
45  Assert::parameterType( 'array|callable', $slots, '$slots' );
46 
47  if ( is_callable( $slots ) ) {
48  $this->slots = $slots;
49  } else {
50  $this->setSlotsInternal( $slots );
51  }
52  }
53 
57  private function setSlotsInternal( array $slots ) {
58  Assert::parameterElementType( SlotRecord::class, $slots, '$slots' );
59 
60  $this->slots = [];
61 
62  // re-key the slot array
63  foreach ( $slots as $slot ) {
64  $role = $slot->getRole();
65  $this->slots[$role] = $slot;
66  }
67  }
68 
74  public function __sleep() {
75  throw new LogicException( __CLASS__ . ' is not serializable.' );
76  }
77 
91  public function getContent( $role ) {
92  // Return a copy to be safe. Immutable content objects return $this from copy().
93  return $this->getSlot( $role )->getContent()->copy();
94  }
95 
106  public function getSlot( $role ) {
107  $slots = $this->getSlots();
108 
109  if ( isset( $slots[$role] ) ) {
110  return $slots[$role];
111  } else {
112  throw new RevisionAccessException( 'No such slot: ' . $role );
113  }
114  }
115 
123  public function hasSlot( $role ) {
124  $slots = $this->getSlots();
125 
126  return isset( $slots[$role] );
127  }
128 
135  public function getSlotRoles() {
136  $slots = $this->getSlots();
137  return array_keys( $slots );
138  }
139 
148  public function computeSize() {
149  return array_reduce( $this->getSlots(), function ( $accu, SlotRecord $slot ) {
150  return $accu + $slot->getSize();
151  }, 0 );
152  }
153 
163  public function getSlots() {
164  if ( is_callable( $this->slots ) ) {
165  $slots = call_user_func( $this->slots );
166 
167  Assert::postcondition(
168  is_array( $slots ),
169  'Slots info callback should return an array of objects'
170  );
171 
172  $this->setSlotsInternal( $slots );
173  }
174 
175  return $this->slots;
176  }
177 
190  public function computeSha1() {
191  $slots = $this->getSlots();
192  ksort( $slots );
193 
194  if ( empty( $slots ) ) {
195  return SlotRecord::base36Sha1( '' );
196  }
197 
198  return array_reduce( $slots, function ( $accu, SlotRecord $slot ) {
199  return $accu === null
200  ? $slot->getSha1()
201  : SlotRecord::base36Sha1( $accu . $slot->getSha1() );
202  }, null );
203  }
204 
213  public function getOriginalSlots() {
214  return array_filter(
215  $this->getSlots(),
216  function ( SlotRecord $slot ) {
217  return !$slot->isInherited();
218  }
219  );
220  }
221 
230  public function getInheritedSlots() {
231  return array_filter(
232  $this->getSlots(),
233  function ( SlotRecord $slot ) {
234  return $slot->isInherited();
235  }
236  );
237  }
238 
248  public function hasSameContent( RevisionSlots $other ) {
249  if ( $other === $this ) {
250  return true;
251  }
252 
253  $aSlots = $this->getSlots();
254  $bSlots = $other->getSlots();
255 
256  ksort( $aSlots );
257  ksort( $bSlots );
258 
259  if ( array_keys( $aSlots ) !== array_keys( $bSlots ) ) {
260  return false;
261  }
262 
263  foreach ( $aSlots as $role => $s ) {
264  $t = $bSlots[$role];
265 
266  if ( !$s->hasSameContent( $t ) ) {
267  return false;
268  }
269  }
270 
271  return true;
272  }
273 
283  public function getRolesWithDifferentContent( RevisionSlots $other ) {
284  if ( $other === $this ) {
285  return [];
286  }
287 
288  $aSlots = $this->getSlots();
289  $bSlots = $other->getSlots();
290 
291  ksort( $aSlots );
292  ksort( $bSlots );
293 
294  $different = array_keys( array_merge(
295  array_diff_key( $aSlots, $bSlots ),
296  array_diff_key( $bSlots, $aSlots )
297  ) );
298 
300  $common = array_intersect_key( $aSlots, $bSlots );
301 
302  foreach ( $common as $role => $s ) {
303  $t = $bSlots[$role];
304 
305  if ( !$s->hasSameContent( $t ) ) {
306  $different[] = $role;
307  }
308  }
309 
310  return $different;
311  }
312 
313 }
314 
319 class_alias( RevisionSlots::class, 'MediaWiki\Storage\RevisionSlots' );
Revision\RevisionSlots\$slots
SlotRecord[] callable $slots
Definition: RevisionSlots.php:38
Revision\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:33
Revision\RevisionSlots\__construct
__construct( $slots)
Definition: RevisionSlots.php:44
Revision\RevisionSlots\__sleep
__sleep()
Implemented to defy serialization.
Definition: RevisionSlots.php:74
Revision\RevisionSlots\getRolesWithDifferentContent
getRolesWithDifferentContent(RevisionSlots $other)
Find roles for which the $other RevisionSlots object has different content as this RevisionSlots obje...
Definition: RevisionSlots.php:283
Revision\RevisionSlots\computeSha1
computeSha1()
Computes the combined hash of the revisions's slots.
Definition: RevisionSlots.php:190
Revision\RevisionSlots\hasSlot
hasSlot( $role)
Returns whether the given slot is set.
Definition: RevisionSlots.php:123
Revision\SlotRecord\isInherited
isInherited()
Whether this slot was inherited from an older revision.
Definition: SlotRecord.php:420
$s
$s
Definition: mergeMessageFileList.php:186
Revision\RevisionSlots\setSlotsInternal
setSlotsInternal(array $slots)
Definition: RevisionSlots.php:57
Revision\RevisionSlots\hasSameContent
hasSameContent(RevisionSlots $other)
Checks whether the other RevisionSlots instance has the same content as this instance.
Definition: RevisionSlots.php:248
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Revision
Created by PhpStorm.
Definition: FallbackSlotRoleHandler.php:23
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:213
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Revision\SlotRecord\base36Sha1
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Definition: SlotRecord.php:607
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
Revision\SlotRecord\getSha1
getSha1()
Returns the content size.
Definition: SlotRecord.php:538
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
Revision\SlotRecord\getSize
getSize()
Returns the content size.
Definition: SlotRecord.php:522
Revision\RevisionSlots\getContent
getContent( $role)
Returns the Content of the given slot.
Definition: RevisionSlots.php:91
Revision\RevisionSlots\getInheritedSlots
getInheritedSlots()
Return all slots that are not not originate in the revision they belong to (that is,...
Definition: RevisionSlots.php:230
Revision\RevisionSlots\getSlots
getSlots()
Returns an associative array that maps role names to SlotRecords.
Definition: RevisionSlots.php:163
Content
Base interface for content objects.
Definition: Content.php:34
Revision\RevisionSlots\getSlot
getSlot( $role)
Returns the SlotRecord of the given slot.
Definition: RevisionSlots.php:106
Revision\RevisionSlots\computeSize
computeSize()
Computes the total nominal size of the revision's slots, in bogo-bytes.
Definition: RevisionSlots.php:148
Revision\RevisionSlots\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionSlots.php:135
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:35
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$t
$t
Definition: testCompression.php:69
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39