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

Change subject: Resolve redirects in JS config variables.
......................................................................


Resolve redirects in JS config variables.

Bug: 66971
Change-Id: I4fb12ed3f834132bf7ef40e62be888d306721b34
---
M lib/includes/store/EntityInfoBuilder.php
M lib/includes/store/GenericEntityInfoBuilder.php
M lib/includes/store/sql/SqlEntityInfoBuilder.php
M lib/tests/phpunit/store/EntityInfoBuilderTest.php
M repo/includes/ParserOutputJsConfigBuilder.php
M repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
6 files changed, 224 insertions(+), 32 deletions(-)

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



diff --git a/lib/includes/store/EntityInfoBuilder.php 
b/lib/includes/store/EntityInfoBuilder.php
index ab54ece..a02efa1 100644
--- a/lib/includes/store/EntityInfoBuilder.php
+++ b/lib/includes/store/EntityInfoBuilder.php
@@ -2,6 +2,8 @@
 
 namespace Wikibase\Lib\Store;
 
+use Wikibase\DataModel\Entity\EntityId;
+
 /**
  * A builder for collecting information about a batch of entities in an 
efficient way.
  *
@@ -34,7 +36,9 @@
         * and must thus not contain any objects.
         *
         * @note: after resolveRedirects() is called, entity records will be 
available under
-        * their actual ID as well as any relevant redirect ID.
+        * their actual ID as well as any relevant redirect ID. If records 
should only be
+        * available under the ID supplied to the builder's constructor, use 
retain() to
+        * strip any others.
         *
         * @return array[]
         */
@@ -88,4 +92,20 @@
         *        or 'remove-redirects'.
         */
        public function removeMissing( $redirects = 'keep-redirects' );
+
+       /**
+        * Remove info records for the given EntityIds.
+        *
+        * @param EntityId[] $ids
+        */
+       public function removeEntityInfo( array $ids );
+
+       /**
+        * Retain only info records for the given EntityIds, and remove all 
other records.
+        * Useful e.g. after resolveRedirects(), to remove explicit entries for
+        * redirect targets not present in the original input.
+        *
+        * @param EntityId[] $ids
+        */
+       public function retainEntityInfo( array $ids );
 }
diff --git a/lib/includes/store/GenericEntityInfoBuilder.php 
b/lib/includes/store/GenericEntityInfoBuilder.php
index 05e6bc5..76ad792 100644
--- a/lib/includes/store/GenericEntityInfoBuilder.php
+++ b/lib/includes/store/GenericEntityInfoBuilder.php
@@ -290,4 +290,37 @@
                return $this->entityInfo;
        }
 
+       /**
+        * @param EntityId[] $ids
+        *
+        * @return string[]
+        */
+       private function convertEntityIdsToStrings( array $ids ) {
+               return array_map( function ( EntityId $id ) {
+                       return $id->getSerialization();
+               }, $ids );
+       }
+
+       /**
+        * Remove info records for the given EntityIds.
+        *
+        * @param EntityId[] $ids
+        */
+       public function removeEntityInfo( array $ids ) {
+               $remove = $this->convertEntityIdsToStrings( $ids );
+               $this->entityInfo = array_diff_key( $this->entityInfo, 
array_flip( $remove ) );
+       }
+
+       /**
+        * Retain only info records for the given EntityIds.
+        * Useful e.g. after resolveRedirects(), to remove explicit entries for
+        * redirect targets not present in the original input.
+        *
+        * @param EntityId[] $ids
+        */
+       public function retainEntityInfo( array $ids ) {
+               $retain = $this->convertEntityIdsToStrings( $ids );
+               $this->entityInfo = array_intersect_key( $this->entityInfo, 
array_flip( $retain ) );
+       }
+
 }
diff --git a/lib/includes/store/sql/SqlEntityInfoBuilder.php 
b/lib/includes/store/sql/SqlEntityInfoBuilder.php
index 77d0b7e..7f6771b 100644
--- a/lib/includes/store/sql/SqlEntityInfoBuilder.php
+++ b/lib/includes/store/sql/SqlEntityInfoBuilder.php
@@ -645,4 +645,39 @@
 
                return $redirects;
        }
+
+       /**
+        * @param EntityId[] $ids
+        *
+        * @return string[]
+        */
+       private function convertEntityIdsToStrings( array $ids ) {
+               return array_map( function ( EntityId $id ) {
+                       return $id->getSerialization();
+               }, $ids );
+       }
+
+       /**
+        * Remove info records for the given EntityIds.
+        *
+        * @param EntityId[] $ids
+        */
+       public function removeEntityInfo( array $ids ) {
+               $remove = $this->convertEntityIdsToStrings( $ids );
+               $this->unsetEntityInfo( $remove );
+       }
+
+       /**
+        * Retain only info records for the given EntityIds.
+        * Useful e.g. after resolveRedirects(), to remove explicit entries for
+        * redirect targets not present in the original input.
+        *
+        * @param EntityId[] $ids
+        */
+       public function retainEntityInfo( array $ids ) {
+               $retain = $this->convertEntityIdsToStrings( $ids );
+               $remove = array_diff( array_keys( $this->entityInfo ), $retain 
);
+               $this->unsetEntityInfo( $remove );
+       }
+
 }
diff --git a/lib/tests/phpunit/store/EntityInfoBuilderTest.php 
b/lib/tests/phpunit/store/EntityInfoBuilderTest.php
index 8673db0..6c1fa47 100644
--- a/lib/tests/phpunit/store/EntityInfoBuilderTest.php
+++ b/lib/tests/phpunit/store/EntityInfoBuilderTest.php
@@ -437,4 +437,76 @@
 
                $this->assertArrayEquals( array_keys( $expected ), array_keys( 
$entityInfo ) );
        }
+
+       public function provideRemoveEntityInfo() {
+               return array(
+                       'empty' => array(
+                               array(),
+                               array(),
+                               array(),
+                       ),
+                       'remove some' => array(
+                               array(
+                                       new ItemId( 'Q1' ),
+                                       new ItemId( 'Q2' ),
+                                       new ItemId( 'Q3' ),
+                               ),
+                               array(
+                                       new ItemId( 'Q2' )
+                               ),
+                               array(
+                                       'Q1',
+                                       'Q3'
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideRemoveEntityInfo
+        */
+       public function testRemoveEntityInfo( array $ids, array $remove, array 
$expected ) {
+               $builder = $this->newEntityInfoBuilder( $ids );
+
+               $builder->removeEntityInfo( $remove );
+               $entityInfo = $builder->getEntityInfo();
+
+               $this->assertArrayEquals( $expected, array_keys( $entityInfo ) 
);
+       }
+
+       public function provideRetainEntityInfo() {
+               return array(
+                       'empty' => array(
+                               array(),
+                               array(),
+                               array(),
+                       ),
+                       'retain some' => array(
+                               array(
+                                       new ItemId( 'Q1' ),
+                                       new ItemId( 'Q2' ),
+                                       new ItemId( 'Q3' ),
+                               ),
+                               array(
+                                       new ItemId( 'Q2' )
+                               ),
+                               array(
+                                       'Q2'
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideRetainEntityInfo
+        */
+       public function testRetainEntityInfo( array $ids, array $retain, array 
$expected ) {
+               $builder = $this->newEntityInfoBuilder( $ids );
+
+               $builder->retainEntityInfo( $retain );
+               $entityInfo = $builder->getEntityInfo();
+
+               $this->assertArrayEquals( $expected, array_keys( $entityInfo ) 
);
+       }
+
 }
diff --git a/repo/includes/ParserOutputJsConfigBuilder.php 
b/repo/includes/ParserOutputJsConfigBuilder.php
index 6ad98dd..771d2a2 100644
--- a/repo/includes/ParserOutputJsConfigBuilder.php
+++ b/repo/includes/ParserOutputJsConfigBuilder.php
@@ -123,9 +123,11 @@
                // TODO: apply language fallback!
                $entityInfoBuilder = 
$this->entityInfoBuilderFactory->newEntityInfoBuilder( $entityIds );
 
+               $entityInfoBuilder->resolveRedirects();
                $entityInfoBuilder->removeMissing();
                $entityInfoBuilder->collectTerms( array( 'label', 'description' 
), array( $this->langCode ) );
                $entityInfoBuilder->collectDataTypes();
+               $entityInfoBuilder->retainEntityInfo( $entityIds );
 
                $entityInfo = $entityInfoBuilder->getEntityInfo();
                $revisionInfo = $this->attachRevisionInfo( $entityInfo );
diff --git a/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php 
b/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
index c9ab899..5086262 100644
--- a/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
+++ b/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
@@ -2,13 +2,13 @@
 
 namespace Wikibase\Test;
 
-use DataValues\StringValue;
 use Language;
 use Title;
 use Wikibase\DataModel\Claim\Claim;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
 use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\Property;
 use Wikibase\DataModel\Entity\PropertyId;
@@ -19,6 +19,7 @@
 use Wikibase\LanguageFallbackChainFactory;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Lib\Store\EntityRedirect;
 use Wikibase\ParserOutputJsConfigBuilder;
 use Wikibase\ReferencedEntitiesFinder;
 
@@ -69,13 +70,22 @@
        }
 
        public function buildProvider() {
-               $entity = $this->getEntity();
+               $entity = $this->getMainItem();
+               $referencedItem = $this->getReferencedItem();
 
                $property = $this->getProperty();
-               $usedEntities = $this->getUsedEntities( $property );
+               $redirect = $this->getRedirect();
+
+               $propertyKey = $property->getId()->getSerialization();
+               $redirectKey = $redirect->getEntityId()->getSerialization();
+
+               $usedEntities = array(
+                       $propertyKey => $this->getPropertyInfo( $property ),
+                       $redirectKey => $this->getEntityInfo( $referencedItem ),
+               );
 
                return array(
-                       array( $entity, $usedEntities, true )
+                       array( $entity, $usedEntities ),
                );
        }
 
@@ -116,13 +126,23 @@
                return $options;
        }
 
-       private function getEntity() {
+       private function getRedirect() {
+               $redirect = new EntityRedirect( new ItemId( 'Q44' ), new 
ItemId( 'Q55' ) );
+               return $redirect;
+       }
+
+       private function getMainItem() {
+               $redirect = $this->getRedirect();
+
                $item = Item::newEmpty();
                $itemId = new ItemId( 'Q5881' );
                $item->setId( $itemId );
                $item->setLabel( 'en', 'Cake' );
 
-               $snak = new PropertyValueSnak( new PropertyId( 'P794' ), new 
StringValue( 'a' ) );
+               $snak = new PropertyValueSnak(
+                       new PropertyId( 'P794' ),
+                       new EntityIdValue( $redirect->getEntityId() )
+               );
 
                $claim = new Claim( $snak );
                $claim->setGuid( 'P794$muahahaha' );
@@ -132,46 +152,56 @@
                return $item;
        }
 
+       private function getReferencedItem() {
+               $item = Item::newEmpty();
+               $itemId = new ItemId( 'Q55' );
+               $item->setId( $itemId );
+               $item->setLabel( 'en', 'Vanilla' );
+
+               return $item;
+       }
+
        private function getProperty() {
-               $property = Property::newFromType( 'string' );
+               $property = Property::newFromType( 'wikibase-item' );
                $property->setId( new PropertyId( 'P794' ) );
                $property->setLabel( 'en', 'AwesomeID' );
 
                return $property;
        }
 
-       private function getUsedEntities( Property $property ) {
-               $propertyId = $property->getId()->getSerialization();
+       private function getPropertyInfo( Property $property ) {
+               $info = $this->getEntityInfo( $property );
+               $info['content']['datatype'] = $property->getDataTypeId();
 
-               $usedEntities = array(
-                       $propertyId => array(
-                               'content' => array(
-                                       'id' => $propertyId,
-                                       'type' => 'property',
-                                       'labels' => array(
-                                               'en' => array(
-                                                       'language' => 'en',
-                                                       'value' => 
$property->getLabel( 'en' )
-                                               )
-                                       ),
-                                       'descriptions' => 
$property->getDescriptions(),
-                                       'datatype' => 
$property->getDataTypeId(),
+               return $info;
+       }
+
+       private function getEntityInfo( Entity $entity ) {
+               $entityId = $entity->getId()->getSerialization();
+
+               return array(
+                       'content' => array(
+                               'id' => $entityId,
+                               'type' => $entity->getType(),
+                               'labels' => array(
+                                       'en' => array(
+                                               'language' => 'en',
+                                               'value' => $entity->getLabel( 
'en' )
+                                       )
                                ),
-                               'title' => "property:$propertyId"
-                       )
+                               'descriptions' => $entity->getDescriptions(),
+                       ),
+                       'title' => $entity->getType() . ":$entityId"
                );
-
-               return $usedEntities;
        }
 
        private function getMockRepository() {
                $mockRepo = new MockRepository();
 
-               $entity = $this->getEntity();
-               $mockRepo->putEntity( $entity );
-
-               $property = $this->getProperty();
-               $mockRepo->putEntity( $property );
+               $mockRepo->putEntity( $this->getMainItem() );
+               $mockRepo->putEntity( $this->getReferencedItem() );
+               $mockRepo->putRedirect( $this->getRedirect() );
+               $mockRepo->putEntity( $this->getProperty() );
 
                return $mockRepo;
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4fb12ed3f834132bf7ef40e62be888d306721b34
Gerrit-PatchSet: 14
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WikidataJenkins <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to