Completed
Push — master ( 4ef44e...f0613e )
by Henri
03:01
created

VocabularyConfig::getLanguageOrder()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 12
nop 1
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 a boolean value based on a literal value from the vocabularies.ttl configuration.
40
     * @param string $property the property to query
41
     * @param string $lang preferred language for the literal,
42
     */
43
    private function getLiteral($property, $lang=null)
44
    {
45
        if (!isset($lang)) {;
46
            $lang = $this->getEnvLang();
47
        }
48
49
        $literal = $this->resource->getLiteral($property, $lang);
50
        if ($literal) {
51
            return $literal->getValue();
52
        }
53
54
        // not found with selected language, try any language
55
        $literal = $this->resource->getLiteral($property);
56
        if ($literal)
57
          return $literal->getValue();
58
    }
59
60
    /**
61
     * Get the default language of this vocabulary
62
     * @return string default language, e.g. 'en'
63
     */
64
65
    public function getDefaultLanguage()
66
    {
67
        $deflang = $this->resource->getLiteral('skosmos:defaultLanguage');
68
        if ($deflang) {
69
            return $deflang->getValue();
70
        }
71
72
        $langs = $this->getLanguages();
73
        $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric
74
        if (sizeof($langs) > 1) {
75
            trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING);
76
        }
77
78
        return $deflang;
79
    }
80
81
    /**
82
     * Wether the alphabetical index is small enough to be shown all at once.
83
     * @return boolean true if all concepts can be shown at once.
84
     */
85
    public function getAlphabeticalFull()
86
    {
87
        return $this->getBoolean('skosmos:fullAlphabeticalIndex');
88
    }
89
90
    /**
91
     * Returns a short name for a vocabulary if configured. If that has not been set
92
     * using vocabId as a fallback.
93
     * @return string
94
     */
95
    public function getShortName()
96
    {
97
        $shortname = $this->getLiteral('skosmos:shortName');
98
        if ($shortname) 
99
          return $shortname;
100
101
        // if no shortname exists fall back to the id
102
        return $this->getId();
103
    }
104
105
    /**
106
     * Get the vocabulary feedback e-mail address and return it.
107
     *
108
     * @return string e-mail address or null if not defined.
109
     */
110
    public function getFeedbackRecipient()
111
    {
112
        $email = $this->resource->get('skosmos:feedbackRecipient');
113
        return isset($email) ? $email->getValue() : null;
114
    }
115
116
    /**
117
     * Returns the human readable vocabulary title.
118
     * @return string the title of the vocabulary
119
     */
120
    public function getTitle($lang = null)
121
    {
122
        return $this->getLiteral('dc:title', $lang);
123
    }
124
125
    /**
126
     * Returns a boolean value set in the vocabularies.ttl config.
127
     * @return boolean
128
     */
129
    public function sortByNotation()
130
    {
131
        return $this->getBoolean('skosmos:sortByNotation');
132
    }
133
    
134
    /**
135
     * Returns a boolean value set in the vocabularies.ttl config.
136
     * @return boolean
137
     */
138
    public function showChangeList()
139
    {
140
        return $this->getBoolean('skosmos:showChangeList');
141
    }
142
143
    /**
144
     * get the URLs from which the vocabulary data can be downloaded
145
     * @return array Array with MIME type as key, URL as value
146
     */
147
    public function getDataURLs()
148
    {
149
        $ret = array();
150
        $urls = $this->resource->allResources("void:dataDump");
151
        foreach ($urls as $url) {
152
            // first try dc:format and dc11:format
153
            $mimetypelit = $url->getLiteral('dc:format');
154
            if ($mimetypelit === null) {
155
                $mimetypelit = $url->getLiteral('dc11:format');
156
            }
157
158
            // if still not found, guess MIME type using file extension
159
            if ($mimetypelit !== null) {
160
                $mimetype = $mimetypelit->getValue();
161
            } else {
162
                $format = EasyRdf\Format::guessFormat(null, $url->getURI());
163
                if ($format === null) {
164
                    trigger_error("Could not guess format for <$url>.", E_USER_WARNING);
165
                    continue;
166
                }
167
                $mimetypes = array_keys($format->getMimeTypes());
168
                $mimetype = $mimetypes[0];
169
            }
170
            $ret[$mimetype] = $url->getURI();
171
        }
172
        return $ret;
173
    }
174
175
    /**
176
     * Returns the main Concept Scheme URI of that Vocabulary,
177
     * or null if not set.
178
     * @return string concept scheme URI or null
179
     */
180
    
181
    public function getMainConceptSchemeURI()
182
    {
183
        $val = $this->resource->getResource("skosmos:mainConceptScheme");
184
        if ($val) {
185
            return $val->getURI();
186
        }
187
        
188
        return null;
189
    }
190
    
191
    
192
    
193
    /**
194
     * Returns the class URI used for concept groups in this vocabulary,
195
     * or null if not set.
196
     * @return string group class URI or null
197
     */
198
199
    public function getGroupClassURI()
200
    {
201
        $val = $this->resource->getResource("skosmos:groupClass");
202
        if ($val) {
203
            return $val->getURI();
204
        }
205
206
        return null;
207
    }
208
209
    /**
210
     * Returns the class URI used for thesaurus arrays in this vocabulary,
211
     * or null if not set.
212
     * @return string array class URI or null
213
     */
214
215
    public function getArrayClassURI()
216
    {
217
        $val = $this->resource->getResource("skosmos:arrayClass");
218
        if ($val) {
219
            return $val->getURI();
220
        }
221
222
        return null;
223
    }
224
225
    /**
226
     * Returns custom properties displayed on the search page if configured.
227
     * @return string array class URI or null
228
     */
229
230 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...
231
    {
232
        $resources = $this->resource->allResources("skosmos:showPropertyInSearch");
233
        $ret = array();
234
        foreach ($resources as $res) {
235
            $prop = $res->getURI();
236
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible
237
            {
238
                $prop = EasyRdf\RdfNamespace::shorten($prop);
239
            }
240
241
            $ret[] = $prop;
242
        }
243
        return $ret;
244
    }
245
246
    /**
247
     * Queries whether the property should be shown with all the label language variations.
248
     * @param string $property
249
     * @return boolean
250
     */
251
    public function hasMultiLingualProperty($property)
252
    {
253
        $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty");
254
        foreach ($resources as $res) {
255
            $prop = $res->getURI();
256
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible
257
            {
258
                $prop = EasyRdf\RdfNamespace::shorten($prop);
259
            }
260
261
            if ($prop === $property) {
262
                return true;
263
            }
264
265
        }
266
        return false;
267
    }
268
269
    /**
270
     * Returns a boolean value set in the vocabularies.ttl config.
271
     * @return boolean
272
     */
273
    public function getShowHierarchy()
274
    {
275
        return $this->getBoolean('skosmos:showTopConcepts');
276
    }
277
278
    /**
279
     * Returns a boolean value set in the vocabularies.ttl config.
280
     * @return boolean
281
     */
282
    public function showConceptSchemesInHierarchy()
283
    {
284
        return $this->getBoolean('skosmos:conceptSchemesInHierarchy');
285
    }
286
287
    /**
288
     * Returns a boolean value set in the vocabularies.ttl config.
289
     * @return boolean defaults to true if fetching hasn't been explicitly denied.
290
     */
291
    public function getExternalResourcesLoading()
292
    {
293
        return $this->getBoolean('skosmos:loadExternalResources', true);
294
    }
295
296
    /**
297
     * Returns a boolean value set in the vocabularies.ttl config.
298
     * @return boolean
299
     */
300
    public function getShowLangCodes()
301
    {
302
        return $this->getBoolean('skosmos:explicitLanguageTags');
303
    }
304
305
    /**
306
     * Returns a boolean value set in the vocabularies.ttl config.
307
     * @return array array of concept class URIs (can be empty)
308
     */
309
    public function getIndexClasses()
310
    {
311
        $resources = $this->resource->allResources("skosmos:indexShowClass");
312
        $ret = array();
313
        foreach ($resources as $res) {
314
            $ret[] = $res->getURI();
315
        }
316
        return $ret;
317
    }
318
319
    /**
320
     * Get the languages supported by this vocabulary
321
     * @return array languages supported by this vocabulary (as language tag strings)
322
     */
323
    public function getLanguages()
324
    {
325
        $langs = $this->resource->allLiterals('skosmos:language');
326
        $ret = array();
327
        foreach ($langs as $lang) {
328
            $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang());
329
            $ret[$langlit] = $lang->getValue();
330
        }
331
        ksort($ret);
332
333
        return $ret;
334
    }
335
336
    /**
337
     * Returns the vocabulary default sidebar view.
338
     * @return string name of the view
339
     */
340
    public function getDefaultSidebarView()
341
    {
342
        $defview = $this->resource->getLiteral('skosmos:defaultSidebarView');
343
        if ($defview) {
344
            $value = $defview->getValue();
345
            if ($value === 'groups' || $value === 'hierarchy') {
346
                return $value;
347
            }
348
349
        }
350
        if ($this->showAlphabeticalIndex() === false) { 
351
            if ($this->getShowHierarchy()) {
352
                return 'hierarchy';
353
            } 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...
354
                return 'groups';
355
            }
356
        }
357
        return 'alphabetical'; // if not defined displaying the alphabetical index
358
    }
359
360
    /**
361
     * Extracts the vocabulary id string from the baseuri of the vocabulary.
362
     * @return string identifier eg. 'mesh'.
363
     */
364
    public function getId()
365
    {
366
        $uriparts = explode("#", $this->resource->getURI());
367
        if (count($uriparts) != 1)
368
        // hash namespace
369
        {
370
            return $uriparts[1];
371
        }
372
373
        // slash namespace
374
        $uriparts = explode("/", $this->resource->getURI());
375
376
        return $uriparts[count($uriparts) - 1];
377
    }
378
379
    public function getShowStatistics() {
380
        return $this->getBoolean('skosmos:showStatistics', true);
381
    }
382
383
    public function getPlugins()
384
    {
385
        return $this->plugins;
386
    }
387
388
    /**
389
     * Returns the property/properties used for visualizing concept hierarchies.
390
     * @return string array class URI or null
391
     */
392
393 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...
394
    {
395
        $resources = $this->resource->allResources("skosmos:hierarchyProperty");
396
        $ret = array();
397
        foreach ($resources as $res) {
398
            $prop = $res->getURI();
399
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // prefixing if possible 
400
            {
401
                $prop = EasyRdf\RdfNamespace::shorten($prop);
402
            }
403
404
            $ret[] = $prop;
405
        }
406
        return empty($ret) ? array('skos:broader') : $ret;
407
    }
408
409
    /**
410
     * Returns a boolean value set in the vocabularies.ttl config.
411
     * @return boolean
412
     */
413
    public function showNotation()
414
    {
415
        return $this->getBoolean('skosmos:showNotation', true);
416
    }
417
418
    /**
419
     * Returns a boolean value set in the vocabularies.ttl config.
420
     * @return boolean
421
     */
422
    public function showAlphabeticalIndex()
423
    {
424
        return $this->getBoolean('skosmos:showAlphabeticalIndex', true);
425
    }
426
    
427
    /**
428
     * Returns a boolean value set in the vocabularies.ttl config.
429
     * @return boolean
430
     */
431
    public function getShowDeprecated()
432
    {
433
        return $this->getBoolean('skosmos:showDeprecated', false);
434
    }
435
436
    /**
437
     * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration.
438
     * @return array of objects or an empty array
439
     */
440
    public function getTypes($lang = null)
441
    {
442
        $resources = $this->resource->allResources("dc:type");
443
        $ret = array();
444
        foreach ($resources as $res) {
445
            $prop = $res->getURI();
446
            $label = $res->label($lang) ? $res->label($lang) : $res->label($this->getDefaultLanguage());
447
            $ret[] = array('uri' => $prop, 'prefLabel' =>  $label->getValue());
448
        }
449
        return $ret;
450
    }
451
452
    /**
453
     * Returns an array of fallback languages that is ordered by priority and
454
     * defined in the vocabulary configuration as a collection.
455
     * Additionally, the chosen content language is inserted with the highest priority
456
     * and the vocab default language is inserted with the lowest priority.
457
     * @param string $clang
458
     * @return array of language code strings
459
     */
460
    public function getLanguageOrder($clang)
461
    {
462
        $ret = array($clang);
463
        $fallbacks = !empty($this->resource->get('skosmos:fallbackLanguages')) ? $this->resource->get('skosmos:fallbackLanguages') : array();
464
        foreach ($fallbacks as $lang) {
465
            if (!in_array($lang, $ret)) {
466
                $ret[] = (string)$lang; // Literal to string conversion
467
            }
468
        }
469
        if (!in_array($this->getDefaultLanguage(), $ret)) {
470
            $ret[] = (string)$this->getDefaultLanguage();
471
        }
472
        return $ret;
473
    }
474
}
475