-
Notifications
You must be signed in to change notification settings - Fork 56
/
SiteLinkUsageLookup.php
216 lines (182 loc) · 5.17 KB
/
SiteLinkUsageLookup.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
<?php
namespace Wikibase\Client\Usage;
use ArrayIterator;
use InvalidArgumentException;
use Traversable;
use Wikibase\Client\Store\TitleFactory;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Lib\Store\SiteLinkLookup;
/**
* UsageLookup implementation based on a SiteLinkLookup.
* This tracks the usage of directly connected items as EntityUsage::SITELINK_USAGE.
* Other types of usage are not tracked.
*
* @license GPL-2.0-or-later
* @author Daniel Kinzler
*/
class SiteLinkUsageLookup implements UsageLookup {
/**
* @var string
*/
protected $clientSiteId;
/**
* @var SiteLinkLookup
*/
protected $siteLinkLookup;
/**
* @var TitleFactory
*/
private $titleFactory;
/**
* @param string $clientSiteId The local wiki's global site id
* @param SiteLinkLookup $siteLinkLookup
* @param TitleFactory $titleFactory
*
* @throws InvalidArgumentException
*/
public function __construct( $clientSiteId, SiteLinkLookup $siteLinkLookup, TitleFactory $titleFactory ) {
if ( !is_string( $clientSiteId ) ) {
throw new InvalidArgumentException( '$clientSiteId must be a string' );
}
$this->clientSiteId = $clientSiteId;
$this->siteLinkLookup = $siteLinkLookup;
$this->titleFactory = $titleFactory;
}
/**
* @see UsageLookup::getUsagesForPage
*
* @param int $pageId
*
* @return EntityUsage[]
* @throws UsageTrackerException
*/
public function getUsagesForPage( $pageId ) {
$usages = [];
$title = $this->titleFactory->newFromID( $pageId );
$id = $this->siteLinkLookup->getItemIdForLink( $this->clientSiteId, $title->getPrefixedText() );
if ( $id !== null ) {
$usages[] = new EntityUsage( $id, EntityUsage::SITELINK_USAGE );
}
return $usages;
}
/**
* @see UsageLookup::getPagesUsing
*
* @param EntityId[] $entityIds
* @param string[] $aspects Which aspects to consider (if omitted, all aspects are considered).
* Use the EntityUsage::XXX_USAGE constants to represent aspects.
*
* @return Traversable of PageEntityUsages
* @throws UsageTrackerException
*/
public function getPagesUsing( array $entityIds, array $aspects = [] ) {
if ( empty( $entityIds ) ) {
return new ArrayIterator();
}
$numericItemIds = $this->getNumericItemIds( $entityIds );
$rows = $this->siteLinkLookup->getLinks( $numericItemIds, [ $this->clientSiteId ] );
$pageIds = $this->getPageEntityUsagesFromSiteLinkRows( $rows );
return new ArrayIterator( $pageIds );
}
/**
* Extracts numeric IDs from ItemIds; Other EntityIds are ignored.
*
* @param EntityId[] $ids
*
* @return int[]
*/
private function getNumericItemIds( array $ids ) {
$ids = array_filter( $ids, function ( EntityId $id ) {
return $id instanceof ItemId;
} );
return array_map(
function ( ItemId $id ) {
return $id->getNumericId();
},
$ids
);
}
/**
* @param array[] $rows Rows as returned by SiteLinkLookup::getLinks
*
* @return PageEntityUsages[]
*/
private function getPageEntityUsagesFromSiteLinkRows( array $rows ) {
$titleFactory = $this->titleFactory;
$pageEntityUsages = array_map(
function ( array $row ) use ( $titleFactory ) {
// $row = [ $siteId, $pageName, $numericItemId ];
$itemId = ItemId::newFromNumber( $row[2] );
$title = $titleFactory->newFromText( $row[1] );
if ( !$title ) {
return null;
}
// NOTE: since we don't know how the item is used on the linked page, assume "all" usage.
$usage = new EntityUsage( $itemId, EntityUsage::ALL_USAGE );
$pageId = $title->getArticleID();
if ( $pageId === 0 ) {
wfDebugLog( 'WikibaseChangeNotification', __METHOD__ . ': Article ID for '
. $title->getFullText() . ' is 0.' );
return null;
}
return new PageEntityUsages( $pageId, [ $usage ] );
},
$rows
);
$pageEntityUsages = array_filter( $pageEntityUsages );
return $pageEntityUsages;
}
/**
* @param array[] $rows Rows as returned by SiteLinkLookup::getLinks
*
* @return int[]
*/
private function getItemIdsFromSiteLinkRows( array $rows ) {
$itemIds = array_map(
function ( array $row ) {
return (int)$row[2];
},
$rows
);
array_unique( $itemIds );
return $itemIds;
}
/**
* @param int[] $numericIds
*
* @return ItemId[]
*/
private function makeItemIds( array $numericIds ) {
return array_map(
function ( $numericId ) {
return ItemId::newFromNumber( $numericId );
},
$numericIds
);
}
/**
* @see UsageLookup::getUnusedEntities
*
* @param EntityId[] $entityIds
*
* @return EntityId[] A list of elements of $entities that are unused.
*/
public function getUnusedEntities( array $entityIds ) {
if ( empty( $entityIds ) ) {
return [];
}
// Non-item entities are always considered unused by this implementation.
$nonItemIds = array_filter( $entityIds, function ( EntityId $id ) {
return !( $id instanceof ItemId );
} );
$numericItemIds = $this->getNumericItemIds( $entityIds );
$rows = $this->siteLinkLookup->getLinks( $numericItemIds, [ $this->clientSiteId ] );
$used = $this->getItemIdsFromSiteLinkRows( $rows );
$unusedIds = array_diff( $numericItemIds, $used );
return array_merge(
$nonItemIds,
$this->makeItemIds( $unusedIds )
);
}
}