|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SEQL; |
|
4
|
|
|
|
|
5
|
|
|
use Onoi\HttpRequest\HttpRequestFactory; |
|
6
|
|
|
use SEQL\ByHttpRequest\JsonResponseParser; |
|
7
|
|
|
use SEQL\ByHttpRequest\QueryResultFetcher; |
|
8
|
|
|
use SMW\SQLStore\SQLStore; |
|
9
|
|
|
use SMW\ApplicationFactory; |
|
10
|
|
|
use SMWQuery as Query; |
|
11
|
|
|
use SMWQueryResult as QueryResult; |
|
12
|
|
|
use Interwiki; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @license GNU GPL v2+ |
|
16
|
|
|
* @since 1.0 |
|
17
|
|
|
* |
|
18
|
|
|
* @author mwjames |
|
19
|
|
|
*/ |
|
20
|
|
|
class ByHttpRequestQueryLookup extends SQLStore { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var QueryResultFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $queryResultFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var CacheFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $cacheFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @since 1.0 |
|
34
|
|
|
* |
|
35
|
|
|
* @param Query $query |
|
36
|
|
|
* |
|
37
|
|
|
* @return QueryResult |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function getQueryResult( Query $query ) { |
|
40
|
|
|
|
|
41
|
3 |
|
$this->queryResultFactory = new QueryResultFactory( $this ); |
|
42
|
|
|
|
|
43
|
3 |
|
if ( $query->querymode === Query::MODE_DEBUG ) { |
|
44
|
1 |
|
$query->addErrors( array( wfMessage( 'seql-debug-query-not-supported' )->text() ) ); |
|
45
|
1 |
|
return $this->queryResultFactory->newEmptyQueryResult( $query ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
$interwiki = $this->tryToMatchInterwikiFor( $query ); |
|
49
|
|
|
|
|
50
|
2 |
|
if ( $interwiki === false || $interwiki === null ) { |
|
51
|
1 |
|
$query->addErrors( array( wfMessage( 'seql-interwiki-prefix-is-missing', $query->getQuerySource() )->text() ) ); |
|
52
|
1 |
|
return $this->queryResultFactory->newEmptyQueryResult( $query ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return $this->fetchQueryResultFor( $query, $interwiki ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
protected function tryToMatchInterwikiFor( Query $query ) { |
|
59
|
1 |
|
return Interwiki::fetch( $query->getQuerySource() ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
protected function fetchQueryResultFor( Query $query, $interwiki ) { |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
1 |
|
$queryResultFetcher = new QueryResultFetcher( |
|
65
|
1 |
|
new HttpRequestFactory( $this->getCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['seqlgHttpResponseCacheType'] ) ), |
|
66
|
1 |
|
$this->queryResultFactory, |
|
67
|
1 |
|
new JsonResponseParser( new DataValueDeserializer( $query->getQuerySource() ) ) |
|
68
|
1 |
|
); |
|
69
|
|
|
|
|
70
|
1 |
|
$queryResultFetcher->setHttpRequestEndpoint( $interwiki->getApi() ); |
|
71
|
1 |
|
$queryResultFetcher->setRepositoryTargetUrl( $interwiki->getUrl() ); |
|
72
|
|
|
|
|
73
|
1 |
|
$queryResultFetcher->setHttpResponseCachePrefix( $this->getCacheFactory()->getCachePrefix() ); |
|
74
|
1 |
|
$queryResultFetcher->setHttpResponseCacheLifetime( $GLOBALS['seqlgHttpResponseCacheLifetime'] ); |
|
75
|
|
|
|
|
76
|
1 |
|
return $queryResultFetcher->fetchQueryResult( $query ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
private function getCacheFactory() { |
|
80
|
|
|
|
|
81
|
1 |
|
if ( $this->cacheFactory === null ) { |
|
82
|
1 |
|
$this->cacheFactory = ApplicationFactory::getInstance()->newCacheFactory(); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return $this->cacheFactory; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: