1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
<?php
/**
* Value object representing the set of slots belonging to a revision.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Revision;
use MediaWiki\Content\Content;
use Wikimedia\Assert\Assert;
use Wikimedia\NonSerializable\NonSerializableTrait;
/**
* Value object representing the set of slots belonging to a revision.
*
* @note RevisionSlots provides "raw" access to the slots and does not apply audience checks.
* If audience checks are desired, use RevisionRecord::getSlot() or RevisionRecord::getContent()
* instead.
*
* @newable
*
* @since 1.31
* @since 1.32 Renamed from MediaWiki\Storage\RevisionSlots
*/
class RevisionSlots {
use NonSerializableTrait;
/** @var SlotRecord[]|callable */
protected $slots;
/**
* @stable to call.
*
* @param SlotRecord[]|callable $slots SlotRecords,
* or a callback that returns such a structure.
*/
public function __construct( $slots ) {
Assert::parameterType( [ 'array', 'callable' ], $slots, '$slots' );
if ( is_callable( $slots ) ) {
$this->slots = $slots;
} else {
$this->setSlotsInternal( $slots );
}
}
/**
* @param SlotRecord[] $slots
*/
private function setSlotsInternal( array $slots ): void {
Assert::parameterElementType( SlotRecord::class, $slots, '$slots' );
$this->slots = [];
// re-key the slot array
foreach ( $slots as $slot ) {
$role = $slot->getRole();
$this->slots[$role] = $slot;
}
}
/**
* Returns the Content of the given slot.
* Call getSlotNames() to get a list of available slots.
*
* Note that for mutable Content objects, each call to this method will return a
* fresh clone.
*
* @see SlotRecord::getContent()
*
* @param string $role The role name of the desired slot
*
* @throws RevisionAccessException if the slot does not exist or slot data
* could not be lazy-loaded. See SlotRecord::getContent() for details.
* @return Content
*/
public function getContent( $role ): Content {
// Return a copy to be safe. Immutable content objects return $this from copy().
return $this->getSlot( $role )->getContent()->copy();
}
/**
* Returns the SlotRecord of the given slot.
* Call getSlotNames() to get a list of available slots.
*
* @param string $role The role name of the desired slot
*
* @throws RevisionAccessException if the slot does not exist or slot data
* could not be lazy-loaded.
* @return SlotRecord
*/
public function getSlot( $role ): SlotRecord {
$slots = $this->getSlots();
if ( isset( $slots[$role] ) ) {
return $slots[$role];
} else {
throw new RevisionAccessException(
'No such slot: {role}',
[ 'role' => $role ]
);
}
}
/**
* Returns whether the given slot is set.
*
* @param string $role The role name of the desired slot
*
* @return bool
*/
public function hasSlot( $role ): bool {
$slots = $this->getSlots();
return isset( $slots[$role] );
}
/**
* Returns the slot names (roles) of all slots present in this revision.
* getContent() will succeed only for the names returned by this method.
*
* @return string[]
*/
public function getSlotRoles(): array {
$slots = $this->getSlots();
return array_keys( $slots );
}
/**
* Computes the total nominal size of the revision's slots, in bogo-bytes.
*
* @warning This is potentially expensive! It may cause some slots' content to be loaded
* and deserialized.
*
* @return int
*/
public function computeSize(): int {
return array_reduce( $this->getPrimarySlots(), static function ( $accu, SlotRecord $slot ) {
return $accu + $slot->getSize();
}, 0 );
}
/**
* Returns an associative array that maps role names to SlotRecords. Each SlotRecord
* represents the content meta-data of a slot, together they define the content of
* a revision.
*
* @note This may cause the content meta-data for the revision to be lazy-loaded.
*
* @return SlotRecord[] revision slot/content rows, keyed by slot role name.
*/
public function getSlots(): array {
if ( is_callable( $this->slots ) ) {
$slots = ( $this->slots )();
Assert::postcondition(
is_array( $slots ),
'Slots info callback should return an array of objects'
);
$this->setSlotsInternal( $slots );
}
return $this->slots;
}
/**
* Computes the combined hash of the revisions's slots.
*
* @note For backwards compatibility, the combined hash of a single slot
* is that slot's hash. For consistency, the combined hash of an empty set of slots
* is the hash of the empty string.
*
* @warning This is potentially expensive! It may cause some slots' content to be loaded
* and deserialized, then re-serialized and hashed.
*
* @return string
*/
public function computeSha1(): string {
$slots = $this->getPrimarySlots();
ksort( $slots );
if ( !$slots ) {
return SlotRecord::base36Sha1( '' );
}
return array_reduce( $slots, static function ( $accu, SlotRecord $slot ) {
return $accu === null
? $slot->getSha1()
: SlotRecord::base36Sha1( $accu . $slot->getSha1() );
}, null );
}
/**
* Return all slots that belong to the revision they originate from (that is,
* they are not inherited from some other revision).
*
* @note This may cause the slot meta-data for the revision to be lazy-loaded.
*
* @return SlotRecord[]
*/
public function getOriginalSlots(): array {
return array_filter(
$this->getSlots(),
static function ( SlotRecord $slot ) {
return !$slot->isInherited();
}
);
}
/**
* Return all slots that are not originate in the revision they belong to (that is,
* they are inherited from some other revision).
*
* @note This may cause the slot meta-data for the revision to be lazy-loaded.
*
* @return SlotRecord[]
*/
public function getInheritedSlots(): array {
return array_filter(
$this->getSlots(),
static function ( SlotRecord $slot ) {
return $slot->isInherited();
}
);
}
/**
* Return all primary slots (those that are not derived).
*
* @return SlotRecord[]
* @since 1.36
*/
public function getPrimarySlots(): array {
return array_filter(
$this->getSlots(),
static function ( SlotRecord $slot ) {
return !$slot->isDerived();
}
);
}
/**
* Checks whether the other RevisionSlots instance has the same content
* as this instance. Note that this does not mean that the slots have to be the same:
* they could for instance belong to different revisions.
*
* @param RevisionSlots $other
*
* @return bool
*/
public function hasSameContent( RevisionSlots $other ): bool {
if ( $other === $this ) {
return true;
}
$aSlots = $this->getSlots();
$bSlots = $other->getSlots();
ksort( $aSlots );
ksort( $bSlots );
if ( array_keys( $aSlots ) !== array_keys( $bSlots ) ) {
return false;
}
foreach ( $aSlots as $role => $s ) {
$t = $bSlots[$role];
if ( !$s->hasSameContent( $t ) ) {
return false;
}
}
return true;
}
/**
* Find roles for which the $other RevisionSlots object has different content
* as this RevisionSlots object, including any roles that are present in one
* but not the other.
*
* @param RevisionSlots $other
*
* @return string[] a list of slot roles that are different.
*/
public function getRolesWithDifferentContent( RevisionSlots $other ): array {
if ( $other === $this ) {
return [];
}
$aSlots = $this->getSlots();
$bSlots = $other->getSlots();
ksort( $aSlots );
ksort( $bSlots );
$different = array_keys( array_merge(
array_diff_key( $aSlots, $bSlots ),
array_diff_key( $bSlots, $aSlots )
) );
/** @var SlotRecord[] $common */
$common = array_intersect_key( $aSlots, $bSlots );
foreach ( $common as $role => $s ) {
$t = $bSlots[$role];
if ( !$s->hasSameContent( $t ) ) {
$different[] = $role;
}
}
return $different;
}
}
|