Lucas Werkmeister (WMDE) has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/404994 )
Change subject: Add Context::getSiblingSnaks method ...................................................................... Add Context::getSiblingSnaks method The “sibling snaks” concept could be used by several checkers, but for now, we will start with the “single value” and “multiple values” checkers. This is also why we copy the ValueCountCheckerHelper semantics of ignoring deprecated statements in MainSnakContext (though we might make this configurable with a method parameter later). Bug: T185209 Change-Id: I6b926bfe4791b2351c16143b478e06980f03b8f7 --- M src/ConstraintCheck/Context/Context.php M src/ConstraintCheck/Context/MainSnakContext.php M src/ConstraintCheck/Context/QualifierContext.php M src/ConstraintCheck/Context/ReferenceContext.php M tests/phpunit/Context/MainSnakContextTest.php M tests/phpunit/Context/QualifierContextTest.php M tests/phpunit/Context/ReferenceContextTest.php M tests/phpunit/Fake/FakeSnakContext.php 8 files changed, 108 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints refs/changes/94/404994/1 diff --git a/src/ConstraintCheck/Context/Context.php b/src/ConstraintCheck/Context/Context.php index 07d590b..82c0d37 100644 --- a/src/ConstraintCheck/Context/Context.php +++ b/src/ConstraintCheck/Context/Context.php @@ -75,6 +75,17 @@ public function getSnakStatement(); /** + * The other snaks in the list of snaks that the snak being checked resides in. + * For a statement context, this is the main snaks of other (non-deprecated) statements; + * for a qualifier context, the other qualifiers of the same statement; + * and for a reference context, the other snaks of the same reference. + * + * @return Snak[] not a SnakList because for a statement context, + * the returned value might contain the same snak several times. + */ + public function getSiblingSnaks(); + + /** * Store the check result serialization $result * at the appropriate location for this context in $container. * diff --git a/src/ConstraintCheck/Context/MainSnakContext.php b/src/ConstraintCheck/Context/MainSnakContext.php index 2970753..fb14a4d 100644 --- a/src/ConstraintCheck/Context/MainSnakContext.php +++ b/src/ConstraintCheck/Context/MainSnakContext.php @@ -4,6 +4,8 @@ use Wikibase\DataModel\Entity\EntityDocument; use Wikibase\DataModel\Statement\Statement; +use Wikibase\DataModel\Statement\StatementList; +use Wikibase\DataModel\Statement\StatementListProvider; /** * A constraint check context for the main snak of a statement. @@ -41,6 +43,15 @@ return $this->statement; } + public function getSiblingSnaks() { + /** @var StatementList $statements */ + $statements = clone $this->entity->getStatements(); + $statements->removeStatementsWithGuid( $this->statement->getGuid() ); + return $statements + ->getByRank( [ Statement::RANK_NORMAL, Statement::RANK_PREFERRED ] ) + ->getMainSnaks(); + } + protected function &getMainArray( array &$container ) { $statementArray = &$this->getStatementArray( $container, diff --git a/src/ConstraintCheck/Context/QualifierContext.php b/src/ConstraintCheck/Context/QualifierContext.php index 3f8fc0b..4bb66e6 100644 --- a/src/ConstraintCheck/Context/QualifierContext.php +++ b/src/ConstraintCheck/Context/QualifierContext.php @@ -31,6 +31,12 @@ return self::TYPE_QUALIFIER; } + public function getSiblingSnaks() { + $snaks = clone $this->statement->getQualifiers(); + $snaks->removeSnak( $this->snak ); + return array_values( $snaks->getArrayCopy() ); + } + protected function &getMainArray( array &$container ) { $statementArray = &$this->getStatementArray( $container, diff --git a/src/ConstraintCheck/Context/ReferenceContext.php b/src/ConstraintCheck/Context/ReferenceContext.php index d0ba238..f83cc17 100644 --- a/src/ConstraintCheck/Context/ReferenceContext.php +++ b/src/ConstraintCheck/Context/ReferenceContext.php @@ -39,6 +39,12 @@ return self::TYPE_REFERENCE; } + public function getSiblingSnaks() { + $snaks = clone $this->reference->getSnaks(); + $snaks->removeSnak( $this->snak ); + return array_values( $snaks->getArrayCopy() ); + } + protected function &getMainArray( array &$container ) { $statementArray = &$this->getStatementArray( $container, diff --git a/tests/phpunit/Context/MainSnakContextTest.php b/tests/phpunit/Context/MainSnakContextTest.php index dc77a12..16227ab 100644 --- a/tests/phpunit/Context/MainSnakContextTest.php +++ b/tests/phpunit/Context/MainSnakContextTest.php @@ -63,6 +63,30 @@ $this->assertSame( $statement, $context->getSnakStatement() ); } + public function testGetSiblingSnaks() { + $statement1 = NewStatement::noValueFor( 'P1' ) + ->withSomeGuid() + ->build(); + $statement2 = NewStatement::noValueFor( 'P1' ) + ->withSomeGuid() + ->build(); + $statement3 = NewStatement::noValueFor( 'P2' ) + ->withDeprecatedRank() + ->withSomeGuid() + ->build(); + $entity = NewItem::withId( 'Q1' ) + ->andStatement( $statement1 ) + ->andStatement( $statement2 ) + ->andStatement( $statement3 ) + ->build(); + $context = new MainSnakContext( $entity, $statement1 ); + + $siblingSnaks = $context->getSiblingSnaks(); + + $this->assertSame( [ $statement2->getMainSnak() ], $siblingSnaks ); + $this->assertCount( 3, $entity->getStatements() ); + } + public function testStoreCheckResultInArray() { $entity = NewItem::withId( 'Q1' )->build(); $statement1 = NewStatement::noValueFor( 'P1' ) diff --git a/tests/phpunit/Context/QualifierContextTest.php b/tests/phpunit/Context/QualifierContextTest.php index 45a321a..e6f29b7 100644 --- a/tests/phpunit/Context/QualifierContextTest.php +++ b/tests/phpunit/Context/QualifierContextTest.php @@ -2,6 +2,9 @@ namespace WikibaseQuality\ConstraintReport\Test\Context; +use Wikibase\DataModel\Entity\PropertyId; +use Wikibase\DataModel\Snak\PropertyNoValueSnak; +use Wikibase\DataModel\Snak\SnakList; use Wikibase\DataModel\Statement\Statement; use Wikibase\Repo\Tests\NewItem; use Wikibase\Repo\Tests\NewStatement; @@ -68,6 +71,25 @@ $this->assertSame( null, $context->getSnakStatement() ); } + public function testGetSiblingSnaks() { + $qualifier1 = NewStatement::noValueFor( 'P10' )->build()->getMainSnak(); + $qualifier2 = NewStatement::someValueFor( 'P20' )->build()->getMainSnak(); + $statement = new Statement( + new PropertyNoValueSnak( new PropertyId( 'P1' ) ), + new SnakList( [ $qualifier1, $qualifier2 ] ) + ); + $entity = NewItem::withId( 'Q1' ) + ->andStatement( $statement ) + ->andStatement( NewStatement::someValueFor( 'P2' ) ) + ->build(); + $context = new QualifierContext( $entity, $statement, $qualifier1 ); + + $siblingSnaks = $context->getSiblingSnaks(); + + $this->assertSame( [ $qualifier2 ], $siblingSnaks ); + $this->assertCount( 2, $statement->getQualifiers() ); + } + public function testStoreCheckResultInArray() { $entity = NewItem::withId( 'Q1' )->build(); $statement1 = NewStatement::noValueFor( 'P1' ) diff --git a/tests/phpunit/Context/ReferenceContextTest.php b/tests/phpunit/Context/ReferenceContextTest.php index 7cb5c63..d6793be 100644 --- a/tests/phpunit/Context/ReferenceContextTest.php +++ b/tests/phpunit/Context/ReferenceContextTest.php @@ -2,7 +2,10 @@ namespace WikibaseQuality\ConstraintReport\Test\Context; +use Wikibase\DataModel\Entity\PropertyId; use Wikibase\DataModel\Reference; +use Wikibase\DataModel\ReferenceList; +use Wikibase\DataModel\Snak\PropertyNoValueSnak; use Wikibase\DataModel\Statement\Statement; use Wikibase\Repo\Tests\NewItem; use Wikibase\Repo\Tests\NewStatement; @@ -79,6 +82,27 @@ $this->assertSame( null, $context->getSnakStatement() ); } + public function testGetSiblingSnaks() { + $referenceSnak1 = NewStatement::noValueFor( 'P100' )->build()->getMainSnak(); + $referenceSnak2 = NewStatement::someValueFor( 'P200' )->build()->getMainSnak(); + $reference1 = new Reference( [ $referenceSnak1, $referenceSnak2 ] ); + $reference2 = new Reference( [ new PropertyNoValueSnak( new PropertyId( 'P300' ) ) ] ); + $statement = new Statement( + new PropertyNoValueSnak( new PropertyId( 'P1' ) ), + null, + new ReferenceList( [ $reference1, $reference2 ] ) + ); + $entity = NewItem::withId( 'Q1' ) + ->andStatement( $statement ) + ->build(); + $context = new ReferenceContext( $entity, $statement, $reference1, $referenceSnak1 ); + + $siblingSnaks = $context->getSiblingSnaks(); + + $this->assertSame( [ $referenceSnak2 ], $siblingSnaks ); + $this->assertCount( 2, $reference1->getSnaks() ); + } + public function testStoreCheckResultInArray() { $entity = NewItem::withId( 'Q1' )->build(); $statement1 = NewStatement::noValueFor( 'P1' ) diff --git a/tests/phpunit/Fake/FakeSnakContext.php b/tests/phpunit/Fake/FakeSnakContext.php index 175c612..5bbcb2a 100644 --- a/tests/phpunit/Fake/FakeSnakContext.php +++ b/tests/phpunit/Fake/FakeSnakContext.php @@ -35,6 +35,10 @@ return 'statement'; } + public function getSiblingSnaks() { + return []; + } + /** * @param array|null $result * @param array[] &$container -- To view, visit https://gerrit.wikimedia.org/r/404994 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6b926bfe4791b2351c16143b478e06980f03b8f7 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints Gerrit-Branch: master Gerrit-Owner: Lucas Werkmeister (WMDE) <lucas.werkmeis...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits