Jakob has uploaded a new change for review.

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

Change subject: Integrate ForeignEntityValidator with ValidatorBuilders.
......................................................................

Integrate ForeignEntityValidator with ValidatorBuilders.

* Adds settings for foreignRepositories with supportedEntities
* Adds ForeignEntityValidator to ValidatorBuilders' EntityValidators

Change-Id: I10565c331318994b7e4e2eab2b5f959d3823d4d5
---
M docs/options.wiki
M repo/config/Wikibase.default.php
M repo/includes/ValidatorBuilders.php
M repo/includes/Validators/ForeignEntityValidator.php
M repo/includes/WikibaseRepo.php
M repo/tests/phpunit/includes/ValidatorBuildersTest.php
M repo/tests/phpunit/includes/Validators/ForeignEntityValidatorTest.php
7 files changed, 70 insertions(+), 17 deletions(-)


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

diff --git a/docs/options.wiki b/docs/options.wiki
index 325d8c6..50ccc67 100644
--- a/docs/options.wiki
+++ b/docs/options.wiki
@@ -50,6 +50,8 @@
 ;allowEntityImport: Allow importing entities via Special:Import and 
importDump.php. Per default, imports are forbidden, since entities defined in 
another wiki would have or use IDs that conflict with entities defined locally.
 ;pagePropertiesRdf: Array that maps between page properties and Wikibase 
predicates for RDF dumps. Maps from database property name to an array that 
contains a key <code>'name'</code> (RDF property name, which will be prefixed 
by <code>wikibase:</code>) and an optional key <code>'type'</code>.
 ;unitStorage: Definition for unit conversion storage. Should be in in the 
format <code>ObjectFactory</code> understands, example: <code>array( 'class' => 
'Wikibase\\Lib\\JsonUnitStorage',  'args' => array( 'myUnits.json' ) )</code>.
+;foreignRepositories: An associative array mapping foreign repository names to 
settings relevant to the particular repository. Each repository's settings are 
an associative array containing the following keys:
+:;'supportedEntityTypes': list of entity types (strings) that the local wiki 
supports from the foreign repository
 
 == Client Settings ==
 
diff --git a/repo/config/Wikibase.default.php b/repo/config/Wikibase.default.php
index 3f59c3f..c29cc00 100644
--- a/repo/config/Wikibase.default.php
+++ b/repo/config/Wikibase.default.php
@@ -193,5 +193,8 @@
        'pagePropertiesRdf' => [
                'wb-sitelinks' => [ 'name' => 'sitelinks', 'type' => 'integer' 
],
                'wb-claims' => [ 'name' => 'statements', 'type' => 'integer' ],
-       ]
+       ],
+
+       // Map of foreign repository names to repository-specific settings such 
as "supportedEntityTypes"
+       'foreignRepositories' => [],
 ];
diff --git a/repo/includes/ValidatorBuilders.php 
b/repo/includes/ValidatorBuilders.php
index 45509ea..627087f 100644
--- a/repo/includes/ValidatorBuilders.php
+++ b/repo/includes/ValidatorBuilders.php
@@ -18,6 +18,7 @@
 use Wikibase\Repo\Validators\DataFieldValidator;
 use Wikibase\Repo\Validators\DataValueValidator;
 use Wikibase\Repo\Validators\EntityExistsValidator;
+use Wikibase\Repo\Validators\ForeignEntityValidator;
 use Wikibase\Repo\Validators\MembershipValidator;
 use Wikibase\Repo\Validators\NumberRangeValidator;
 use Wikibase\Repo\Validators\NumberValidator;
@@ -82,12 +83,18 @@
        private $mediaFileNameLookup;
 
        /**
+        * @var array[]
+        */
+       private $supportedEntityTypes;
+
+       /**
         * @param EntityLookup $lookup
         * @param EntityIdParser $idParser
         * @param string[] $urlSchemes
         * @param string $vocabularyBaseUri The base URI for vocabulary 
concepts.
         * @param ContentLanguages $contentLanguages
         * @param CachingCommonsMediaFileNameLookup 
$cachingCommonsMediaFileNameLookup
+        * @param array $supportedEntityTypes map of repository names to lists 
of supported entity types
         */
        public function __construct(
                EntityLookup $lookup,
@@ -95,7 +102,8 @@
                array $urlSchemes,
                $vocabularyBaseUri,
                ContentLanguages $contentLanguages,
-               CachingCommonsMediaFileNameLookup 
$cachingCommonsMediaFileNameLookup
+               CachingCommonsMediaFileNameLookup 
$cachingCommonsMediaFileNameLookup,
+               array $supportedEntityTypes
        ) {
                $this->entityLookup = $lookup;
                $this->entityIdParser = $idParser;
@@ -103,6 +111,7 @@
                $this->vocabularyBaseUri = $vocabularyBaseUri;
                $this->contentLanguages = $contentLanguages;
                $this->mediaFileNameLookup = $cachingCommonsMediaFileNameLookup;
+               $this->supportedEntityTypes = $supportedEntityTypes;
        }
 
        /**
@@ -135,6 +144,7 @@
                return [
                        new TypeValidator( EntityIdValue::class ),
                        new EntityExistsValidator( $this->entityLookup, 
$entityType ),
+                       new ForeignEntityValidator( $this->supportedEntityTypes 
),
                ];
        }
 
diff --git a/repo/includes/Validators/ForeignEntityValidator.php 
b/repo/includes/Validators/ForeignEntityValidator.php
index a5cb479..933f0f9 100644
--- a/repo/includes/Validators/ForeignEntityValidator.php
+++ b/repo/includes/Validators/ForeignEntityValidator.php
@@ -7,6 +7,7 @@
 use ValueValidators\ValueValidator;
 use Wikibase\DataModel\Assert\RepositoryNameAssert;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikimedia\Assert\Assert;
 
 /**
@@ -36,11 +37,14 @@
         * Ensures an entity's repository name is known and
         * the corresponding repository supports the entity's type.
         *
-        * @param EntityId $id
+        * @param EntityId|EntityIdValue $id
         *
         * @return Result
         */
        public function validate( $id ) {
+               if ( $id instanceof EntityIdValue ) {
+                       $id = $id->getEntityId();
+               }
                Assert::parameterType( EntityId::class, $id, '$id' );
 
                if ( !$id->isForeign() ) {
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 9a9d24b..a1a1396 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -352,6 +352,9 @@
         */
        private function newValidatorBuilders() {
                $urlSchemes = $this->settings->getSetting( 'urlSchemes' );
+               $supportedEntityTypes = array_map( function( $repoSettings ) {
+                       return $repoSettings[ 'supportedEntityTypes' ];
+               }, $this->settings->getSetting( 'foreignRepositories' ) );
 
                return new ValidatorBuilders(
                        $this->getEntityLookup(),
@@ -359,7 +362,8 @@
                        $urlSchemes,
                        $this->getVocabularyBaseUri(),
                        $this->getMonolingualTextLanguages(),
-                       $this->getCachingCommonsMediaFileNameLookup()
+                       $this->getCachingCommonsMediaFileNameLookup(),
+                       $supportedEntityTypes
                );
        }
 
diff --git a/repo/tests/phpunit/includes/ValidatorBuildersTest.php 
b/repo/tests/phpunit/includes/ValidatorBuildersTest.php
index e86b6df..bb0ec62 100644
--- a/repo/tests/phpunit/includes/ValidatorBuildersTest.php
+++ b/repo/tests/phpunit/includes/ValidatorBuildersTest.php
@@ -13,12 +13,15 @@
 use PHPUnit_Framework_TestCase;
 use ValueValidators\Result;
 use ValueValidators\ValueValidator;
+use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\Property;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Services\Lookup\DispatchingEntityLookup;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
 use Wikibase\Lib\StaticContentLanguages;
 use Wikibase\Lib\Tests\MockRepository;
 use Wikibase\Repo\CachingCommonsMediaFileNameLookup;
@@ -38,27 +41,44 @@
        private function newValidatorBuilders() {
                $entityIdParser = new BasicEntityIdParser();
 
+               $urlSchemes = array( 'http', 'https', 'ftp', 'mailto' );
+
+               $builders = new ValidatorBuilders(
+                       $this->getEntityLookup(),
+                       $entityIdParser,
+                       $urlSchemes,
+                       'http://qudt.org/vocab/',
+                       new StaticContentLanguages( array( 'contentlanguage' ) 
),
+                       $this->getCachingCommonsMediaFileNameLookup(),
+                       [ 'foo' => [ Item::ENTITY_TYPE ] ]
+               );
+
+               return $builders;
+       }
+
+       private function getEntityLookup() {
                $q8 = new Item( new ItemId( 'Q8' ) );
 
                $p8 = Property::newFromType( 'string' );
                $p8->setId( new PropertyId( 'P8' ) );
 
-               $entityLookup = new MockRepository();
-               $entityLookup->putEntity( $q8 );
-               $entityLookup->putEntity( $p8 );
+               $localLookup = new MockRepository();
+               $localLookup->putEntity( $q8 );
+               $localLookup->putEntity( $p8 );
 
-               $urlSchemes = array( 'http', 'https', 'ftp', 'mailto' );
+               $fooQ123 = new ItemId( 'foo:Q123' );
+               $fooP42 = new PropertyId( 'foo:P42' );
 
-               $builders = new ValidatorBuilders(
-                       $entityLookup,
-                       $entityIdParser,
-                       $urlSchemes,
-                       'http://qudt.org/vocab/',
-                       new StaticContentLanguages( array( 'contentlanguage' ) 
),
-                       $this->getCachingCommonsMediaFileNameLookup()
-               );
+               $foreignLookup = $this->getMock( EntityLookup::class );
+               $foreignLookup->method( 'hasEntity' )
+                       ->willReturnCallback( function( EntityId $id ) use ( 
$fooQ123, $fooP42 ) {
+                               return $id->equals( $fooQ123 ) || $id->equals( 
$fooP42 );
+                       } );
 
-               return $builders;
+               return new DispatchingEntityLookup( [
+                       '' => $localLookup,
+                       'foo' => $foreignLookup,
+               ] );
        }
 
        /**
@@ -99,6 +119,9 @@
                        array( 'wikibase-entity', new EntityIdValue( new 
PropertyId( 'p8' ) ), true, 'existing entity' ),
                        array( 'wikibase-entity', new EntityIdValue( new 
ItemId( 'q8' ) ), true, 'existing entity' ),
                        array( 'wikibase-entity', new EntityIdValue( new 
ItemId( 'q3' ) ), false, 'missing entity' ),
+                       array( 'wikibase-entity', new EntityIdValue( new 
ItemId( 'bar:Q123' ) ), false, 'unknown repository' ),
+                       array( 'wikibase-entity', new EntityIdValue( new 
ItemId( 'foo:Q123' ) ), true, 'foreign entity' ),
+                       array( 'wikibase-entity', new EntityIdValue( new 
PropertyId( 'foo:P42' ) ), false, 'unsupported foreign entity type' ),
                        array( 'wikibase-entity', new StringValue( 'q8' ), 
false, 'Expected EntityId, StringValue supplied' ),
 
                        //commonsMedia
diff --git 
a/repo/tests/phpunit/includes/Validators/ForeignEntityValidatorTest.php 
b/repo/tests/phpunit/includes/Validators/ForeignEntityValidatorTest.php
index 3993304..50a1b02 100644
--- a/repo/tests/phpunit/includes/Validators/ForeignEntityValidatorTest.php
+++ b/repo/tests/phpunit/includes/Validators/ForeignEntityValidatorTest.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Test\Repo\Validators;
 
 use InvalidArgumentException;
+use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\Property;
@@ -61,6 +62,12 @@
                ];
        }
 
+       public function testGivenEntityIdValue_validateExtractsEntityId() {
+               $validator = new ForeignEntityValidator( [] );
+               $this->assertTrue( $validator->validate( new EntityIdValue( new 
ItemId( 'Q123' ) ) )->isValid() );
+               $this->assertFalse( $validator->validate( new EntityIdValue( 
new ItemId( 'foo:Q123' ) ) )->isValid() );
+       }
+
        public function testGivenLocalEntity_validateEntityReturnsTrue() {
                $validator = new ForeignEntityValidator( [] );
 

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

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

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

Reply via email to