Passed
Pull Request — main (#1863)
by
unknown
03:47
created

GlobalConfig::getServiceNameLong()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Setting some often needed namespace prefixes
5
 */
6
EasyRdf\RdfNamespace::set('skosmos', 'http://purl.org/net/skosmos#');
7
EasyRdf\RdfNamespace::set('skosext', 'http://purl.org/finnonto/schema/skosext#');
8
EasyRdf\RdfNamespace::delete('geo');
9
EasyRdf\RdfNamespace::set('wgs84', 'http://www.w3.org/2003/01/geo/wgs84_pos#');
10
EasyRdf\RdfNamespace::set('isothes', 'http://purl.org/iso25964/skos-thes#');
11
EasyRdf\RdfNamespace::set('mads', 'http://www.loc.gov/mads/rdf/v1#');
12
EasyRdf\RdfNamespace::set('wd', 'http://www.wikidata.org/entity/');
13
EasyRdf\RdfNamespace::set('wdt', 'http://www.wikidata.org/prop/direct/');
14
15
/**
16
 * GlobalConfig provides access to the Skosmos configuration in config.ttl.
17
 */
18
class GlobalConfig extends BaseConfig
19
{
20
    /** Cache reference */
21
    private $cache;
22
    /** Location of the configuration file. Used for caching. */
23
    private $filePath;
24
    /** Namespaces from vocabularies configuration file. */
25
    private $namespaces;
26
    /** EasyRdf\Graph graph */
27
    private $graph;
28
    /**
29
     * @var int the time the config file was last modified
30
     */
31
    private $configModifiedTime = null;
32
33
    public function __construct(Model $model, string $config_name = '../../config.ttl')
34
    {
35
        $this->cache = new Cache();
36
        $this->filePath = realpath(dirname(__FILE__) . "/" . $config_name);
37
        if (!file_exists($this->filePath)) {
38
            throw new Exception('config.ttl file is missing, please provide one.');
39
        }
40
        $resource = $this->initializeConfig();
41
        parent::__construct($model, $resource);
42
    }
43
44
    public function getCache()
45
    {
46
        return $this->cache;
47
    }
48
49
    /**
50
     * @return int the time the config file was last modified
51
     */
52
    public function getConfigModifiedTime()
53
    {
54
        return $this->configModifiedTime;
55
    }
56
57
    /**
58
     * Initialize configuration, reading the configuration file from the disk,
59
     * and creating the graph and resources objects. Uses a cache if available,
60
     * in order to avoid re-loading the complete configuration on each request.
61
     */
62
    private function initializeConfig(): EasyRdf\Resource
63
    {
64
        // retrieve last modified time for config file (filemtime returns int|bool!)
65
        $configModifiedTime = filemtime($this->filePath);
66
        if (!is_bool($configModifiedTime)) {
0 ignored issues
show
introduced by
The condition is_bool($configModifiedTime) is always false.
Loading history...
67
            $this->configModifiedTime = $configModifiedTime;
68
        }
69
        // use APC user cache to store parsed config.ttl configuration
70
        if ($this->cache->isAvailable() && !is_null($this->configModifiedTime)) {
71
            // @codeCoverageIgnoreStart
72
            $key = realpath($this->filePath) . ", " . $this->configModifiedTime;
73
            $nskey = "namespaces of " . $key;
74
            $this->graph = $this->cache->fetch($key);
75
            $this->namespaces = $this->cache->fetch($nskey);
76
            if ($this->graph === false || $this->namespaces === false) { // was not found in cache
77
                $this->parseConfig($this->filePath);
78
                $this->cache->store($key, $this->graph);
79
                $this->cache->store($nskey, $this->namespaces);
80
            }
81
            // @codeCoverageIgnoreEnd
82
        } else { // APC not available, parse on every request
83
            $this->parseConfig($this->filePath);
84
        }
85
        $this->initializeNamespaces();
86
87
        $configResources = $this->graph->allOfType("skosmos:Configuration");
88
        if (is_null($configResources) || !is_array($configResources) || count($configResources) !== 1) {
0 ignored issues
show
introduced by
The condition is_array($configResources) is always true.
Loading history...
89
            throw new Exception("config.ttl must have exactly one skosmos:Configuration");
90
        }
91
        return $configResources[0];
92
    }
93
94
    /**
95
     * Parses configuration from the config.ttl file
96
     * @param string $filename path to config.ttl file
97
     * @throws \EasyRdf\Exception
98
     */
99
    private function parseConfig($filename)
100
    {
101
        $this->graph = new EasyRdf\Graph();
102
        $parser = new SkosmosTurtleParser();
103
        $parser->parse($this->graph, file_get_contents($filename), 'turtle', $filename);
104
        $this->namespaces = $parser->getNamespaces();
105
    }
106
107
    /**
108
     * Returns the graph created after parsing the configuration file.
109
     * @return \EasyRdf\Graph
110
     */
111
    public function getGraph()
112
    {
113
        return $this->graph;
114
    }
115
116
    /**
117
     * Registers RDF namespaces from the config.ttl file for use by EasyRdf (e.g. serializing)
118
     */
119
    private function initializeNamespaces()
120
    {
121
        foreach ($this->namespaces as $prefix => $fullUri) {
122
            if ($prefix != '' && EasyRdf\RdfNamespace::get($prefix) === null) { // if not already defined
123
                EasyRdf\RdfNamespace::set($prefix, $fullUri);
124
            }
125
        }
126
    }
127
128
    /**
129
     * Returns the UI languages specified in the configuration or defaults to
130
     * only show English
131
     * @return array
132
     */
133
    public function getLanguages()
134
    {
135
        $languageResources = $this->getResource()->getResource('skosmos:languages');
136
        if (!is_null($languageResources) && !empty($languageResources)) {
137
            $languages = array();
138
            foreach ($languageResources as $languageResource) {
139
                /** @var \EasyRdf\Literal $languageName */
140
                $languageName = $languageResource->getLiteral('rdfs:label');
141
                /** @var \EasyRdf\Literal $languageValue */
142
                $languageValue = $languageResource->getLiteral('rdf:value');
143
                if ($languageName && $languageValue) {
144
                    $languages[$languageName->getValue()] = $languageValue->getValue();
145
                }
146
            }
147
            return $languages;
148
        } else {
149
            return array('en' => 'en_GB.utf8');
150
        }
151
    }
152
153
    /**
154
     * Returns the external HTTP request timeout in seconds or the default value
155
     * of 5 seconds if not specified in the configuration.
156
     * @return integer
157
     */
158
    public function getHttpTimeout()
159
    {
160
        return $this->getLiteral('skosmos:httpTimeout', 5);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLiteral...kosmos:httpTimeout', 5) returns the type string which is incompatible with the documented return type integer.
Loading history...
161
    }
162
163
    /**
164
     * Returns the SPARQL HTTP request timeout in seconds or the default value
165
     * of 20 seconds if not specified in the configuration.
166
     * @return integer
167
     */
168
    public function getSparqlTimeout()
169
    {
170
        return $this->getLiteral('skosmos:sparqlTimeout', 20);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLiteral...mos:sparqlTimeout', 20) returns the type string which is incompatible with the documented return type integer.
Loading history...
171
    }
172
173
    /**
174
     * Returns the sparql endpoint address defined in the configuration. If
175
     * not then defaulting to http://localhost:3030/ds/sparql
176
     * @return string
177
     */
178
    public function getDefaultEndpoint()
179
    {
180
        $endpoint = $this->resource->get('skosmos:sparqlEndpoint');
181
        if ($endpoint) {
182
            return $endpoint->getUri();
183
        } elseif (getenv('SKOSMOS_SPARQL_ENDPOINT')) {
184
            return getenv('SKOSMOS_SPARQL_ENDPOINT');
185
        } else {
186
            return 'http://localhost:3030/ds/sparql';
187
        }
188
    }
189
190
    /**
191
     * Returns the maximum number of items to return in transitive queries if defined
192
     * in the configuration or the default value of 1000.
193
     * @return integer
194
     */
195
    public function getDefaultTransitiveLimit()
196
    {
197
        return $this->getLiteral('skosmos:transitiveLimit', 1000);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLiteral...transitiveLimit', 1000) returns the type string which is incompatible with the documented return type integer.
Loading history...
198
    }
199
200
    /**
201
     * Returns the maximum number of items to parse in RDF lists if defined
202
     * in the configuration or the default value of 1000.
203
     * A value of 0 means no limit (use with caution).
204
     * @return integer
205
     */
206
    public function getRdfListItemsLimit()
207
    {
208
        return $this->getLiteral('skosmos:rdfListItemsLimit', 1000);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLiteral...fListItemsLimit', 1000) returns the type string which is incompatible with the documented return type integer.
Loading history...
209
    }
210
211
    /**
212
     * Returns the maximum number of items to load at a time if defined
213
     * in the configuration or the default value of 20.
214
     * @return integer
215
     */
216
    public function getSearchResultsSize()
217
    {
218
        return $this->getLiteral('skosmos:searchResultsSize', 20);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLiteral...searchResultsSize', 20) returns the type string which is incompatible with the documented return type integer.
Loading history...
219
    }
220
221
    /**
222
     * Returns the configured location for the twig template cache and if not
223
     * defined defaults to "/tmp/skosmos-template-cache"
224
     * @return string
225
     */
226
    public function getTemplateCache()
227
    {
228
        return $this->getLiteral('skosmos:templateCache', '/tmp/skosmos-template-cache');
229
    }
230
231
    /**
232
     * Returns the defined sparql-query extension eg. "JenaText" or
233
     * if not defined falling back to SPARQL 1.1
234
     * @return string
235
     */
236
    public function getDefaultSparqlDialect()
237
    {
238
        return $this->getLiteral('skosmos:sparqlDialect', 'Generic');
239
    }
240
241
    /**
242
     * Returns the feedback address defined in the configuration.
243
     * @return string
244
     */
245
    public function getFeedbackAddress()
246
    {
247
        return $this->getLiteral('skosmos:feedbackAddress', null);
248
    }
249
250
    /**
251
     * Returns the feedback sender address defined in the configuration.
252
     * @return string
253
     */
254
    public function getFeedbackSender()
255
    {
256
        return $this->getLiteral('skosmos:feedbackSender', null);
257
    }
258
259
    /**
260
     * Returns the feedback envelope sender address defined in the configuration.
261
     * @return string
262
     */
263
    public function getFeedbackEnvelopeSender()
264
    {
265
        return $this->getLiteral('skosmos:feedbackEnvelopeSender', null);
266
    }
267
268
    /**
269
     * Returns true if exception logging has been configured.
270
     * @return boolean
271
     */
272
    public function getLogCaughtExceptions()
273
    {
274
        return $this->getBoolean('skosmos:logCaughtExceptions', false);
275
    }
276
277
    /**
278
     * Returns true if browser console logging has been enabled,
279
     * @return boolean
280
     */
281
    public function getLoggingBrowserConsole()
282
    {
283
        return $this->getBoolean('skosmos:logBrowserConsole', false);
284
    }
285
286
    /**
287
     * Returns the name of a log file if configured, or NULL otherwise.
288
     * @return string
289
     */
290
    public function getLoggingFilename()
291
    {
292
        return $this->getLiteral('skosmos:logFileName', null);
293
    }
294
295
    /**
296
     * @return string
297
     */
298
    public function getServiceName()
299
    {
300
        return $this->getLiteral('skosmos:serviceName', 'Skosmos');
301
    }
302
303
    /**
304
     * Returns the long version of the service name in the requested language.
305
     * @return string the long name of the service
306
     */
307
    public function getServiceNameLong($lang)
308
    {
309
        $val = $this->getLiteral('skosmos:serviceNameLong', false, $lang);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of BaseConfig::getLiteral(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

309
        $val = $this->getLiteral('skosmos:serviceNameLong', /** @scrutinizer ignore-type */ false, $lang);
Loading history...
310
311
        if ($val === false) {
0 ignored issues
show
introduced by
The condition $val === false is always false.
Loading history...
312
            // fall back to short service name if not configured
313
            return $this->getServiceName();
314
        }
315
316
        return $val;
317
    }
318
319
    /**
320
     * Returns the service description in the requested language.
321
     * @return string the description of the service
322
     */
323
    public function getServiceDescription($lang)
324
    {
325
        return $this->getLiteral('skosmos:serviceDescription', null, $lang);
326
    }
327
328
    /**
329
     * Returns the feedback page description in the requested language.
330
     * @return string the description of the feedback page
331
     */
332
    public function getFeedbackDescription($lang)
333
    {
334
        return $this->getLiteral('skosmos:feedbackDescription', null, $lang);
335
    }
336
337
    /**
338
     * Returns the about page description in the requested language.
339
     * @return string the description of the about page
340
     */
341
    public function getAboutDescription($lang)
342
    {
343
        return $this->getLiteral('skosmos:aboutDescription', null, $lang);
344
    }
345
346
    /**
347
     * @return string
348
     */
349
    public function getCustomCss()
350
    {
351
        return $this->getLiteral('skosmos:customCss', null);
352
    }
353
354
    /**
355
     * @return boolean
356
     */
357
    public function getUiLanguageDropdown()
358
    {
359
        return $this->getBoolean('skosmos:uiLanguageDropdown', false);
360
    }
361
362
    /**
363
     * @return boolean
364
     */
365
    public function getUiDevMode()
366
    {
367
        return $this->getBoolean('skosmos:uiDevMode', false);
368
    }
369
370
    /**
371
     * @return string
372
     */
373
    public function getBaseHref()
374
    {
375
        return $this->getLiteral('skosmos:baseHref', null);
376
    }
377
378
    /**
379
     * @return array
380
     */
381
    public function getGlobalPlugins()
382
    {
383
        $globalPlugins = array();
384
        $globalPluginsResource =  $this->getResource()->getResource("skosmos:globalPlugins");
385
        if ($globalPluginsResource) {
0 ignored issues
show
introduced by
$globalPluginsResource is of type EasyRdf\Resource, thus it always evaluated to true.
Loading history...
386
            foreach ($globalPluginsResource as $resource) {
387
                $globalPlugins[] = $resource->getValue();
388
            }
389
        }
390
        return $globalPlugins;
391
    }
392
393
    /**
394
     * @return boolean
395
     */
396
    public function getHoneypotEnabled()
397
    {
398
        return $this->getBoolean('skosmos:feedbackHoneypotEnabled', true);
399
    }
400
401
    /**
402
     * @return integer
403
     */
404
    public function getHoneypotTime()
405
    {
406
        return $this->getLiteral('skosmos:feedbackHoneypotTime', 5);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getLiteral...edbackHoneypotTime', 5) returns the type string which is incompatible with the documented return type integer.
Loading history...
407
    }
408
409
    /**
410
     * @return boolean
411
     */
412
    public function getCollationEnabled()
413
    {
414
        return $this->getBoolean('skosmos:sparqlCollationEnabled', false);
415
    }
416
}
417