Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/66008
Change subject: Added InternalEntityIdInterpreter ...................................................................... Added InternalEntityIdInterpreter Change-Id: Ie5a05b5764944722d43ec3941f83a73af88978fa --- M QueryEngine/includes/SQLStore/EntityIdTransformer.php A QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php M QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php 3 files changed, 134 insertions(+), 14 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/08/66008/1 diff --git a/QueryEngine/includes/SQLStore/EntityIdTransformer.php b/QueryEngine/includes/SQLStore/EntityIdTransformer.php index c73b517..36c1668 100644 --- a/QueryEngine/includes/SQLStore/EntityIdTransformer.php +++ b/QueryEngine/includes/SQLStore/EntityIdTransformer.php @@ -2,6 +2,7 @@ namespace Wikibase\QueryEngine\SQLStore; +use OutOfBoundsException; use Wikibase\EntityId; /** @@ -31,15 +32,16 @@ * @author Jeroen De Dauw < [email protected] > * @author Denny Vrandecic */ -class EntityIdTransformer implements InternalEntityIdFinder { +class EntityIdTransformer implements InternalEntityIdFinder, InternalEntityIdInterpreter { - protected $idMap; + protected $stringTypeToInt; + protected $intTypeToString; /** * @param int[] $idMap Maps entity types (strings) to a unique one digit integer */ public function __construct( array $idMap ) { - $this->idMap = $idMap; + $this->stringTypeToInt = $idMap; } /** @@ -50,19 +52,56 @@ * @return int */ public function getInternalIdForEntity( EntityId $entityId ) { - $this->ensureEntityTypeIsKnown( $entityId->getEntityType() ); + $this->ensureEntityStringTypeIsKnown( $entityId->getEntityType() ); return $this->getComputedId( $entityId ); } - protected function ensureEntityTypeIsKnown( $entityType ) { - if ( !array_key_exists( $entityType, $this->idMap ) ) { - throw new \OutOfBoundsException( "Id of unknown entity type '$entityType' cannot be transformed" ); + protected function ensureEntityStringTypeIsKnown( $entityType ) { + if ( !array_key_exists( $entityType, $this->stringTypeToInt ) ) { + throw new OutOfBoundsException( "Id of unknown entity type '$entityType' cannot be transformed" ); } } protected function getComputedId( EntityId $entityId ) { - return $entityId->getNumericId() * 10 + $this->idMap[$entityId->getEntityType()]; + return $entityId->getNumericId() * 10 + $this->stringTypeToInt[$entityId->getEntityType()]; + } + + /** + * @see InternalEntityIdInterpreter::getExternalIdForEntity + * + * @param int $internalEntityId + * + * @return EntityId + */ + public function getExternalIdForEntity( $internalEntityId ) { + $this->buildIntToStringMap(); + + $numericId = (int)floor( $internalEntityId / 10 ); + $typeId = $internalEntityId % 10; + + $this->ensureEntityIntTypeIsKnown( $typeId ); + $typeId = $this->intTypeToString[$typeId]; + + return new EntityId( $typeId, $numericId ); + } + + protected function buildIntToStringMap() { + if ( is_array( $this->intTypeToString ) ) { + return; + } + + $this->intTypeToString = array(); + + foreach ( $this->stringTypeToInt as $string => $int ) { + $this->intTypeToString[$int] = $string; + } + } + + protected function ensureEntityIntTypeIsKnown( $intType ) { + if ( !array_key_exists( $intType, $this->intTypeToString ) ) { + throw new OutOfBoundsException( "Id of unknown entity type '$intType' cannot be interpreted" ); + } } } diff --git a/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php b/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php new file mode 100644 index 0000000..6bff3c3 --- /dev/null +++ b/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php @@ -0,0 +1,42 @@ +<?php + +namespace Wikibase\QueryEngine\SQLStore; + +use Wikibase\EntityId; + +/** + * Finds the external entity id for the given internal entity 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.1 + * + * @file + * @ingroup WikibaseSQLStore + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +interface InternalEntityIdInterpreter { + + /** + * @param int $internalEntityId + * + * @return EntityId + */ + public function getExternalIdForEntity( $internalEntityId ); + +} diff --git a/QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php b/QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php index 5b32a92..b88fca2 100644 --- a/QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php +++ b/QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php @@ -37,9 +37,11 @@ */ class EntityIdTransformerTest extends \PHPUnit_Framework_TestCase { - public function testConstruct() { - new EntityIdTransformer( $this->getIdMap() ); - $this->assertTrue( true ); + public function testConstructAndImplementsInterfaces() { + $transformer = new EntityIdTransformer( $this->getIdMap() ); + + $this->assertInstanceOf( 'Wikibase\QueryEngine\SQLStore\InternalEntityIdFinder' , $transformer ); + $this->assertInstanceOf( 'Wikibase\QueryEngine\SQLStore\InternalEntityIdInterpreter' , $transformer ); } protected function getIdMap() { @@ -91,12 +93,49 @@ /** * @dataProvider idProvider */ - public function testGetForNotSetType( $entityType, $numericId ) { - $this->setExpectedException( 'OutOfBoundsException' ); - + public function testGetInternalIdForNotSetType( $entityType, $numericId ) { $transformer = new EntityIdTransformer( array() ); + $this->setExpectedException( 'OutOfBoundsException' ); + $transformer->getInternalIdForEntity( new EntityId( $entityType, $numericId ) ); } + /** + * @dataProvider idProvider + */ + public function testGetExternalIdForEntity( $entityType, $numericId ) { + $transformer = new EntityIdTransformer( $this->getIdMap() ); + $expected = new EntityId( $entityType, $numericId ); + + $internalId = $transformer->getInternalIdForEntity( $expected ); + $actual = $transformer->getExternalIdForEntity( $internalId ); + + $this->assertEquals( $expected, $actual ); + } + + /** + * @dataProvider internalIdProvider + */ + public function testGetExternalIdForNotSetType( $internalId ) { + $transformer = new EntityIdTransformer( array() ); + + $this->setExpectedException( 'OutOfBoundsException' ); + + $transformer->getExternalIdForEntity( $internalId ); + } + + public function internalIdProvider() { + $argLists = array(); + + $argLists[] = array( 10 ); + $argLists[] = array( 11 ); + $argLists[] = array( 19 ); + $argLists[] = array( 123450 ); + $argLists[] = array( 123451 ); + $argLists[] = array( 123459 ); + + return $argLists; + } + } -- To view, visit https://gerrit.wikimedia.org/r/66008 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie5a05b5764944722d43ec3941f83a73af88978fa Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
