jenkins-bot has submitted this change and it was merged.
Change subject: Introduce EntityInfoTermLookup.
......................................................................
Introduce EntityInfoTermLookup.
This provides a term lookup based on data pre-fetched by
EntityInfoBuilder.
Bug: 72307
Change-Id: I954197e733166941b4d38e273512c04a2aea4557
---
A lib/includes/store/EntityInfoTermLookup.php
M lib/includes/store/TermLookup.php
A lib/tests/phpunit/store/EntityInfoTermLookupTest.php
M lib/tests/phpunit/store/EntityTermLookupTest.php
4 files changed, 280 insertions(+), 2 deletions(-)
Approvals:
Adrian Lang: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/includes/store/EntityInfoTermLookup.php
b/lib/includes/store/EntityInfoTermLookup.php
new file mode 100644
index 0000000..bc2d677
--- /dev/null
+++ b/lib/includes/store/EntityInfoTermLookup.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use OutOfBoundsException;
+use Wikibase\DataModel\Entity\EntityId;
+
+/**
+ * TermLookup based on plain array data structures.
+ * This allows term lookups to be performed directly on prefetched data,
+ * such as the data structured generated by EntityInfoBuilder.
+ *
+ * @see EntityInfoBuilder
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityInfoTermLookup implements TermLookup {
+
+ /**
+ * @var array
+ */
+ private $entityRecords;
+
+ /**
+ * @param array $entityRecords 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;
+ }
+
+ /**
+ * Gets the label of an Entity with the specified EntityId and language
code.
+ *
+ * @param EntityId $entityId
+ * @param string $languageCode
+ *
+ * @return string
+ */
+ public function getLabel( EntityId $entityId, $languageCode ) {
+ return $this->getValue( $entityId, 'labels', $languageCode );
+ }
+
+ /**
+ * Gets all labels of an Entity with the specified EntityId.
+ *
+ * @param EntityId $entityId
+ *
+ * @return string[]
+ */
+ public function getLabels( EntityId $entityId ) {
+ return $this->getLanguageToValueMapping( $entityId, 'labels' );
+ }
+
+ /**
+ * Gets the description of an Entity with the specified EntityId and
language code.
+ *
+ * @param EntityId $entityId
+ * @param string $languageCode
+ *
+ * @return string
+ */
+ public function getDescription( EntityId $entityId, $languageCode ) {
+ return $this->getValue( $entityId, 'descriptions',
$languageCode );
+ }
+
+ /**
+ * Gets all descriptions of an Entity with the specified EntityId.
+ *
+ * @param EntityId $entityId
+ *
+ * @return string[]
+ */
+ public function getDescriptions( EntityId $entityId ) {
+ return $this->getLanguageToValueMapping( $entityId,
'descriptions' );
+ }
+
+}
diff --git a/lib/includes/store/TermLookup.php
b/lib/includes/store/TermLookup.php
index 067fe35..4c1b065 100644
--- a/lib/includes/store/TermLookup.php
+++ b/lib/includes/store/TermLookup.php
@@ -23,6 +23,7 @@
* @throws OutOfBoundsException for label not found
* @throws StorageException for Entity not found
* @return string
+ * @throws OutOfBoundsException if no such label was found
*/
public function getLabel( EntityId $entityId, $languageCode );
@@ -32,7 +33,7 @@
* @param EntityId $entityId
*
* @throws StorageException for Entity not found
- * @return string[]
+ * @return string[] labels, keyed by language.
*/
public function getLabels( EntityId $entityId );
@@ -45,6 +46,7 @@
* @throws OutOfBoundsException for description not found
* @throws StorageException for Entity not found
* @return string
+ * @throws OutOfBoundsException if no such description was found
*/
public function getDescription( EntityId $entityId, $languageCode );
@@ -54,7 +56,7 @@
* @param EntityId $entityId
*
* @throws StorageException for Entity not found
- * @return string[]
+ * @return string[] descriptions, keyed by language.
*/
public function getDescriptions( EntityId $entityId );
diff --git a/lib/tests/phpunit/store/EntityInfoTermLookupTest.php
b/lib/tests/phpunit/store/EntityInfoTermLookupTest.php
new file mode 100644
index 0000000..8a74e10
--- /dev/null
+++ b/lib/tests/phpunit/store/EntityInfoTermLookupTest.php
@@ -0,0 +1,125 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityInfoTermLookup;
+
+/**
+ * @covers Wikibase\Lib\Store\EntityInfoTermLookup
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert
+ * @author Daniel Kinzler
+ */
+class EntityInfoTermLookupTest extends \MediaWikiTestCase {
+
+ public function testGetLabel() {
+ $termLookup = $this->getEntityInfoTermLookup();
+
+ $label = $termLookup->getLabel( new ItemId( 'Q116' ), 'en' );
+ $this->assertEquals( 'New York City', $label );
+ }
+
+ public function testGetLabel_notFoundThrowsException() {
+ $termLookup = $this->getEntityInfoTermLookup();
+
+ $this->setExpectedException( 'OutOfBoundsException' );
+ $termLookup->getLabel( new ItemId( 'Q120' ), 'en' );
+ }
+
+ /**
+ * @dataProvider getLabelsProvider
+ */
+ public function testGetLabels( $expected, EntityId $entityId ) {
+ $termLookup = $this->getEntityInfoTermLookup();
+
+ $labels = $termLookup->getLabels( $entityId );
+ $this->assertEquals( $expected, $labels );
+ }
+
+ public function getLabelsProvider() {
+ return array(
+ array(
+ array( 'en' => 'New York City', 'es' => 'Nueva
York' ),
+ new ItemId( 'Q116' )
+ ),
+ array(
+ array(),
+ new ItemId( 'Q120' )
+ )
+ );
+ }
+
+ public function testGetDescription() {
+ $termLookup = $this->getEntityInfoTermLookup();
+
+ $description = $termLookup->getDescription( new ItemId( 'Q116'
), 'de' );
+ $expected = 'Metropole an der Ostküste der Vereinigten Staaten';
+
+ $this->assertEquals( $expected, $description );
+ }
+
+ public function testGetDescription_notFoundThrowsException() {
+ $termLookup = $this->getEntityInfoTermLookup();
+
+ $this->setExpectedException( 'OutOfBoundsException' );
+ $termLookup->getDescription( new ItemId( 'Q90000' ), 'fr' );
+ }
+
+ /**
+ * @dataProvider getDescriptionsProvider
+ */
+ public function getDescriptions( $expected, EntityId $entityId ) {
+ $termLookup = $this->getEntityInfoTermLookup();
+
+ $descriptions = $termLookup->getDescriptions( $entityId );
+ $this->assertEquals( $expected, $descriptions );
+ }
+
+ public function getDescriptionsProvider() {
+ return array(
+ array(
+ array(
+ 'de' => 'Metropole an der Ostküste der
Vereinigten Staaten',
+ 'en' => 'largest city in New York and
the United States of America',
+ ),
+ new ItemId( 'Q116' )
+ ),
+ array(
+ array(),
+ new ItemId( 'Q90001' )
+ )
+ );
+ }
+
+ private function getEntityInfoTermLookup() {
+ $entityInfo = $this->makeEntityInfo();
+ return new EntityInfoTermLookup( $entityInfo );
+ }
+
+ private function makeEntityInfo() {
+ $entityInfo = array(
+ 'Q116' => array(
+ 'labels' => array(
+ 'en' => array( 'language' => 'en',
'value' => 'New York City' ),
+ 'es' => 'Nueva York', // terse form
also supported
+ ),
+ 'descriptions' => array(
+ 'en' => array( 'language' => 'en',
'value' => 'largest city in New York and the United States of America' ),
+ 'de' => array( 'language' => 'de',
'value' => 'Metropole an der Ostküste der Vereinigten Staaten' ),
+ ),
+ ),
+
+ 'Q117' => array(
+ 'labels' => array(
+ 'de' => array( 'language' => 'de',
'value' => 'Berlin' ),
+ ),
+ ),
+ );
+
+ return $entityInfo;
+ }
+
+}
diff --git a/lib/tests/phpunit/store/EntityTermLookupTest.php
b/lib/tests/phpunit/store/EntityTermLookupTest.php
index c237da9..f1236b8 100644
--- a/lib/tests/phpunit/store/EntityTermLookupTest.php
+++ b/lib/tests/phpunit/store/EntityTermLookupTest.php
@@ -6,6 +6,12 @@
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Lib\Store\EntityTermLookup;
+/**
+ * @covers Wikibase\Lib\Store\EntityTermLookup
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert
+ */
class EntityTermLookupTest extends \MediaWikiTestCase {
public function testGetLabel() {
--
To view, visit https://gerrit.wikimedia.org/r/171813
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I954197e733166941b4d38e273512c04a2aea4557
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits