-
Notifications
You must be signed in to change notification settings - Fork 59
/
EntityContent.php
486 lines (413 loc) 路 13.6 KB
/
EntityContent.php
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
namespace Wikibase\Repo\Content;
use AbstractContent;
use Content;
use Diff\Differ\MapDiffer;
use Diff\DiffOp\Diff\Diff;
use Diff\Patcher\MapPatcher;
use Diff\Patcher\PatcherException;
use LogicException;
use MediaWiki\MediaWikiServices;
use RuntimeException;
use Serializers\Exceptions\SerializationException;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\EntityRedirect;
use Wikibase\DataModel\Term\LabelsProvider;
use Wikibase\Repo\ArrayValueCollector;
use Wikibase\Repo\FingerprintSearchTextGenerator;
use Wikibase\Repo\WikibaseRepo;
/**
* Abstract content object for articles representing Wikibase entities.
*
* For more information on the relationship between entities and wiki pages, see
* docs/entity-storage.wiki.
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
* @author Daniel Kinzler
* @author Bene* < benestar.wikimedia@gmail.com >
*
* @method \Wikibase\Repo\Content\EntityHandler getContentHandler()
*/
abstract class EntityContent extends AbstractContent {
/**
* Flag for use with EntityHandler::validateSave(), indicating that no pre-save validation should be applied.
* Can be passed in via EditEntity::attemptSave, EntityStore::saveEntity,
* as well as WikiPage::doUserEditContent()
*
* @note: must not collide with the EDIT_XXX flags defined by MediaWiki core in Defines.php.
*/
public const EDIT_IGNORE_CONSTRAINTS = 1024;
/**
* @see Content::isValid()
*
* @return bool True if this content object is valid for saving. False if there is no entity, or
* the entity does not have an ID set.
*/
public function isValid() {
if ( $this->isRedirect() ) {
// Under some circumstances, the handler will not support redirects,
// but it's still possible to construct Content objects that represent
// redirects. In such a case, make sure such Content objects are considered
// invalid and do not get saved.
return $this->getContentHandler()->supportsRedirects();
}
$holder = $this->getEntityHolder();
return $holder !== null && $holder->getEntityId() !== null;
}
/**
* @see EntityContent::isCountable
*
* @param bool|null $hasLinks
*
* @return bool True if this is not a redirect and the item is not empty.
*/
public function isCountable( $hasLinks = null ) {
return !$this->isRedirect() && !$this->isEmpty();
}
/**
* Returns the EntityRedirect represented by this EntityContent, or null if this
* EntityContent is not a redirect.
*
* @note This default implementation will fail if isRedirect() is true.
* Subclasses that support redirects must override getEntityRedirect().
*
* @throws LogicException
* @return EntityRedirect|null
*/
public function getEntityRedirect() {
if ( $this->isRedirect() ) {
throw new LogicException( 'EntityContent subclasses that support redirects must override getEntityRedirect()' );
}
return null;
}
/**
* Returns the entity contained by this entity content.
* Deriving classes typically have a more specific get method as
* for greater clarity and type hinting.
* @note This method cannot be called on redirects (targets will never be resolved)
*
* @return EntityDocument
*/
abstract public function getEntity();
/**
* Returns a holder for the entity contained in this EntityContent object.
* @note This method cannot be called on redirects (targets will never be resolved)
*
* @return EntityHolder|null
*/
abstract public function getEntityHolder();
/**
* @throws RuntimeException if the content object is empty or no entity ID is set
* @return EntityId
*/
public function getEntityId(): EntityId {
if ( $this->isRedirect() ) {
return $this->getEntityRedirect()->getEntityId();
}
$holder = $this->getEntityHolder();
if ( $holder !== null ) {
$id = $holder->getEntityId();
if ( $id !== null ) {
return $id;
}
}
throw new RuntimeException( 'EntityContent was constructed without an EntityId!' );
}
/**
* @return string A string representing the content in a way useful for building a full text
* search index.
*/
public function getTextForSearchIndex() {
if ( $this->isRedirect() ) {
return '';
}
$searchTextGenerator = new FingerprintSearchTextGenerator();
$text = $searchTextGenerator->generate( $this->getEntity() );
if ( !MediaWikiServices::getInstance()->getHookContainer()
->run( 'WikibaseTextForSearchIndex', [ $this, &$text ] )
) {
return '';
}
return $text;
}
/**
* @return string Returns the string representation of the redirect
* represented by this EntityContent (if any).
*
* @note Will fail if this EntityContent is not a redirect.
*/
protected function getRedirectText() {
$target = $this->getRedirectTarget();
return '#REDIRECT [[' . $target->getFullText() . ']]';
}
/**
* Get the keys within this Contents Entity JSON that should be removed for
* text passed to edit filters.
*
* @return string[] Keys to ignore
*/
abstract protected function getIgnoreKeysForFilters();
/**
* @return string A string representing the content in a way useful for content filtering as
* performed by extensions like AbuseFilter.
*/
public function getTextForFilters() {
if ( $this->isRedirect() ) {
return $this->getRedirectText();
}
// @todo this text for filters stuff should be it's own class with test coverage!
$codec = WikibaseRepo::getEntityContentDataCodec();
$json = $codec->encodeEntity( $this->getEntity(), CONTENT_FORMAT_JSON );
$data = json_decode( $json, true );
$values = ArrayValueCollector::collectValues( $data, $this->getIgnoreKeysForFilters() );
return implode( "\n", $values );
}
/**
* @return string|false The wikitext to include when another page includes this content, or false if
* the content is not includable in a wikitext page.
*/
public function getWikitextForTransclusion() {
return false;
}
/**
* Returns a textual representation of the content suitable for use in edit summaries and log messages.
*
* @param int $maxLength maximum length of the summary text
* @return string
*/
public function getTextForSummary( $maxLength = 250 ) {
if ( $this->isRedirect() ) {
return $this->getRedirectText();
}
$entity = $this->getEntity();
// TODO: This assumes all entities not implementing their own getTextForSummary are LabelsProvider. Fix it.
if ( !( $entity instanceof LabelsProvider ) ) {
throw new LogicException(
"Entity type {$entity->getType()} must implement its own getTextForSummary method."
);
}
$labels = $entity->getLabels();
if ( $labels->isEmpty() ) {
return '';
}
$language = MediaWikiServices::getInstance()->getContentLanguage();
if ( $labels->hasTermForLanguage( $language->getCode() ) ) {
$label = $labels->getByLanguage( $language->getCode() )->getText();
return $language->truncateForDatabase( $label, $maxLength );
}
// Return first term it can find
$term = $labels->getIterator()->current();
return $language->truncateForDatabase( $term->getText(), $maxLength );
}
/**
* Returns an array structure for the redirect represented by this EntityContent, if any.
*
* @note This may or may not be consistent with what EntityContentCodec does.
* It is intended to be used primarily for diffing.
* @return string[]
*/
private function getRedirectData(): array {
// NOTE: keep in sync with getPatchedRedirect
$data = [];
if ( $this->isValid() ) {
$data['entity'] = $this->getEntityId()->getSerialization();
}
if ( $this->isRedirect() ) {
$data['redirect'] = $this->getEntityRedirect()->getTargetId()->getSerialization();
}
return $data;
}
/**
* @see Content::getNativeData
*
* @note Avoid relying on this method! It bypasses EntityContentCodec, and does
* not make any guarantees about the structure of the array returned.
*
* @return string[]|EntityDocument An undefined data structure representing the content. This is
* not guaranteed to conform to any serialization structure used in the database or externally.
*/
public function getNativeData() {
if ( $this->isRedirect() ) {
return $this->getRedirectData();
}
// NOTE: this may or may not be consistent with what EntityContentCodec does!
$serializer = WikibaseRepo::getAllTypesEntitySerializer();
try {
return $serializer->serialize( $this->getEntity() );
} catch ( SerializationException $ex ) {
return $this->getEntity();
}
}
/**
* returns the content's nominal size in bogo-bytes.
*
* @return int
*/
public function getSize() {
return strlen( serialize( $this->getNativeData() ) );
}
/**
* Both contents will be considered equal if they have the same ID and equal Entity data. If
* one of the contents is considered "new", then matching IDs is not a criteria for them to be
* considered equal.
*
* @see Content::equals
*
* @param Content|null $that
*
* @return bool
*/
public function equals( Content $that = null ) {
if ( $that === $this ) {
return true;
}
if ( !( $that instanceof self ) || $that->getModel() !== $this->getModel() ) {
return false;
}
$thisRedirect = $this->getRedirectTarget();
$thatRedirect = $that->getRedirectTarget();
if ( $thisRedirect !== null ) {
if ( $thatRedirect === null ) {
return false;
} else {
return $thisRedirect->equals( $thatRedirect )
&& $this->getEntityRedirect()->equals( $that->getEntityRedirect() );
}
} elseif ( $thatRedirect !== null ) {
return false;
}
$thisHolder = $this->getEntityHolder();
$thatHolder = $that->getEntityHolder();
if ( !$thisHolder && !$thatHolder ) {
return true;
} elseif ( !$thisHolder || !$thatHolder ) {
return false;
}
$thisId = $thisHolder->getEntityId();
$thatId = $thatHolder->getEntityId();
if ( $thisId && $thatId && !$thisId->equals( $thatId ) ) {
return false;
}
return $thisHolder->getEntity()->equals( $thatHolder->getEntity() );
}
/**
* @return EntityDocument
*/
private function makeEmptyEntity() {
$handler = $this->getContentHandler();
return $handler->makeEmptyEntity();
}
/**
* Returns a diff between this EntityContent and the given EntityContent.
*
* @param self $toContent
*
* @return EntityContentDiff
*/
public function getDiff( EntityContent $toContent ) {
$fromContent = $this;
$differ = new MapDiffer();
$redirectDiffOps = $differ->doDiff(
$fromContent->getRedirectData(),
$toContent->getRedirectData()
);
$redirectDiff = new Diff( $redirectDiffOps, true );
$fromEntity = ( $fromContent->isRedirect() || $fromContent->getEntityHolder() === null ) ?
$this->makeEmptyEntity() : $fromContent->getEntity();
$toEntity = ( $toContent->isRedirect() || $toContent->getEntityHolder() === null ) ?
$this->makeEmptyEntity() : $toContent->getEntity();
$entityDiffer = WikibaseRepo::getEntityDiffer();
$entityDiff = $entityDiffer->diffEntities( $fromEntity, $toEntity );
return new EntityContentDiff( $entityDiff, $redirectDiff, $fromEntity->getType() );
}
/**
* Returns a patched copy of this Content object.
*
* @param EntityContentDiff $patch
*
* @throws PatcherException
* @return self
*/
public function getPatchedCopy( EntityContentDiff $patch ) {
$handler = $this->getContentHandler();
$redirAfterPatch = $this->getPatchedRedirect( $patch->getRedirectDiff() );
if ( $redirAfterPatch ) {
$patched = $handler->makeEntityRedirectContent( $redirAfterPatch );
if ( !$patched ) {
throw new PatcherException( 'Cannot create a redirect using content model '
. $this->getModel() . '!' );
}
} else {
if ( $this->isRedirect() ) {
$entityAfterPatch = $this->makeEmptyEntity();
$entityAfterPatch->setId( $this->getEntityId() );
} else {
$entityAfterPatch = $this->getEntity()->copy();
}
$patcher = WikibaseRepo::getEntityPatcher();
$patcher->patchEntity( $entityAfterPatch, $patch->getEntityDiff() );
$patched = $handler->makeEntityContent( new EntityInstanceHolder( $entityAfterPatch ) );
}
return $patched;
}
/**
* @param Diff $redirectPatch
*
* @return EntityRedirect|null
*/
private function getPatchedRedirect( Diff $redirectPatch ) {
// See getRedirectData() for the structure of the data array.
$redirData = $this->getRedirectData();
if ( !$redirectPatch->isEmpty() ) {
$patcher = new MapPatcher();
$redirData = $patcher->patch( $redirData, $redirectPatch );
}
if ( isset( $redirData['redirect'] ) ) {
$handler = $this->getContentHandler();
$entityId = $this->getEntityId();
$targetId = $handler->makeEntityId( $redirData['redirect'] );
return new EntityRedirect( $entityId, $targetId );
} else {
return null;
}
}
/**
* @return bool True if this is not a redirect and the page is empty.
*/
public function isEmpty() {
if ( $this->isRedirect() ) {
return false;
}
$holder = $this->getEntityHolder();
return $holder === null || $holder->getEntity()->isEmpty();
}
/**
* @see Content::copy
*
* @return self
*/
public function copy() {
$handler = $this->getContentHandler();
if ( $this->isRedirect() ) {
return $handler->makeEntityRedirectContent( $this->getEntityRedirect() );
}
$holder = $this->getEntityHolder();
if ( $holder !== null ) {
return $handler->makeEntityContent( new DeferredCopyEntityHolder( $holder ) );
}
// There is nothing mutable on an entirely empty content object.
return $this;
}
/**
* Returns a map of properties about the entity, to be recorded in
* MediaWiki's page_props table. The idea is to allow efficient lookups
* of entities based on such properties.
*
* @return array A map from property names to property values.
*/
public function getEntityPageProperties() {
return [];
}
}