Skip to content

Commit

Permalink
Search entities in the correct namespace
Browse files Browse the repository at this point in the history
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 filbertkm#22.
lucaswerkmeister committed May 10, 2017
1 parent d9705f1 commit f669157
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/EntityImporterFactory.php
Original file line number Diff line number Diff line change
@@ -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";
12 changes: 9 additions & 3 deletions src/PagePropsStatementCountLookup.php
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit f669157

Please sign in to comment.