Jeroen De Dauw has submitted this change and it was merged.
Change subject: Wired up ByPropertyValue API module.
......................................................................
Wired up ByPropertyValue API module.
Change-Id: Ie8945f16b654a3e5ca368e5eea39ce0681dfff27
---
M Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
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
11 files changed, 288 insertions(+), 12 deletions(-)
Approvals:
Jeroen De Dauw: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
index 36896c1..2bd36c1 100644
--- a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
+++ b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php
@@ -29,9 +29,7 @@
* @dataProvider queryProvider
*/
public function testFindEntities( $propertyIdString,
$dataValueSerialization, Description $description, QueryOptions $options ) {
- $expectedIds = array(
- new EntityId( 'hax', 1337 )
- );
+ $expectedIds = array( 'q42' );
$entityFinder = $this->newEntityFinder( $description, $options,
$expectedIds, $propertyIdString );
@@ -67,7 +65,14 @@
->with( $this->equalTo( $propertyIdString ) )
->will( $this->returnValue( $this->mockProperty() ) );
- return new ByPropertyValueEntityFinder( $queryEngine,
$dvFactory, $idParser );
+ $idFormatter = $this->getMockBuilder(
'Wikibase\Lib\EntityIdFormatter' )
+ ->disableOriginalConstructor()->getMock();
+
+ $idFormatter->expects( $this->once() )
+ ->method( 'format' )
+ ->will( $this->returnValue( 'q42' ) );
+
+ return new ByPropertyValueEntityFinder( $queryEngine,
$dvFactory, $idParser, $idFormatter );
}
protected function mockProperty() {
@@ -76,7 +81,7 @@
protected function assertEntityIdsEqual( array $expected, $actual ) {
$this->assertInternalType( 'array', $actual );
- $this->assertContainsOnlyInstancesOf( 'Wikibase\EntityId',
$actual );
+ $this->assertContainsOnly( 'string', $actual );
$this->assertEquals( $expected, $actual );
}
@@ -144,7 +149,14 @@
->method( 'parse' )
->will( $this->returnValue( $this->mockProperty() ) );
- return new ByPropertyValueEntityFinder( $queryEngine,
$dvFactory, $idParser );
+ $idFormatter = $this->getMockBuilder(
'Wikibase\Lib\EntityIdFormatter' )
+ ->disableOriginalConstructor()->getMock();
+
+ $idFormatter->expects( $this->any() )
+ ->method( 'format' )
+ ->will( $this->returnValue( 'q42' ) );
+
+ return new ByPropertyValueEntityFinder( $queryEngine,
$dvFactory, $idParser, $idFormatter );
}
public function invalidLimitProvider() {
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..ff25b78
--- /dev/null
+++ b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
@@ -0,0 +1,101 @@
+<?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 );
+
+ $this->assertArrayHasKey( 'entities', $resultArray );
+
+ $entities = $resultArray['entities'];
+
+ $this->assertEquals( array( 'q42' ), $entities );
+ }
+
+ 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..beb33bb 100644
--- a/src/Wikibase/Query/ByPropertyValueEntityFinder.php
+++ b/src/Wikibase/Query/ByPropertyValueEntityFinder.php
@@ -10,8 +10,10 @@
use InvalidArgumentException;
use RuntimeException;
use Wikibase\EntityId;
+use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\EntityIdParser;
use Wikibase\QueryEngine\QueryEngine;
+use Wikibase\Repo\WikibaseRepo;
/**
* @since 0.1
@@ -26,20 +28,32 @@
protected $queryEngine;
protected $dvFactory;
+ protected $idParser;
+ protected $idFormatter;
- public function __construct( QueryEngine $queryEngine, DataValueFactory
$dvFactory, EntityIdParser $idParser ) {
+ public function __construct( QueryEngine $queryEngine, DataValueFactory
$dvFactory, EntityIdParser $idParser, EntityIdFormatter $idFormatter ) {
$this->queryEngine = $queryEngine;
$this->dvFactory = $dvFactory;
$this->idParser = $idParser;
+ $this->idFormatter = $idFormatter;
}
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 ) {
+ $formattedIds[] = $this->idFormatter->format( $entityId
);
+ }
+
+ return $formattedIds;
}
/**
diff --git
a/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php
b/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php
index 5f50907..0f5b20b 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,13 @@
* @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(),
+
WikibaseRepo::getDefaultInstance()->getEntityIdFormatter()
+ );
}
}
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: merged
Gerrit-Change-Id: Ie8945f16b654a3e5ca368e5eea39ce0681dfff27
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseQuery
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[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