jenkins-bot has submitted this change and it was merged.
Change subject: (bug 49742) Introducing PropertyInfoDataTypeLookup
......................................................................
(bug 49742) Introducing PropertyInfoDataTypeLookup
Adding an implementation of DataTypeLookup that is based on a
PropertyInfoStore.
Change-Id: Ibb8f50230be53575bee69b4fb64f6bd27e02ccbf
---
M lib/WikibaseLib.classes.php
M lib/includes/EntityRetrievingDataTypeLookup.php
A lib/includes/PropertyInfoDataTypeLookup.php
M lib/tests/phpunit/EntityRetrievingDataTypeLookupTest.php
A lib/tests/phpunit/PropertyInfoDataTypeLookupTest.php
5 files changed, 252 insertions(+), 2 deletions(-)
Approvals:
Aude: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php
index 0bac36d..b226735 100644
--- a/lib/WikibaseLib.classes.php
+++ b/lib/WikibaseLib.classes.php
@@ -44,6 +44,7 @@
'Wikibase\Lib\GuidGenerator' => 'includes/GuidGenerator.php',
'Wikibase\Lib\V4GuidGenerator' => 'includes/GuidGenerator.php',
'Wikibase\Lib\EntityRetrievingDataTypeLookup' =>
'includes/EntityRetrievingDataTypeLookup.php',
+ 'Wikibase\Lib\PropertyInfoDataTypeLookup' =>
'includes/PropertyInfoDataTypeLookup.php',
'Wikibase\Lib\ClaimGuidGenerator' =>
'includes/GuidGenerator.php',
'Wikibase\Lib\ClaimGuidValidator' =>
'includes/ClaimGuidValidator.php',
'Wikibase\Lib\InMemoryDataTypeLookup' =>
'includes/InMemoryDataTypeLookup.php',
diff --git a/lib/includes/EntityRetrievingDataTypeLookup.php
b/lib/includes/EntityRetrievingDataTypeLookup.php
index d273084..add731d 100644
--- a/lib/includes/EntityRetrievingDataTypeLookup.php
+++ b/lib/includes/EntityRetrievingDataTypeLookup.php
@@ -8,7 +8,8 @@
use Wikibase\Property;
/**
- * PropertyDataTypeLookup that uses an EntityLookup to find the
+ * PropertyDataTypeLookup that uses an EntityLookup to find
+ * a property's data type ID.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -52,7 +53,6 @@
* @return string
*/
public function getDataTypeIdForProperty( EntityId $propertyId ) {
- //TODO: use memcached here, or wrap this object!
$this->verifyIdIsOfAProperty( $propertyId );
return $this->getProperty( $propertyId )->getDataTypeId();
}
diff --git a/lib/includes/PropertyInfoDataTypeLookup.php
b/lib/includes/PropertyInfoDataTypeLookup.php
new file mode 100644
index 0000000..8f8ab24
--- /dev/null
+++ b/lib/includes/PropertyInfoDataTypeLookup.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use InvalidArgumentException;
+use Wikibase\EntityId;
+use Wikibase\EntityLookup;
+use Wikibase\Property;
+use Wikibase\PropertyInfoStore;
+
+/**
+ * PropertyDataTypeLookup that uses an PropertyInfoStore to find
+ * a property's data type ID.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @file
+ * @ingroup WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class PropertyInfoDataTypeLookup implements PropertyDataTypeLookup {
+
+ /**
+ * @var PropertyDataTypeLookup
+ */
+ private $fallbackLookup;
+
+ /**
+ * @var PropertyInfoStore
+ */
+ private $infoStore;
+
+ /**
+ * @param PropertyInfoStore $infoStore
+ * @param PropertyDataTypeLookup $fallbackLookup
+ */
+ public function __construct( PropertyInfoStore $infoStore,
PropertyDataTypeLookup $fallbackLookup = null ) {
+ $this->infoStore = $infoStore;
+ $this->fallbackLookup = $fallbackLookup;
+ }
+
+ /**
+ * @since 0.4
+ *
+ * @param EntityId $propertyId
+ *
+ * @return string
+ */
+ public function getDataTypeIdForProperty( EntityId $propertyId ) {
+ $dataTypeId = null;
+ $info = $this->infoStore->getPropertyInfo( $propertyId );
+
+ if ( $info !== null && isset(
$info[PropertyInfoStore::KEY_DATA_TYPE] ) ) {
+ $dataTypeId = $info[PropertyInfoStore::KEY_DATA_TYPE];
+ }
+
+ if ( $dataTypeId === null && $this->fallbackLookup !== null ) {
+ $dataTypeId =
$this->fallbackLookup->getDataTypeIdForProperty( $propertyId );
+
+ if ( $dataTypeId !== null ) {
+ wfDebugLog( __CLASS__, __FUNCTION__ . ': No
property info found for '
+ . $propertyId . ', but property ID
could be retrieved from fallback store!' );
+
+ //TODO: Automatically update the info store?
+ //TODO: Suggest to run rebuildPropertyInfo.php
+ }
+ }
+
+ if ( $dataTypeId === null ) {
+ throw new PropertyNotFoundException( $propertyId );
+ }
+
+ return $dataTypeId;
+ }
+
+}
diff --git a/lib/tests/phpunit/EntityRetrievingDataTypeLookupTest.php
b/lib/tests/phpunit/EntityRetrievingDataTypeLookupTest.php
index 17dcfd5..e4b42f3 100644
--- a/lib/tests/phpunit/EntityRetrievingDataTypeLookupTest.php
+++ b/lib/tests/phpunit/EntityRetrievingDataTypeLookupTest.php
@@ -31,6 +31,7 @@
* @ingroup WikibaseLib
* @ingroup Test
*
+ * @group Wikibase
* @group WikibaseLib
* @group DataTypeLookupTest
*
diff --git a/lib/tests/phpunit/PropertyInfoDataTypeLookupTest.php
b/lib/tests/phpunit/PropertyInfoDataTypeLookupTest.php
new file mode 100644
index 0000000..29b7c1d
--- /dev/null
+++ b/lib/tests/phpunit/PropertyInfoDataTypeLookupTest.php
@@ -0,0 +1,155 @@
+<?php
+
+namespace Wikibase\Lib\Test;
+
+use Wikibase\EntityId;
+use Wikibase\Lib\EntityRetrievingDataTypeLookup;
+use Wikibase\Lib\PropertyDataTypeLookup;
+use Wikibase\Lib\PropertyInfoDataTypeLookup;
+use Wikibase\Property;
+use Wikibase\PropertyInfoStore;
+use Wikibase\Test\MockPropertyInfoStore;
+use Wikibase\Test\MockRepository;
+
+/**
+ * Tests for the Wikibase\Lib\PropertyInfoDataTypeLookup class.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @file
+ * @ingroup WikibaseLib
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseLib
+ * @group DataTypeLookupTest
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ * @author Daniel Kinzler
+ */
+class PropertyInfoDataTypeLookupTest extends \PHPUnit_Framework_TestCase {
+
+ private $propertiesAndTypes = array(
+ 1 => 'NyanData all the way across the sky',
+ 42 => 'string',
+ 1337 => 'percentage',
+ 9001 => 'positive whole number',
+ );
+
+ private function newEntityLookup() {
+ $lookup = new MockRepository();
+
+ foreach ( $this->propertiesAndTypes as $propertyId =>
$dataTypeId ) {
+ $property = Property::newEmpty();
+ $property->setId( $propertyId );
+ $property->setDataTypeId( $dataTypeId );
+
+ $lookup->putEntity( $property );
+ }
+
+ return $lookup;
+ }
+
+ public function getDataTypeForPropertyProvider() {
+ $argLists = array();
+
+ $emptyInfoStore = new MockPropertyInfoStore();
+ $mockInfoStore = new MockPropertyInfoStore();
+
+ $mockRepo = new MockRepository();
+ $mockDataTypeLookup = new EntityRetrievingDataTypeLookup(
$mockRepo );
+
+ foreach ( $this->propertiesAndTypes as $propertyId =>
$dataTypeId ) {
+ $id = new EntityId( Property::ENTITY_TYPE, $propertyId
);
+
+ // register property info
+ $mockInfoStore->setPropertyInfo(
+ $id,
+ array( PropertyInfoStore::KEY_DATA_TYPE =>
$dataTypeId )
+ );
+
+ // register property as an entity, for the fallback
+ $property = Property::newEmpty();
+ $property->setId( $id );
+ $property->setDataTypeId( $dataTypeId );
+ $mockRepo->putEntity( $property );
+
+ // try with a working info store
+ $argLists[] = array(
+ $mockInfoStore,
+ null,
+ $id,
+ $dataTypeId
+ );
+
+ // try with via fallback
+ $argLists[] = array(
+ $emptyInfoStore,
+ $mockDataTypeLookup,
+ $id,
+ $dataTypeId
+ );
+ }
+
+ // try unknown property
+ $id = new EntityId( Property::ENTITY_TYPE, 23 );
+
+ // try with a working info store
+ $argLists[] = array(
+ $mockInfoStore,
+ null,
+ $id,
+ false
+ );
+
+ // try with via fallback
+ $argLists[] = array(
+ $emptyInfoStore,
+ $mockDataTypeLookup,
+ $id,
+ false
+ );
+
+ return $argLists;
+ }
+
+ /**
+ * @dataProvider getDataTypeForPropertyProvider
+ */
+ public function testGetDataTypeForProperty(
+ PropertyInfoStore $infoStore,
+ PropertyDataTypeLookup $fallbackLookup = null,
+ EntityId $propertyId,
+ $expectedDataType
+ ) {
+ if ( $expectedDataType === false ) {
+ $this->setExpectedException(
'Wikibase\Lib\PropertyNotFoundException' );
+ }
+
+ $lookup = new PropertyInfoDataTypeLookup( $infoStore,
$fallbackLookup );
+
+ $actualDataType = $lookup->getDataTypeIdForProperty(
$propertyId );
+
+ if ( $expectedDataType !== false ) {
+ $this->assertInternalType( 'string', $actualDataType );
+ $this->assertEquals( $expectedDataType, $actualDataType
);
+ }
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/71341
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ibb8f50230be53575bee69b4fb64f6bd27e02ccbf
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits