Ladsgroup has uploaded a new change for review. https://gerrit.wikimedia.org/r/323144
Change subject: Make Differ work with Lemma ...................................................................... Make Differ work with Lemma Bug: T148827 Change-Id: I2c86ca82e658ccadf6ebb8acee954e774747b6f9 --- A src/DataModel/Services/Diff/LexemeDiff.php M src/DataModel/Services/Diff/LexemeDiffer.php 2 files changed, 86 insertions(+), 6 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme refs/changes/44/323144/1 diff --git a/src/DataModel/Services/Diff/LexemeDiff.php b/src/DataModel/Services/Diff/LexemeDiff.php new file mode 100644 index 0000000..1bac7e9 --- /dev/null +++ b/src/DataModel/Services/Diff/LexemeDiff.php @@ -0,0 +1,48 @@ +<?php + +namespace Wikibase\Lexeme\DataModel\Services\Diff; + +use Diff\DiffOp\Diff\Diff; +use Diff\DiffOp\DiffOp; +use Wikibase\DataModel\Services\Diff\EntityDiff; + +/** + * Represents a diff between two lexemes. + * + * @since 1.0 + * + * @license GPL-2.0+ + * @author Amir Sarabadani <ladsgr...@gmail.com> + */ +class LexemeDiff extends EntityDiff { + + /** + * @param DiffOp[] $operations + */ + public function __construct( array $operations = [] ) { + $this->fixSubstructureDiff( $operations, 'lemma' ); + $this->fixSubstructureDiff( $operations, 'claim' ); + + parent::__construct( $operations, true ); + } + + /** + * Returns a Diff object with the lemma differences. + * + * @return Diff + */ + public function getLemmaDiff() { + return isset( $this['lemma'] ) ? $this['lemma'] : new Diff( [], true ); + } + + /** + * Returns if there are any changes (equivalent to: any differences between the entities). + * + * @return bool + */ + public function isEmpty() { + return $this->getLemmaDiff()->isEmpty() + && $this->getClaimsDiff()->isEmpty(); + } + +} diff --git a/src/DataModel/Services/Diff/LexemeDiffer.php b/src/DataModel/Services/Diff/LexemeDiffer.php index 3fa0c3b..f518355 100644 --- a/src/DataModel/Services/Diff/LexemeDiffer.php +++ b/src/DataModel/Services/Diff/LexemeDiffer.php @@ -2,6 +2,7 @@ namespace Wikibase\Lexeme\DataModel\Services\Diff; +use Diff\Differ\MapDiffer; use Wikibase\DataModel\Entity\EntityDocument; use Wikibase\DataModel\Services\Diff\EntityDiff; use Wikibase\DataModel\Services\Diff\EntityDifferStrategy; @@ -20,7 +21,16 @@ */ private $statementListDiffer; + /** + * @var MapDiffer + */ + private $recursiveMapDiffer; + + /** + * LexemeDiffer constructor. + */ public function __construct() { + $this->recursiveMapDiffer = new MapDiffer( true ); $this->statementListDiffer = new StatementListDiffer(); } @@ -57,12 +67,34 @@ * @return EntityDiff */ public function diffLexemes( Lexeme $from, Lexeme $to ) { - return new EntityDiff( [ - 'claim' => $this->statementListDiffer->getDiff( - $from->getStatements(), - $to->getStatements() - ), - ] ); + $diffOps = $this->recursiveMapDiffer->doDiff( + $this->toDiffArray( $from ), + $this->toDiffArray( $to ) + ); + + $diffOps['claim'] = $this->statementListDiffer->getDiff( + $from->getStatements(), + $to->getStatements() + ); + + return new LexemeDiff( $diffOps ); + } + + /** + * @param Lexeme $lexeme + * + * @return string[] + */ + private function toDiffArray( Lexeme $lexeme ) { + $array = []; + $lemma = $lexeme->getLemma(); + + $array['lemma'] = [ + 'language' => $lemma->getLanguageCode(), + 'value' => $lemma->getText() + ]; + + return $array; } /** -- To view, visit https://gerrit.wikimedia.org/r/323144 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2c86ca82e658ccadf6ebb8acee954e774747b6f9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseLexeme Gerrit-Branch: master Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits