Skip to content

Commit

Permalink
Add QueryEngine and StoreAware interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Sep 1, 2016
1 parent 8b480a4 commit 3967001
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 135 deletions.
11 changes: 2 additions & 9 deletions includes/query/SMW_QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ParamProcessor\Processor;
use SMW\Query\PrintRequest;
use SMW\Query\PrintRequestFactory;
use SMW\ApplicationFactory;

/**
* This file contains a static class for accessing functions to generate and execute
Expand Down Expand Up @@ -488,15 +489,7 @@ public static function getResultFromQuery( SMWQuery $query, array $params, $outp
}

private static function getStoreFromParams( array $params ) {

$storeId = null;
$source = $params['source']->getValue();

if ( $source !== '' ) {
$storeId = $GLOBALS['smwgQuerySources'][$source];
}

return \SMW\StoreFactory::getStore( $storeId );
return ApplicationFactory::getInstance()->getQuerySource( $params['source']->getValue() );
}

/**
Expand Down
11 changes: 2 additions & 9 deletions includes/specials/SMW_SpecialAsk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use SMW\Query\PrintRequest;
use SMW\Query\QueryLinker;
use SMW\MediaWiki\Specials\Ask\HtmlContentBuilder;
use SMW\ApplicationFactory;

/**
* This special page for MediaWiki implements a customisable form for
Expand Down Expand Up @@ -200,15 +201,7 @@ protected function extractQueryParameters( $p ) {
}

private function getStoreFromParams( array $params ) {

$storeId = null;
$source = $params['source']->getValue();

if ( $source !== '' ) {
$storeId = $GLOBALS['smwgQuerySources'][$source];
}

return \SMW\StoreFactory::getStore( $storeId );
return ApplicationFactory::getInstance()->getQuerySource( $params['source']->getValue() );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion includes/storage/SMW_Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SMWRequestOptions;
use SMWSemanticData;
use Title;
use SMW\QueryEngine;

/**
* This group contains all parts of SMW that relate to storing and retrieving
Expand All @@ -28,7 +29,7 @@
*
* @author Markus Krötzsch
*/
abstract class Store {
abstract class Store implements QueryEngine {

/**
* @var boolean
Expand Down
17 changes: 14 additions & 3 deletions src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ public function newCacheFactory() {
return $this->callbackLoader->load( 'CacheFactory', $this->getSettings()->get( 'smwgCacheType' ) );
}

/**
* @since 2.5
*
* @param string|null $source
*
* @return QueryEngine
*/
public function getQuerySource( $source = null ) {
return $this->callbackLoader->singleton( 'QuerySourceFactory' )->getWithLocalFallback( $source );
}

/**
* @since 2.0
*
Expand Down Expand Up @@ -364,12 +375,12 @@ public function newQueryParser() {
}

/**
* @since 2.4
* @since 2.5
*
* @return QueryFactory
*/
public function newQueryFactory() {
return new QueryFactory();
public function getQueryFactory() {
return $this->callbackLoader->singleton( 'QueryFactory' );
}

private static function registerBuilder( CallbackLoader $callbackLoader = null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct( CachedPropertyValuesPrefetcher $cachedPropertyValue
$this->cachedPropertyValuesPrefetcher = ApplicationFactory::getInstance()->getCachedPropertyValuesPrefetcher();
}

$this->queryFactory = ApplicationFactory::getInstance()->newQueryFactory();
$this->queryFactory = ApplicationFactory::getInstance()->getQueryFactory();
}

/**
Expand Down
74 changes: 74 additions & 0 deletions src/Query/QuerySourceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace SMW\Query;

use SMW\Store;
use SMW\QueryEngine;
use SMW\StoreAware;
use RuntimeException;

/**
* @private
*
* @license GNU GPL v2+
* @since 2.5
*
* @author mwjames
*/
class QuerySourceFactory {

/**
* @var Store
*/
private $store;

/**
* @var array
*/
private $querySources = array();

/**
* @since 2.5
*
* @param Store $store
* @param array $querySources
*/
public function __construct( Store $store, $querySources = array() ) {
$this->store = $store;
$this->querySources = $querySources;
}

/**
* @see DefaultSettings::$smwgQuerySources
*
* @since 2.5
*
* @param string|null $source
*
* @return QueryEngine|Store
* @throws RuntimeException
*/
public function getWithLocalFallback( $source = null ) {

if ( $source !== '' && isset( $this->querySources[$source] ) ) {
$source = $this->querySources[$source];
}

if ( $source !== '' && class_exists( $source ) ) {
$source = new $source;
} else {
$source = $this->store;
}

if ( !$source instanceof QueryEngine && !$source instanceof Store ) {
throw new RuntimeException( get_class( $source ) . " does not match the expected QueryEngine interface." );
}

if ( $source instanceof StoreAware ) {
$source->setStore( $this->store );
}

return $source;
}

}
35 changes: 35 additions & 0 deletions src/QueryEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace SMW;

use SMWQuery as Query;
use SMWQueryResult as QueryResult;

/**
* Interface for query answering that depend on concrete implementations to
* provide the filtering and matching process for specific conditions against a
* select back-end.
*
* @license GNU GPL v2
* @since 2.5
*
* @author mwjames
*/
interface QueryEngine {

/**
* Returns a QueryResult object that matches the condition described by a
* query.
*
* @note If the request was made for a debug (querymode MODE_DEBUG) query
* then a simple HTML-compatible string is returned.
*
* @since 2.5
*
* @param Query $query
*
* @return QueryResult|string
*/
public function getQueryResult( Query $query );

}
3 changes: 2 additions & 1 deletion src/SPARQLStore/QueryEngine/QueryEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SMW\SPARQLStore\RepositoryConnection;
use SMWQuery as Query;
use SMWQueryResult as QueryResult;
use SMW\QueryEngine as QueryEngineInterface;

/**
* Class mapping SMWQuery objects to SPARQL, and for controlling the execution
Expand All @@ -23,7 +24,7 @@
* @author Markus Krötzsch
* @author mwjames
*/
class QueryEngine {
class QueryEngine implements QueryEngineInterface {

/// The name of the SPARQL variable that represents the query result.
const RESULT_VARIABLE = 'result';
Expand Down
3 changes: 2 additions & 1 deletion src/SQLStore/QueryEngine/QueryEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SMWQueryResult as QueryResult;
use SMWSql3SmwIds;
use SMWSQLStore3 as SQLStore;
use SMW\QueryEngine as QueryEngineInterface;

/**
* Class that implements query answering for SQLStore.
Expand All @@ -26,7 +27,7 @@
* @author Jeroen De Dauw
* @author mwjames
*/
class QueryEngine {
class QueryEngine implements QueryEngineInterface {

/**
* @var SQLStore
Expand Down
21 changes: 21 additions & 0 deletions src/SharedCallbackContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SMW\MediaWiki\MediaWikiNsContentReader;
use SMW\MediaWiki\PageCreator;
use SMW\MediaWiki\TitleCreator;
use SMW\Query\QuerySourceFactory;

/**
* @license GNU GPL v2+
Expand Down Expand Up @@ -110,6 +111,26 @@ private function registerCallbackHandlers( $callbackLoader ) {
return new FactboxFactory();
} );

/**
* @var QuerySourceFactory
*/
$callbackLoader->registerCallback( 'QuerySourceFactory', function() use ( $callbackLoader ) {
$callbackLoader->registerExpectedReturnType( 'QuerySourceFactory', '\SMW\Query\QuerySourceFactory' );

return new QuerySourceFactory(
$callbackLoader->load( 'Store' ),
$callbackLoader->load( 'Settings' )->get( 'smwgQuerySources' )
);
} );

/**
* @var QueryFactory
*/
$callbackLoader->registerCallback( 'QueryFactory', function() use ( $callbackLoader ) {
$callbackLoader->registerExpectedReturnType( 'QueryFactory', '\SMW\QueryFactory' );
return new QueryFactory();
} );

$callbackLoader->registerExpectedReturnType( 'DeferredCallableUpdate', '\SMW\DeferredCallableUpdate' );

$callbackLoader->registerCallback( 'DeferredCallableUpdate', function( \Closure $callback ) {
Expand Down
22 changes: 22 additions & 0 deletions src/StoreAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace SMW;

/**
* Describes an instance that is aware of a Store object.
*
* @license GNU GPL v2
* @since 2.5
*
* @author mwjames
*/
interface StoreAware {

/**
* @since 2.5
*
* @param Store $store
*/
public function setStore( Store $store );

}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private function tryToProcessQueryTestCase( $jsonTestCaseFileHandler ) {
// Set query parser late to ensure that expected settings are adjusted
// (language etc.) because the __construct relies on the context language
$this->queryTestCaseProcessor->setQueryParser(
ApplicationFactory::getInstance()->newQueryFactory()->newQueryParser()
ApplicationFactory::getInstance()->getQueryFactory()->newQueryParser()
);

$this->queryTestCaseProcessor->setDebugMode(
Expand Down
Loading

0 comments on commit 3967001

Please sign in to comment.