MediaWiki  1.31.0
RevisionSlots.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Storage;
24 
25 use Content;
26 use LogicException;
27 use 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 }
MediaWiki\Storage\RevisionSlots\getSlot
getSlot( $role)
Returns the SlotRecord of the given slot.
Definition: RevisionSlots.php:103
MediaWiki\Storage\SlotRecord\getSha1
getSha1()
Returns the content size.
Definition: SlotRecord.php:495
MediaWiki\Storage\SlotRecord\base36Sha1
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Definition: SlotRecord.php:564
MediaWiki\Storage\RevisionSlots\__sleep
__sleep()
Implemented to defy serialization.
Definition: RevisionSlots.php:71
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
MediaWiki\Storage\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:34
MediaWiki\Storage\RevisionSlots\getSlots
getSlots()
Returns an associative array that maps role names to SlotRecords.
Definition: RevisionSlots.php:160
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\Storage\RevisionSlots\computeSize
computeSize()
Computes the total nominal size of the revision's slots, in bogo-bytes.
Definition: RevisionSlots.php:145
MediaWiki\Storage\RevisionSlots\setSlotsInternal
setSlotsInternal(array $slots)
Definition: RevisionSlots.php:56
MediaWiki\Storage\RevisionSlots\computeSha1
computeSha1()
Computes the combined hash of the revisions's slots.
Definition: RevisionSlots.php:187
MediaWiki\Storage\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:32
MediaWiki\Storage\RevisionSlots\getContent
getContent( $role)
Returns the Content of the given slot.
Definition: RevisionSlots.php:88
MediaWiki\Storage\RevisionSlots\__construct
__construct( $slots)
Definition: RevisionSlots.php:43
MediaWiki\Storage
Definition: BlobAccessException.php:23
Content
Base interface for content objects.
Definition: Content.php:34
MediaWiki\Storage\RevisionSlots\hasSlot
hasSlot( $role)
Returns whether the given slot is set.
Definition: RevisionSlots.php:120
MediaWiki\Storage\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:38
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
MediaWiki\Storage\SlotRecord\getSize
getSize()
Returns the content size.
Definition: SlotRecord.php:479
MediaWiki\Storage\RevisionSlots\$slots
SlotRecord[] callable $slots
Definition: RevisionSlots.php:37
MediaWiki\Storage\RevisionSlots\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionSlots.php:132
array
the array() calling protocol came about after MediaWiki 1.4rc1.