Completed
Push — master ( 835fd2...95e077 )
by
unknown
15s queued 13s
created

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