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

Streamline FingerprintPatcher #120

Merged
merged 1 commit into from
Apr 20, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
178 changes: 154 additions & 24 deletions src/Diff/Internal/FingerprintPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Wikibase\DataModel\Services\Diff\Internal;

use Diff\DiffOp\AtomicDiffOp;
use Diff\DiffOp\Diff\Diff;
use Diff\DiffOp\DiffOp;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpChange;
use Diff\DiffOp\DiffOpRemove;
use Diff\Patcher\MapPatcher;
use InvalidArgumentException;
use Diff\Patcher\PatcherException;
use Wikibase\DataModel\Services\Diff\EntityDiff;
use Wikibase\DataModel\Term\AliasGroupList;
use Wikibase\DataModel\Term\Fingerprint;
Expand All @@ -19,20 +24,11 @@
*/
class FingerprintPatcher {

/**
* @var MapPatcher
*/
private $patcher;

public function __construct() {
$this->patcher = new MapPatcher();
}

/**
* @param Fingerprint $fingerprint
* @param EntityDiff $patch
*
* @throws InvalidArgumentException
* @throws PatcherException
*/
public function patchFingerprint( Fingerprint $fingerprint, EntityDiff $patch ) {
$this->patchTermList( $fingerprint->getLabels(), $patch->getLabelsDiff() );
Expand All @@ -41,30 +37,164 @@ public function patchFingerprint( Fingerprint $fingerprint, EntityDiff $patch )
$this->patchAliasGroupList( $fingerprint->getAliasGroups(), $patch->getAliasesDiff() );
}

/**
* @param TermList $terms
* @param Diff $patch
*
* @throws PatcherException
*/
private function patchTermList( TermList $terms, Diff $patch ) {
$original = $terms->toTextArray();
$patched = $this->patcher->patch( $original, $patch );

foreach ( $patched as $languageCode => $text ) {
$terms->setTextForLanguage( $languageCode, $text );
foreach ( $patch as $lang => $diffOp ) {
$this->patchTerm( $terms, $lang, $diffOp );
}
}

foreach ( array_diff_key( $original, $patched ) as $languageCode => $text ) {
$terms->removeByLanguage( $languageCode );
/**
* @see MapPatcher
*
* @param TermList $terms
* @param string $lang
* @param AtomicDiffOp $diffOp
*
* @throws PatcherException
*/
private function patchTerm( TermList $terms, $lang, AtomicDiffOp $diffOp ) {
$hasLang = $terms->hasTermForLanguage( $lang );

if ( $diffOp instanceof DiffOpAdd ) {
if ( !$hasLang ) {
$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
}
} elseif ( $diffOp instanceof DiffOpChange ) {
if ( $hasLang
&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
) {
$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
}
} elseif ( $diffOp instanceof DiffOpRemove ) {
if ( $hasLang
&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
) {
$terms->removeByLanguage( $lang );
}
} else {
throw new PatcherException( 'Invalid terms diff' );
}
}

/**
* @param AliasGroupList $groups
* @param Diff $patch
*
* @throws PatcherException
*/
private function patchAliasGroupList( AliasGroupList $groups, Diff $patch ) {
$original = $groups->toTextArray();
$patched = $this->patcher->patch( $original, $patch );
foreach ( $patch as $lang => $diffOp ) {
$this->patchAliasGroup( $groups, $lang, $diffOp );
}
}

/**
* @see MapPatcher
*
* @param AliasGroupList $groups
* @param string $lang
* @param DiffOp $diffOp
*
* @throws PatcherException
*/
private function patchAliasGroup( AliasGroupList $groups, $lang, DiffOp $diffOp ) {
$hasLang = $groups->hasGroupForLanguage( $lang );

foreach ( $patched as $languageCode => $aliases ) {
$groups->setAliasesForLanguage( $languageCode, $aliases );
if ( $diffOp instanceof DiffOpAdd ) {
if ( !$hasLang ) {
$groups->setAliasesForLanguage( $lang, $diffOp->getNewValue() );
}
} elseif ( $diffOp instanceof DiffOpChange ) {
$this->applyAliasGroupChange( $groups, $lang, $diffOp );
} elseif ( $diffOp instanceof DiffOpRemove ) {
if ( $hasLang
&& $groups->getByLanguage( $lang )->getAliases() === $diffOp->getOldValue()
) {
$groups->removeByLanguage( $lang );
}
} elseif ( $diffOp instanceof Diff ) {
$this->applyAliasGroupDiff( $groups, $lang, $diffOp );
} else {
throw new PatcherException( 'Invalid aliases diff' );
}
}

/**
* @param AliasGroupList $groups
* @param string $lang
* @param DiffOpChange $patch
*/
private function applyAliasGroupChange( AliasGroupList $groups, $lang, DiffOpChange $patch ) {
if ( $groups->hasGroupForLanguage( $lang )
&& $groups->getByLanguage( $lang )->getAliases() === $patch->getOldValue()
) {
$groups->setAliasesForLanguage( $lang, $patch->getNewValue() );
}
}

/**
* @param AliasGroupList $groups
* @param string $lang
* @param Diff $patch
*/
private function applyAliasGroupDiff( AliasGroupList $groups, $lang, Diff $patch ) {
$hasLang = $groups->hasGroupForLanguage( $lang );

if ( $hasLang || !$this->containsOperationsOnOldValues( $patch ) ) {
$aliases = $hasLang ? $groups->getByLanguage( $lang )->getAliases() : array();
$aliases = $this->getPatchedAliases( $aliases, $patch );
$groups->setAliasesForLanguage( $lang, $aliases );
}
}

foreach ( array_diff_key( $original, $patched ) as $languageCode => $aliases ) {
$groups->removeByLanguage( $languageCode );
/**
* @param Diff $diff
*
* @return bool
*/
private function containsOperationsOnOldValues( Diff $diff ) {
return $diff->getChanges() !== array()
|| $diff->getRemovals() !== array();
}

/**
* @see ListPatcher
*
* @param string[] $aliases
* @param Diff $patch
*
* @throws PatcherException
* @return string[]
*/
private function getPatchedAliases( array $aliases, Diff $patch ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This whole method could be replaced with a ListPatcher call, but this needs wmde/Diff#65.

foreach ( $patch as $diffOp ) {
if ( $diffOp instanceof DiffOpAdd ) {
$aliases[] = $diffOp->getNewValue();
} elseif ( $diffOp instanceof DiffOpChange ) {
$key = array_search( $diffOp->getOldValue(), $aliases, true );

if ( $key !== false ) {
unset( $aliases[$key] );
$aliases[] = $diffOp->getNewValue();
}
} elseif ( $diffOp instanceof DiffOpRemove ) {
$key = array_search( $diffOp->getOldValue(), $aliases, true );

if ( $key !== false ) {
unset( $aliases[$key] );
}
} else {
throw new PatcherException( 'Invalid aliases diff' );
}
}

return $aliases;
}

}