jenkins-bot has submitted this change and it was merged.

Change subject: Introduce MentionedEntityTracker for tracking mentioned 
interfaces.
......................................................................


Introduce MentionedEntityTracker for tracking mentioned interfaces.

Previously, we were using callbacks, which was somewhat confusing.

Change-Id: I303c97a79c272635e5aa7c61ea20790dccdfb4ed
---
A repo/includes/rdf/EntityMentionListener.php
M repo/includes/rdf/FullStatementRdfBuilder.php
A repo/includes/rdf/NullEntityMentionListener.php
M repo/includes/rdf/RdfBuilder.php
M repo/includes/rdf/SimpleValueRdfBuilder.php
M repo/tests/phpunit/includes/rdf/ComplexValueRdfBuilderTest.php
M repo/tests/phpunit/includes/rdf/FullStatementsRdfBuilderTest.php
M repo/tests/phpunit/includes/rdf/SimpleValueRdfBuilderTest.php
8 files changed, 138 insertions(+), 56 deletions(-)

Approvals:
  Thiemo Mättig (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/rdf/EntityMentionListener.php 
b/repo/includes/rdf/EntityMentionListener.php
new file mode 100644
index 0000000..8f35fd5
--- /dev/null
+++ b/repo/includes/rdf/EntityMentionListener.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Wikibase\Rdf;
+
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\PropertyId;
+
+/**
+ * Interface for tracking entities mentioned while generating RDF.
+ *
+ * This information can be used to generate "stub" entries for entities that
+ * are referenced in the RDF output. Such stubs would typically give at
+ * least a type and a label for the entity.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+interface EntityMentionListener {
+
+       /**
+        * Should be called when an entity reference (an EntityIdValue object) 
is encountered.
+        *
+        * @param EntityId $id
+        */
+       public function entityReferenceMentioned( EntityId $id );
+
+       /**
+        * Should be called when a property is used in a PropertySnak.
+        *
+        * @param PropertyId $id
+        */
+       public function propertyMentioned( PropertyId $id );
+
+}
diff --git a/repo/includes/rdf/FullStatementRdfBuilder.php 
b/repo/includes/rdf/FullStatementRdfBuilder.php
index 0fa5d74..dc95ccd 100644
--- a/repo/includes/rdf/FullStatementRdfBuilder.php
+++ b/repo/includes/rdf/FullStatementRdfBuilder.php
@@ -27,9 +27,9 @@
 class FullStatementRdfBuilder implements EntityRdfBuilder {
 
        /**
-        * @var callable
+        * @var EntityMentionListener
         */
-       private $propertyMentionCallback = null;
+       private $mentionedEntityTracker;
 
        /**
         * @var DedupeBag
@@ -81,21 +81,22 @@
 
                $this->valueBuilder = $valueBuilder;
 
+               $this->mentionedEntityTracker = new NullEntityMentionListener();
                $this->dedupeBag = new NullDedupeBag();
        }
 
        /**
-        * @return callable
+        * @return EntityMentionListener
         */
-       public function getPropertyMentionCallback() {
-               return $this->propertyMentionCallback;
+       public function getEntityMentionListener() {
+               return $this->mentionedEntityTracker;
        }
 
        /**
-        * @param callable $propertyMentionCallback
+        * @param EntityMentionListener $mentionedEntityTracker
         */
-       public function setPropertyMentionCallback( $propertyMentionCallback ) {
-               $this->propertyMentionCallback = $propertyMentionCallback;
+       public function setEntityMentionListener( $mentionedEntityTracker ) {
+               $this->mentionedEntityTracker = $mentionedEntityTracker;
        }
 
        /**
@@ -230,7 +231,7 @@
                $this->statementWriter->about( RdfVocabulary::NS_STATEMENT, 
$statementLName );
                $this->addSnak( $this->statementWriter, $snak, 
RdfVocabulary::NS_VALUE );
 
-               $this->propertyMentioned( $snak->getPropertyId() );
+               $this->mentionedEntityTracker->propertyMentioned( 
$snak->getPropertyId() );
 
                $rank = $statement->getRank();
                if ( isset( RdfVocabulary::$rankMap[$rank] ) ) {
@@ -277,12 +278,6 @@
        }
 
        /**
-        * @param EntityId $propertyId
-        */
-       private function propertyMentioned( EntityId $propertyId ) {
-               if ( $this->propertyMentionCallback ) {
-                       call_user_func( $this->propertyMentionCallback, 
$propertyId );
-               }
        }
 
        /**
diff --git a/repo/includes/rdf/NullEntityMentionListener.php 
b/repo/includes/rdf/NullEntityMentionListener.php
new file mode 100644
index 0000000..bd39618
--- /dev/null
+++ b/repo/includes/rdf/NullEntityMentionListener.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace Wikibase\Rdf;
+
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\PropertyId;
+
+/**
+ * Null implementation of EntityMentionListener
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class NullEntityMentionListener implements EntityMentionListener {
+
+       /**
+        * Should be called when an entity reference (an EntityIdValue object) 
is encountered.
+        *
+        * @param EntityId $id
+        */
+       public function entityReferenceMentioned( EntityId $id ) {}
+
+       /**
+        * Should be called when a property is used in a PropertySnak.
+        *
+        * @param PropertyId $id
+        */
+       public function propertyMentioned( PropertyId $id ) {}
+
+}
diff --git a/repo/includes/rdf/RdfBuilder.php b/repo/includes/rdf/RdfBuilder.php
index be660b4..fc9b99a 100644
--- a/repo/includes/rdf/RdfBuilder.php
+++ b/repo/includes/rdf/RdfBuilder.php
@@ -9,6 +9,7 @@
 use Wikibase\DataModel\Entity\Property;
 use Wikibase\DataModel\Entity\PropertyDataTypeLookup;
 use Wikibase\DataModel\Term\FingerprintProvider;
+use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\Lib\Store\EntityLookup;
 use Wikibase\RdfProducer;
 use Wikimedia\Purtle\RdfWriter;
@@ -24,7 +25,7 @@
  * @author Daniel Kinzler
  * @author Stas Malyshev
  */
-class RdfBuilder implements EntityRdfBuilder {
+class RdfBuilder implements EntityRdfBuilder, EntityMentionListener {
 
        /**
         * A list of entities mentioned/touched to or by this builder.
@@ -118,10 +119,7 @@
         */
        private function newSimpleValueRdfBuilder() {
                $simpleValueBuilder = new SimpleValueRdfBuilder( 
$this->vocabulary, $this->propertyLookup );
-
-               if ( $this->shouldProduce( 
RdfProducer::PRODUCE_RESOLVED_ENTITIES ) ) {
-                       $simpleValueBuilder->setEntityMentionCallback( array( 
$this, 'entityMentioned' ) );
-               }
+               $simpleValueBuilder->setEntityMentionListener( $this );
 
                return $simpleValueBuilder;
        }
@@ -136,10 +134,7 @@
 
                        $statementValueBuilder = new ComplexValueRdfBuilder( 
$this->vocabulary, $valueWriter, $this->propertyLookup );
                        $statementValueBuilder->setDedupeBag( $this->dedupBag );
-
-                       if ( $this->shouldProduce( 
RdfProducer::PRODUCE_RESOLVED_ENTITIES ) ) {
-                               
$statementValueBuilder->setEntityMentionCallback( array( $this, 
'entityMentioned' ) );
-                       }
+                       $statementValueBuilder->setEntityMentionListener( $this 
);
                } else {
                        $statementValueBuilder = 
$this->newSimpleValueRdfBuilder();
 
@@ -166,11 +161,7 @@
 
                $statementBuilder = new FullStatementRdfBuilder( 
$this->vocabulary, $this->writer, $statementValueBuilder );
                $statementBuilder->setDedupeBag( $this->dedupBag );
-
-               if ( $this->shouldProduce( RdfProducer::PRODUCE_PROPERTIES ) ) {
-                       $statementBuilder->setPropertyMentionCallback( array( 
$this, 'entityMentioned' ) );
-               }
-
+               $statementBuilder->setEntityMentionListener( $this );
                $statementBuilder->setProduceQualifiers( $this->shouldProduce( 
RdfProducer::PRODUCE_QUALIFIERS ) );
                $statementBuilder->setProduceReferences( $this->shouldProduce( 
RdfProducer::PRODUCE_REFERENCES ) );
 
@@ -227,15 +218,35 @@
        }
 
        /**
+        * @see EntityMentionListener::entityReferenceMentioned
+        *
+        * @param EntityId $id
+        */
+       public function entityReferenceMentioned( EntityId $id ) {
+               if ( $this->shouldProduce( 
RdfProducer::PRODUCE_RESOLVED_ENTITIES ) ) {
+                       $this->entityToResolve( $id );
+               }
+       }
+
+       /**
+        * @see EntityMentionListener::propertyMentioned
+        *
+        * @param PropertyId $id
+        */
+       public function propertyMentioned( PropertyId $id ) {
+               if ( $this->shouldProduce( RdfProducer::PRODUCE_PROPERTIES ) ) {
+                       $this->entityToResolve( $id );
+               }
+       }
+
+       /**
         * Registers an entity as mentioned.
         * Will be recorded as unresolved
         * if it wasn't already marked as resolved.
         *
-        * @todo Make callback private once we drop PHP 5.3 compat.
-        *
         * @param EntityId $entityId
         */
-       public function entityMentioned( EntityId $entityId ) {
+       private function entityToResolve( EntityId $entityId ) {
                $prefixedId = $entityId->getSerialization();
 
                if ( !isset( $this->entitiesResolved[$prefixedId] ) ) {
diff --git a/repo/includes/rdf/SimpleValueRdfBuilder.php 
b/repo/includes/rdf/SimpleValueRdfBuilder.php
index 686e97c..fecf602 100644
--- a/repo/includes/rdf/SimpleValueRdfBuilder.php
+++ b/repo/includes/rdf/SimpleValueRdfBuilder.php
@@ -29,9 +29,9 @@
 class SimpleValueRdfBuilder implements SnakValueRdfBuilder {
 
        /**
-        * @var callable
+        * @var EntityMentionListener
         */
-       private $entityMentionCallback = null;
+       private $mentionedEntityTracker;
 
        /**
         * @var RdfVocabulary
@@ -59,20 +59,21 @@
                // TODO: if data is fixed to be always Gregorian, replace with
                // DateTimeValueCleaner
                $this->dateCleaner = new JulianDateTimeValueCleaner();
+               $this->mentionedEntityTracker = new NullEntityMentionListener();
        }
 
        /**
-        * @return callable
+        * @return EntityMentionListener
         */
-       public function getEntityMentionCallback() {
-               return $this->entityMentionCallback;
+       public function getEntityMentionListener() {
+               return $this->mentionedEntityTracker;
        }
 
        /**
-        * @param callable $entityMentionCallback
+        * @param EntityMentionListener $mentionedEntityTracker
         */
-       public function setEntityMentionCallback( $entityMentionCallback ) {
-               $this->entityMentionCallback = $entityMentionCallback;
+       public function setEntityMentionListener( $mentionedEntityTracker ) {
+               $this->mentionedEntityTracker = $mentionedEntityTracker;
        }
 
        /**
@@ -146,9 +147,7 @@
                $entityLName = $this->vocabulary->getEntityLName( $entityId );
                $writer->say( $propertyValueNamespace, $propertyValueLName 
)->is( RdfVocabulary::NS_ENTITY, $entityLName );
 
-               if ( $this->entityMentionCallback ) {
-                       call_user_func( $this->entityMentionCallback, $entityId 
);
-               }
+               $this->mentionedEntityTracker->entityReferenceMentioned( 
$entityId );
        }
 
        /**
diff --git a/repo/tests/phpunit/includes/rdf/ComplexValueRdfBuilderTest.php 
b/repo/tests/phpunit/includes/rdf/ComplexValueRdfBuilderTest.php
index 4a7e275..7f3320a 100644
--- a/repo/tests/phpunit/includes/rdf/ComplexValueRdfBuilderTest.php
+++ b/repo/tests/phpunit/includes/rdf/ComplexValueRdfBuilderTest.php
@@ -60,16 +60,19 @@
         * @return ComplexValueRdfBuilder
         */
        private function newBuilder( array &$mentioned = array(), DedupeBag 
$bag = null ) {
-               $entityMentioned = function( EntityId $id ) use ( &$mentioned ) 
{
-                       $key = $id->getSerialization();
-                       $mentioned[$key] = $id;
-               };
+               $mentionTracker = $this->getMock( 
'Wikibase\Rdf\EntityMentionListener' );
+               $mentionTracker->expects( $this->any() )
+                       ->method( 'entityReferenceMentioned' )
+                       ->will( $this->returnCallback( function( EntityId $id ) 
use ( &$mentioned ) {
+                               $key = $id->getSerialization();
+                               $mentioned[$key] = $id;
+                       } ) );
 
                $vocabulary = $this->getTestData()->getVocabulary();
                $valueWriter = $this->getTestData()->getNTriplesWriter();
 
                $builder = new ComplexValueRdfBuilder( $vocabulary, 
$valueWriter, $this->getTestData()->getMockRepository() );
-               $builder->setEntityMentionCallback( $entityMentioned );
+               $builder->setEntityMentionListener( $mentionTracker );
                $builder->setDedupeBag( $bag ?: new NullDedupeBag() );
 
                // HACK: glue on the value writer as a public field, so we can 
evaluate it later.
diff --git a/repo/tests/phpunit/includes/rdf/FullStatementsRdfBuilderTest.php 
b/repo/tests/phpunit/includes/rdf/FullStatementsRdfBuilderTest.php
index 95f67bf..d2734d3 100644
--- a/repo/tests/phpunit/includes/rdf/FullStatementsRdfBuilderTest.php
+++ b/repo/tests/phpunit/includes/rdf/FullStatementsRdfBuilderTest.php
@@ -55,10 +55,13 @@
 
                $writer = $this->getTestData()->getNTriplesWriter();
 
-               $entityMentioned = function( EntityId $id ) use ( &$mentioned ) 
{
-                       $key = $id->getSerialization();
-                       $mentioned[$key] = $id;
-               };
+               $mentionTracker = $this->getMock( 
'Wikibase\Rdf\EntityMentionListener' );
+               $mentionTracker->expects( $this->any() )
+                       ->method( 'propertyMentioned' )
+                       ->will( $this->returnCallback( function( EntityId $id ) 
use ( &$mentioned ) {
+                               $key = $id->getSerialization();
+                               $mentioned[$key] = $id;
+                       } ) );
 
                if ( $flavor & RdfProducer::PRODUCE_FULL_VALUES ) {
                        $valueWriter = $writer->sub();
@@ -72,7 +75,7 @@
                $statementBuilder->setDedupeBag( $dedupe ?: new NullDedupeBag() 
);
 
                if ( $flavor & RdfProducer::PRODUCE_PROPERTIES  ) {
-                       $statementBuilder->setPropertyMentionCallback( 
$entityMentioned );
+                       $statementBuilder->setEntityMentionListener( 
$mentionTracker );
                }
 
                $statementBuilder->setProduceQualifiers( $flavor & 
RdfProducer::PRODUCE_QUALIFIERS );
diff --git a/repo/tests/phpunit/includes/rdf/SimpleValueRdfBuilderTest.php 
b/repo/tests/phpunit/includes/rdf/SimpleValueRdfBuilderTest.php
index 9c4d6cc..5f1f850 100644
--- a/repo/tests/phpunit/includes/rdf/SimpleValueRdfBuilderTest.php
+++ b/repo/tests/phpunit/includes/rdf/SimpleValueRdfBuilderTest.php
@@ -56,15 +56,18 @@
         * @return SimpleValueRdfBuilder
         */
        private function newBuilder( array &$mentioned = array() ) {
-               $entityMentioned = function( EntityId $id ) use ( &$mentioned ) 
{
+               $mentionTracker = $this->getMock( 
'Wikibase\Rdf\EntityMentionListener' );
+               $mentionTracker->expects( $this->any() )
+                       ->method( 'entityReferenceMentioned' )
+                       ->will( $this->returnCallback( function( EntityId $id ) 
use ( &$mentioned ) {
                        $key = $id->getSerialization();
                        $mentioned[$key] = $id;
-               };
+               } ) );
 
                $vocabulary = $this->getTestData()->getVocabulary();
 
                $builder = new SimpleValueRdfBuilder( $vocabulary, 
$this->getTestData()->getMockRepository() );
-               $builder->setEntityMentionCallback( $entityMentioned );
+               $builder->setEntityMentionListener( $mentionTracker );
 
                return $builder;
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I303c97a79c272635e5aa7c61ea20790dccdfb4ed
Gerrit-PatchSet: 14
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to