1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\Config; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
6
|
|
|
use Doctrine\Common\Cache\Cache; |
7
|
|
|
use Doctrine\Common\Cache\ChainCache; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Mediawiki\Api\MediawikiApi; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Symfony\Component\Config\Definition\Processor; |
12
|
|
|
use Wikibase\EntityStore\Api\ApiEntityStore; |
13
|
|
|
use Wikibase\EntityStore\Cache\CachedEntityStore; |
14
|
|
|
use Wikibase\EntityStore\EntityStore; |
15
|
|
|
use Wikibase\EntityStore\EntityStoreOptions; |
16
|
|
|
use Wikibase\EntityStore\MongoDB\MongoDBEntityStore; |
17
|
|
|
use WikidataQueryApi\WikidataQueryApi; |
18
|
|
|
|
19
|
|
|
class EntityStoreFromConfigurationBuilder { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $configurationFileName |
23
|
|
|
* @return EntityStore |
24
|
|
|
*/ |
25
|
2 |
|
public function buildEntityStore( $configurationFileName ) { |
26
|
2 |
|
$config = $this->parseConfiguration( $configurationFileName ); |
27
|
|
|
|
28
|
2 |
|
$store = $this->buildEntityStoreFromConfig( $config ); |
29
|
|
|
|
30
|
2 |
|
if( array_key_exists( 'cache', $config ) ) { |
31
|
1 |
|
$cache = $this->buildCacheFromConfig( $config['cache'] ); |
32
|
|
|
|
33
|
1 |
|
if( $cache !== null ) { |
34
|
1 |
|
return new CachedEntityStore( $store, $cache, $config['cache']['lifetime'] ); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
return $store; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $configurationFileName |
43
|
|
|
* @return Cache |
44
|
|
|
*/ |
45
|
|
|
public function buildCache( $configurationFileName ) { |
46
|
|
|
$config = $this->parseConfiguration( $configurationFileName ); |
47
|
|
|
|
48
|
|
|
if( !array_key_exists( 'cache', $config ) ) { |
49
|
|
|
throw new InvalidArgumentException( 'No cache key in configuration' ); |
50
|
|
|
} |
51
|
|
|
return $this->buildCacheFromConfig( $config['cache'] ); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
private function buildEntityStoreFromConfig( $config ) { |
55
|
2 |
|
$options = new EntityStoreOptions( $config['options'] ); |
56
|
|
|
|
57
|
2 |
|
switch( $config['backend'] ) { |
58
|
2 |
|
case 'api': |
59
|
1 |
|
return new ApiEntityStore( |
60
|
1 |
|
$this->getWikibaseApi( $config['api'] ), |
61
|
1 |
|
$this->getWikidataQueryApi( $config['api'] ), |
62
|
|
|
$options |
63
|
1 |
|
); |
64
|
1 |
|
case 'mongodb': |
65
|
1 |
|
return new MongoDBEntityStore( $this->getMongoDbDatabase( $config['mongodb'] ), $options ); |
66
|
1 |
|
default: |
67
|
|
|
throw new InvalidArgumentException( 'Unknown backend: ' . $config['backend'] ); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
private function getWikibaseApi( $config ) { |
72
|
1 |
|
return MediawikiApi::newFromApiEndpoint( $config['url'] ); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
private function getWikidataQueryApi( $config ) { |
76
|
1 |
|
if( !array_key_exists( 'wikidataquery_url', $config ) ) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return new WikidataQueryApi( $config['wikidataquery_url'] ); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
private function getMongoDbDatabase( $config ) { |
84
|
1 |
|
$connection = new \Doctrine\MongoDB\Connection( $config['server'] ); |
85
|
1 |
|
if( !$connection->connect() ) { |
86
|
|
|
throw new RuntimeException( 'Fail to connect to MongoDb' ); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
return $connection |
90
|
1 |
|
->selectDatabase( $config['database'] ); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
private function buildCacheFromConfig( $config ) { |
94
|
1 |
|
$caches = []; |
95
|
|
|
|
96
|
1 |
|
if( $config['array']['enabled'] ) { |
97
|
1 |
|
$caches[] = new ArrayCache(); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
if( $config['memcached']['enabled'] ) { |
101
|
1 |
|
$memcached = new \Memcached(); |
102
|
|
|
|
103
|
1 |
|
if( !$memcached->addServer( $config['memcached']['host'], $config['memcached']['port'] ) ) { |
104
|
|
|
throw new RuntimeException( 'Fail to connect to Memcached' ); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$memcachedCache = new \Doctrine\Common\Cache\MemcachedCache(); |
108
|
1 |
|
$memcachedCache->setMemcached($memcached); |
109
|
1 |
|
$caches[] = $memcachedCache; |
110
|
1 |
|
} |
111
|
|
|
|
112
|
1 |
|
switch( count( $caches ) ) { |
113
|
1 |
|
case 0: |
114
|
|
|
return null; |
115
|
1 |
|
case 1: |
116
|
|
|
return reset( $caches ); |
117
|
1 |
|
default: |
118
|
1 |
|
return new ChainCache( $caches ); |
119
|
1 |
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
private function parseConfiguration( $configurationFileName ) { |
123
|
2 |
|
$configValues = json_decode( file_get_contents( $configurationFileName ), true ); |
124
|
|
|
|
125
|
2 |
|
$processor = new Processor(); |
126
|
2 |
|
$configuration = new EntityStoreConfiguration(); |
127
|
2 |
|
return $processor->processConfiguration( |
128
|
2 |
|
$configuration, |
129
|
2 |
|
[ $configValues ] |
130
|
2 |
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|