From 307b44ba6f2cb51061e682b5d422374244576804 Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Fri, 21 Apr 2017 19:05:42 +0200 Subject: [PATCH] Search entities in the correct namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of assuming that all entities are in namespace 0 (which is only true in a Wikidata-like installation, but not in a default Wikibase installation, and even then only for items, not properties), take the actual namespace of the requested entity from an injected EntityNamespaceLookup. Also, if we can’t find the entity, throw an exception instead of returning count 0: it’s better to fail the import than to duplicate statements. Fixes #22. --- src/EntityImporterFactory.php | 8 +++++++- src/PagePropsStatementCountLookup.php | 12 +++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/EntityImporterFactory.php b/src/EntityImporterFactory.php index 9394128..3564133 100644 --- a/src/EntityImporterFactory.php +++ b/src/EntityImporterFactory.php @@ -70,7 +70,7 @@ public function newEntityImporter() { $this->getApiEntityLookup(), $this->entityStore, $this->getImportedEntityMappingStore(), - new PagePropsStatementCountLookup( $this->loadBalancer ), + new PagePropsStatementCountLookup( $this->loadBalancer, $this->getEntityNamespaceLookup() ), $this->logger ); } @@ -127,6 +127,12 @@ private function newSerializerFactory() { new DataValueSerializer() ); } + + private function getEntityNamespaceLookup() { + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + + return $wikibaseRepo->getEntityNamespaceLookup(); + } } $maintClass = "Wikibase\Import\Maintenance\ImportEntities"; diff --git a/src/PagePropsStatementCountLookup.php b/src/PagePropsStatementCountLookup.php index 3799f46..cc6b65b 100644 --- a/src/PagePropsStatementCountLookup.php +++ b/src/PagePropsStatementCountLookup.php @@ -2,15 +2,21 @@ namespace Wikibase\Import; +use Exception; use LoadBalancer; use Wikibase\DataModel\Entity\EntityId; +use Wikibase\Lib\Store\EntityNamespaceLookup; +use Wikibase\Repo\WikibaseRepo; class PagePropsStatementCountLookup implements StatementsCountLookup { private $loadBalancer; - public function __construct( LoadBalancer $loadBalancer ) { + private $lookup; + + public function __construct( LoadBalancer $loadBalancer, EntityNamespaceLookup $lookup ) { $this->loadBalancer = $loadBalancer; + $this->lookup = $lookup; } public function getStatementCount( EntityId $entityId ) { @@ -20,7 +26,7 @@ public function getStatementCount( EntityId $entityId ) { array( 'page_props', 'page' ), array( 'pp_value' ), array( - 'page_namespace' => 0, + 'page_namespace' => $this->lookup->getEntityNamespace( $entityId->getEntityType() ), 'page_title' => $entityId->getSerialization(), 'pp_propname' => 'wb-claims' ), @@ -32,7 +38,7 @@ public function getStatementCount( EntityId $entityId ) { $this->loadBalancer->closeConnection( $db ); if ( $res === false ) { - return 0; + throw new Exception( 'Could not find entity ' . $entityId->getSerialization() . ' in page_props!' ); } return (int)$res->pp_value;