Daniel Werner has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/84125


Change subject: Introduction of FetchedEntitiesFinder::findDataValueLinks
......................................................................

Introduction of FetchedEntitiesFinder::findDataValueLinks

Change-Id: I7e105441533e0060500de3cf214b0eeeb843ac29
---
M lib/includes/ReferencedEntitiesFinder.php
M lib/tests/phpunit/ReferencedEntitiesFinderTest.php
2 files changed, 89 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/25/84125/1

diff --git a/lib/includes/ReferencedEntitiesFinder.php 
b/lib/includes/ReferencedEntitiesFinder.php
index 54ab263..2bfafde 100644
--- a/lib/includes/ReferencedEntitiesFinder.php
+++ b/lib/includes/ReferencedEntitiesFinder.php
@@ -3,6 +3,7 @@
 namespace Wikibase;
 
 use Wikibase\DataModel\Entity\EntityIdValue;
+use DataValues\DataValue;
 
 /**
  * Finds linked entities given a list of entities or a list of claims.
@@ -14,12 +15,15 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
- * @author Daniel Werner < [email protected] >
+ * @author Daniel Werner < [email protected] >
+ * @author Katie Filbert
  * @author Daniel Kinzler
  */
 class ReferencedEntitiesFinder {
 
        /**
+        * Finds linked entities within a set of snaks.
+        *
         * @param Snak[] $snaks
         *
         * @return EntityId[]
@@ -40,22 +44,34 @@
                                        continue;
                                }
 
-                               switch( $snakValue->getType() ) {
-                                       case 'wikibase-entityid':
-                                               if( $snakValue instanceof 
EntityIdValue ) {
-                                                       $foundEntities[] = 
$snakValue->getEntityId();
-                                               }
-                                               break;
-                                       // TODO: handle values in other 
formats. E.g. in an earlier version the
-                                       //  'wikibase-entity' data type has 
been using 'string' values to store its ID.
-
-                                       // TODO: we might want to allow 
extensions to add handling for their custom
-                                       //  data value types here. Either use a 
hook or a proper registration for that.
-                               }
+                               $entitiesInSnakDataValue = 
$this->findDataValueLinks( $snakValue );
+                               $foundEntities = array_merge( $foundEntities, 
$entitiesInSnakDataValue );
                        }
                }
 
                return array_unique( $foundEntities );
        }
 
+       /**
+        * Finds linked entities within a given data value.
+        *
+        * @since 0.5
+        *
+        * @param DataValue $dataValue
+        * @return EntityId[]
+        */
+       public function findDataValueLinks( DataValue $dataValue ) {
+               switch( $dataValue->getType() ) {
+                       case 'wikibase-entityid':
+                               if( $dataValue instanceof EntityIdValue ) {
+                                       return array(
+                                               $dataValue->getEntityId() );
+                               }
+                               break;
+
+                       // TODO: we might want to allow extensions to add 
handling for their custom
+                       //  data value types here. Either use a hook or a 
proper registration for that.
+               }
+               return array();
+       }
 }
diff --git a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php 
b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
index b26fc0a..b5177ee 100644
--- a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
+++ b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
@@ -18,6 +18,7 @@
 use Wikibase\PropertyValueSnak;
 use Wikibase\ReferencedEntitiesFinder;
 use Wikibase\Snak;
+use Wikibase\Validators\EntityIdValidator;
 
 /**
  * @covers Wikibase\ReferencedEntitiesFinder
@@ -31,6 +32,7 @@
  * @group Wikibase
  * @group WikibaseLib
  * @group EntityLinkFinder
+ * @group xxx
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
@@ -45,8 +47,8 @@
                $p27 = new PropertyId( 'p27' );
                $p44 = new PropertyId( 'p44' );
 
-               $q23 = new EntityIdValue( new ItemId( 'q23' ) );
-               $q24 = new EntityIdValue( new ItemId( 'q24' ) );
+               $q23Value = new EntityIdValue( new ItemId( 'q23' ) );
+               $q24Value = new EntityIdValue( new ItemId( 'q24' ) );
 
                $argLists[] = array(
                        array(),
@@ -73,20 +75,20 @@
                );
 
                $argLists[] = array(
-                       array( new PropertyValueSnak( $p27, $q23 ) ),
-                       array( $p27, $q23->getEntityId() ),
+                       array( new PropertyValueSnak( $p27, $q23Value ) ),
+                       array( $p27, $q23Value->getEntityId() ),
                        "PropertyValueSnak with EntityId"
                );
 
                $argLists[] = array(
                        array(
-                               new PropertyValueSnak( $p11, $q23 ),
+                               new PropertyValueSnak( $p11, $q23Value ),
                                new PropertyNoValueSnak( $p27 ),
                                new PropertySomeValueSnak( $p44 ),
                                new PropertyValueSnak( $p44, new StringValue( 
'onoez' ) ),
-                               new PropertyValueSnak( $p44, $q24 ),
+                               new PropertyValueSnak( $p44, $q24Value ),
                        ),
-                       array( $p11, $q23->getEntityId(), $p27, $p44, 
$q24->getEntityId() ),
+                       array( $p11, $q23Value->getEntityId(), $p27, $p44, 
$q24Value->getEntityId() ),
                        "PropertyValueSnak with EntityId"
                );
 
@@ -114,4 +116,55 @@
                $this->assertEquals( $expected, $actual, $message );
        }
 
+       public function dataValuesProvider() {
+               $p21 = new PropertyId( 'p11' );
+               $q42 = new ItemId( 'q42' );
+               $stringValue = new StringValue( 'q1337' );
+
+               return array(
+                       $this->dataValuesTestCaseFromEntity( $p21 ),
+                       $this->dataValuesTestCaseFromEntity( $q42 ),
+                       array(
+                               $stringValue,
+                               array(),
+                               'StringValue without references'
+                       )
+               );
+       }
+
+       /**
+        * Returns a test definition suitable for "testFindDataValueLinks".
+        *
+        * @param string $entity
+        * @return array
+        */
+       private function dataValuesTestCaseFromEntity( $entity ) {
+               $definition = array(
+                       new EntityIdValue( $entity ),
+                       array( $entity ),
+                       $entity->getEntityType()
+               );
+               return $definition;
+       }
+
+       /**
+        * @dataProvider dataValuesProvider
+        *
+        * @param DataValue $dataValue
+        * @param array $expected
+        * @param string $message
+        */
+       public function testFindDataValueLinks( DataValue $dataValue, array 
$expected, $message = '' ) {
+               $linkFinder = new ReferencedEntitiesFinder();
+
+               $actual = $linkFinder->findDataValueLinks( $dataValue );
+
+               $expected = array_values( $expected );
+               $actual = array_values( $actual );
+
+               asort( $expected );
+               asort( $actual );
+
+               $this->assertEquals( $expected, $actual, $message );
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7e105441533e0060500de3cf214b0eeeb843ac29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>

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

Reply via email to