Soeren.oldag has submitted this change and it was merged. Change subject: refactor TimeValueComparer and add potential match for time values ......................................................................
refactor TimeValueComparer and add potential match for time values Conflicts: includes/CrossCheck/Comparer/TimeValueComparer.php Change-Id: Ic4a6085982d4df34f23dc7c82576d95c2620a7e1 --- M includes/CrossCheck/Comparer/TimeValueComparer.php M tests/phpunit/CrossCheck/Comparer/TimeValueComparerTest.php 2 files changed, 82 insertions(+), 12 deletions(-) Approvals: Soeren.oldag: Verified; Looks good to me, approved diff --git a/includes/CrossCheck/Comparer/TimeValueComparer.php b/includes/CrossCheck/Comparer/TimeValueComparer.php index 0dfc4cc..ee9926f 100755 --- a/includes/CrossCheck/Comparer/TimeValueComparer.php +++ b/includes/CrossCheck/Comparer/TimeValueComparer.php @@ -5,14 +5,12 @@ use DataValues\DataValue; use DataValues\TimeValue; use Doctrine\Instantiator\Exception\InvalidArgumentException; -use ValueFormatters\FormatterOptions; -use ValueFormatters\ValueFormatter; use ValueParsers\ParserOptions; use ValueParsers\ValueParser; -use Wikibase\Lib\MwTimeIsoFormatter; use Wikibase\Lib\Parsers\TimeParserFactory; use WikibaseQuality\ExternalValidation\CrossCheck\Result\CompareResult; use WikibaseQuality\ExternalValidation\DumpMetaInformation\DumpMetaInformation; +use MWTimestamp; /** @@ -38,19 +36,21 @@ throw new InvalidArgumentException( 'TimeValueComparer can only compare TimeValue objects.objects.' ); } - $formatterOptions = new FormatterOptions(); - $formatterOptions->setOption( ValueFormatter::OPT_LANG, $dumpMetaInformation->getLanguage() ); - $timeFormatter = new MwTimeIsoFormatter( $formatterOptions ); - $formattedDataValue = $timeFormatter->format( $localValue ); - $externalValues = $this->parseExternalValues( $externalValues, $dumpMetaInformation ); + if ( $externalValues ) { $status = CompareResult::STATUS_MISMATCH; + foreach ( $externalValues as $externalValue ) { - $formattedExternalValue = $timeFormatter->format( $externalValue ); - if ( $formattedDataValue === $formattedExternalValue ) { + $localTimestamp = new MWTimestamp( substr( $localValue->getTime(), 1 ) ); + $externalTimestamp = new MWTimestamp( substr( $externalValue->getTime(), 1 ) ); + $diff = $localTimestamp->diff( $externalTimestamp, true ); + + if ( $localValue->getPrecision() === $externalValue->getPrecision() + && $this->resultOfDiffWithPrecision( $diff, $localValue->getPrecision() ) ) { $status = CompareResult::STATUS_MATCH; - break; + } elseif ( $this->resultOfDiffWithPrecision( $diff, min( $localValue->getPrecision(), $externalValue->getPrecision() ) ) ) { + $status = CompareResult::STATUS_PARTIAL_MATCH; } } @@ -59,6 +59,53 @@ } /** + * Returns boolean if diff is equal depending on the given precision + * + * @param \DateInterval $diff + * @param int $precision + * + * @return bool + */ + private function resultOfDiffWithPrecision( $diff, $precision ) { + $result = true; + switch ( $precision ) { + case TimeValue::PRECISION_MINUTE: + $result = $result && $diff->i === 0; + case TimeValue::PRECISION_HOUR: + $result = $result && $diff->h === 0; + case TimeValue::PRECISION_DAY: + $result = $result && $diff->d === 0; + case TimeValue::PRECISION_MONTH: + $result = $result && $diff->m === 0; + case TimeValue::PRECISION_YEAR: + $result = $result && $diff->y === 0; + case TimeValue::PRECISION_10a: + $result = $result && $diff->y < 10; + case TimeValue::PRECISION_100a: + $result = $result && $diff->y < 100; + case TimeValue::PRECISION_ka: + $result = $result && $diff->y < 1000; + case TimeValue::PRECISION_10ka: + $result = $result && $diff->y < 10000; + case TimeValue::PRECISION_100ka: + $result = $result && $diff->y < 100000; + case TimeValue::PRECISION_Ma: + $result = $result && $diff->y < 1000000; + case TimeValue::PRECISION_10Ma: + $result = $result && $diff->y < 10000000; + case TimeValue::PRECISION_100Ma: + $result = $result && $diff->y < 100000000; + case TimeValue::PRECISION_Ga: + $result = $result && $diff->y < 1000000000; + break; + default: + $result = false; + } + + return $result; + } + + /** * @see DataValueComparerBase::getExternalValueParser * * @param DumpMetaInformation $dumpMetaInformation diff --git a/tests/phpunit/CrossCheck/Comparer/TimeValueComparerTest.php b/tests/phpunit/CrossCheck/Comparer/TimeValueComparerTest.php index 980c220..7b740a7 100755 --- a/tests/phpunit/CrossCheck/Comparer/TimeValueComparerTest.php +++ b/tests/phpunit/CrossCheck/Comparer/TimeValueComparerTest.php @@ -39,6 +39,7 @@ public function comparisonProvider() { $localValue1955 = new TimeValue( '+0000000000001955-03-11T00:00:00Z', 0, 0, 0, 11, 'http://www.wikidata.org/entity/Q1985727' ); $localValue2015 = new TimeValue( '+0000000000002015-00-00T00:00:00Z', 0, 0, 0, 9, 'http://www.wikidata.org/entity/Q1985727' ); + $localValue2016 = new TimeValue( '+0000000000002016-03-00T00:00:00Z', 0, 0, 0, 10, 'http://www.wikidata.org/entity/Q1985727' ); return array( // Correct formatted external values @@ -102,7 +103,29 @@ ), $localValue1955, array( 'foobar', '11.03.1955' ) - ) + ), + array( + new CompareResult( + $localValue2015, + array( + new TimeValue( '+0000000000002015-03-00T00:00:00Z', 0, 0, 0, 10, 'http://www.wikidata.org/entity/Q1985727' ) + ), + CompareResult::STATUS_PARTIAL_MATCH + ), + $localValue2015, + array( '03.2015' ) + ), + array( + new CompareResult( + $localValue2016, + array( + new TimeValue( '+0000000000002016-00-00T00:00:00Z', 0, 0, 0, 9, 'http://www.wikidata.org/entity/Q1985727' ) + ), + CompareResult::STATUS_PARTIAL_MATCH + ), + $localValue2016, + array( '2016' ) + ), ); } -- To view, visit https://gerrit.wikimedia.org/r/218632 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic4a6085982d4df34f23dc7c82576d95c2620a7e1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikidataQualityExternalValidation Gerrit-Branch: v1 Gerrit-Owner: Dominic.sauer <dominic.sa...@yahoo.de> Gerrit-Reviewer: Soeren.oldag <soeren_ol...@freenet.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits