Jeroen De Dauw has submitted this change and it was merged.

Change subject: Introducing Entity::getAllSnaks
......................................................................


Introducing Entity::getAllSnaks

This logic was factored out of ReferencedEntityFinder,
to improve testability, and to be able to use it for
future ReferencedUrlFinder, ReferencedImageFinder, etc.

Change-Id: Iae5f7817cfe17bbfe03ddb0ffeabf04bc59742bd
---
M DataModel/Claim/Claim.php
M DataModel/Claim/Statement.php
M DataModel/Entity/Entity.php
M tests/phpunit/Claim/ClaimTest.php
M tests/phpunit/Claim/StatementTest.php
M tests/phpunit/Entity/EntityTest.php
6 files changed, 101 insertions(+), 0 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved



diff --git a/DataModel/Claim/Claim.php b/DataModel/Claim/Claim.php
index f8e0e22..cd1378e 100644
--- a/DataModel/Claim/Claim.php
+++ b/DataModel/Claim/Claim.php
@@ -261,6 +261,23 @@
                return self::RANK_TRUTH;
        }
 
+       /**
+        * Returns a list of all Snaks on this Claim. This includes at least 
the main snak,
+        * and the snaks from qualifiers.
+        *
+        * This is a convenience method for use in code that needs to operate 
on all snaks, e.g.
+        * to find all referenced Entities.
+        *
+        * @return Snak[]
+        */
+       public function getAllSnaks() {
+               $snaks = array();
+
+               $snaks[] = $this->getMainSnak();
+               $snaks = array_merge( $snaks, iterator_to_array( 
$this->getQualifiers() ) );
+
+               return $snaks;
+       }
 }
 
 /**
diff --git a/DataModel/Claim/Statement.php b/DataModel/Claim/Statement.php
index 15c6ceb..83cfcbc 100644
--- a/DataModel/Claim/Statement.php
+++ b/DataModel/Claim/Statement.php
@@ -188,6 +188,24 @@
                $this->setReferences( $instance->getReferences() );
        }
 
+       /**
+        * @see Claim::getAllSnaks.
+        *
+        * In addition to the Snaks returned by Claim::getAllSnaks(), this also 
includes all
+        * snaks from any References in this Statement.
+        *
+        * @return Snak[]
+        */
+       public function getAllSnaks() {
+               $snaks = parent::getAllSnaks();
+
+               /* @var Reference $reference */
+               foreach( $this->getReferences() as $reference ) {
+                       $snaks = array_merge( $snaks, iterator_to_array( 
$reference->getSnaks() ) );
+               }
+
+               return $snaks;
+       }
 }
 
 /**
diff --git a/DataModel/Entity/Entity.php b/DataModel/Entity/Entity.php
index d3cfc5f..50f330b 100644
--- a/DataModel/Entity/Entity.php
+++ b/DataModel/Entity/Entity.php
@@ -935,4 +935,24 @@
                return $keyParts[0];
        }
 
+       /**
+        * Returns a list of all Snaks on this Entity. This includes at least 
the main snaks of
+        * Claims, the snaks from Claim qualifiers, and the snaks from 
Statement References.
+        *
+        * This is a convenience method for use in code that needs to operate 
on all snaks, e.g.
+        * to find all referenced Entities.
+        *
+        * @return Snak[]
+        */
+       public function getAllSnaks() {
+               $claims = $this->getClaims();
+               $snaks = array();
+
+               foreach ( $claims as $claim ) {
+                       $snaks = array_merge( $snaks, $claim->getAllSnaks() );
+               }
+
+               return $snaks;
+       }
+
 }
diff --git a/tests/phpunit/Claim/ClaimTest.php 
b/tests/phpunit/Claim/ClaimTest.php
index a5247ec..cac518b 100644
--- a/tests/phpunit/Claim/ClaimTest.php
+++ b/tests/phpunit/Claim/ClaimTest.php
@@ -43,6 +43,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 class ClaimTest extends \PHPUnit_Framework_TestCase {
 
@@ -218,4 +219,14 @@
                $this->assertEquals( Claim::RANK_TRUTH, $claim->getRank() );
        }
 
+       /**
+        * @dataProvider instanceProvider
+        *
+        * @param Claim $claim
+        */
+       public function testGetAllSnaks( Claim $claim ) {
+               $snaks = $claim->getAllSnaks();
+
+               $this->assertGreaterThanOrEqual( count( $claim->getQualifiers() 
) +1, count( $snaks ), "At least one snak per Qualifier" );
+       }
 }
diff --git a/tests/phpunit/Claim/StatementTest.php 
b/tests/phpunit/Claim/StatementTest.php
index cfa213f..2cb1dbb 100644
--- a/tests/phpunit/Claim/StatementTest.php
+++ b/tests/phpunit/Claim/StatementTest.php
@@ -186,4 +186,23 @@
                $this->assertEquals( $claim0->getHash(), $claim1->getHash() );
        }
 
+       /**
+        * @dataProvider instanceProvider
+        *
+        * @param Statement $statement
+        */
+       public function testGetAllSnaks( Claim $claim ) {
+               /* @var Statement $statement */
+               $statement = $claim;
+               $snaks = $statement->getAllSnaks();
+
+               $c = count( $statement->getQualifiers() ) + 1;
+
+               /* @var Reference $reference */
+               foreach ( $statement->getReferences() as $reference ) {
+                       $c += count( $reference->getSnaks() );
+               }
+
+               $this->assertGreaterThanOrEqual( $c, count( $snaks ), "At least 
one snak per Qualifier and Reference" );
+       }
 }
diff --git a/tests/phpunit/Entity/EntityTest.php 
b/tests/phpunit/Entity/EntityTest.php
index 458bb69..769bd6e 100644
--- a/tests/phpunit/Entity/EntityTest.php
+++ b/tests/phpunit/Entity/EntityTest.php
@@ -17,6 +17,7 @@
 use Wikibase\PropertyNoValueSnak;
 use Wikibase\PropertySomeValueSnak;
 use Wikibase\PropertyValueSnak;
+use Wikibase\Reference;
 use Wikibase\Statement;
 
 /**
@@ -48,6 +49,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 abstract class EntityTest extends \PHPUnit_Framework_TestCase {
 
@@ -800,4 +802,18 @@
                $this->assertTrue( $expected->equals( $source ) );
        }
 
+       /**
+        * @dataProvider instanceProvider
+        *
+        * @param Entity $entity
+        */
+       public function testGetAllSnaks( Entity $entity ) {
+               $snaks = $entity->getAllSnaks();
+               $claims = $entity->getClaims();
+
+               $this->assertInternalType( 'array', $snaks );
+
+               $this->assertGreaterThanOrEqual( count( $claims ), count( 
$snaks ), "At least one snak per Claim" );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/72960
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iae5f7817cfe17bbfe03ddb0ffeabf04bc59742bd
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to