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

Change subject: Fix undefined EntityId in BufferingTermLookup
......................................................................


Fix undefined EntityId in BufferingTermLookup

BufferingTermLookup needs the Term objects to contain
the entity id. To do that, we need to fetch both entity id
(numeric) and the entity type.

This patch adds those fields to the selection, and since
this is now identical to getTermsOfEntities, then the code
is changed to have getTermOfEntity to delegate to the
getTermsOfEntities method and fixed the overly long lines.

Also added tests for TermSqlIndex::getEntityTerms.

Bug: T86522
Change-Id: I3092fc5f1eaba4beb9d5db1aa8c1c1aa6ccbb6cc
---
M lib/includes/store/sql/TermSqlIndex.php
M lib/tests/phpunit/store/TermIndexTest.php
M repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
3 files changed, 88 insertions(+), 28 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/includes/store/sql/TermSqlIndex.php 
b/lib/includes/store/sql/TermSqlIndex.php
index 462aeb8..32236aa 100644
--- a/lib/includes/store/sql/TermSqlIndex.php
+++ b/lib/includes/store/sql/TermSqlIndex.php
@@ -193,17 +193,27 @@
                // FIXME: OCP violation. No support for new types of entities 
can be registered
 
                if ( $entity instanceof FingerprintProvider ) {
-                       return $this->getFingerprintTerms( 
$entity->getFingerprint() );
+                       $extraFields = array(
+                               'entityType' => $entity->getType(),
+                       );
+
+                       $entityId = $entity->getId();
+
+                       if ( $entityId !== null ) {
+                               $extraFields['entityId'] = 
$entityId->getNumericId();
+                       }
+
+                       return $this->getFingerprintTerms( 
$entity->getFingerprint(), $extraFields );
                }
 
                return array();
        }
 
-       private function getFingerprintTerms( Fingerprint $fingerprint ) {
+       private function getFingerprintTerms( Fingerprint $fingerprint, array 
$extraFields = array() ) {
                $terms = array();
 
                foreach ( $fingerprint->getDescriptions()->toTextArray() as 
$languageCode => $description ) {
-                       $term = new Term();
+                       $term = new Term( $extraFields );
 
                        $term->setLanguage( $languageCode );
                        $term->setType( Term::TYPE_DESCRIPTION );
@@ -213,7 +223,7 @@
                }
 
                foreach ( $fingerprint->getLabels()->toTextArray() as 
$languageCode => $label ) {
-                       $term = new Term();
+                       $term = new Term( $extraFields );
 
                        $term->setLanguage( $languageCode );
                        $term->setType( Term::TYPE_LABEL );
@@ -224,7 +234,7 @@
 
                foreach ( $fingerprint->getAliasGroups() as $aliasGroup ) {
                        foreach ( $aliasGroup->getAliases() as $alias ) {
-                               $term = new Term();
+                               $term = new Term( $extraFields );
 
                                $term->setLanguage( 
$aliasGroup->getLanguageCode() );
                                $term->setType( Term::TYPE_ALIAS );
@@ -236,7 +246,6 @@
 
                return $terms;
        }
-
 
        /**
         * Internal callback for deleting a list of terms.
@@ -383,14 +392,17 @@
         *
         * @return Term[]
         */
-       public function getTermsOfEntity( EntityId $entityId, array $termTypes 
= null, array $languageCodes = null ) {
-               $fields = array(
-                       'term_language',
-                       'term_type',
-                       'term_text',
+       public function getTermsOfEntity(
+               EntityId $entityId,
+               array $termTypes = null,
+               array $languageCodes = null
+       ) {
+               return $this->getTermsOfEntities(
+                       array( $entityId ),
+                       $entityId->getEntityType(),
+                       $termTypes,
+                       $languageCodes
                );
-
-               return $this->fetchTerms( array( $entityId ), 
$entityId->getEntityType(), $fields, $termTypes, $languageCodes );
        }
 
        /**
@@ -406,29 +418,24 @@
         * @throws \MWException
         * @return Term[]
         */
-       public function getTermsOfEntities( array $entityIds, $entityType = 
null, array $termTypes = null, array $languageCodes = null ) {
-               $fields = array(
-                       'term_entity_id',
-                       'term_entity_type',
-                       'term_language',
-                       'term_type',
-                       'term_text',
-               );
-
-               return $this->fetchTerms( $entityIds, $entityType, $fields, 
$termTypes, $languageCodes );
+       public function getTermsOfEntities( array $entityIds, $entityType = 
null, array $termTypes = null,
+               array $languageCodes = null
+       ) {
+               return $this->fetchTerms( $entityIds, $entityType, $termTypes, 
$languageCodes );
        }
 
        /**
         * @param EntityId[] $entityIds
         * @param string|null $entityType
-        * @param string[] $fields
         * @param string[]|null $termTypes
         * @param string[]|null $languageCodes
         *
         * @throws \MWException
         * @return array
         */
-       private function fetchTerms( array $entityIds, $entityType, array 
$fields, array $termTypes = null, array $languageCodes = null ) {
+       private function fetchTerms( array $entityIds, $entityType, array 
$termTypes = null,
+               array $languageCodes = null
+       ) {
                if ( is_array( $entityIds ) && empty( $entityIds ) ) {
                        return array();
                }
@@ -472,7 +479,7 @@
 
                $res = $dbr->select(
                        $this->tableName,
-                       $fields,
+                       array_keys( $this->termFieldMap ),
                        $conditions,
                        __METHOD__
                );
diff --git a/lib/tests/phpunit/store/TermIndexTest.php 
b/lib/tests/phpunit/store/TermIndexTest.php
index b206372..95e5266 100644
--- a/lib/tests/phpunit/store/TermIndexTest.php
+++ b/lib/tests/phpunit/store/TermIndexTest.php
@@ -11,7 +11,7 @@
 use Wikibase\TermIndex;
 
 /**
- * Base class for tests for calsses implementing Wikibase\TermIndex.
+ * Base class for tests for classes implementing Wikibase\TermIndex.
  *
  * @group Wikibase
  *
@@ -102,7 +102,7 @@
 
                $this->assertInternalType( 'array', $actual );
                $this->assertCount( 2, $actual );
-               
+
                /**
                 * @var Term $term
                 * @var Term $expected
diff --git a/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php 
b/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
index 7c2943e..5ad6332 100644
--- a/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
+++ b/repo/tests/phpunit/includes/store/sql/TermSqlIndexTest.php
@@ -2,7 +2,9 @@
 
 namespace Wikibase\Test;
 
+use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Term\AliasGroupList;
 use Wikibase\DataModel\Term\Fingerprint;
 use Wikibase\DataModel\Term\TermList;
@@ -360,4 +362,55 @@
                $this->assertEquals( $normalized, $key );
        }
 
+       /**
+        * @dataProvider getEntityTermsProvider
+        */
+       public function testGetEntityTerms( $expectedTerms, EntityDocument 
$entity ) {
+               $termIndex = $this->getTermIndex();
+               $wikibaseTerms = $termIndex->getEntityTerms( $entity );
+
+               $this->assertEquals( $expectedTerms, $wikibaseTerms );
+       }
+
+       public function getEntityTermsProvider() {
+               $fingerprint = Fingerprint::newEmpty();
+               $fingerprint->setLabel( 'en', 'kittens!!!:)' );
+               $fingerprint->setDescription( 'es', 'es un gato!' );
+               $fingerprint->setAliasGroup( 'en', array( 'kitten-alias' ) );
+
+               $item = Item::newEmpty();
+               $item->setId( new ItemId( 'Q999' ) );
+               $item->setFingerprint( $fingerprint );
+
+               $expectedTerms = array(
+                       new Term( array(
+                               'entityId' => 999,
+                               'entityType' => 'item',
+                               'termText' => 'es un gato!',
+                               'termLanguage' => 'es',
+                               'termType' => 'description'
+                       ) ),
+                       new Term( array(
+                               'entityId' => 999,
+                               'entityType' => 'item',
+                               'termText' => 'kittens!!!:)',
+                               'termLanguage' => 'en',
+                               'termType' => 'label'
+                       ) ),
+                       new Term( array(
+                               'entityId' => 999,
+                               'entityType' => 'item',
+                               'termText' => 'kitten-alias',
+                               'termLanguage' => 'en',
+                               'termType' => 'alias'
+                       ) )
+               );
+
+               return array(
+                       array( $expectedTerms, $item ),
+                       array( array(), Item::newEmpty() ),
+                       array( array(), $this->getMock( 
'Wikibase\DataModel\Entity\EntityDocument' ) )
+               );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I3092fc5f1eaba4beb9d5db1aa8c1c1aa6ccbb6cc
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Adrian Lang <adrian.he...@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