Tobias Gritschacher has submitted this change and it was merged. Change subject: (bug 46366) Use SnakFormatter for summaries ......................................................................
(bug 46366) Use SnakFormatter for summaries Change-Id: Ic3e1e78a5f4bb89d0d423ed8852610080fcc4102 --- M repo/includes/ClaimSummaryBuilder.php M repo/includes/api/SetClaim.php M repo/tests/phpunit/includes/ClaimSummaryBuilderTest.php 3 files changed, 41 insertions(+), 27 deletions(-) Approvals: Tobias Gritschacher: Verified; Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/ClaimSummaryBuilder.php b/repo/includes/ClaimSummaryBuilder.php index e338fd0..ee2359b 100644 --- a/repo/includes/ClaimSummaryBuilder.php +++ b/repo/includes/ClaimSummaryBuilder.php @@ -4,7 +4,7 @@ use DataValues\TimeValue; use InvalidArgumentException; -use Wikibase\Lib\EntityIdFormatter; +use Wikibase\Lib\SnakFormatter; /** * EditSummary-Builder for claim operations @@ -16,6 +16,7 @@ * * @licence GNU GPL v2+ * @author Tobias Gritschacher < tobias.gritschac...@wikimedia.de > + * @author Daniel Kinzler */ class ClaimSummaryBuilder { @@ -30,9 +31,9 @@ private $claimDiffer; /** - * @var EntityIdFormatter + * @var Lib\SnakFormatter */ - private $idFormatter; + private $snakValueFormatter; /** * Constructs a new ClaimSummaryBuilder @@ -41,18 +42,24 @@ * * @param string $apiModuleName * @param ClaimDiffer $claimDiffer - * @param EntityIdFormatter $idFormatter + * @param SnakFormatter $snakValueFormatter * * @throws InvalidArgumentException */ - public function __construct( $apiModuleName, ClaimDiffer $claimDiffer, EntityIdFormatter $idFormatter ) { + public function __construct( $apiModuleName, ClaimDiffer $claimDiffer, SnakFormatter $snakValueFormatter ) { if ( !is_string( $apiModuleName ) ) { throw new InvalidArgumentException( '$apiModuleName needs to be a string' ); } + if ( $snakValueFormatter->getFormat() !== SnakFormatter::FORMAT_PLAIN ) { + throw new InvalidArgumentException( + 'Expected $snakValueFormatter to procude plain text output, not ' + . $snakValueFormatter->getFormat() ); + } + $this->apiModuleName = $apiModuleName; $this->claimDiffer = $claimDiffer; - $this->idFormatter = $idFormatter; + $this->snakValueFormatter = $snakValueFormatter; } /** @@ -122,22 +129,13 @@ foreach( $guids as $guid ) { if ( $claims->hasClaimWithGuid( $guid ) ) { $snak = $claims->getClaimWithGuid( $guid )->getMainSnak(); - $key = $this->idFormatter->format( $snak->getPropertyId() ); + $key = $snak->getPropertyId()->getPrefixedId(); if ( !array_key_exists( $key, $pairs ) ) { $pairs[$key] = array(); } - if ( $snak instanceof PropertyValueSnak ) { - $value = $snak->getDataValue(); - // TODO: we should use value formatters here! - if ( $value instanceof TimeValue ) { - $value = $value->getTime(); - } - } else { - $value = $snak->getType(); // todo handle no values in general way (needed elsewhere) - } - + $value = $this->snakValueFormatter->formatSnak( $snak ); $pairs[$key][] = $value; } } diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php index 1789c52..e312f77 100644 --- a/repo/includes/api/SetClaim.php +++ b/repo/includes/api/SetClaim.php @@ -8,12 +8,15 @@ use MWException; use ApiBase; use Diff\ListDiffer; +use ValueFormatters\FormatterOptions; +use ValueFormatters\ValueFormatter; use Wikibase\EntityContent; use Wikibase\Claim; use Wikibase\EntityContentFactory; use Wikibase\ClaimDiffer; use Wikibase\ClaimSaver; use Wikibase\ClaimSummaryBuilder; +use Wikibase\Lib\SnakFormatter; use Wikibase\Repo\WikibaseRepo; use Wikibase\Summary; use Wikibase\Validators\ValidatorErrorLocalizer; @@ -85,10 +88,16 @@ }; $claimDiffer = new ClaimDiffer( new CallbackListDiffer( $comparer ) ); + + $options = new FormatterOptions( array( + //TODO: fallback chain + ValueFormatter::OPT_LANG => $this->getContext()->getLanguage()->getCode() + ) ); + $claimSummaryBuilder = new ClaimSummaryBuilder( $this->getModuleName(), $claimDiffer, - WikibaseRepo::getDefaultInstance()->getIdFormatter() + WikibaseRepo::getDefaultInstance()->getSnakFormatterFactory()->getFormatter( SnakFormatter::FORMAT_PLAIN, $options ) ); $claimSaver = new ClaimSaver(); diff --git a/repo/tests/phpunit/includes/ClaimSummaryBuilderTest.php b/repo/tests/phpunit/includes/ClaimSummaryBuilderTest.php index 7e9a0de..a9f5551 100644 --- a/repo/tests/phpunit/includes/ClaimSummaryBuilderTest.php +++ b/repo/tests/phpunit/includes/ClaimSummaryBuilderTest.php @@ -8,7 +8,7 @@ use Wikibase\Claim; use Wikibase\Claims; use Wikibase\ClaimSummaryBuilder; -use Wikibase\Lib\EntityIdFormatter; +use Wikibase\Lib\SnakFormatter; use Wikibase\Repo\WikibaseRepo; /** @@ -40,6 +40,7 @@ * * @licence GNU GPL v2+ * @author Tobias Gritschacher < tobias.gritschac...@wikimedia.de > + * @author Daniel Kinzler */ class ClaimSummaryBuilderTest extends \MediaWikiTestCase { @@ -138,11 +139,14 @@ } public function testBuildCreateClaimSummary() { - $idFormatter = $this->getMockBuilder( 'Wikibase\Lib\EntityIdFormatter' ) + $snakFormatter = $this->getMockBuilder( 'Wikibase\Lib\SnakFormatter' ) ->disableOriginalConstructor()->getMock(); - $idFormatter->expects( $this->any() ) - ->method( 'format' ) + $snakFormatter->expects( $this->any() ) + ->method( 'formatSnak' ) ->will( $this->returnValue( 'foo' ) ); + $snakFormatter->expects( $this->any() ) + ->method( 'getFormat' ) + ->will( $this->returnValue( SnakFormatter::FORMAT_PLAIN ) ); $comparer = function( \Comparable $old, \Comparable $new ) { return $old->equals( $new ); @@ -151,7 +155,7 @@ $claimSummaryBuilder = new ClaimSummaryBuilder( 'wbsetclaim', new ClaimDiffer( new CallbackListDiffer( $comparer ) ), - $idFormatter + $snakFormatter ); $claims = new Claims(); @@ -173,11 +177,14 @@ * @param string $action */ public function testBuildUpdateClaimSummary( $originalClaim, $modifiedClaim, $action ) { - $idFormatter = $this->getMockBuilder( 'Wikibase\Lib\EntityIdFormatter' ) + $snakFormatter = $this->getMockBuilder( 'Wikibase\Lib\SnakFormatter' ) ->disableOriginalConstructor()->getMock(); - $idFormatter->expects( $this->any() ) - ->method( 'format' ) + $snakFormatter->expects( $this->any() ) + ->method( 'formatSnak' ) ->will( $this->returnValue( 'foo' ) ); + $snakFormatter->expects( $this->any() ) + ->method( 'getFormat' ) + ->will( $this->returnValue( SnakFormatter::FORMAT_PLAIN ) ); $comparer = function( \Comparable $old, \Comparable $new ) { return $old->equals( $new ); @@ -186,7 +193,7 @@ $claimSummaryBuilder = new ClaimSummaryBuilder( 'wbsetclaim', new ClaimDiffer( new CallbackListDiffer( $comparer ) ), - $idFormatter + $snakFormatter ); $claims = new Claims(); -- To view, visit https://gerrit.wikimedia.org/r/84492 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic3e1e78a5f4bb89d0d423ed8852610080fcc4102 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits