Skip to content

Commit 6ba7596

Browse files
committed
Merge pull request #10 from brightbyte/GracefulMappingFailure
Avoid fatal error when an EntityIdValue cannot be mapped
2 parents 252d7bc + 26299fb commit 6ba7596

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

src/EntityImporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function importEntities( array $ids, $importStatements = true ) {
8181

8282
$localId = $this->entityMappingStore->getLocalId( $entity->getId() );
8383

84-
if ( !$this->statementsCountLookup->hasStatements( $localId ) ) {
84+
if ( $localId && !$this->statementsCountLookup->hasStatements( $localId ) ) {
8585
$this->statementsImporter->importStatements( $entity );
8686
} else {
8787
$this->logger->info(

src/StatementCopier.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ public function copy( Statement $statement ) {
5050
}
5151

5252
private function copySnak( Snak $mainSnak ) {
53-
$newPropertyId = $this->entityMappingStore->getLocalId( $mainSnak->getPropertyId() );
53+
$oldPropertyId = $mainSnak->getPropertyId();
54+
$newPropertyId = $this->entityMappingStore->getLocalId( $oldPropertyId );
55+
56+
if ( !$newPropertyId ) {
57+
$this->logger->error( "Entity not found for $oldPropertyId." );
58+
$newPropertyId = $oldPropertyId;
59+
}
5460

5561
switch( $mainSnak->getType() ) {
5662
case 'somevalue':
@@ -61,7 +67,13 @@ private function copySnak( Snak $mainSnak ) {
6167
$value = $mainSnak->getDataValue();
6268

6369
if ( $value instanceof EntityIdValue ) {
64-
$value = $this->replaceEntityIdValue( $value );
70+
$newValue = $this->replaceEntityIdValue( $value );
71+
72+
if ( $newValue ) {
73+
// If we can't map the id, keep the old one.
74+
// replaceEntityIdValue() already logged the issue.
75+
$value = $newValue;
76+
}
6577
}
6678

6779
return new PropertyValueSnak( $newPropertyId, $value );
@@ -74,6 +86,7 @@ private function replaceEntityIdValue( EntityIdValue $value ) {
7486

7587
if ( !$localId ) {
7688
$this->logger->error( "Entity not found for $originalId." );
89+
return null;
7790
}
7891

7992
return new EntityIdValue( $localId );

src/StatementsImporter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public function importStatements( EntityDocument $entity ) {
5757

5858
if ( !$localId ) {
5959
$this->logger->error( $entityId->getSerialization() . ' not found' );
60-
}
61-
62-
try {
63-
$this->addStatementList( $localId, $statements );
64-
} catch ( \Exception $ex ) {
65-
$this->logger->error( $ex->getMessage() );
60+
} else {
61+
try {
62+
$this->addStatementList( $localId, $statements );
63+
} catch ( \Exception $ex ) {
64+
$this->logger->error( $ex->getMessage() );
65+
}
6666
}
6767
} else {
6868
$this->logger->error( 'EntityId not set for entity' );

src/Store/DBImportedEntityMappingStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function add( EntityId $originalId, EntityId $localId ) {
4343
/**
4444
* @param EntityId $originalId
4545
*
46-
* @return EntityId
46+
* @return EntityId|null
4747
*/
4848
public function getLocalId( EntityId $originalId ) {
4949
$dbw = $this->loadBalancer->getConnection( DB_MASTER );
@@ -67,7 +67,7 @@ public function getLocalId( EntityId $originalId ) {
6767
/**
6868
* @param EntityId $localId
6969
*
70-
* @return EntityId
70+
* @return EntityId|null
7171
*/
7272
public function getOriginalId( EntityId $localId ) {
7373
$dbw = $this->loadBalancer->getConnection( DB_MASTER );

src/Store/ImportedEntityMappingStore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public function add( EntityId $originalId, EntityId $localId );
1515
/**
1616
* @param EntityId $originalId
1717
*
18-
* @return EntityId
18+
* @return EntityId|null
1919
*/
2020
public function getLocalId( EntityId $originalId );
2121

2222
/**
2323
* @param EntityId $localId
2424
*
25-
* @return EntityId
25+
* @return EntityId|null
2626
*/
2727
public function getOriginalId( EntityId $localId );
2828

0 commit comments

Comments
 (0)