Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/74639
Change subject: Work on ByPropertyValue API module [DO NOT MERGE] ...................................................................... Work on ByPropertyValue API module [DO NOT MERGE] Change-Id: Ie8945f16b654a3e5ca368e5eea39ce0681dfff27 --- A Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php A Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php M Tests/evilMediaWikiBootstrap.php M WikibaseQuery.php M phpunit.xml.dist R src/Wikibase/Query/Api/EntitiesByPropertyValue.php M src/Wikibase/Query/ByPropertyValueEntityFinder.php M src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php M src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php A src/Wikibase/Query/PropertyDataValueTypeFinder.php 10 files changed, 261 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery refs/changes/39/74639/1 diff --git a/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php b/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php new file mode 100644 index 0000000..bc32417 --- /dev/null +++ b/Tests/Phpunit/Wikibase/Query/PropertyDataValueTypeFinderTest.php @@ -0,0 +1,53 @@ +<?php + +namespace Tests\Phpunit\Wikibase\Query; + +use Wikibase\EntityId; +use Wikibase\Query\PropertyDataValueTypeFinder; + +/** + * @covers Wikibase\Query\PropertyDataValueTypeFinder + * + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class PropertyDataValueTypeFinderTest extends \PHPUnit_Framework_TestCase { + + public function testGetDataValueTypeForProperty() { + $propertyId = new EntityId( 'property', 1337 ); + $dataTypeId = 'awesomeType'; + $dataValueType = 'awesomeDvType'; + + $dtIdLookup = $this->getMock( 'Wikibase\Lib\PropertyDataTypeLookup' ); + + $dtIdLookup->expects( $this->once() ) + ->method( 'getDataTypeIdForProperty' ) + ->with( $this->equalTo( $propertyId ) ) + ->will( $this->returnValue( $dataTypeId ) ); + + $dataType = $this->getMockBuilder( 'DataTypes\DataType' ) + ->disableOriginalConstructor()->getMock(); + + $dataType->expects( $this->once() ) + ->method( 'getDataValueType' ) + ->will( $this->returnValue( $dataValueType ) ); + + $dtFactory = $this->getMock( 'DataTypes\DataTypeFactory' ); + + $dtFactory->expects( $this->once() ) + ->method( 'getType' ) + ->with( $this->equalTo( $dataTypeId ) ) + ->will( $this->returnValue( $dataType ) ); + + $dvTypeFinder = new PropertyDataValueTypeFinder( $dtIdLookup, $dtFactory ); + + $foundDvType = $dvTypeFinder->getDataValueTypeForProperty( $propertyId ); + + $this->assertEquals( $dataValueType, $foundDvType ); + } + +} diff --git a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php new file mode 100644 index 0000000..371bf81 --- /dev/null +++ b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php @@ -0,0 +1,97 @@ +<?php + +namespace Tests\Integration\Wikibase\Query; + +use DataValues\StringValue; +use Wikibase\EntityId; +use Wikibase\Item; +use Wikibase\Property; +use Wikibase\PropertyContent; +use Wikibase\PropertyValueSnak; +use Wikibase\Query\DIC\ExtensionAccess; +use Wikibase\Statement; + +/** + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * @group Database + * @group large + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class EntitiesByPropertyValueApiTest extends \ApiTestCase { + + protected function getQueryStore() { + return ExtensionAccess::getWikibaseQuery()->getQueryStore(); + } + + protected function reinitializeStore() { + $queryStore = $this->getQueryStore(); + $setup = $queryStore->newSetup(); + + $setup->uninstall(); + $setup->install(); + } + + public function setUp() { + parent::setUp(); + $this->reinitializeStore(); + } + + public function tearDown() { + $this->reinitializeStore(); + parent::tearDown(); + } + + public function testMakeApiRequest() { + $this->createNewProperty(); + + $storeUpdater = $this->getQueryStore()->getUpdater(); + + $item = $this->newMockItem(); + + $storeUpdater->insertEntity( $item ); + + $params = array( + 'action' => 'entitiesByPropertyValue', + 'property' => 'p31337', + 'value' => json_encode( $this->newMockValue()->toArray() ), + ); + + list( $resultArray, ) = $this->doApiRequest( $params ); + + q($resultArray); + } + + protected function createNewProperty() { + $property = Property::newEmpty(); + $property->setId( new EntityId( 'property', 31337 ) ); + $property->setDataTypeId( 'string' ); + + $propertyContent = PropertyContent::newFromProperty( $property ); + + $propertyContent->save(); + } + + protected function newMockItem() { + $item = Item::newEmpty(); + + $item->setId( new EntityId( 'item', 42 ) ); + + $item->addClaim( new Statement( + new PropertyValueSnak( + new EntityId( 'property', 31337 ), + $this->newMockValue() + ) + ) ); + + return $item; + } + + protected function newMockValue() { + return new StringValue( 'API tests really suck' ); + } + +} diff --git a/Tests/evilMediaWikiBootstrap.php b/Tests/evilMediaWikiBootstrap.php index 887d2ec..6ba1e6e 100644 --- a/Tests/evilMediaWikiBootstrap.php +++ b/Tests/evilMediaWikiBootstrap.php @@ -53,6 +53,11 @@ $wgTitle = null; global $wgAutoloadClasses; + +if ( is_null( $wgAutoloadClasses ) ) { + $wgAutoloadClasses = array(); +} + require_once $IP . '/tests/TestsAutoLoader.php'; function loadSettings() { diff --git a/WikibaseQuery.php b/WikibaseQuery.php index 753efa7..58072b7 100644 --- a/WikibaseQuery.php +++ b/WikibaseQuery.php @@ -88,7 +88,7 @@ call_user_func( function() { global $wgExtensionCredits, $wgExtensionMessagesFiles, $wgHooks, $wgWBRepoSettings; - global $wgExtraNamespaces, $wgContentHandlers; + global $wgExtraNamespaces, $wgContentHandlers, $wgAPIModules; $wgExtensionCredits['wikibase'][] = array( 'path' => __DIR__, @@ -103,6 +103,8 @@ $wgExtensionMessagesFiles['WikibaseQuery'] = __DIR__ . '/WikibaseQuery.i18n.php'; + $wgAPIModules['entitiesByPropertyValue'] = 'Wikibase\Query\Api\EntitiesByPropertyValue'; + /** * Hook to add PHPUnit test cases. * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4ea344c..68df2e4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,6 +15,7 @@ <testsuites> <testsuite name="WikibaseQuery"> <directory>Tests/Phpunit</directory> + <directory>Tests/Integration</directory> </testsuite> </testsuites> </phpunit> diff --git a/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php similarity index 73% rename from src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php rename to src/Wikibase/Query/Api/EntitiesByPropertyValue.php index c40575a..7b0ab1a 100644 --- a/src/Wikibase/Query/Api/EntitiesByPropertyValueWrapper.php +++ b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php @@ -23,7 +23,11 @@ */ public function execute() { $entityFinder = ExtensionAccess::getWikibaseQuery()->getByPropertyValueEntityFinder(); - $entityFinder->findEntities( $this->extractRequestParams() ); + + // TODO: handle exceptions + $entityIds = $entityFinder->findEntities( $this->extractRequestParams() ); + + $this->getResult()->addValue( null, 'entities', $entityIds ); // TODO: add to API output // TODO: system test @@ -51,6 +55,22 @@ ApiBase::PARAM_REQUIRED => false, ApiBase::PARAM_DFLT => 'item', ), + 'limit' => array( + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => false, + ApiBase::PARAM_DFLT => 10, + ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1, // TODO: policy decision + ApiBase::PARAM_MIN => 0, + ApiBase::PARAM_RANGE_ENFORCE => true, + ), + 'offset' => array( + ApiBase::PARAM_TYPE => 'string', + ApiBase::PARAM_REQUIRED => false, + ApiBase::PARAM_DFLT => 0, + ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1, // TODO: policy decision + ApiBase::PARAM_MIN => 0, + ApiBase::PARAM_RANGE_ENFORCE => true, + ), ); } diff --git a/src/Wikibase/Query/ByPropertyValueEntityFinder.php b/src/Wikibase/Query/ByPropertyValueEntityFinder.php index 19ba590..c31abbc 100644 --- a/src/Wikibase/Query/ByPropertyValueEntityFinder.php +++ b/src/Wikibase/Query/ByPropertyValueEntityFinder.php @@ -12,6 +12,7 @@ use Wikibase\EntityId; use Wikibase\Lib\EntityIdParser; use Wikibase\QueryEngine\QueryEngine; +use Wikibase\Repo\WikibaseRepo; /** * @since 0.1 @@ -34,12 +35,22 @@ } public function findEntities( array $requestArguments ) { - return $this->findEntitiesGivenRawArguments( + // TODO: verify element existence + $entityIds = $this->findEntitiesGivenRawArguments( $requestArguments['property'], $requestArguments['value'], $requestArguments['limit'], $requestArguments['offset'] ); + + $formattedIds = array(); + + foreach ( $entityIds as $entityId ) { + // TODO: inject ID formatter + $formattedIds[] = $entityId->getPrefixedId(); + } + + return $formattedIds; } /** diff --git a/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php b/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php index 5f50907..e992c85 100644 --- a/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php +++ b/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php @@ -2,10 +2,12 @@ namespace Wikibase\Query\DIC\Builders; +use DataValues\DataValueFactory; use Wikibase\Query\ByPropertyValueEntityFinder; use Wikibase\Query\DIC\DependencyBuilder; use Wikibase\Query\DIC\DependencyManager; use Wikibase\QueryEngine\QueryStore; +use Wikibase\Repo\WikibaseRepo; /** * @since 1.0 @@ -30,7 +32,12 @@ * @var QueryStore $queryStore */ $queryStore = $dependencyManager->newObject( 'queryStore' ); - return new ByPropertyValueEntityFinder( $queryStore->getQueryEngine() ); + + return new ByPropertyValueEntityFinder( + $queryStore->getQueryEngine(), + DataValueFactory::singleton(), // TODO: get instance from repo factory + WikibaseRepo::getDefaultInstance()->getEntityIdParser() + ); } } diff --git a/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php b/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php index 338944b..ee0d6eb 100644 --- a/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php +++ b/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php @@ -5,8 +5,11 @@ use Wikibase\Query\ByPropertyValueEntityFinder; use Wikibase\Query\DIC\DependencyBuilder; use Wikibase\Query\DIC\DependencyManager; +use Wikibase\Query\PropertyDataValueTypeFinder; +use Wikibase\QueryEngine\SQLStore\DataValueHandlers; use Wikibase\QueryEngine\SQLStore\Store; use Wikibase\QueryEngine\SQLStore\StoreConfig; +use Wikibase\Repo\WikibaseRepo; /** * @since 1.0 @@ -27,12 +30,22 @@ * @return ByPropertyValueEntityFinder */ public function buildObject( DependencyManager $dependencyManager ) { + // TODO: provide an extension mechanism for the DV handlers + $dvHandlers = new DataValueHandlers(); + $config = new StoreConfig( 'Wikibase Query store v0.1', 'wbq_', - array() // TODO: add dv handlers + $dvHandlers->getHandlers() ); + $dvtLookup = new PropertyDataValueTypeFinder( + WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup(), + WikibaseRepo::getDefaultInstance()->getDataTypeFactory() + ); + + $config->setPropertyDataValueTypeLookup( $dvtLookup ); + return new Store( $config, $dependencyManager->newObject( 'slaveQueryInterface' ) diff --git a/src/Wikibase/Query/PropertyDataValueTypeFinder.php b/src/Wikibase/Query/PropertyDataValueTypeFinder.php new file mode 100644 index 0000000..4be1084 --- /dev/null +++ b/src/Wikibase/Query/PropertyDataValueTypeFinder.php @@ -0,0 +1,47 @@ +<?php + +namespace Wikibase\Query; + +use DataTypes\DataTypeFactory; +use DataValues\DataValue; +use Wikibase\Lib\PropertyDataTypeLookup; +use Wikibase\QueryEngine\PropertyDataValueTypeLookup; + +/** + * @since 0.1 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class PropertyDataValueTypeFinder implements PropertyDataValueTypeLookup { + + protected $propertyDtLookup; + protected $dtFactory; + + public function __construct( PropertyDataTypeLookup $propertyDtLookup, DataTypeFactory $dtFactory ) { + $this->propertyDtLookup = $propertyDtLookup; + $this->dtFactory = $dtFactory; + } + + /** + * @see PropertyDataValueTypeLookup::getDataValueTypeForProperty + * + * @param DataValue $propertyId + * + * @return string + */ + public function getDataValueTypeForProperty( DataValue $propertyId ) { + // TODO: verify is EntityId + + $dataTypeId = $this->propertyDtLookup->getDataTypeIdForProperty( $propertyId ); + + // TODO: catch OutOfBoundsException + $dataType = $this->dtFactory->getType( $dataTypeId ); + + return $dataType->getDataValueType(); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/74639 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie8945f16b654a3e5ca368e5eea39ce0681dfff27 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseQuery Gerrit-Branch: master Gerrit-Owner: Jeroen De Dauw <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
