-
Notifications
You must be signed in to change notification settings - Fork 59
/
AffectedPagesFinder.php
385 lines (322 loc) 路 9.51 KB
/
AffectedPagesFinder.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
<?php
declare( strict_types = 1 );
namespace Wikibase\Client\Changes;
use ArrayIterator;
use InvalidArgumentException;
use MediaWiki\Cache\LinkBatchFactory;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Title;
use TitleFactory;
use Traversable;
use UnexpectedValueException;
use Wikibase\Client\Usage\EntityUsage;
use Wikibase\Client\Usage\PageEntityUsages;
use Wikibase\Client\Usage\UsageAspectTransformer;
use Wikibase\Client\Usage\UsageLookup;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\Lib\Changes\Change;
use Wikibase\Lib\Changes\EntityChange;
use Wikibase\Lib\Changes\ItemChange;
/**
* @license GPL-2.0-or-later
* @author Daniel Kinzler
* @author Katie Filbert < aude.wiki@gmail.com >
*/
class AffectedPagesFinder {
/**
* @var UsageLookup
*/
private $usageLookup;
/**
* @var TitleFactory
*/
private $titleFactory;
/** @var LinkBatchFactory */
private $linkBatchFactory;
/**
* @var string
*/
private $siteId;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var bool
*/
private $checkPageExistence;
/**
* @param UsageLookup $usageLookup
* @param TitleFactory $titleFactory
* @param LinkBatchFactory $linkBatchFactory
* @param string $siteId
* @param LoggerInterface|null $logger
* @param bool $checkPageExistence To disable slow filtering that is not relevant in test
* scenarios. Not meant to be used in production!
*
* @throws InvalidArgumentException
*/
public function __construct(
UsageLookup $usageLookup,
TitleFactory $titleFactory,
LinkBatchFactory $linkBatchFactory,
string $siteId,
?LoggerInterface $logger = null,
bool $checkPageExistence = true
) {
$this->usageLookup = $usageLookup;
$this->titleFactory = $titleFactory;
$this->linkBatchFactory = $linkBatchFactory;
$this->siteId = $siteId;
$this->logger = $logger ?: new NullLogger();
$this->checkPageExistence = $checkPageExistence;
}
/**
* @param Change $change
*
* @return PageEntityUsages[]
*/
public function getAffectedUsagesByPage( Change $change ) {
if ( $change instanceof EntityChange ) {
$usages = $this->getAffectedPages( $change );
return $this->filterUpdates( $usages );
}
return [];
}
/**
* @param EntityChange $change
*
* @return string[]
*/
public function getChangedAspects( EntityChange $change ) {
$aspects = [];
$diffAspects = $change->getCompactDiff();
if ( $diffAspects->getSiteLinkChanges() !== [] ) {
$sitelinkChanges = $diffAspects->getSiteLinkChanges();
$aspects[] = EntityUsage::SITELINK_USAGE;
if ( isset( $sitelinkChanges[$this->siteId] )
&& !$this->isBadgesOnlyChange( $sitelinkChanges[$this->siteId] )
) {
$aspects[] = EntityUsage::TITLE_USAGE;
}
}
if ( $diffAspects->getLabelChanges() !== [] ) {
$labelAspects = $this->getChangedTermAspects(
EntityUsage::LABEL_USAGE,
$diffAspects->getLabelChanges()
);
$aspects = array_merge( $aspects, $labelAspects );
}
if ( $diffAspects->getDescriptionChanges() !== [] ) {
$descriptionsAspects = $this->getChangedTermAspects(
EntityUsage::DESCRIPTION_USAGE,
$diffAspects->getDescriptionChanges()
);
$aspects = array_merge( $aspects, $descriptionsAspects );
}
if ( $diffAspects->getStatementChanges() !== [] ) {
$statementAspects = $this->getChangedStatementAspects(
$diffAspects->getStatementChanges()
);
$aspects = array_merge( $aspects, $statementAspects );
}
if ( $diffAspects->hasOtherChanges() !== false ) {
$aspects[] = EntityUsage::OTHER_USAGE;
}
if ( $aspects === [] ) {
// This is needed when diff is suppressed for performance reasons
$aspects[] = EntityUsage::OTHER_USAGE;
}
return $aspects;
}
/**
* @param string[] $diff
*
* @return string[]
*/
private function getChangedStatementAspects( array $diff ) {
$aspects = [];
foreach ( $diff as $propertyId ) {
$aspects[] = EntityUsage::makeAspectKey( EntityUsage::STATEMENT_USAGE, $propertyId );
}
$aspects[] = EntityUsage::makeAspectKey( EntityUsage::STATEMENT_USAGE );
return $aspects;
}
/**
* @param string $aspect
* @param string[] $diff
*
* @return string[]
*/
private function getChangedTermAspects( $aspect, array $diff ) {
$aspects = [];
foreach ( $diff as $lang ) {
$aspects[] = EntityUsage::makeAspectKey( $aspect, $lang );
}
$aspects[] = EntityUsage::makeAspectKey( $aspect );
return $aspects;
}
/**
* Returns the page updates implied by the given the change.
*
* @param EntityChange $change
*
* @return Traversable of PageEntityUsages
*/
private function getAffectedPages( EntityChange $change ) {
$entityId = $change->getEntityId();
$changedAspects = $this->getChangedAspects( $change );
$usages = $this->usageLookup->getPagesUsing(
// @todo: more than one entity at once!
[ $entityId ],
// Look up pages that are marked as either using one of the changed or all aspects
array_merge( $changedAspects, [ EntityUsage::ALL_USAGE ] )
);
// @todo: use iterators throughout!
$usages = iterator_to_array( $usages, true );
$usages = $this->transformAllPageEntityUsages( $usages, $entityId, $changedAspects );
if ( $change instanceof ItemChange && in_array( EntityUsage::TITLE_USAGE, $changedAspects ) ) {
$diffChangedAspects = $change->getCompactDiff();
$namesFromDiff = $this->getPagesReferencedInDiff(
$diffChangedAspects->getSiteLinkChanges()
);
$titlesFromDiff = $this->getTitlesFromTexts( $namesFromDiff );
$usagesFromDiff = $this->makeVirtualUsages(
$titlesFromDiff,
$entityId,
[ EntityUsage::SITELINK_USAGE ]
);
//FIXME: we can't really merge if $usages is an iterator, not an array.
//TODO: Inject $usagesFromDiff "on the fly" while streaming other usages.
//NOTE: $usages must pass through mergeUsagesInto for re-indexing
$mergedUsages = [];
$this->mergeUsagesInto( $usages, $mergedUsages );
$this->mergeUsagesInto( $usagesFromDiff, $mergedUsages );
$usages = $mergedUsages;
}
return new ArrayIterator( $usages );
}
/**
* @param PageEntityUsages[] $from
* @param PageEntityUsages[] &$into Array to merge into
*/
private function mergeUsagesInto( array $from, array &$into ) {
foreach ( $from as $pageEntityUsages ) {
$key = $pageEntityUsages->getPageId();
if ( isset( $into[$key] ) ) {
$into[$key]->addUsages( $pageEntityUsages->getUsages() );
} else {
$into[$key] = $pageEntityUsages;
}
}
}
/**
* @param array[] $siteLinkDiff
*
* @throws UnexpectedValueException
* @return string[]
*/
private function getPagesReferencedInDiff( array $siteLinkDiff ) {
$pagesToUpdate = [];
$siteLinkDiffWiki = $siteLinkDiff[$this->siteId];
if ( $siteLinkDiffWiki[0] !== null ) {
$pagesToUpdate[] = $siteLinkDiffWiki[0];
}
if ( $siteLinkDiffWiki[1] !== null ) {
$pagesToUpdate[] = $siteLinkDiffWiki[1];
}
return $pagesToUpdate;
}
/**
* @param array $siteLinkDiff
*
* @return bool
*/
private function isBadgesOnlyChange( array $siteLinkDiff ) {
return ( $siteLinkDiff[0] === $siteLinkDiff[1] && $siteLinkDiff[2] === true );
}
/**
* Filters updates. This removes duplicates and non-existing pages.
*
* @param Traversable $usages A traversable of PageEntityUsages.
*
* @return PageEntityUsages[]
*/
private function filterUpdates( Traversable $usages ) {
$titlesToUpdate = [];
/** @var PageEntityUsages $pageEntityUsages */
foreach ( $usages as $pageEntityUsages ) {
$title = $this->titleFactory->newFromID( $pageEntityUsages->getPageId() );
if ( !$title || ( $this->checkPageExistence && !$title->exists() ) ) {
continue;
}
$key = $pageEntityUsages->getPageId();
$titlesToUpdate[$key] = $pageEntityUsages;
}
return $titlesToUpdate;
}
/**
* @param string[] $names
*
* @return Title[]
*/
private function getTitlesFromTexts( array $names ) {
$titles = [];
foreach ( $names as $name ) {
$title = $this->titleFactory->newFromText( $name );
if ( $title ) {
$titles[] = $title;
}
}
return $titles;
}
/**
* @param Title[] $titles
* @param EntityId $entityId
* @param string[] $aspects
*
* @return PageEntityUsages[]
*/
private function makeVirtualUsages( array $titles, EntityId $entityId, array $aspects ) {
$usagesForItem = [];
foreach ( $aspects as $aspect ) {
list( $aspect, $modifier ) = EntityUsage::splitAspectKey( $aspect );
$usagesForItem[] = new EntityUsage( $entityId, $aspect, $modifier );
}
// bulk-load the page IDs into the LinkCache
$this->linkBatchFactory->newLinkBatch( $titles )->execute();
$usagesPerPage = [];
foreach ( $titles as $title ) {
$pageId = $title->getArticleID();
if ( $pageId === 0 ) {
$this->logger->debug(
'{method}: Article ID for {titleFullText} is 0',
[ 'method' => __METHOD__, 'titleFullText' => $title->getFullText() ]
);
continue;
}
$usagesPerPage[$pageId] = new PageEntityUsages( $pageId, $usagesForItem );
}
return $usagesPerPage;
}
/**
* @param PageEntityUsages[] $usages
* @param EntityId $entityId
* @param string[] $changedAspects
*
* @return PageEntityUsages[]
*/
private function transformAllPageEntityUsages( array $usages, EntityId $entityId, array $changedAspects ) {
$aspectTransformer = new UsageAspectTransformer();
$aspectTransformer->setRelevantAspects( $entityId, $changedAspects );
$transformed = [];
foreach ( $usages as $key => $usagesOnPage ) {
$transformedUsagesOnPage = $aspectTransformer->transformPageEntityUsages( $usagesOnPage );
if ( !$transformedUsagesOnPage->isEmpty() ) {
$transformed[$key] = $transformedUsagesOnPage;
}
}
return $transformed;
}
}