Daniel Kinzler has uploaded a new change for review.

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


Change subject: Generalized access to Claims by property ID.
......................................................................

Generalized access to Claims by property ID.

This provides functions to access claims by property directly from
the Entity object, and it generalizes the PropertyLookup interface
to return Claims.

Change-Id: I76e82720f3e8e9a076d4d43750f87b31337a43be
---
M DataModel/DataModel/Claim/Claim.php
M DataModel/DataModel/Claim/ClaimListAccess.php
M DataModel/DataModel/Claim/Claims.php
M DataModel/tests/phpunit/Claim/ClaimsTest.php
M client/includes/parserhooks/PropertyParserFunction.php
M lib/includes/store/PropertyLookup.php
M lib/includes/store/sql/PropertySQLLookup.php
M lib/tests/phpunit/store/sql/PropertySQLLookupTest.php
8 files changed, 168 insertions(+), 97 deletions(-)


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

diff --git a/DataModel/DataModel/Claim/Claim.php 
b/DataModel/DataModel/Claim/Claim.php
index 1620060..ea361a6 100644
--- a/DataModel/DataModel/Claim/Claim.php
+++ b/DataModel/DataModel/Claim/Claim.php
@@ -132,7 +132,7 @@
         *
         * @since 0.2
         *
-        * @return integer
+        * @return EntityId
         */
        public function getPropertyId() {
                return $this->getMainSnak()->getPropertyId();
diff --git a/DataModel/DataModel/Claim/ClaimListAccess.php 
b/DataModel/DataModel/Claim/ClaimListAccess.php
index d57dbb0..38c3c87 100644
--- a/DataModel/DataModel/Claim/ClaimListAccess.php
+++ b/DataModel/DataModel/Claim/ClaimListAccess.php
@@ -27,6 +27,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 interface ClaimListAccess {
 
@@ -90,4 +91,24 @@
         */
        public function getClaimWithGuid( $claimGuid );
 
+       /**
+        * Returns the claims for the given property.
+        *
+        * @since 0.4
+        *
+        * @param int $propertyId
+        *
+        * @return Claims
+        */
+       public function getClaimsForProperty( $propertyId );
+
+       /**
+        * Returns the main Snaks of the claims in this list.
+        *
+        * @since 0.4
+        *
+        * @return SnakList
+        */
+       public function getMainSnaks();
+
 }
diff --git a/DataModel/DataModel/Claim/Claims.php 
b/DataModel/DataModel/Claim/Claims.php
index 8ee664b..9163bcc 100644
--- a/DataModel/DataModel/Claim/Claims.php
+++ b/DataModel/DataModel/Claim/Claims.php
@@ -35,6 +35,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
  */
 class Claims extends HashArray implements ClaimListAccess {
 
@@ -160,6 +161,60 @@
        }
 
        /**
+        * @see   ClaimListAccess::getClaimsForProperty
+        *
+        * @since 0.4
+        *
+        * @param int|EntityId $propertyId
+        *
+        * @throws \MWException if $propertyId isn't valid
+        * @return Claims
+        */
+       public function getClaimsForProperty( $propertyId ) {
+               $claims = new Claims();
+
+               if ( $propertyId instanceof EntityId) {
+                       if ( $propertyId->getEntityType() !== 
Property::ENTITY_TYPE ) {
+                               throw new MWException( "ID must refer to a 
property, not "
+                                       . $propertyId->getEntityType() );
+                       }
+
+                       $propertyId = $propertyId->getNumericId();
+               }
+
+               if ( !is_int( $propertyId ) ) {
+                       throw new MWException( "ID must an int." );
+               }
+
+               /* @var Claim $claim */
+               foreach ( $this as $claim ) {
+                       if ( $propertyId == 
$claim->getPropertyId()->getNumericId() ) {
+                               $claims[] = $claim;
+                       }
+               }
+
+               return $claims;
+       }
+
+       /**
+        * @see ClaimListAccess::getMainSnaks
+        *
+        * @since 0.4
+        *
+        * @return SnakList
+        */
+       public function getMainSnaks() {
+               $snaks = new SnakList();
+
+               /* @var Claim $claim */
+               foreach ( $this as $claim ) {
+                       $snaks[] = $claim->getMainSnak();
+               }
+
+               return $snaks;
+       }
+
+       /**
         * @see GenericArrayObject::preSetElement
         *
         * @since 0.3
diff --git a/DataModel/tests/phpunit/Claim/ClaimsTest.php 
b/DataModel/tests/phpunit/Claim/ClaimsTest.php
index 143ab29..6a12647 100644
--- a/DataModel/tests/phpunit/Claim/ClaimsTest.php
+++ b/DataModel/tests/phpunit/Claim/ClaimsTest.php
@@ -57,8 +57,17 @@
                $instances = array();
 
                $instances[] = new \Wikibase\Claim(
-                       new \Wikibase\PropertyNoValueSnak( new 
\Wikibase\EntityId( \Wikibase\Property::ENTITY_TYPE, 42 ) )
-               );
+                       new \Wikibase\PropertyNoValueSnak(
+                               new \Wikibase\EntityId( 
\Wikibase\Property::ENTITY_TYPE, 23 ) ) );
+
+               $instances[] = new \Wikibase\Claim(
+                       new \Wikibase\PropertySomeValueSnak(
+                               new \Wikibase\EntityId( 
\Wikibase\Property::ENTITY_TYPE, 42 ) ) );
+
+               $instances[] = new \Wikibase\Claim(
+                       new \Wikibase\PropertyValueSnak(
+                               new \Wikibase\EntityId( 
\Wikibase\Property::ENTITY_TYPE, 42 ),
+                               new \DataValues\StringValue( "foo" ) ) );
 
                return $instances;
        }
@@ -136,6 +145,30 @@
                $this->assertEquals( $elementCount, count( $array ) );
        }
 
+       /**
+        * @dataProvider instanceProvider
+        *
+        * @param \Wikibase\Claims $array
+        */
+       public function testGetMainSnaks( Claims $array ) {
+               $snaks = $array->getMainSnaks();
+               $this->assertInstanceOf( 'Wikibase\SnakList', $snaks );
+               $this->assertEquals( count( $array ), count( $snaks ) );
+       }
+
+       public function testGetClaimsForProperty() {
+               $array = new Claims( $this->getElementInstances() );
+
+               $claims = $array->getClaimsForProperty( 42 );
+               $this->assertInstanceOf( 'Wikibase\Claims', $claims );
+               $this->assertEquals( 2, count( $claims ) );
+
+               $propId = new \Wikibase\EntityId( 
\Wikibase\Property::ENTITY_TYPE, 23 );
+               $claims = $array->getClaimsForProperty( $propId );
+               $this->assertInstanceOf( 'Wikibase\Claims', $claims );
+               $this->assertEquals( 1, count( $claims ) );
+       }
+
        public function testDuplicateClaims() {
                $firstClaim = new Claim( new \Wikibase\PropertyNoValueSnak( 42 
) );
                $secondClaim = new Claim( new \Wikibase\PropertyNoValueSnak( 42 
) );
diff --git a/client/includes/parserhooks/PropertyParserFunction.php 
b/client/includes/parserhooks/PropertyParserFunction.php
index 05160bf..84bd5b1 100644
--- a/client/includes/parserhooks/PropertyParserFunction.php
+++ b/client/includes/parserhooks/PropertyParserFunction.php
@@ -132,31 +132,41 @@
        /**
         * @since 0.4
         *
-        * @param string $propertyLabel
+        * @param EntityId $entityId
+        * @param string   $propertyLabel
         *
         * @return string - wikitext format
         */
-    public function evaluate( EntityId $entityId, $propertyLabel ) {
+       public function evaluate( EntityId $entityId, $propertyLabel ) {
                wfProfileIn( __METHOD__ );
 
-               $snakList = new SnakList();
+               $entity = $this->entityLookup->getEntity( $entityId );
 
-               $propertyIdToFind = EntityId::newFromPrefixedId( $propertyLabel 
);
-
-               if ( $propertyIdToFind !== null ) {
-                       $snakList = 
$this->propertyLookup->getMainSnaksByPropertyId( $entityId, $propertyIdToFind );
-               } else {
-                       $langCode = $this->language->getCode();
-                       $snakList = 
$this->propertyLookup->getMainSnaksByPropertyLabel( $entityId, $propertyLabel, 
$langCode );
-               }
-
-               if ( $snakList->isEmpty() ) {
+               if ( !$entity ) {
                        wfProfileOut( __METHOD__ );
                        return '';
                }
 
+               $propertyIdToFind = EntityId::newFromPrefixedId( $propertyLabel 
);
+
+               if ( $propertyIdToFind !== null ) {
+                       $allClaims = new Claims( $entity->getClaims() );
+                       $claims = $allClaims->getClaimsForProperty( 
$propertyIdToFind->getNumericId() );
+               } else {
+                       $langCode = $this->language->getCode();
+                       $claims = 
$this->propertyLookup->getClaimsByPropertyLabel( $entity, $propertyLabel, 
$langCode );
+               }
+
+               if ( $claims->isEmpty() ) {
+                       wfProfileOut( __METHOD__ );
+                       return '';
+               }
+
+               $snakList = $claims->getMainSnaks();
+               $text = $this->formatSnakList( $snakList, $propertyLabel );
+
                wfProfileOut( __METHOD__ );
-               return $this->formatSnakList( $snakList, $propertyLabel );
+               return $text;
        }
 
        /**
diff --git a/lib/includes/store/PropertyLookup.php 
b/lib/includes/store/PropertyLookup.php
index 8469d71..17766c8 100644
--- a/lib/includes/store/PropertyLookup.php
+++ b/lib/includes/store/PropertyLookup.php
@@ -29,28 +29,19 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < [email protected] >
+ * @author Daniel Kinzler
  */
 interface PropertyLookup {
 
        /**
-        * @since 0.4
+        * @since    0.4
         *
-        * @param EntityId $entityId
-        * @param string $propertyLabel
-        *
-        * @return SnakList
-        */
-       public function getMainSnaksByPropertyId( EntityId $entityId, EntityId 
$propertyId );
-
-       /**
-        * @since 0.4
-        *
-        * @param EntityId $entityId
+        * @param Entity $entity
         * @param string $propertyLabel
         * @param string $langCode
         *
-        * @return SnakList
+        * @return Claims
         */
-       public function getMainSnaksByPropertyLabel( EntityID $entityId, 
$propertyLabel, $langCode );
+       public function getClaimsByPropertyLabel( Entity $entity, 
$propertyLabel, $langCode );
 
 }
diff --git a/lib/includes/store/sql/PropertySQLLookup.php 
b/lib/includes/store/sql/PropertySQLLookup.php
index 93f5dd9..63128f3 100644
--- a/lib/includes/store/sql/PropertySQLLookup.php
+++ b/lib/includes/store/sql/PropertySQLLookup.php
@@ -90,32 +90,6 @@
        /**
         * @since 0.4
         *
-        * @param EntityId $entityId
-        * @param string $propertyLabel
-        *
-        * @return SnakList
-        */
-       public function getMainSnaksByPropertyId( EntityId $entityId, EntityId 
$propertyId ) {
-               $entity = $this->entityLookup->getEntity( $entityId );
-               $statements = $entity->getClaims();
-
-               $snakList = new SnakList();
-
-               foreach( $statements as $statement ) {
-                       $snak = $statement->getMainSnak();
-                       $snakPropertyId = $snak->getPropertyId();
-
-                       if ( $snakPropertyId->getPrefixedId()  === 
$propertyId->getPrefixedId() ) {
-                               $snakList->addSnak( $snak );
-                       }
-               }
-
-               return $snakList;
-       }
-
-       /**
-        * @since 0.4
-        *
         * @param EntityId $propertyId
         *
         * @return string|false
@@ -155,26 +129,6 @@
        /**
         * @since 0.4
         *
-        * @param EntityId $propertyId
-        *
-        * @return SnakList
-        */
-       protected function getSnakListForProperty( EntityId $propertyId ) {
-               wfProfileIn( __METHOD__ );
-               $statements = $this->getStatementsByProperty( $propertyId );
-               $snakList = new SnakList();
-
-               foreach( $statements as $statement ) {
-                       $snakList->addSnak( $statement->getMainSnak() );
-               }
-
-               wfProfileOut( __METHOD__ );
-               return $snakList;
-       }
-
-       /**
-        * @since 0.4
-        *
         * @param string $propertyLabel
         * @param string $langCode
         *
@@ -201,16 +155,16 @@
        /**
         * @since 0.4
         *
-        * @param EntityId $entityId
+        * @param Entity $entity
         * @param string $propertyLabel
         * @param string $langCode
         *
-        * @return SnakList
+        * @return Claims
         */
-       public function getMainSnaksByPropertyLabel( EntityId $entityId, 
$propertyLabel, $langCode ) {
+       public function getClaimsByPropertyLabel( Entity $entity, 
$propertyLabel, $langCode ) {
                wfProfileIn( __METHOD__ );
 
-               $snakList = new SnakList();
+               $claims = null;
 
                if ( defined( 'WB_EXPERIMENTAL_FEATURES' ) && 
WB_EXPERIMENTAL_FEATURES ) {
                        $propertyId = $this->getPropertyIdByLabel( 
$propertyLabel, $langCode );
@@ -220,15 +174,22 @@
                                //      in the item, we'll always try to 
re-index the item's properties.
                                //      We just hope that this is rare, because 
people notice when a label
                                //      doesn't work.
-                               $this->indexPropertiesByLabel( $entityId, 
$langCode );
+                               $this->indexPropertiesByLabel( 
$entity->getId(), $langCode );
                                $propertyId = $this->getPropertyIdByLabel( 
$propertyLabel, $langCode );
                        }
 
-                       $snakList = $propertyId !== null ? 
$this->getSnakListForProperty( $propertyId ) : $snakList;
+                       if ( $propertyId !== null ) {
+                               $allClaims = new Claims( $entity->getClaims() );
+                               $claims = $allClaims->getClaimsForProperty( 
$propertyId->getNumericId() );
+                       }
+               }
+
+               if ( $claims === null ) {
+                       $claims = new Claims();
                }
 
                wfProfileOut( __METHOD__ );
-               return $snakList;
+               return $claims;
        }
 
 }
diff --git a/lib/tests/phpunit/store/sql/PropertySQLLookupTest.php 
b/lib/tests/phpunit/store/sql/PropertySQLLookupTest.php
index 1c8596e..ab67677 100644
--- a/lib/tests/phpunit/store/sql/PropertySQLLookupTest.php
+++ b/lib/tests/phpunit/store/sql/PropertySQLLookupTest.php
@@ -6,7 +6,6 @@
 use Wikibase\EntityId;
 use Wikibase\Property;
 use Wikibase\Item;
-use Wikibase\SnakList;
 use Wikibase\Statement;
 use Wikibase\Claims;
 use Wikibase\PropertyValueSnak;
@@ -41,6 +40,7 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < [email protected] >
+ * @author Daniel Kinzler
  */
 class PropertySQLLookupTest extends \MediaWikiTestCase {
 
@@ -86,7 +86,6 @@
                        )
                );
 
-               $entityFactory = EntityFactory::singleton();
                $properties = array();
 
                foreach( $propertyData as $data ) {
@@ -205,10 +204,11 @@
                        $this->markTestSkipped( "getMainSnaksByPropertyLabel is 
experimental" );
                }
 
-               $snakList = $this->propertyLookup->getMainSnaksByPropertyLabel( 
$entityId, $propertyLabel, $langCode );
+               $entity = $this->entityLookup->getEntity( $entityId );
+               $claims = $this->propertyLookup->getClaimsByPropertyLabel( 
$entity, $propertyLabel, $langCode );
 
-               $this->assertInstanceOf( '\Wikibase\SnakList', $snakList );
-               $this->assertEquals( $expected, $snakList->count() );
+               $this->assertInstanceOf( '\Wikibase\Claims', $claims );
+               $this->assertEquals( $expected, count( $claims ) );
        }
 
        public function testGetMainSnaksByPropertyLabel2( ) {
@@ -216,19 +216,19 @@
                        $this->markTestSkipped( "getMainSnaksByPropertyLabel is 
experimental" );
                }
 
-               $entity126 = new EntityId( Item::ENTITY_TYPE, 126 );
+               $entity126 = $this->entityLookup->getEntity( new EntityId( 
Item::ENTITY_TYPE, 126 ) );
 
-               $snakList = $this->propertyLookup->getMainSnaksByPropertyLabel( 
$entity126, 'capital', 'en' );
-               $this->assertEquals( 2, $snakList->count() );
+               $claims = $this->propertyLookup->getClaimsByPropertyLabel( 
$entity126, 'capital', 'en' );
+               $this->assertEquals( 2, count( $claims ) );
 
-               $snakList = $this->propertyLookup->getMainSnaksByPropertyLabel( 
$entity126, 'country code', 'en' );
-               $this->assertEquals( 0, $snakList->count() );
+               $claims = $this->propertyLookup->getClaimsByPropertyLabel( 
$entity126, 'country code', 'en' );
+               $this->assertEquals( 0, count( $claims ) );
 
                // try to find a property in another entity, if that property 
wasn't used by the previous entity.
-               $entity128 = new EntityId( Item::ENTITY_TYPE, 128 );
+               $entity128 = $this->entityLookup->getEntity( new EntityId( 
Item::ENTITY_TYPE, 128 ) );
 
-               $snakList = $this->propertyLookup->getMainSnaksByPropertyLabel( 
$entity128, 'country code', 'en' );
-               $this->assertEquals( 1, $snakList->count(), "property unknown 
to the first item" );
+               $claims = $this->propertyLookup->getClaimsByPropertyLabel( 
$entity128, 'country code', 'en' );
+               $this->assertEquals( 1, count( $claims ), "property unknown to 
the first item" );
        }
 
        public function getPropertyLabelProvider() {

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

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

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

Reply via email to