Aude has uploaded a new change for review.

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

Change subject: Implement LabelLookup
......................................................................

Implement LabelLookup

Change-Id: I420df654a8ef7d17e3e45b7e2b0444f5c130a873
---
A lib/includes/store/LabelLookup.php
M lib/includes/store/TermIndex.php
A lib/includes/store/TermLookup.php
M lib/includes/store/sql/TermSqlIndex.php
A lib/tests/phpunit/store/LabelLookupTest.php
M lib/tests/phpunit/store/MockTermIndex.php
6 files changed, 258 insertions(+), 10 deletions(-)


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

diff --git a/lib/includes/store/LabelLookup.php 
b/lib/includes/store/LabelLookup.php
new file mode 100644
index 0000000..6c078d2
--- /dev/null
+++ b/lib/includes/store/LabelLookup.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use OutOfBoundsException;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\LanguageFallbackChain;
+use Wikibase\TermIndex;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+class LabelLookup {
+
+       /**
+        * @var TermLookup
+        */
+       private $termLookup;
+
+       /**
+        * @var string
+        */
+       private $languageCode;
+
+       /**
+        * @param TermLookup $termLookup
+        * @param string $languageCode
+        */
+       public function __construct( TermLookup $termLookup, $languageCode ) {
+               $this->termLookup = $termLookup;
+               $this->languageCode = $languageCode;
+       }
+
+       /**
+        * @param EntityId $entityId
+        *
+        * @throws OutOfBoundsException
+        * @return string
+        */
+       public function getLabel( EntityId $entityId ) {
+               $labels = $this->getLabels( $entityId );
+
+               return $this->filterByLanguage( $labels, $this->languageCode );
+       }
+
+       /**
+        * @param EntityId $entityId
+        * @param LanguageFallbackChain $languageFallbackChain
+        *
+        * @return string|false
+        */
+       public function getLabelForFallbackChain(
+               EntityId $entityId,
+               LanguageFallbackChain $languageFallbackChain
+       ) {
+               $labels = $this->getLabels( $entityId );
+               $extractedData = $languageFallbackChain->extractPreferredValue( 
$labels );
+
+               return $extractedData ? $extractedData['value'] : false;
+       }
+
+       private function getLabels( EntityId $entityId ) {
+               $wikibaseTerms = $this->termLookup->getTermsOfEntity( $entityId 
);
+               return $this->convertTermsToLabelArray( $wikibaseTerms );
+       }
+
+       /**
+        * @param string[] $labels
+        * @param string $languageCode
+        *
+        * @throws OutOfBoundsException
+        * @return string
+        */
+       private function filterByLanguage( array $labels, $languageCode ) {
+               if ( array_key_exists( $languageCode, $labels ) ) {
+                       return $labels[$languageCode];
+               }
+
+               throw new OutOfBoundsException( 'Label not found for ' . 
$languageCode );
+       }
+
+       /**
+        * @param Wikibase\Term[] $wikibaseTerms
+        *
+        * @return string[]
+        */
+       private function convertTermsToLabelArray( array $wikibaseTerms ) {
+               $terms = array();
+
+               foreach( $wikibaseTerms as $wikibaseTerm ) {
+                       if ( $wikibaseTerm->getType() === 'label' ) {
+                               $languageCode = $wikibaseTerm->getLanguage();
+                               $terms[$languageCode] = 
$wikibaseTerm->getText();
+                       }
+               }
+
+               return $terms;
+       }
+
+}
diff --git a/lib/includes/store/TermIndex.php b/lib/includes/store/TermIndex.php
index 3ee94e9..8b36af2 100644
--- a/lib/includes/store/TermIndex.php
+++ b/lib/includes/store/TermIndex.php
@@ -57,11 +57,11 @@
        /**
         * Returns the terms stored for the given entity.
         *
-        * @param EntityId $id
+        * @param EntityId $entityId
         *
         * @return Term[]
         */
-       public function getTermsOfEntity( EntityId $id );
+       public function getTermsOfEntity( EntityId $entityId );
 
        /**
         * Returns the terms stored for the given entities. Can be filtered by 
language.
diff --git a/lib/includes/store/TermLookup.php 
b/lib/includes/store/TermLookup.php
new file mode 100644
index 0000000..3861e7d
--- /dev/null
+++ b/lib/includes/store/TermLookup.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Wikibase\Lib\Store;
+
+use Wikibase\DataModel\Entity\EntityId;
+
+/**
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+interface TermLookup {
+
+       /**
+        * Gets terms of an Entity with the given EnitityId.
+        *
+        * @param EntityId $entityId
+        *
+        * @return Wikibase\Term[]
+        */
+       public function getTermsOfEntity( EntityId $entityId );
+
+}
diff --git a/lib/includes/store/sql/TermSqlIndex.php 
b/lib/includes/store/sql/TermSqlIndex.php
index ec9e931..a99562b 100644
--- a/lib/includes/store/sql/TermSqlIndex.php
+++ b/lib/includes/store/sql/TermSqlIndex.php
@@ -11,6 +11,7 @@
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\LegacyIdInterpreter;
+use Wikibase\Lib\Store\TermLookup;
 
 /**
  * Term lookup cache.
@@ -23,7 +24,7 @@
  * @author Daniel Kinzler
  * @author Denny
  */
-class TermSqlIndex extends DBAccessBase implements TermIndex {
+class TermSqlIndex extends DBAccessBase implements TermIndex, TermLookup {
 
        /**
         * @since 0.1
diff --git a/lib/tests/phpunit/store/LabelLookupTest.php 
b/lib/tests/phpunit/store/LabelLookupTest.php
new file mode 100644
index 0000000..7b6ae1f
--- /dev/null
+++ b/lib/tests/phpunit/store/LabelLookupTest.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Language;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Lib\Store\LabelLookup;
+
+class LabelLookupTest extends \MediaWikiTestCase {
+
+       public function testGetLabel() {
+               $termLookup = $this->getTermLookup();
+               $labelLookup = new LabelLookup( $termLookup, 'en' );
+
+               $label = $labelLookup->getLabel( new ItemId( 'Q116' ), 'en' );
+
+               $this->assertEquals( 'New York City', $label );
+       }
+
+       public function testGetLabel_notFound() {
+               $termLookup = $this->getTermLookup();
+               $labelLookup = new LabelLookup( $termLookup, 'en' );
+
+               $this->setExpectedException( 'OutOfBoundsException' );
+               $label = $labelLookup->getLabel( new ItemId( 'Q120' ), 'en' );
+       }
+
+       public function testGetLabelForFallbackChain() {
+               $languageFallbackChainFactory = new 
LanguageFallbackChainFactory();
+               $fallbackChain = 
$languageFallbackChainFactory->newFromLanguage( Language::factory( 'zh' ) );
+
+               $termLookup = $this->getTermLookup();
+               $labelLookup = new LabelLookup( $termLookup, 'en' );
+
+               $label = $labelLookup->getLabelForFallbackChain( new ItemId( 
'Q118' ), $fallbackChain );
+               $this->assertEquals( '测试', $label );
+       }
+
+       private function getTermLookup() {
+               $terms = array(
+                       new \Wikibase\Term( array(
+                               'entityId' => 116,
+                               'entityType' => 'item',
+                               'termType' => 'label',
+                               'termLanguage' => 'en',
+                               'termText' => 'New York City'
+                       ) ),
+                       new \Wikibase\Term( array(
+                               'entityId' => 116,
+                               'entityType' => 'item',
+                               'termType' => 'label',
+                               'termLanguage' => 'es',
+                               'termText' => 'New York City'
+                       ) ),
+                       new \Wikibase\Term( array(
+                               'entityId' => 116,
+                               'entityType' => 'item',
+                               'termType' => 'description',
+                               'termLanguage' => 'en',
+                               'termText' => 'Big Apple'
+                       ) ),
+                       new \Wikibase\Term( array(
+                               'entityId' => 117,
+                               'entityType' => 'item',
+                               'termType' => 'label',
+                               'termLanguage' => 'en',
+                               'termText' => 'Berlin'
+                       ) ),
+                       new \Wikibase\Term( array(
+                               'entityId' => 118,
+                               'entityType' => 'item',
+                               'termType' => 'label',
+                               'termLanguage' => 'zh-cn',
+                               'termText' => '测试'
+                       ) ),
+               );
+
+               return new MockTermIndex( $terms );
+       }
+
+}
diff --git a/lib/tests/phpunit/store/MockTermIndex.php 
b/lib/tests/phpunit/store/MockTermIndex.php
index 24cf50c..c306c5d 100644
--- a/lib/tests/phpunit/store/MockTermIndex.php
+++ b/lib/tests/phpunit/store/MockTermIndex.php
@@ -6,6 +6,7 @@
 use InvalidArgumentException;
 use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\Lib\Store\TermLookup;
 use Wikibase\Term;
 use Wikibase\TermIndex;
 
@@ -24,7 +25,7 @@
  * @licence GNU GPL v2+
  * @author Daniel Kinzler
  */
-class MockTermIndex implements TermIndex {
+class MockTermIndex implements TermIndex, TermLookup {
 
        /**
         * @var Term[]
@@ -34,7 +35,7 @@
        /**
         * @param Term[] $terms
         */
-       public function __construct( $terms ) {
+       public function __construct( array $terms ) {
                $this->terms = $terms;
        }
 
@@ -131,10 +132,38 @@
        }
 
        /**
-        * @throws Exception always
+        * @return EntityId[]
         */
-       public function getEntityIdsForLabel( $label, $languageCode = null, 
$entityType = null, $fuzzySearch = false ) {
-               throw new Exception( 'not implemented by mock class ' );
+       public function getEntityIdsForLabel( $label, $languageCode = null, 
$entityType = null,
+               $fuzzySearch = false
+       ) {
+               $entityIds = array();
+
+               foreach( $this->terms as $term ) {
+                       if ( $languageCode !== null && $term->getLanguage() !== 
$languageCode ) {
+                               continue;
+                       }
+
+                       if ( $entityType !== null && $term->getEntityType() !== 
$entityType ) {
+                               continue;
+                       }
+
+                       if ( $term->getType() !== 'label' ) {
+                               continue;
+                       }
+
+                       if ( !$fuzzySearch ) {
+                               if ( $term->getText() === $label ) {
+                                       $entityIds[] = $term->getEntityId();
+                               }
+                       } else {
+                               if ( strpos( $term->getText(), $label ) !== 
false ) {
+                                       $entityIds[] = $term->getEntityId();
+                               }
+                       }
+               }
+
+               return $entityIds;
        }
 
        /**
@@ -152,10 +181,18 @@
        }
 
        /**
-        * @throws Exception always
+        * @return Term[]
         */
        public function getTermsOfEntity( EntityId $id ) {
-               throw new Exception( 'not implemented by mock class ' );
+               $matchingTerms = array();
+
+               foreach( $this->terms as $term ) {
+                       if ( $term->getEntityId()->equals( $id ) ) {
+                               $matchingTerms[] = $term;
+                       }
+               }
+
+               return $matchingTerms;
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I420df654a8ef7d17e3e45b7e2b0444f5c130a873
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to