Completed
Push — master ( 650298...bfb300 )
by Osma
02:33
created

GlobalConfig::getConfigModifiedTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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