-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathGlobalConfig.php
397 lines (359 loc) · 12 KB
/
GlobalConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* Setting some often needed namespace prefixes
*/
EasyRdf\RdfNamespace::set('skosmos', 'http://purl.org/net/skosmos#');
EasyRdf\RdfNamespace::set('skosext', 'http://purl.org/finnonto/schema/skosext#');
EasyRdf\RdfNamespace::delete('geo');
EasyRdf\RdfNamespace::set('wgs84', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
EasyRdf\RdfNamespace::set('isothes', 'http://purl.org/iso25964/skos-thes#');
EasyRdf\RdfNamespace::set('mads', 'http://www.loc.gov/mads/rdf/v1#');
EasyRdf\RdfNamespace::set('wd', 'http://www.wikidata.org/entity/');
EasyRdf\RdfNamespace::set('wdt', 'http://www.wikidata.org/prop/direct/');
/**
* GlobalConfig provides access to the Skosmos configuration in config.ttl.
*/
class GlobalConfig extends BaseConfig
{
/** Cache reference */
private $cache;
/** Location of the configuration file. Used for caching. */
private $filePath;
/** Namespaces from vocabularies configuration file. */
private $namespaces;
/** EasyRdf\Graph graph */
private $graph;
/**
* @var int the time the config file was last modified
*/
private $configModifiedTime = null;
public function __construct(Model $model, string $config_name = '../../config.ttl')
{
$this->cache = new Cache();
$this->filePath = realpath(dirname(__FILE__) . "/" . $config_name);
if (!file_exists($this->filePath)) {
throw new Exception('config.ttl file is missing, please provide one.');
}
$resource = $this->initializeConfig();
parent::__construct($model, $resource);
}
public function getCache()
{
return $this->cache;
}
/**
* @return int the time the config file was last modified
*/
public function getConfigModifiedTime()
{
return $this->configModifiedTime;
}
/**
* Initialize configuration, reading the configuration file from the disk,
* and creating the graph and resources objects. Uses a cache if available,
* in order to avoid re-loading the complete configuration on each request.
*/
private function initializeConfig(): EasyRdf\Resource
{
// retrieve last modified time for config file (filemtime returns int|bool!)
$configModifiedTime = filemtime($this->filePath);
if (!is_bool($configModifiedTime)) {
$this->configModifiedTime = $configModifiedTime;
}
// use APC user cache to store parsed config.ttl configuration
if ($this->cache->isAvailable() && !is_null($this->configModifiedTime)) {
// @codeCoverageIgnoreStart
$key = realpath($this->filePath) . ", " . $this->configModifiedTime;
$nskey = "namespaces of " . $key;
$this->graph = $this->cache->fetch($key);
$this->namespaces = $this->cache->fetch($nskey);
if ($this->graph === false || $this->namespaces === false) { // was not found in cache
$this->parseConfig($this->filePath);
$this->cache->store($key, $this->graph);
$this->cache->store($nskey, $this->namespaces);
}
// @codeCoverageIgnoreEnd
} else { // APC not available, parse on every request
$this->parseConfig($this->filePath);
}
$this->initializeNamespaces();
$configResources = $this->graph->allOfType("skosmos:Configuration");
if (is_null($configResources) || !is_array($configResources) || count($configResources) !== 1) {
throw new Exception("config.ttl must have exactly one skosmos:Configuration");
}
return $configResources[0];
}
/**
* Parses configuration from the config.ttl file
* @param string $filename path to config.ttl file
* @throws \EasyRdf\Exception
*/
private function parseConfig($filename)
{
$this->graph = new EasyRdf\Graph();
$parser = new SkosmosTurtleParser();
$parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename);
$this->namespaces = $parser->getNamespaces();
}
/**
* Returns the graph created after parsing the configuration file.
* @return \EasyRdf\Graph
*/
public function getGraph()
{
return $this->graph;
}
/**
* Registers RDF namespaces from the config.ttl file for use by EasyRdf (e.g. serializing)
*/
private function initializeNamespaces()
{
foreach ($this->namespaces as $prefix => $fullUri) {
if ($prefix != '' && EasyRdf\RdfNamespace::get($prefix) === null) { // if not already defined
EasyRdf\RdfNamespace::set($prefix, $fullUri);
}
}
}
/**
* Returns the UI languages specified in the configuration or defaults to
* only show English
* @return array
*/
public function getLanguages()
{
$languageResources = $this->getResource()->getResource('skosmos:languages');
if (!is_null($languageResources) && !empty($languageResources)) {
$languages = array();
foreach ($languageResources as $languageResource) {
/** @var \EasyRdf\Literal $languageName */
$languageName = $languageResource->getLiteral('rdfs:label');
/** @var \EasyRdf\Literal $languageValue */
$languageValue = $languageResource->getLiteral('rdf:value');
if ($languageName && $languageValue) {
$languages[$languageName->getValue()] = $languageValue->getValue();
}
}
return $languages;
} else {
return array('en' => 'en_GB.utf8');
}
}
/**
* Returns the external HTTP request timeout in seconds or the default value
* of 5 seconds if not specified in the configuration.
* @return integer
*/
public function getHttpTimeout()
{
return $this->getLiteral('skosmos:httpTimeout', 5);
}
/**
* Returns the SPARQL HTTP request timeout in seconds or the default value
* of 20 seconds if not specified in the configuration.
* @return integer
*/
public function getSparqlTimeout()
{
return $this->getLiteral('skosmos:sparqlTimeout', 20);
}
/**
* Returns the sparql endpoint address defined in the configuration. If
* not then defaulting to http://localhost:3030/ds/sparql
* @return string
*/
public function getDefaultEndpoint()
{
$endpoint = $this->resource->get('skosmos:sparqlEndpoint');
if ($endpoint) {
return $endpoint->getUri();
} elseif (getenv('SKOSMOS_SPARQL_ENDPOINT')) {
return getenv('SKOSMOS_SPARQL_ENDPOINT');
} else {
return 'http://localhost:3030/ds/sparql';
}
}
/**
* Returns the maximum number of items to return in transitive queries if defined
* in the configuration or the default value of 1000.
* @return integer
*/
public function getDefaultTransitiveLimit()
{
return $this->getLiteral('skosmos:transitiveLimit', 1000);
}
/**
* Returns the maximum number of items to load at a time if defined
* in the configuration or the default value of 20.
* @return integer
*/
public function getSearchResultsSize()
{
return $this->getLiteral('skosmos:searchResultsSize', 20);
}
/**
* Returns the configured location for the twig template cache and if not
* defined defaults to "/tmp/skosmos-template-cache"
* @return string
*/
public function getTemplateCache()
{
return $this->getLiteral('skosmos:templateCache', '/tmp/skosmos-template-cache');
}
/**
* Returns the defined sparql-query extension eg. "JenaText" or
* if not defined falling back to SPARQL 1.1
* @return string
*/
public function getDefaultSparqlDialect()
{
return $this->getLiteral('skosmos:sparqlDialect', 'Generic');
}
/**
* Returns the feedback address defined in the configuration.
* @return string
*/
public function getFeedbackAddress()
{
return $this->getLiteral('skosmos:feedbackAddress', null);
}
/**
* Returns the feedback sender address defined in the configuration.
* @return string
*/
public function getFeedbackSender()
{
return $this->getLiteral('skosmos:feedbackSender', null);
}
/**
* Returns the feedback envelope sender address defined in the configuration.
* @return string
*/
public function getFeedbackEnvelopeSender()
{
return $this->getLiteral('skosmos:feedbackEnvelopeSender', null);
}
/**
* Returns true if exception logging has been configured.
* @return boolean
*/
public function getLogCaughtExceptions()
{
return $this->getBoolean('skosmos:logCaughtExceptions', false);
}
/**
* Returns true if browser console logging has been enabled,
* @return boolean
*/
public function getLoggingBrowserConsole()
{
return $this->getBoolean('skosmos:logBrowserConsole', false);
}
/**
* Returns the name of a log file if configured, or NULL otherwise.
* @return string
*/
public function getLoggingFilename()
{
return $this->getLiteral('skosmos:logFileName', null);
}
/**
* @return string
*/
public function getServiceName()
{
return $this->getLiteral('skosmos:serviceName', 'Skosmos');
}
/**
* Returns the long version of the service name in the requested language.
* @return string the long name of the service
*/
public function getServiceNameLong($lang)
{
$val = $this->getLiteral('skosmos:serviceNameLong', false, $lang);
if ($val === false) {
// fall back to short service name if not configured
return $this->getServiceName();
}
return $val;
}
/**
* Returns the service description in the requested language.
* @return string the description of the service
*/
public function getServiceDescription($lang)
{
return $this->getLiteral('skosmos:serviceDescription', null, $lang);
}
/**
* Returns the feedback page description in the requested language.
* @return string the description of the feedback page
*/
public function getFeedbackDescription($lang)
{
return $this->getLiteral('skosmos:feedbackDescription', null, $lang);
}
/**
* Returns the about page description in the requested language.
* @return string the description of the about page
*/
public function getAboutDescription($lang)
{
return $this->getLiteral('skosmos:aboutDescription', null, $lang);
}
/**
* @return string
*/
public function getCustomCss()
{
return $this->getLiteral('skosmos:customCss', null);
}
/**
* @return boolean
*/
public function getUiLanguageDropdown()
{
return $this->getBoolean('skosmos:uiLanguageDropdown', false);
}
/**
* @return string
*/
public function getBaseHref()
{
return $this->getLiteral('skosmos:baseHref', null);
}
/**
* @return array
*/
public function getGlobalPlugins()
{
$globalPlugins = array();
$globalPluginsResource = $this->getResource()->getResource("skosmos:globalPlugins");
if ($globalPluginsResource) {
foreach ($globalPluginsResource as $resource) {
$globalPlugins[] = $resource->getValue();
}
}
return $globalPlugins;
}
/**
* @return boolean
*/
public function getHoneypotEnabled()
{
return $this->getBoolean('skosmos:uiHoneypotEnabled', true);
}
/**
* @return integer
*/
public function getHoneypotTime()
{
return $this->getLiteral('skosmos:uiHoneypotTime', 5);
}
/**
* @return boolean
*/
public function getCollationEnabled()
{
return $this->getBoolean('skosmos:sparqlCollationEnabled', false);
}
}