Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FingerprintDiffer #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 62 additions & 0 deletions src/Diff/FingerprintDiffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Wikibase\DataModel\Services\Diff;

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

/**
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < [email protected] >
*/
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/Diff/FingerprintDifferTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Wikibase\DataModel\Services\Tests\Diff;

use PHPUnit\Framework\TestCase;
use Wikibase\DataModel\Services\Diff\FingerprintDiffer;
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;

/**
* @covers \Wikibase\DataModel\Services\Diff\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' ] ),
] )
)
)
);
}

}