forked from filbertkm/WikibaseImport
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEntityImporter.php
More file actions
189 lines (143 loc) · 4.83 KB
/
EntityImporter.php
File metadata and controls
189 lines (143 loc) · 4.83 KB
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
<?php
namespace Wikibase\Import;
use Psr\Log\LoggerInterface;
use User;
use Wikibase\DataModel\Entity\BasicEntityIdParser;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\EntityIdValue;
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikibase\DataModel\Statement\StatementList;
use Wikibase\Import\Store\ImportedEntityMappingStore;
use Wikibase\Lib\Store\EntityStore;
class EntityImporter {
private $statementsImporter;
private $badgeItemUpdater;
private $apiEntityLookup;
private $entityStore;
private $entityMappingStore;
private $logger;
private $statementsCountLookup;
private $idParser;
private $importUser;
private $batchSize;
public function __construct(
StatementsImporter $statementsImporter,
BadgeItemUpdater $badgeItemUpdater,
ApiEntityLookup $apiEntityLookup,
EntityStore $entityStore,
ImportedEntityMappingStore $entityMappingStore,
StatementsCountLookup $statementsCountLookup,
LoggerInterface $logger
) {
$this->statementsImporter = $statementsImporter;
$this->badgeItemUpdater = $badgeItemUpdater;
$this->apiEntityLookup = $apiEntityLookup;
$this->entityStore = $entityStore;
$this->entityMappingStore = $entityMappingStore;
$this->statementsCountLookup = $statementsCountLookup;
$this->logger = $logger;
$this->idParser = new BasicEntityIdParser();
$this->importUser = User::newFromSession();
$this->batchSize = 10;
}
public function importEntities( array $ids, $importStatements = true ) {
$batches = array_chunk( $ids, $this->batchSize );
$stashedEntities = array();
foreach( $batches as $batch ) {
$entities = $this->apiEntityLookup->getEntities( $batch );
if ( $entities ) {
$this->importBadgeItems( $entities );
} else {
$this->logger->error( 'Failed to retrieve items for batch' );
}
$stashedEntities = array_merge( $stashedEntities, $this->importBatch( $batch ) );
}
if ( $importStatements === true ) {
foreach( $stashedEntities as $entity ) {
$referencedEntities = $this->getReferencedEntities( $entity );
$this->importEntities( $referencedEntities, false );
$localId = $this->entityMappingStore->getLocalId( $entity->getId() );
if ( $localId && !$this->statementsCountLookup->hasStatements( $localId ) ) {
$this->statementsImporter->importStatements( $entity );
} else {
$this->logger->info(
'Statements already imported for ' . $entity->getId()->getSerialization()
);
}
}
}
}
private function importBatch( array $batch ) {
$entities = $this->apiEntityLookup->getEntities( $batch );
if ( !is_array( $entities ) ) {
$this->logger->error( 'Failed to import batch' );
return array();
}
$stashedEntities = array();
foreach( $entities as $originalId => $entity ) {
$stashedEntities[] = $entity->copy();
$originalEntityId = $this->idParser->parse( $originalId );
if ( !$this->entityMappingStore->getLocalId( $originalEntityId ) ) {
try {
$this->logger->info( "Creating $originalId" );
$entityRevision = $this->createEntity( $entity );
$localId = $entityRevision->getEntity()->getId();
$this->entityMappingStore->add( $originalEntityId, $localId );
} catch( \Exception $ex ) {
$this->logger->error( "Failed to add $originalId" );
$this->logger->error( $ex->getMessage() );
}
} else {
$this->logger->info( "$originalId already imported" );
}
}
return $stashedEntities;
}
private function createEntity( EntityDocument $entity ) {
$entity->setId( null );
$entity->setStatements( new StatementList() );
if ( $entity instanceof Item ) {
$siteLinkList = $this->badgeItemUpdater->replaceBadges( $entity->getSiteLinkList() );
$entity->setSiteLinkList( $siteLinkList );
}
return $this->entityStore->saveEntity(
$entity,
'Import entity',
$this->importUser,
EDIT_NEW
);
}
private function getBadgeItems( array $entities ) {
$badgeItems = array();
foreach( $entities as $entity ) {
if ( !$entity instanceof Item ) {
continue;
}
foreach( $entity->getSiteLinks() as $siteLink ) {
foreach( $siteLink->getBadges() as $badge ) {
$badgeItems[] = $badge->getSerialization();
}
}
}
return $badgeItems;
}
private function getReferencedEntities( EntityDocument $entity ) {
$snaks = $entity->getStatements()->getAllSnaks();
$entities = array();
foreach( $snaks as $snak ) {
$entities[] = $snak->getPropertyId()->getSerialization();
if ( $snak instanceof PropertyValueSnak ) {
$value = $snak->getDataValue();
if ( $value instanceof EntityIdValue ) {
$entities[] = $value->getEntityId()->getSerialization();
}
}
}
return array_unique( $entities );
}
private function importBadgeItems( array $entities ) {
$badgeItems = $this->getBadgeItems( $entities );
$this->importEntities( $badgeItems, false );
}
}