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