Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/72960
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/Entity/Entity.php
M tests/phpunit/Entity/EntityTest.php
2 files changed, 43 insertions(+), 0 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDataModel
refs/changes/60/72960/1
diff --git a/DataModel/Entity/Entity.php b/DataModel/Entity/Entity.php
index c9e02af..c3437d7 100644
--- a/DataModel/Entity/Entity.php
+++ b/DataModel/Entity/Entity.php
@@ -933,4 +933,31 @@
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[] = $claim->getMainSnak();
+ $snaks = array_merge( $snaks, iterator_to_array(
$claim->getQualifiers() ) );
+
+ if( $claim instanceof Statement ) {
+ foreach( $claim->getReferences() as $reference
) {
+ $snaks = array_merge( $snaks,
iterator_to_array( $reference->getSnaks() ) );
+ }
+ }
+ }
+
+ return $snaks;
+ }
+
}
diff --git a/tests/phpunit/Entity/EntityTest.php
b/tests/phpunit/Entity/EntityTest.php
index 929bc13..340ba9b 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 ) !== 0, 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: newchange
Gerrit-Change-Id: Iae5f7817cfe17bbfe03ddb0ffeabf04bc59742bd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits