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

Change subject: Introduce EntityInfo class
......................................................................


Introduce EntityInfo class

Wrap the entity info array structure in an object,
for type safety and conveniant access.

Note that the individual entity records are still plain
array structures, for speed and memory efficiency.

Change-Id: I859f581c59d7ff5dd7d282e895174dd634773337
---
A lib/includes/store/EntityInfo.php
M lib/includes/store/EntityInfoBuilder.php
M lib/includes/store/EntityInfoTermLookup.php
M lib/includes/store/GenericEntityInfoBuilder.php
M lib/includes/store/sql/SqlEntityInfoBuilder.php
M lib/tests/phpunit/store/EntityInfoBuilderTest.php
M lib/tests/phpunit/store/EntityInfoTermLookupTest.php
A lib/tests/phpunit/store/EntityInfoTest.php
M repo/includes/EntityParserOutputGenerator.php
M repo/includes/ParserOutputJsConfigBuilder.php
M repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
M repo/tests/phpunit/includes/View/EntityViewFactoryTest.php
12 files changed, 425 insertions(+), 119 deletions(-)

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



diff --git a/lib/includes/store/EntityInfo.php 
b/lib/includes/store/EntityInfo.php
new file mode 100644
index 0000000..dbc0782
--- /dev/null
+++ b/lib/includes/store/EntityInfo.php
@@ -0,0 +1,173 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use Wikibase\DataModel\Entity\EntityId;
+
+/**
+ * A wrapper for a simple array structure representing pre-fetched data about 
entities.
+ *
+ * The entity info is represented by a nested array structure. On the top 
level,
+ * entity id strings are used as keys that refer to entity "records".
+ *
+ * Each record is an associative array with at least the fields "id" and 
"type".
+ * Which other fields are present depends on which methods have been called on
+ * the EntityInfoBuilder in order to gather information about the entities.
+ *
+ * The array structure should be compatible with the structure generated by
+ * EntitySerializer and related classes. It should be suitable for 
serialization,
+ * and must thus not contain any objects.
+ *
+ * @see EntityInfoBuilder
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityInfo {
+
+       /**
+        * @var array[]
+        */
+       private $info;
+
+       /**
+        * @param array[] $info An array of entity info records.
+        * See the class level documentation for information about the expected 
structure of this array.
+        */
+       public function __construct( array $info ) {
+               $this->info = $info;
+       }
+
+       /**
+        * Returns the array of entity info records as provided to the 
constructor.
+        * See the class level documentation for information about the expected 
structure of this array.
+        *
+        * @return array[]
+        */
+       public function asArray() {
+               return $this->info;
+       }
+
+       /**
+        * @param EntityId $id
+        *
+        * @return bool
+        */
+       public function hasEntityInfo( EntityId $id ) {
+               $key = $id->getSerialization();
+
+               return isset( $this->info[$key] );
+       }
+
+       /**
+        * @param EntityId $id
+        *
+        * @return array An array structure representing information about the 
given entity.
+        *         If that entity isn't know, the resulting structure will 
contain only the ID.
+        */
+       public function getEntityInfo( EntityId $id ) {
+               $key = $id->getSerialization();
+
+               if ( isset( $this->info[$key] ) ) {
+                       return $this->info[$key];
+               } else {
+                       return array( 'id' => $key );
+               }
+       }
+
+       /**
+        * @param EntityId $id
+        * @param string $languageCode
+        *
+        * @throws StorageException
+        * @return string|null The entity's label in the given language,
+        *         or null if no such descriptions is known.
+        */
+       public function getLabel( EntityId $id, $languageCode ) {
+               return $this->getTermValue( $id, 'labels', $languageCode );
+       }
+
+       /**
+        * @param EntityId $id
+        * @param string $languageCode
+        *
+        * @throws StorageException
+        * @return string|null The entity's description in the given language,
+        *         or null if no such descriptions is known.
+        */
+       public function getDescription( EntityId $id, $languageCode ) {
+               return $this->getTermValue( $id, 'descriptions', $languageCode 
);
+       }
+
+       /**
+        * @param EntityId $id
+        *
+        * @throws StorageException
+        * @return string[] The given entity's labels, keyed by language.
+        */
+       public function getLabels( EntityId $id ) {
+               return $this->getTermValues( $id, 'labels' );
+       }
+
+       /**
+        * @param EntityId $id
+        *
+        * @throws StorageException
+        * @return string[] The given entity's descriptions, keyed by language.
+        */
+       public function getDescriptions( EntityId $id ) {
+               return $this->getTermValues( $id, 'descriptions' );
+       }
+
+       /**
+        * @param EntityId $id
+        * @param string $termField The term field (e.g. 'labels' or 
'descriptions').
+        * @param string $languageCode
+        *
+        * @throws StorageException
+        * @return string|null The term value, or null if no such term is known.
+        */
+       private function getTermValue( EntityId $id, $termField, $languageCode 
) {
+               $entityInfo = $this->getEntityInfo( $id );
+
+               if ( !isset( $entityInfo[$termField][$languageCode] ) ) {
+                       return null;
+               }
+
+               if ( !isset( $entityInfo[$termField][$languageCode]['value'] ) 
) {
+                       throw new StorageException( 'Term record is missing 
`value` key (' . $id->getSerialization() . ')' );
+               }
+
+               return $entityInfo[$termField][$languageCode]['value'];
+       }
+
+       /**
+        * @param EntityId $id
+        * @param string $termField The term field (e.g. 'labels' or 
'descriptions').
+        *
+        * @throws StorageException
+        * @return string[] The entity's term values, keyed by language.
+        */
+       private function getTermValues( EntityId $id, $termField ) {
+               $entityInfo = $this->getEntityInfo( $id );
+
+               if ( !isset( $entityInfo[$termField] ) ) {
+                       return array();
+               }
+
+               $values = array();
+
+               foreach ( $entityInfo[$termField] as $key => $entry ) {
+                       if ( !isset( $entry['value'] ) ) {
+                               throw new StorageException( 'Term record is 
missing `value` key (' . $id->getSerialization() . ')' );
+                       }
+
+                       $values[$key] = $entry['value'];
+               }
+
+               return $values;
+       }
+
+}
diff --git a/lib/includes/store/EntityInfoBuilder.php 
b/lib/includes/store/EntityInfoBuilder.php
index a02efa1..990c81f 100644
--- a/lib/includes/store/EntityInfoBuilder.php
+++ b/lib/includes/store/EntityInfoBuilder.php
@@ -8,9 +8,9 @@
  * A builder for collecting information about a batch of entities in an 
efficient way.
  *
  * @note: The batch of entities to work on would typically be supplied to the 
constructor
- * by the newEntityInfoBuilder method of the EntityBuilderFactory.
+ * by the newEntityInfoBuilder method of the EntityInfoBuilderFactory.
  *
- * @see EntityBuilderFactory
+ * @see EntityInfoBuilderFactory
  *
  * @since 0.5
  *
@@ -20,45 +20,37 @@
 interface EntityInfoBuilder {
 
        /**
-        * Returns an entity info data structure for the entities this builder 
is defined to
-        * operate on (typically, based on a list of EntityIds supplied to the 
constructor
-        * by an implementation of EntityBuilderFactory::newEntityInfoBuilder).
+        * Returns an EntityInfo object, representing information collected 
about the entities
+        * this builder is defined to operate on (typically, based on a list of 
EntityIds
+        * supplied to the constructor by an implementation of
+        * EntityBuilderFactory::newEntityInfoBuilder).
         *
-        * The entity info is represented by a nested array structure. On the 
top level,
-        * entity id strings are used as keys that refer to entity "records".
+        * @see EntityInfo::asArray()
         *
-        * Each record is an associative array with at least the fields "id" 
and "type".
-        * Which other fields are present depends on which methods have been 
called on
-        * the EntityInfoBuilder in order to gather information about the 
entities.
-        *
-        * The array structure should be compatible with the structure 
generated by
-        * EntitySerializer and related classes. It should be suitable for 
serialization,
-        * and must thus not contain any objects.
-        *
-        * @note: after resolveRedirects() is called, entity records will be 
available under
+        * @note: after resolveRedirects() is called, entities will be 
available under
         * 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[]
+        * @return EntityInfo
         */
        public function getEntityInfo();
 
        /**
         * Resolves any redirects.
         *
-        * This updates the 'id' field of the records in the data structure
+        * This updates the 'id' field of the records in the EntityInfo
         * returned by getEntityInfo() to the id of the target redirect, if the
         * original ID referred to a redirect.
         *
-        * Thus, the keys in the data structure returned by getEntityInfo()
+        * Thus, the keys in the EntityInfo returned by getEntityInfo()
         * may come to be different from the respective record's id field.
         */
        public function resolveRedirects();
 
        /**
         * Adds terms (like labels and/or descriptions) to the entity info.
-        * After calling this, the entity records in the data structure 
returned by getEntityInfo
+        * After calling this, the entity records in the EntityInfo returned by 
getEntityInfo
         * may have entries for the given term types (e.g. 'labels', 
'descriptions', or 'aliases').
         *
         * @note: For historical reasons, the types expected by $termTypes are 
different from the
@@ -78,7 +70,7 @@
         * Adds property data types to the entries in $entityInfo. Entities 
that do not have a data type
         * remain unchanged.
         *
-        * After calling this, the entity records in the data structure 
returned by getEntityInfo
+        * After calling this, the entity records in the EntityInfo returned by 
getEntityInfo
         * will have a 'datatype' field if they represent a Property entity.
         *
         */
diff --git a/lib/includes/store/EntityInfoTermLookup.php 
b/lib/includes/store/EntityInfoTermLookup.php
index bc2d677..09c9dcb 100644
--- a/lib/includes/store/EntityInfoTermLookup.php
+++ b/lib/includes/store/EntityInfoTermLookup.php
@@ -20,80 +20,16 @@
 class EntityInfoTermLookup implements TermLookup {
 
        /**
-        * @var array
+        * @var EntityInfo
         */
-       private $entityRecords;
+       private $entityInfo;
 
        /**
-        * @param array $entityRecords An array of entity records, as returned
+        * @param EntityInfo $entityInfo An array of entity records, as returned
         * by EntityInfoBuilder::getEntityInfo.
         */
-       public function __construct( array $entityRecords ) {
-               $this->entityRecords = $entityRecords;
-       }
-
-       /**
-        * @param EntityId $entityId
-        * @param string $field
-        * @param string $languageCode
-        *
-        * @return string|null
-        * @throws OutOfBoundsException
-        */
-       private function getValue( EntityId $entityId, $field, $languageCode ) {
-               $id = $entityId->getSerialization();
-
-               if ( !isset( $this->entityRecords[$id] ) ) {
-                       throw new OutOfBoundsException( 'No terms known for 
entity ' . $id );
-               }
-
-               if ( !isset( $this->entityRecords[$id][$field] ) ) {
-                       throw new OutOfBoundsException( 'No ' . $field . ' 
known for entity ' . $id );
-               }
-
-               if ( !isset( $this->entityRecords[$id][$field][$languageCode] ) 
) {
-                       throw new OutOfBoundsException( 'No ' . $field . ' 
known in ' . $languageCode . ' for entity ' . $id );
-               }
-
-               $value = $this->entityRecords[$id][$field][$languageCode];
-
-               if ( is_array( $value ) ) {
-                       // $value may be a record with "language" and "value" 
fields.
-                       $value = $value['value'];
-               }
-
-               return $value;
-       }
-
-       /**
-        * @param EntityId $entityId
-        * @param string $field
-        *
-        * @return string[]
-        */
-       private function getLanguageToValueMapping( EntityId $entityId, $field 
) {
-               $id = $entityId->getSerialization();
-
-               if ( !isset( $this->entityRecords[$id] ) ) {
-                       return array();
-               }
-
-               if ( !isset( $this->entityRecords[$id][$field] ) ) {
-                       return array();
-               }
-
-               $values = array();
-
-               foreach ( $this->entityRecords[$id][$field] as $languageCode => 
$value ) {
-                       if ( is_array( $value ) ) {
-                               // $value may be a record with "language" and 
"value" fields.
-                               $value = $value['value'];
-                       }
-
-                       $values[$languageCode] = $value;
-               }
-
-               return $values;
+       public function __construct( EntityInfo $entityInfo ) {
+               $this->entityInfo = $entityInfo;
        }
 
        /**
@@ -105,7 +41,10 @@
         * @return string
         */
        public function getLabel( EntityId $entityId, $languageCode ) {
-               return $this->getValue( $entityId, 'labels', $languageCode );
+               $label = $this->entityInfo->getLabel( $entityId, $languageCode 
);
+
+               $this->checkOutOfBOunds( $label, $languageCode );
+               return $label;
        }
 
        /**
@@ -116,7 +55,8 @@
         * @return string[]
         */
        public function getLabels( EntityId $entityId ) {
-               return $this->getLanguageToValueMapping( $entityId, 'labels' );
+               $labels = $this->entityInfo->getLabels( $entityId );
+               return $labels;
        }
 
        /**
@@ -128,7 +68,10 @@
         * @return string
         */
        public function getDescription( EntityId $entityId, $languageCode ) {
-               return $this->getValue( $entityId, 'descriptions', 
$languageCode );
+               $description = $this->entityInfo->getDescription( $entityId, 
$languageCode );
+
+               $this->checkOutOfBOunds( $description, $languageCode );
+               return $description;
        }
 
        /**
@@ -139,7 +82,19 @@
         * @return string[]
         */
        public function getDescriptions( EntityId $entityId ) {
-               return $this->getLanguageToValueMapping( $entityId, 
'descriptions' );
+               $descriptions = $this->entityInfo->getDescriptions( $entityId );
+               return $descriptions;
        }
 
+       /**
+        * @param string $value
+        * @param string $languageCode
+        *
+        * @throws OutOfBoundsException
+        */
+       private function checkOutOfBOunds( $value, $languageCode ) {
+               if ( $value === null ) {
+                       throw new OutOfBoundsException( 'No entry found for 
language ' . $languageCode );
+               }
+       }
 }
diff --git a/lib/includes/store/GenericEntityInfoBuilder.php 
b/lib/includes/store/GenericEntityInfoBuilder.php
index 5a32b29..f2ca161 100644
--- a/lib/includes/store/GenericEntityInfoBuilder.php
+++ b/lib/includes/store/GenericEntityInfoBuilder.php
@@ -284,10 +284,10 @@
        /**
         * @see EntityInfoBuilder::getEntityInfo
         *
-        * @return array[]
+        * @return EntityInfo
         */
        public function getEntityInfo() {
-               return $this->entityInfo;
+               return new EntityInfo( $this->entityInfo );
        }
 
        /**
diff --git a/lib/includes/store/sql/SqlEntityInfoBuilder.php 
b/lib/includes/store/sql/SqlEntityInfoBuilder.php
index 8d6c469..24cf39b 100644
--- a/lib/includes/store/sql/SqlEntityInfoBuilder.php
+++ b/lib/includes/store/sql/SqlEntityInfoBuilder.php
@@ -13,6 +13,7 @@
 use Wikibase\DataModel\Entity\Property;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\DataModel\LegacyIdInterpreter;
+use Wikibase\Lib\Store\EntityInfo;
 use Wikibase\Lib\Store\EntityInfoBuilder;
 
 /**
@@ -165,10 +166,10 @@
        /**
         * @see EntityInfoBuilder::getEntityInfo
         *
-        * @return array[]
+        * @return EntityInfo
         */
        public function getEntityInfo() {
-               return $this->entityInfo;
+               return new EntityInfo( $this->entityInfo );
        }
 
        /**
diff --git a/lib/tests/phpunit/store/EntityInfoBuilderTest.php 
b/lib/tests/phpunit/store/EntityInfoBuilderTest.php
index 65e3c8e..a4e3056 100644
--- a/lib/tests/phpunit/store/EntityInfoBuilderTest.php
+++ b/lib/tests/phpunit/store/EntityInfoBuilderTest.php
@@ -111,7 +111,7 @@
         */
        public function testGetEntityInfo( array $ids, array $expected ) {
                $builder = $this->newEntityInfoBuilder( $ids );
-               $actual = $builder->getEntityInfo();
+               $actual = $builder->getEntityInfo()->asArray();
 
                $this->assertArrayEquals( $expected, $actual, false, true );
        }
@@ -145,7 +145,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->resolveRedirects();
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $resolvedIds = array_map(
                        function( $record ) {
@@ -249,7 +249,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->collectTerms( $types, $languages );
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertSameSize( $expected, $entityInfo );
 
@@ -280,7 +280,7 @@
 
                $builder->resolveRedirects();
                $builder->collectTerms( array( 'label' ), array( 'de' ) );
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertEquals( array_keys( $expected ), array_keys( 
$entityInfo ) );
 
@@ -323,7 +323,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->collectDataTypes();
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertSameSize( $expected, $entityInfo );
 
@@ -380,7 +380,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->removeMissing( 'remove-redirects' );
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertArrayEquals( array_keys( $expected ), array_keys( 
$entityInfo ) );
        }
@@ -433,7 +433,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->removeMissing();
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertArrayEquals( array_keys( $expected ), array_keys( 
$entityInfo ) );
        }
@@ -475,7 +475,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->removeEntityInfo( $remove );
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertArrayEquals( $expected, array_keys( $entityInfo ) 
);
        }
@@ -517,7 +517,7 @@
                $builder = $this->newEntityInfoBuilder( $ids );
 
                $builder->retainEntityInfo( $retain );
-               $entityInfo = $builder->getEntityInfo();
+               $entityInfo = $builder->getEntityInfo()->asArray();
 
                $this->assertArrayEquals( $expected, array_keys( $entityInfo ) 
);
        }
diff --git a/lib/tests/phpunit/store/EntityInfoTermLookupTest.php 
b/lib/tests/phpunit/store/EntityInfoTermLookupTest.php
index 8a74e10..689aaf0 100644
--- a/lib/tests/phpunit/store/EntityInfoTermLookupTest.php
+++ b/lib/tests/phpunit/store/EntityInfoTermLookupTest.php
@@ -4,6 +4,7 @@
 
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityInfo;
 use Wikibase\Lib\Store\EntityInfoTermLookup;
 
 /**
@@ -104,7 +105,7 @@
                        'Q116' => array(
                                'labels' => array(
                                        'en' => array( 'language' => 'en', 
'value' => 'New York City' ),
-                                       'es' => 'Nueva York', // terse form 
also supported
+                                       'es' => array( 'language' => 'es', 
'value' => 'Nueva York' ),
                                ),
                                'descriptions' => array(
                                        'en' => array( 'language' => 'en', 
'value' => 'largest city in New York and the United States of America' ),
@@ -119,7 +120,7 @@
                        ),
                );
 
-               return $entityInfo;
+               return new EntityInfo( $entityInfo );
        }
 
 }
diff --git a/lib/tests/phpunit/store/EntityInfoTest.php 
b/lib/tests/phpunit/store/EntityInfoTest.php
new file mode 100644
index 0000000..341184b
--- /dev/null
+++ b/lib/tests/phpunit/store/EntityInfoTest.php
@@ -0,0 +1,176 @@
+<?php
+
+namespace Wikibase\Test;
+
+use PHPUnit_Framework_TestCase;
+use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityInfo;
+use Wikibase\Lib\Store\GenericEntityInfoBuilder;
+
+/**
+ * @covers Wikibase\Lib\Store\EntityInfo
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityInfoTest extends PHPUnit_Framework_TestCase {
+
+       /**
+        * @param Entity[] $entities
+        *
+        * @return EntityInfo
+        */
+       private function getEntityInfo( array $entities ) {
+               $repo = new MockRepository();
+               $ids = array();
+
+               foreach ( $entities as $entity ) {
+                       $repo->putEntity( $entity );
+                       $ids[] = $entity->getId();
+               }
+
+               $builder = new GenericEntityInfoBuilder(
+                       $ids,
+                       new BasicEntityIdParser(),
+                       $repo
+               );
+
+               $builder->collectTerms();
+
+               return $builder->getEntityInfo();
+       }
+
+       /**
+        * @param string $id
+        * @param string $label
+        *
+        * @return Item
+        */
+       private function makeItemWithLabel( $id, $label ) {
+               $item = Item::newEmpty();
+               $item->setId( new ItemId( $id ) );
+
+               $item->setLabel( 'en', $label );
+
+               return $item;
+       }
+
+       /**
+        * @param string $id
+        * @param string $label
+        *
+        * @return Item
+        */
+       private function makeItemWithDescription( $id, $label ) {
+               $item = Item::newEmpty();
+               $item->setId( new ItemId( $id ) );
+
+               $item->setDescription( 'en', $label );
+
+               return $item;
+       }
+
+       public function asArrayProvider() {
+               $infoWithLabels = $this->getEntityInfo( array(
+                       $this->makeItemWithLabel( 'Q11', 'London' ),
+                       $this->makeItemWithLabel( 'Q33', 'Berlin' ),
+               ) );
+
+               return array(
+                       'empty' => array( array() ),
+                       'labels' => array( $infoWithLabels->asArray() ),
+               );
+       }
+
+       /**
+        * @dataProvider asArrayProvider
+        */
+       public function testAsArray( array $records ) {
+               $entityInfo = new EntityInfo( $records );
+
+               $actual = $entityInfo->asArray();
+               $this->assertEquals( $records, $actual );
+       }
+
+       public function testHasEntityInfo() {
+               $info = $this->getEntityInfo( array(
+                       $this->makeItemWithLabel( 'Q11', 'London' ),
+                       $this->makeItemWithLabel( 'Q33', 'Berlin' ),
+               ) );
+
+               $this->assertTrue( $info->hasEntityInfo( new ItemId( 'Q11' ) ) 
);
+               $this->assertTrue( $info->hasEntityInfo( new ItemId( 'Q33' ) ) 
);
+               $this->assertFalse( $info->hasEntityInfo( new ItemId( 'Q99' ) ) 
);
+       }
+
+       public function testGetEntityInfo() {
+               $info = $this->getEntityInfo( array(
+                       $this->makeItemWithLabel( 'Q11', 'London' ),
+                       $this->makeItemWithLabel( 'Q33', 'Berlin' ),
+               ) );
+
+               $record = $info->getEntityInfo( new ItemId( 'Q11' ) );
+               $this->assertInternalType( 'array', $record );
+               $this->assertEquals( 'Q11', $record['id'] );
+
+               $record = $info->getEntityInfo( new ItemId( 'Q33' ) );
+               $this->assertInternalType( 'array', $record );
+               $this->assertEquals( 'Q33', $record['id'] );
+               $this->assertArrayHasKey( 'labels', $record );
+
+               $record = $info->getEntityInfo( new ItemId( 'Q99' ) );
+               $this->assertInternalType( 'array', $record );
+               $this->assertEquals( 'Q99', $record['id'] );
+               $this->assertArrayNotHasKey( 'labels', $record );
+       }
+
+       public function testGetLabel() {
+               $info = $this->getEntityInfo( array(
+                       $this->makeItemWithLabel( 'Q11', 'London' ),
+                       $this->makeItemWithLabel( 'Q33', 'Berlin' ),
+               ) );
+
+               $this->assertEquals( 'London', $info->getLabel( new ItemId( 
'Q11' ), 'en' ) );
+               $this->assertEquals( 'Berlin', $info->getLabel( new ItemId( 
'Q33' ), 'en' ) );
+               $this->assertNull( $info->getLabel( new ItemId( 'Q11' ), 'zh' ) 
);
+               $this->assertNull( $info->getLabel( new ItemId( 'Q99' ), 'en' ) 
);
+       }
+
+       public function testGetLabels() {
+               $info = $this->getEntityInfo( array(
+                       $this->makeItemWithLabel( 'Q11', 'London' ),
+                       $this->makeItemWithLabel( 'Q33', 'Berlin' ),
+               ) );
+
+               $this->assertEquals( array( 'en' => 'London' ), 
$info->getLabels( new ItemId( 'Q11' ) ) );
+               $this->assertEquals( array( 'en' => 'Berlin' ), 
$info->getLabels( new ItemId( 'Q33' ) ) );
+               $this->assertEquals( array(), $info->getLabels( new ItemId( 
'Q99' ) ) );
+       }
+
+       public function testGetDescription() {
+               $info = $this->getEntityInfo( array(
+                       $this->makeItemWithDescription( 'Q11', 'London' ),
+                       $this->makeItemWithDescription( 'Q33', 'Berlin' ),
+               ) );
+
+               $this->assertEquals( 'London', $info->getDescription( new 
ItemId( 'Q11' ), 'en' ) );
+               $this->assertEquals( 'Berlin', $info->getDescription( new 
ItemId( 'Q33' ), 'en' ) );
+               $this->assertNull( $info->getDescription( new ItemId( 'Q11' ), 
'zh' ) );
+               $this->assertNull( $info->getDescription( new ItemId( 'Q99' ), 
'en' ) );
+       }
+
+       public function testGetDescriptions() {
+               $info = $this->getEntityInfo( array(
+                       $this->makeItemWithDescription( 'Q11', 'London' ),
+                       $this->makeItemWithDescription( 'Q33', 'Berlin' ),
+               ) );
+
+               $this->assertEquals( array( 'en' => 'London' ), 
$info->getDescriptions( new ItemId( 'Q11' ) ) );
+               $this->assertEquals( array( 'en' => 'Berlin' ), 
$info->getDescriptions( new ItemId( 'Q33' ) ) );
+               $this->assertEquals( array(), $info->getDescriptions( new 
ItemId( 'Q99' ) ) );
+       }
+
+}
diff --git a/repo/includes/EntityParserOutputGenerator.php 
b/repo/includes/EntityParserOutputGenerator.php
index 31f1e44..bc7e02a 100644
--- a/repo/includes/EntityParserOutputGenerator.php
+++ b/repo/includes/EntityParserOutputGenerator.php
@@ -6,7 +6,9 @@
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\SiteLinkList;
+use Wikibase\DataModel\Snak\Snak;
 use Wikibase\DataModel\StatementListProvider;
+use Wikibase\Lib\Store\EntityInfo;
 use Wikibase\Lib\Store\EntityInfoBuilderFactory;
 use Wikibase\Lib\Store\EntityInfoTermLookup;
 use Wikibase\Lib\Store\EntityTitleLookup;
@@ -208,7 +210,8 @@
         * Fetches some basic entity information from a set of entity IDs.
         *
         * @param EntityId[] $entityIds
-        * @return array obtained from EntityInfoBuilder::getEntityInfo
+        *
+        * @return EntityInfo
         */
        private function getEntityInfo( array $entityIds ) {
                wfProfileIn( __METHOD__ );
@@ -247,13 +250,13 @@
        /**
         * @param ParserOutput $parserOutput
         * @param EntityRevision $entityRevision
-        * @param array $entityInfo obtained from 
EntityInfoBuilder::getEntityInfo
+        * @param EntityInfo $entityInfo obtained from 
EntityInfoBuilder::getEntityInfo
         * @param boolean $editable
         */
        private function addHtmlToParserOutput(
                ParserOutput $parserOutput,
                EntityRevision $entityRevision,
-               array $entityInfo,
+               EntityInfo $entityInfo,
                $editable
        ) {
 
diff --git a/repo/includes/ParserOutputJsConfigBuilder.php 
b/repo/includes/ParserOutputJsConfigBuilder.php
index bc5d378..e4e230d 100644
--- a/repo/includes/ParserOutputJsConfigBuilder.php
+++ b/repo/includes/ParserOutputJsConfigBuilder.php
@@ -8,6 +8,7 @@
 use Wikibase\Lib\Serializers\EntitySerializer;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Lib\Store\EntityInfo;
 use Wikibase\Lib\Store\EntityTitleLookup;
 
 /**
@@ -60,11 +61,11 @@
 
        /**
         * @param Entity $entity
-        * @param array $entityInfo
+        * @param EntityInfo $entityInfo
         *
         * @return array
         */
-       public function build( Entity $entity, array $entityInfo ) {
+       public function build( Entity $entity, EntityInfo $entityInfo ) {
                $entityId = $entity->getId();
 
                if ( !$entityId ) {
@@ -73,7 +74,8 @@
                        $entityId = $entityId->getSerialization();
                }
 
-               $revisionInfo = $this->attachRevisionInfo( $entityInfo );
+               $entityInfoArray = $entityInfo->asArray();
+               $revisionInfo = $this->attachRevisionInfo( $entityInfoArray );
 
                $configVars = array(
                        'wbEntityId' => $entityId,
diff --git a/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php 
b/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
index 76e30c8..a223cca 100644
--- a/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
+++ b/repo/tests/phpunit/includes/ParserOutputJsConfigBuilderTest.php
@@ -18,6 +18,7 @@
 use Wikibase\LanguageFallbackChainFactory;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Lib\Store\EntityInfo;
 use Wikibase\Lib\Store\EntityTitleLookup;
 use Wikibase\ParserOutputJsConfigBuilder;
 
@@ -37,7 +38,7 @@
        /**
         * @dataProvider buildProvider
         */
-       public function testBuild( array $usedEntities, Entity $entity, array 
$entityInfo ) {
+       public function testBuild( array $usedEntities, Entity $entity, 
EntityInfo $entityInfo ) {
                $configBuilder = $this->getConfigBuilder( 'en', array( 'de', 
'en', 'es', 'fr' ) );
                $configVars = $configBuilder->build( $entity, $entityInfo );
 
@@ -144,9 +145,9 @@
        }
 
        private function getEntityInfo( Entity $entity ) {
-               return array(
+               return new EntityInfo( array(
                        $this->getEntityInfoContent( $entity )
-               );
+               ) );
        }
 
        private function getEntityInfoContent( Entity $entity ) {
diff --git a/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php 
b/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php
index 2870359..309c699 100644
--- a/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php
+++ b/repo/tests/phpunit/includes/View/EntityViewFactoryTest.php
@@ -5,6 +5,7 @@
 use Wikibase\LanguageFallbackChain;
 use Wikibase\Lib\EntityIdHtmlLinkFormatterFactory;
 use Wikibase\Lib\SnakFormatter;
+use Wikibase\Lib\Store\EntityInfo;
 use Wikibase\Lib\Store\EntityInfoTermLookup;
 use Wikibase\Lib\Store\LanguageLabelLookup;
 use Wikibase\Repo\View\EntityViewFactory;
@@ -22,7 +23,8 @@
                $entityViewFactory = $this->getEntityViewFactory();
 
                $languageFallback = new LanguageFallbackChain( array() );
-               $labelLookup = new LanguageLabelLookup( new 
EntityInfoTermLookup( array() ), 'de' );
+               $termLookup = new EntityInfoTermLookup( new EntityInfo( array() 
) );
+               $labelLookup = new LanguageLabelLookup( $termLookup, 'de' );
 
                $entityView = $entityViewFactory->newEntityView(
                        $entityType,

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I859f581c59d7ff5dd7d282e895174dd634773337
Gerrit-PatchSet: 17
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: JanZerebecki <jan.wikime...@zerebecki.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
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