MediaWiki
REL1_31
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
34
class
RevisionSlots
{
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\RevisionAccessException
Exception representing a failure to look up a revision.
Definition
RevisionAccessException.php:32
MediaWiki\Storage\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition
RevisionSlots.php:34
MediaWiki\Storage\RevisionSlots\getSlot
getSlot( $role)
Returns the SlotRecord of the given slot.
Definition
RevisionSlots.php:103
MediaWiki\Storage\RevisionSlots\getSlots
getSlots()
Returns an associative array that maps role names to SlotRecords.
Definition
RevisionSlots.php:160
MediaWiki\Storage\RevisionSlots\getContent
getContent( $role)
Returns the Content of the given slot.
Definition
RevisionSlots.php:88
MediaWiki\Storage\RevisionSlots\__sleep
__sleep()
Implemented to defy serialization.
Definition
RevisionSlots.php:71
MediaWiki\Storage\RevisionSlots\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition
RevisionSlots.php:132
MediaWiki\Storage\RevisionSlots\hasSlot
hasSlot( $role)
Returns whether the given slot is set.
Definition
RevisionSlots.php:120
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\RevisionSlots\__construct
__construct( $slots)
Definition
RevisionSlots.php:43
MediaWiki\Storage\RevisionSlots\$slots
SlotRecord[] callable $slots
Definition
RevisionSlots.php:37
MediaWiki\Storage\SlotRecord
Value object representing a content slot associated with a page revision.
Definition
SlotRecord.php:38
MediaWiki\Storage\SlotRecord\getSha1
getSha1()
Returns the content size.
Definition
SlotRecord.php:495
MediaWiki\Storage\SlotRecord\getSize
getSize()
Returns the content size.
Definition
SlotRecord.php:479
MediaWiki\Storage\SlotRecord\base36Sha1
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Definition
SlotRecord.php:564
Content
Base interface for content objects.
Definition
Content.php:34
MediaWiki\Storage
Definition
BlobAccessException.php:23
includes
Storage
RevisionSlots.php
Generated on Mon Nov 25 2024 15:35:31 for MediaWiki by
1.10.0