Skip to content
This repository has been archived by the owner on Jan 27, 2020. It is now read-only.

Create FingerprintDiffer #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
58 changes: 58 additions & 0 deletions src/PackagePrivate/FingerprintDiffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Wikibase\TermStore\PackagePrivate;

use Wikibase\DataModel\Term\AliasGroup;
use Wikibase\DataModel\Term\Fingerprint;

class FingerprintDiffer {

/**
* @param Fingerprint $first
* @param Fingerprint $second
*
* @return Fingerprint Contains the terms present in $first that are not present in $second
*/
public function diff( Fingerprint $first, Fingerprint $second ): Fingerprint {
$difference = new Fingerprint();

foreach ( $first->getLabels() as $term ) {
if ( !$second->getLabels()->hasTerm( $term ) ) {
$difference->getLabels()->setTerm( $term );
}
}

foreach ( $first->getDescriptions() as $term ) {
if ( !$second->getDescriptions()->hasTerm( $term ) ) {
$difference->getDescriptions()->setTerm( $term );
}
}

foreach ( $first->getAliasGroups() as $aliasGroup ) {
$difference->setAliasGroup(
$aliasGroup->getLanguageCode(),
$this->diffAliasGroup( $aliasGroup, $second )
);
}

return $difference;
}

private function diffAliasGroup( AliasGroup $aliasGroup, Fingerprint $second ) {
if ( $second->hasAliasGroup( $aliasGroup->getLanguageCode() ) ) {
$secondAliases = $second->getAliasGroup( $aliasGroup->getLanguageCode() )->getAliases();
$differenceAliases = [];

foreach ( $aliasGroup->getAliases() as $aliasText ) {
if ( !in_array( $aliasText, $secondAliases ) ) {
$differenceAliases[] = $aliasText;
}
}

return $differenceAliases;
}

return $aliasGroup->getAliases();
}

}
118 changes: 118 additions & 0 deletions tests/Unit/FingerprintDifferTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Wikibase\TermStore\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Wikibase\DataModel\Term\AliasGroup;
use Wikibase\DataModel\Term\AliasGroupList;
use Wikibase\DataModel\Term\Fingerprint;
use Wikibase\DataModel\Term\Term;
use Wikibase\DataModel\Term\TermList;
use Wikibase\TermStore\PackagePrivate\FingerprintDiffer;

/**
* @covers \Wikibase\TermStore\PackagePrivate\FingerprintDiffer
*/
class FingerprintDifferTest extends TestCase {

public function testEmptyFingerprints() {
$this->assertEquals(
new Fingerprint(),
$this->diff( new Fingerprint(), new Fingerprint() )
);
}

private function diff( Fingerprint $first, Fingerprint $second ): Fingerprint {
return ( new FingerprintDiffer() )->diff( $first, $second );
}

public function testTermsNotInSecondAreReturned() {
$first = new Fingerprint(
new TermList( [ new Term( 'en', 'FirstLabel' ) ] ),
new TermList( [ new Term( 'en', 'FirstDesc' ) ] ),
new AliasGroupList( [
new AliasGroup( 'en', [ 'FirstAliasOne', 'FirstAliasTwo' ] ),
new AliasGroup( 'de', [ 'DeAliasOne', 'DeAliasTwo' ] ),
] )
);

$this->assertEquals(
$first,
$this->diff(
$first,
new Fingerprint(
new TermList( [ new Term( 'en', 'SecondLabel' ) ] ),
new TermList( [ new Term( 'en', 'SecondDesc' ) ] ),
new AliasGroupList( [
new AliasGroup( 'en', [ 'SecondAliasOne', 'SecondAliasTwo' ] ),
new AliasGroup( 'different', [ 'DeAliasOne', 'DeAliasTwo' ] ),
] )
)
)
);
}

public function testTermsInSecondAreNotReturned() {
$fingerprint = new Fingerprint(
new TermList( [ new Term( 'en', 'FirstLabel' ) ] ),
new TermList( [ new Term( 'en', 'FirstDesc' ) ] ),
new AliasGroupList( [
new AliasGroup( 'en', [ 'FirstAliasOne', 'FirstAliasTwo' ] ),
] )
);

$this->assertEquals(
new Fingerprint(),
$this->diff(
$fingerprint,
$fingerprint
)
);
}

public function testAllTheThings() {
$this->assertEquals(
new Fingerprint(
new TermList( [
new Term( 'fr', 'OnlyInFirst' )
] ),
new TermList( [
new Term( 'de', 'MismatchLanguage' )
] ),
new AliasGroupList( [
new AliasGroup( 'en', [ 'AliasOnlyInFirst' ] ),
new AliasGroup( 'de', [ 'AliasesOnlyInFirst' ] )
] )
),
$this->diff(
new Fingerprint(
new TermList( [
new Term( 'en', 'MatchingTerm' ),
new Term( 'fr', 'OnlyInFirst' )
] ),
new TermList( [
new Term( 'de', 'MismatchLanguage' )
] ),
new AliasGroupList( [
new AliasGroup( 'en', [ 'MatchingAlias', 'AliasOnlyInFirst' ] ),
new AliasGroup( 'de', [ 'AliasesOnlyInFirst' ] ),
] )
),
new Fingerprint(
new TermList( [
new Term( 'en', 'MatchingTerm' ),
new Term( 'ru', 'OnlyInSecond' )
] ),
new TermList( [
new Term( 'different', 'MismatchLanguage' )
] ),
new AliasGroupList( [
new AliasGroup( 'en', [ 'MatchingAlias', 'AliasOnlyInSecond' ] ),
new AliasGroup( 'ru', [ 'AliasesOnlyInSecond' ] ),
] )
)
)
);
}

}