Completed
Push — master ( a2b3dc...439a57 )
by Henri
14s
created

VocabularyConfig::getExtProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * VocabularyConfig provides access to the vocabulary configuration defined in vocabularies.ttl.
5
 */
6
class VocabularyConfig extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    private $plugins;
9
10
    public function __construct($resource, $globalPlugins=array())
11
    {
12
        $this->resource = $resource;
13
        $plugins = $this->resource->allLiterals('skosmos:usePlugin');
14
        $pluginArray = array();
15
        if ($plugins) {
16
            foreach ($plugins as $pluginlit) {
17
                $pluginArray[] = $pluginlit->getValue();
18
            }
19
        }
20
        $this->plugins = new PluginRegister(array_merge($globalPlugins, $pluginArray));
21
    }
22
23
    /**
24
     * Returns a boolean value based on a literal value from the vocabularies.ttl configuration.
25
     * @param string $property the property to query
26
     * @param boolean $default the default value if the value is not set in configuration
27
     */
28
    private function getBoolean($property, $default = false)
29
    {
30
        $val = $this->resource->getLiteral($property);
31
        if ($val) {
32
            return filter_var($val->getValue(), FILTER_VALIDATE_BOOLEAN);
33
        }
34
35
        return $default;
36
    }
37
38
    /**
39
     * Returns an array of URIs based on a property from the vocabularies.ttl configuration.
40
     * @param string $property the property to query
41
     * @return string[] List of URIs
42
     */
43
    private function getResources($property)
44
    {
45
        $resources = $this->resource->allResources($property);
46
        $ret = array();
47
        foreach ($resources as $res) {
48
            $ret[] = $res->getURI();
49
        }
50
51
        return $ret;
52
    }
53
54
    /**
55
     * Returns a boolean value based on a literal value from the vocabularies.ttl configuration.
56
     * @param string $property the property to query
57
     * @param string $lang preferred language for the literal,
58
     */
59
    private function getLiteral($property, $lang=null)
60
    {
61
        if (!isset($lang)) {;
62
            $lang = $this->getEnvLang();
63
        }
64
65
        $literal = $this->resource->getLiteral($property, $lang);
66
        if ($literal) {
67
            return $literal->getValue();
68
        }
69
70
        // not found with selected language, try any language
71
        $literal = $this->resource->getLiteral($property);
72
        if ($literal)
73
          return $literal->getValue();
74
    }
75
76
    /**
77
     * Get the default language of this vocabulary
78
     * @return string default language, e.g. 'en'
79
     */
80
81
    public function getDefaultLanguage()
82
    {
83
        $deflang = $this->resource->getLiteral('skosmos:defaultLanguage');
84
        if ($deflang) {
85
            return $deflang->getValue();
86
        }
87
88
        $langs = $this->getLanguages();
89
        $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric
90
        if (sizeof($langs) > 1) {
91
            trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING);
92
        }
93
94
        return $deflang;
95
    }
96
97
    /**
98
     * Wether the alphabetical index is small enough to be shown all at once.
99
     * @return boolean true if all concepts can be shown at once.
100
     */
101
    public function getAlphabeticalFull()
102
    {
103
        return $this->getBoolean('skosmos:fullAlphabeticalIndex');
104
    }
105
106
    /**
107
     * Returns a short name for a vocabulary if configured. If that has not been set
108
     * using vocabId as a fallback.
109
     * @return string
110
     */
111
    public function getShortName()
112
    {
113
        $shortname = $this->getLiteral('skosmos:shortName');
114
        if ($shortname)
115
          return $shortname;
116
117
        // if no shortname exists fall back to the id
118
        return $this->getId();
119
    }
120
121
    /**
122
     * Get the vocabulary feedback e-mail address and return it.
123
     *
124
     * @return string e-mail address or null if not defined.
125
     */
126
    public function getFeedbackRecipient()
127
    {
128
        $email = $this->resource->get('skosmos:feedbackRecipient');
129
        return isset($email) ? $email->getValue() : null;
130
    }
131
132
    /**
133
     * Returns the human readable vocabulary title.
134
     * @return string the title of the vocabulary
135
     */
136
    public function getTitle($lang = null)
137
    {
138
        return $this->getLiteral('dc:title', $lang);
139
    }
140
141
    /**
142
     * Returns a boolean value set in the vocabularies.ttl config.
143
     * @return boolean
144
     */
145
    public function sortByNotation()
146
    {
147
        return $this->getBoolean('skosmos:sortByNotation');
148
    }
149
150
    /**
151
     * Returns a boolean value set in the vocabularies.ttl config.
152
     * @return boolean
153
     */
154
    public function showChangeList()
155
    {
156
        return $this->getBoolean('skosmos:showChangeList');
157
    }
158
159
    /**
160
     * get the URLs from which the vocabulary data can be downloaded
161
     * @return array Array with MIME type as key, URL as value
162
     */
163
    public function getDataURLs()
164
    {
165
        $ret = array();
166
        $urls = $this->resource->allResources("void:dataDump");
167
        foreach ($urls as $url) {
168
            // first try dc:format and dc11:format
169
            $mimetypelit = $url->getLiteral('dc:format');
170
            if ($mimetypelit === null) {
171
                $mimetypelit = $url->getLiteral('dc11:format');
172
            }
173
174
            // if still not found, guess MIME type using file extension
175
            if ($mimetypelit !== null) {
176
                $mimetype = $mimetypelit->getValue();
177
            } else {
178
                $format = EasyRdf\Format::guessFormat(null, $url->getURI());
179
                if ($format === null) {
180
                    trigger_error("Could not guess format for <$url>.", E_USER_WARNING);
181
                    continue;
182
                }
183
                $mimetypes = array_keys($format->getMimeTypes());
184
                $mimetype = $mimetypes[0];
185
            }
186
            $ret[$mimetype] = $url->getURI();
187
        }
188
        return $ret;
189
    }
190
191
    /**
192
     * Returns the main Concept Scheme URI of that Vocabulary,
193
     * or null if not set.
194
     * @return string concept scheme URI or null
195
     */
196
197
    public function getMainConceptSchemeURI()
198
    {
199
        $val = $this->resource->getResource("skosmos:mainConceptScheme");
200
        if ($val) {
201
            return $val->getURI();
202
        }
203
204
        return null;
205
    }
206
207
    /**
208
     * Returns the class URI used for concept groups in this vocabulary,
209
     * or null if not set.
210
     * @return string group class URI or null
211
     */
212
213
    public function getGroupClassURI()
214
    {
215
        $val = $this->resource->getResource("skosmos:groupClass");
216
        if ($val) {
217
            return $val->getURI();
218
        }
219
220
        return null;
221
    }
222
223
    /**
224
     * Returns the class URI used for thesaurus arrays in this vocabulary,
225
     * or null if not set.
226
     * @return string array class URI or null
227
     */
228
229
    public function getArrayClassURI()
230
    {
231
        $val = $this->resource->getResource("skosmos:arrayClass");
232
        if ($val) {
233
            return $val->getURI();
234
        }
235
236
        return null;
237
    }
238
239
    /**
240
     * Returns custom properties displayed on the search page if configured.
241
     * @return string array class URI or null
242
     */
243
244 View Code Duplication
    public function getAdditionalSearchProperties()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
    {
246
        $resources = $this->resource->allResources("skosmos:showPropertyInSearch");
247
        $ret = array();
248
        foreach ($resources as $res) {
249
            $prop = $res->getURI();
250
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible
251
            {
252
                $prop = EasyRdf\RdfNamespace::shorten($prop);
253
            }
254
255
            $ret[] = $prop;
256
        }
257
        return $ret;
258
    }
259
260
    /**
261
     * Queries whether the property should be shown with all the label language variations.
262
     * @param string $property
263
     * @return boolean
264
     */
265
    public function hasMultiLingualProperty($property)
266
    {
267
        $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty");
268
        foreach ($resources as $res) {
269
            $prop = $res->getURI();
270
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible
271
            {
272
                $prop = EasyRdf\RdfNamespace::shorten($prop);
273
            }
274
275
            if ($prop === $property) {
276
                return true;
277
            }
278
279
        }
280
        return false;
281
    }
282
283
    /**
284
     * Returns a boolean value set in the vocabularies.ttl config.
285
     * @return boolean
286
     */
287
    public function getShowHierarchy()
288
    {
289
        return $this->getBoolean('skosmos:showTopConcepts');
290
    }
291
292
    /**
293
     * Returns a boolean value set in the vocabularies.ttl config.
294
     * @return boolean
295
     */
296
    public function showConceptSchemesInHierarchy()
297
    {
298
        return $this->getBoolean('skosmos:conceptSchemesInHierarchy');
299
    }
300
301
    /**
302
     * Returns a boolean value set in the vocabularies.ttl config.
303
     * @return boolean defaults to true if fetching hasn't been explicitly denied.
304
     */
305
    public function getExternalResourcesLoading()
306
    {
307
        return $this->getBoolean('skosmos:loadExternalResources', true);
308
    }
309
310
    /**
311
     * Returns a boolean value set in the vocabularies.ttl config.
312
     * @return boolean
313
     */
314
    public function getShowLangCodes()
315
    {
316
        return $this->getBoolean('skosmos:explicitLanguageTags');
317
    }
318
319
    /**
320
     * Returns a boolean value set in the vocabularies.ttl config.
321
     * @return array array of concept class URIs (can be empty)
322
     */
323
    public function getIndexClasses()
324
    {
325
        return $this->getResources("skosmos:indexShowClass");
326
    }
327
328
    /**
329
     * Returns skosmos:externalProperty values set in the vocabularies.ttl config.
330
     * @return array array of external property URIs (can be empty)
331
     */
332
    public function getExtProperties()
333
    {
334
        return $this->getResources("skosmos:externalProperty");
335
    }
336
337
    /**
338
     * Get the languages supported by this vocabulary
339
     * @return array languages supported by this vocabulary (as language tag strings)
340
     */
341
    public function getLanguages()
342
    {
343
        $langs = $this->resource->allLiterals('skosmos:language');
344
        $ret = array();
345
        foreach ($langs as $lang) {
346
            $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang());
347
            $ret[$langlit] = $lang->getValue();
348
        }
349
        ksort($ret);
350
351
        return $ret;
352
    }
353
354
    /**
355
     * Returns the vocabulary default sidebar view.
356
     * @return string name of the view
357
     */
358
    public function getDefaultSidebarView()
359
    {
360
        $defview = $this->resource->getLiteral('skosmos:defaultSidebarView');
361
        if ($defview) {
362
            $value = $defview->getValue();
363
            if ($value === 'groups' || $value === 'hierarchy') {
364
                return $value;
365
            }
366
367
        }
368
        if ($this->showAlphabeticalIndex() === false) {
369
            if ($this->getShowHierarchy()) {
370
                return 'hierarchy';
371
            } else if ($this->getGroupClassURI()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getGroupClassURI() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
372
                return 'groups';
373
            }
374
        }
375
        return 'alphabetical'; // if not defined displaying the alphabetical index
376
    }
377
378
    /**
379
     * Extracts the vocabulary id string from the baseuri of the vocabulary.
380
     * @return string identifier eg. 'mesh'.
381
     */
382
    public function getId()
383
    {
384
        $uriparts = explode("#", $this->resource->getURI());
385
        if (count($uriparts) != 1)
386
        // hash namespace
387
        {
388
            return $uriparts[1];
389
        }
390
391
        // slash namespace
392
        $uriparts = explode("/", $this->resource->getURI());
393
394
        return $uriparts[count($uriparts) - 1];
395
    }
396
397
    public function getShowStatistics() {
398
        return $this->getBoolean('skosmos:showStatistics', true);
399
    }
400
401
    public function getPlugins()
402
    {
403
        return $this->plugins;
404
    }
405
406
    /**
407
     * Returns the property/properties used for visualizing concept hierarchies.
408
     * @return string array class URI or null
409
     */
410
411 View Code Duplication
    public function getHierarchyProperty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
412
    {
413
        $resources = $this->resource->allResources("skosmos:hierarchyProperty");
414
        $ret = array();
415
        foreach ($resources as $res) {
416
            $prop = $res->getURI();
417
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // prefixing if possible
418
            {
419
                $prop = EasyRdf\RdfNamespace::shorten($prop);
420
            }
421
422
            $ret[] = $prop;
423
        }
424
        return empty($ret) ? array('skos:broader') : $ret;
425
    }
426
427
    /**
428
     * Returns a boolean value set in the vocabularies.ttl config.
429
     * @return boolean
430
     */
431
    public function showNotation()
432
    {
433
        return $this->getBoolean('skosmos:showNotation', true);
434
    }
435
436
    /**
437
     * Returns a boolean value set in the vocabularies.ttl config.
438
     * @return boolean
439
     */
440
    public function showAlphabeticalIndex()
441
    {
442
        return $this->getBoolean('skosmos:showAlphabeticalIndex', true);
443
    }
444
445
    /**
446
     * Returns a boolean value set in the vocabularies.ttl config.
447
     * @return boolean
448
     */
449
    public function getShowDeprecated()
450
    {
451
        return $this->getBoolean('skosmos:showDeprecated', false);
452
    }
453
454
    /**
455
     * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration.
456
     * @return array of objects or an empty array
457
     */
458
    public function getTypes($lang = null)
459
    {
460
        $resources = $this->resource->allResources("dc:type");
461
        $ret = array();
462
        foreach ($resources as $res) {
463
            $prop = $res->getURI();
464
            $label = $res->label($lang) ? $res->label($lang) : $res->label($this->getDefaultLanguage());
465
            $ret[] = array('uri' => $prop, 'prefLabel' =>  $label->getValue());
466
        }
467
        return $ret;
468
    }
469
470
    /**
471
     * Returns an array of fallback languages that is ordered by priority and
472
     * defined in the vocabulary configuration as a collection.
473
     * Additionally, the chosen content language is inserted with the highest priority
474
     * and the vocab default language is inserted with the lowest priority.
475
     * @param string $clang
476
     * @return array of language code strings
477
     */
478
    public function getLanguageOrder($clang)
479
    {
480
        $ret = array($clang);
481
        $fallbacks = !empty($this->resource->get('skosmos:fallbackLanguages')) ? $this->resource->get('skosmos:fallbackLanguages') : array();
482
        foreach ($fallbacks as $lang) {
483
            if (!in_array($lang, $ret)) {
484
                $ret[] = (string)$lang; // Literal to string conversion
485
            }
486
        }
487
        if (!in_array($this->getDefaultLanguage(), $ret)) {
488
            $ret[] = (string)$this->getDefaultLanguage();
489
        }
490
        return $ret;
491
    }
492
}
493