diff --git a/composer.json b/composer.json index 9427745..604865d 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "license": "BSD-3-Clause", "description": "Small library for persistence of Wikibase terms via Doctrine DBAL", "require": { - "php": "^5.6.99|^7.0", + "php": "^7.0", "wikibase/term-store": "~1.0", "doctrine/dbal": "~2.5", "onoi/message-reporter": "~1.3" diff --git a/src/PackagePrivate/Doctrine/DoctrinePropertyTermStore.php b/src/PackagePrivate/Doctrine/DoctrinePropertyTermStore.php index 5872a9d..ca6a779 100644 --- a/src/PackagePrivate/Doctrine/DoctrinePropertyTermStore.php +++ b/src/PackagePrivate/Doctrine/DoctrinePropertyTermStore.php @@ -24,9 +24,14 @@ public function __construct( Connection $connection, TableNames $tableNames ) { } public function storeTerms( PropertyId $propertyId, Fingerprint $terms ) { + $oldTerms = $this->getTerms( $propertyId ); + + $termsToAdd = new Fingerprint(); // TODO: diff + $termsToRemove = new Fingerprint(); // TODO: diff + try { - $this->deleteTerms( $propertyId ); - $this->insertTerms( $propertyId, $terms ); + $this->insertTerms( $propertyId, $termsToAdd ); + $this->removeTerms( $propertyId, $termsToRemove ); } catch ( DBALException $ex ) { throw new TermStoreException( $ex->getMessage(), $ex->getCode() ); @@ -53,6 +58,34 @@ private function insertTerms( PropertyId $propertyId, Fingerprint $terms ) { } } + private function removeTerms( PropertyId $propertyId, Fingerprint $terms ) { + $termInLangIds = []; + + foreach ( $terms->getLabels() as $label ) { + $termInLangIds[] = 0; // TODO + } + + foreach ( $terms->getDescriptions() as $description ) { + $termInLangIds[] = 0; // TODO + } + + foreach ( $terms->getAliasGroups() as $aliasGroup ) { + foreach ( $aliasGroup->getAliases() as $alias ) { + $termInLangIds[] = 0; // TODO + } + } + + $this->connection->delete( + $this->tableNames->propertyTerms(), + [ + 'wbpt_property_id' => $propertyId->getNumericId(), + 'wbpt_term_in_lang_id' => $termInLangIds, + ] + ); + + // TODO: cleanup + } + private function insertTerm( PropertyId $propertyId, Term $term, $termType ) { $this->connection->insert( $this->tableNames->propertyTerms(), @@ -70,6 +103,7 @@ public function deleteTerms( PropertyId $propertyId ) { [ 'wbpt_property_id' => $propertyId->getNumericId() ], [ \PDO::PARAM_INT ] ); + // TODO: cleanup (maybe: removeTerms(id, getTerms(id))) } catch ( DBALException $ex ) { throw new TermStoreException( $ex->getMessage(), $ex->getCode() );