Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/73982
Change subject: Work on object constructrion in Wikibase Query ...................................................................... Work on object constructrion in Wikibase Query Change-Id: If26f772c3f4f29870f31149bfc9cc53ef11c3b08 --- A Tests/Integration/Wikibase/Query/ObjectConstructionTest.php A Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php D Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php D Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php M Tests/Phpunit/Wikibase/Query/DIC/WikibaseQueryTest.php M WikibaseQuery.php M src/Wikibase/Query/ByPropertyValueEntityFinder.php M src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php A src/Wikibase/Query/DIC/Builders/QueryInterfaceBuilder.php A src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php M src/Wikibase/Query/DIC/WikibaseQuery.php 11 files changed, 291 insertions(+), 64 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuery refs/changes/82/73982/1 diff --git a/Tests/Integration/Wikibase/Query/ObjectConstructionTest.php b/Tests/Integration/Wikibase/Query/ObjectConstructionTest.php new file mode 100644 index 0000000..d76fb77 --- /dev/null +++ b/Tests/Integration/Wikibase/Query/ObjectConstructionTest.php @@ -0,0 +1,22 @@ +<?php + +namespace Tests\Integration\Wikibase\Query; + +use Wikibase\Query\DIC\ExtensionAccess; + +/** + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class ObjectConstructionTest extends \PHPUnit_Framework_TestCase { + + public function testConstructQueryStore() { + $queryStore = ExtensionAccess::getWikibaseQuery()->getQueryStore(); + $this->assertInstanceOf( 'Wikibase\QueryEngine\QueryStore', $queryStore ); + } + +} diff --git a/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php new file mode 100644 index 0000000..c70e75f --- /dev/null +++ b/Tests/Phpunit/Wikibase/Query/ByPropertyValueEntityFinderTest.php @@ -0,0 +1,78 @@ +<?php + +namespace Tests\Phpunit\Wikibase\Query; + +use Ask\Language\Description\Description; +use Ask\Language\Description\SomeProperty; +use Ask\Language\Description\ValueDescription; +use Ask\Language\Option\QueryOptions; +use Ask\Language\Query; +use DataValues\DataValue; +use DataValues\StringValue; +use Wikibase\EntityId; +use Wikibase\Query\ByPropertyValueEntityFinder; + +/** + * @covers Wikibase\Query\ByPropertyValueEntityFinder + * + * @file + * @ingroup WikibaseQuery + * @group WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class ByPropertyValueEntityFinderTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider queryProvider + */ + public function testCanConstruct( EntityId $propertyId, DataValue $dataValue, Description $description, QueryOptions $options ) { + $queryEngine = $this->getMock( 'Wikibase\QueryEngine\QueryEngine' ); + + $queryEngine->expects( $this->once() ) + ->method( 'getMatchingEntities' ) + ->with( + $this->equalTo( $description ), + $this->equalTo( $options ) + ); + + $entityFinder = new ByPropertyValueEntityFinder( $queryEngine ); + $entities = $entityFinder->findEntities( $propertyId, $dataValue, $options->getLimit(), $options->getOffset() ); + + $this->assertInternalType( 'array', $entities ); + $this->assertContainsOnlyInstancesOf( 'Wikibase\EntityId', $entities ); + } + + public function queryProvider() { + $argLists = array(); + + $p42 = new EntityId( 'property', 42 ); + $p9001 = new EntityId( 'property', 9001 ); + $fooString = new StringValue( 'foo' ); + $barString = new StringValue( 'bar baz' ); + + $argLists[] = array( + $p42, + $fooString, + new SomeProperty( + $p42, + new ValueDescription( $fooString ) + ), + new QueryOptions( 10, 0 ) + ); + + $argLists[] = array( + $p9001, + $barString, + new SomeProperty( + $p9001, + new ValueDescription( $barString ) + ), + new QueryOptions( 42, 100 ) + ); + + return $argLists; + } + +} diff --git a/Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php b/Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php deleted file mode 100644 index 3732d0b..0000000 --- a/Tests/Phpunit/Wikibase/Query/DIC/DependencyBuilderTest.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -namespace Tests\Phpunit\Wikibase\Query\DIC; - -/** - * @covers Wikibase\Query\DIC\DependencyBuilder - * - * @file - * @ingroup WikibaseQuery - * @group WikibaseQuery - * - * @licence GNU GPL v2+ - * @author Jeroen De Dauw < [email protected] > - */ -class DependencyBuilderTest extends \PHPUnit_Framework_TestCase { - - public function testTrue() { - $this->assertTrue( true ); // TODO - } - -} diff --git a/Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php b/Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php deleted file mode 100644 index 043b06c..0000000 --- a/Tests/Phpunit/Wikibase/Query/DIC/ExtensionAccessTest.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace Tests\Phpunit\Wikibase\Query\DIC; - -use Wikibase\Query\DIC\ExtensionAccess; - -/** - * @covers Wikibase\Query\DIC\ExtensionAccess - * - * @file - * @ingroup WikibaseQuery - * @group WikibaseQuery - * - * @licence GNU GPL v2+ - * @author Jeroen De Dauw < [email protected] > - */ -class ExtensionAccessTest extends \PHPUnit_Framework_TestCase { - - public function testCanSetAndGetRegistry() { - $registry = $this->getMockBuilder( 'Wikibase\Query\DIC\WikibaseQuery' ) - ->disableOriginalConstructor()->getMock(); - - ExtensionAccess::setRegistryBuilder( - function() use ( $registry ) { - return $registry; - } - ); - - $this->assertEquals( $registry, ExtensionAccess::getWikibaseQuery() ); - } - -} diff --git a/Tests/Phpunit/Wikibase/Query/DIC/WikibaseQueryTest.php b/Tests/Phpunit/Wikibase/Query/DIC/WikibaseQueryTest.php index ce0377f..852c70c 100644 --- a/Tests/Phpunit/Wikibase/Query/DIC/WikibaseQueryTest.php +++ b/Tests/Phpunit/Wikibase/Query/DIC/WikibaseQueryTest.php @@ -16,21 +16,33 @@ */ class WikibaseQueryTest extends \PHPUnit_Framework_TestCase { - public function testGetByPropertyValueEntityFinder() { + /** + * @dataProvider objectKeyAndMethodProvider + */ + public function testCanGetObjects( $objectKey, $registryMethodName ) { $dependencyManager = $this->getMock( 'Wikibase\Query\DIC\DependencyManager' ); $expectedObject = (object)array( 'awesomeness' => 9001 ); $dependencyManager->expects( $this->once() ) ->method( 'newObject' ) - ->with( $this->equalTo( 'byPropertyValueEntityFinder' ) ) + ->with( $this->equalTo( $objectKey ) ) ->will( $this->returnValue( $expectedObject ) ); $registry = new WikibaseQuery( $dependencyManager ); - $entityFinder = $registry->getByPropertyValueEntityFinder(); + $actualObject = call_user_func( array( $registry, $registryMethodName ) ); - $this->assertEquals( $expectedObject, $entityFinder ); + $this->assertEquals( $expectedObject, $actualObject ); + } + + public function objectKeyAndMethodProvider() { + $argLists = array(); + + $argLists[] = array( 'byPropertyValueEntityFinder', 'getByPropertyValueEntityFinder' ); + $argLists[] = array( 'queryStore', 'getQueryStore' ); + + return $argLists; } } diff --git a/WikibaseQuery.php b/WikibaseQuery.php index 8aae056..753efa7 100644 --- a/WikibaseQuery.php +++ b/WikibaseQuery.php @@ -27,8 +27,6 @@ * @author Jeroen De Dauw < [email protected] > */ -use Wikibase\Query\DIC\Builders\ByPropertyValueEntityFinderBuilder; - if ( !defined( 'MEDIAWIKI' ) ) { die( 'Not an entry point.' ); } @@ -118,7 +116,7 @@ * @return boolean */ $wgHooks['UnitTestsList'][] = function( array &$files ) { - $directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/Tests/Phpunit/' ); + $directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/Tests/' ); /** * @var SplFileInfo $fileInfo @@ -151,9 +149,24 @@ \Wikibase\Query\DIC\ExtensionAccess::setRegistryBuilder( function() { $dependencyManager = new \Wikibase\Query\DIC\DependencyManager(); - $dependencyManager->registerBuilder( 'byPropertyValueEntityFinder', new ByPropertyValueEntityFinderBuilder() ); + $dependencyManager->registerBuilder( + 'byPropertyValueEntityFinder', + new Wikibase\Query\DIC\Builders\ByPropertyValueEntityFinderBuilder() + ); + $dependencyManager->registerBuilder( + 'queryStore', + new \Wikibase\Query\DIC\Builders\QueryStoreBuilder() + ); + + $dependencyManager->registerBuilder( + 'slaveQueryInterface', + new \Wikibase\Query\DIC\Builders\QueryInterfaceBuilder( + DB_SLAVE, + $GLOBALS['wgDBtype'] + ) + ); return new \Wikibase\Query\DIC\WikibaseQuery( $dependencyManager ); } ); \ No newline at end of file diff --git a/src/Wikibase/Query/ByPropertyValueEntityFinder.php b/src/Wikibase/Query/ByPropertyValueEntityFinder.php index b490177..a620155 100644 --- a/src/Wikibase/Query/ByPropertyValueEntityFinder.php +++ b/src/Wikibase/Query/ByPropertyValueEntityFinder.php @@ -2,6 +2,15 @@ namespace Wikibase\Query; +use Ask\Language\Description\SomeProperty; +use Ask\Language\Description\ValueDescription; +use Ask\Language\Option\QueryOptions; +use Ask\Language\Query; +use DataValues\DataValue; +use Wikibase\EntityId; +use Wikibase\QueryEngine\QueryEngine; +use Wikibase\QueryEngine\SQLStore\Engine\DescriptionMatchFinder; + /** * @since 0.1 * @@ -13,6 +22,31 @@ */ class ByPropertyValueEntityFinder { - // TODO + protected $queryEngine; + + public function __construct( QueryEngine $queryEngine ) { + $this->queryEngine = $queryEngine; + } + + /** + * @param EntityId $propertyId + * @param DataValue $value + * @param int $limit + * @param int $offset + * + * @return EntityId[] + */ + public function findEntities( EntityId $propertyId, DataValue $value, $limit, $offset ) { + $description = new SomeProperty( + $propertyId, + new ValueDescription( $value ) + ); + + $options = new QueryOptions( $limit, $offset ); + + $this->queryEngine->getMatchingEntities( $description, $options ); + + return array(); // TODO + } } diff --git a/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php b/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php index e778664..5f50907 100644 --- a/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php +++ b/src/Wikibase/Query/DIC/Builders/ByPropertyValueEntityFinderBuilder.php @@ -5,6 +5,7 @@ use Wikibase\Query\ByPropertyValueEntityFinder; use Wikibase\Query\DIC\DependencyBuilder; use Wikibase\Query\DIC\DependencyManager; +use Wikibase\QueryEngine\QueryStore; /** * @since 1.0 @@ -25,7 +26,11 @@ * @return ByPropertyValueEntityFinder */ public function buildObject( DependencyManager $dependencyManager ) { - return new ByPropertyValueEntityFinder(); + /** + * @var QueryStore $queryStore + */ + $queryStore = $dependencyManager->newObject( 'queryStore' ); + return new ByPropertyValueEntityFinder( $queryStore->getQueryEngine() ); } } diff --git a/src/Wikibase/Query/DIC/Builders/QueryInterfaceBuilder.php b/src/Wikibase/Query/DIC/Builders/QueryInterfaceBuilder.php new file mode 100644 index 0000000..13119b0 --- /dev/null +++ b/src/Wikibase/Query/DIC/Builders/QueryInterfaceBuilder.php @@ -0,0 +1,64 @@ +<?php + +namespace Wikibase\Query\DIC\Builders; + +use Wikibase\Database\LazyDBConnectionProvider; +use Wikibase\Database\MediaWikiQueryInterface; +use Wikibase\Database\MWDB\ExtendedMySQLAbstraction; +use Wikibase\Query\ByPropertyValueEntityFinder; +use Wikibase\Query\DIC\DependencyBuilder; +use Wikibase\Query\DIC\DependencyManager; + +/** + * @since 1.0 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class QueryInterfaceBuilder extends DependencyBuilder { + + protected $connectionId; + protected $dbType; + + /** + * @param int $connectionId ie DB_MASTER, DB_SLAVE + * @param string $dbType ie mysql, sqlite + */ + public function __construct( $connectionId, $dbType ) { + $this->connectionId = $connectionId; + $this->dbType = $dbType; + } + + /** + * @see DependencyBuilder::buildObject + * + * @param DependencyManager $dependencyManager + * + * @return ByPropertyValueEntityFinder + */ + public function buildObject( DependencyManager $dependencyManager ) { + $connectionProvider = $this->newConnectionProvider(); + + return new MediaWikiQueryInterface( + $connectionProvider, + $this->newExtendedAbstraction( $connectionProvider ) + ); + } + + protected function newConnectionProvider() { + return new LazyDBConnectionProvider( $this->connectionId ); + } + + // TODO: there should be a factory for this in the Wikibase Database component + private function newExtendedAbstraction( $connectionProvider ) { + if ( $this->dbType === 'mysql' ) { + return new ExtendedMySQLAbstraction( $connectionProvider ); + } + + throw new \Exception( 'Support for this dbType not implemented' ); + } + +} diff --git a/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php b/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php new file mode 100644 index 0000000..338944b --- /dev/null +++ b/src/Wikibase/Query/DIC/Builders/QueryStoreBuilder.php @@ -0,0 +1,42 @@ +<?php + +namespace Wikibase\Query\DIC\Builders; + +use Wikibase\Query\ByPropertyValueEntityFinder; +use Wikibase\Query\DIC\DependencyBuilder; +use Wikibase\Query\DIC\DependencyManager; +use Wikibase\QueryEngine\SQLStore\Store; +use Wikibase\QueryEngine\SQLStore\StoreConfig; + +/** + * @since 1.0 + * + * @file + * @ingroup WikibaseQuery + * + * @licence GNU GPL v2+ + * @author Jeroen De Dauw < [email protected] > + */ +class QueryStoreBuilder extends DependencyBuilder { + + /** + * @see DependencyBuilder::buildObject + * + * @param DependencyManager $dependencyManager + * + * @return ByPropertyValueEntityFinder + */ + public function buildObject( DependencyManager $dependencyManager ) { + $config = new StoreConfig( + 'Wikibase Query store v0.1', + 'wbq_', + array() // TODO: add dv handlers + ); + + return new Store( + $config, + $dependencyManager->newObject( 'slaveQueryInterface' ) + ); + } + +} diff --git a/src/Wikibase/Query/DIC/WikibaseQuery.php b/src/Wikibase/Query/DIC/WikibaseQuery.php index 1003de2..44c6afe 100644 --- a/src/Wikibase/Query/DIC/WikibaseQuery.php +++ b/src/Wikibase/Query/DIC/WikibaseQuery.php @@ -2,6 +2,9 @@ namespace Wikibase\Query\DIC; +use Wikibase\Query\ByPropertyValueEntityFinder; +use Wikibase\QueryEngine\QueryStore; + /** * This class exposes methods to retrieve each type of generally accessible object * from the dependency manager. This is the only class that should retrieve objects @@ -29,10 +32,17 @@ } /** - * @return mixed + * @return ByPropertyValueEntityFinder */ public function getByPropertyValueEntityFinder() { return $this->dependencyManager->newObject( 'byPropertyValueEntityFinder' ); } + /** + * @return QueryStore + */ + public function getQueryStore() { + return $this->dependencyManager->newObject( 'queryStore' ); + } + } \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/73982 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If26f772c3f4f29870f31149bfc9cc53ef11c3b08 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
