Completed
Push — master ( a56b99...4d505a )
by
unknown
02:02 queued 11s
created

VocabularyConfig::searchByNotation()   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
 * VocabularyConfig provides access to the vocabulary configuration defined in config.ttl.
5
 */
6
class VocabularyConfig extends BaseConfig
7
{
8
    private $plugins;
9
    private $languageOrderCache = array();
10
11
    public function __construct($resource, $globalPlugins=array())
12
    {
13
        $this->resource = $resource;
14
        $plugins = $this->resource->allLiterals('skosmos:usePlugin');
15
        $pluginArray = array();
16
        if ($plugins) {
17
            foreach ($plugins as $pluginlit) {
18
                $pluginArray[] = $pluginlit->getValue();
19
            }
20
        }
21
        $this->plugins = new PluginRegister(array_merge($globalPlugins, $pluginArray));
22
    }
23
24
    /**
25
     * Get the default language of this vocabulary
26
     * @return string default language, e.g. 'en'
27
     */
28
29
    public function getDefaultLanguage()
30
    {
31
        $deflang = $this->resource->getLiteral('skosmos:defaultLanguage');
32
        if ($deflang) {
33
            return $deflang->getValue();
34
        }
35
36
        $langs = $this->getLanguages();
37
        $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric
38
        if (sizeof($langs) > 1) {
39
            trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING);
40
        }
41
42
        return $deflang;
43
    }
44
45
    /**
46
     * Wether the alphabetical index is small enough to be shown all at once.
47
     * @return boolean true if all concepts can be shown at once.
48
     */
49
    public function getAlphabeticalFull()
50
    {
51
        return $this->getBoolean('skosmos:fullAlphabeticalIndex');
52
    }
53
54
    /**
55
     * Returns a short name for a vocabulary if configured. If that has not been set
56
     * using vocabId as a fallback.
57
     * @return string
58
     */
59
    public function getShortName()
60
    {
61
        $shortname = $this->getLiteral('skosmos:shortName');
62
        if ($shortname)
63
          return $shortname;
64
65
        // if no shortname exists fall back to the id
66
        return $this->getId();
67
    }
68
69
    /**
70
     * Get the vocabulary feedback e-mail address and return it.
71
     *
72
     * @return string e-mail address or null if not defined.
73
     */
74
    public function getFeedbackRecipient()
75
    {
76
        $email = $this->resource->get('skosmos:feedbackRecipient');
77
        return isset($email) ? $email->getValue() : null;
78
    }
79
80
    /**
81
     * Returns the human readable vocabulary title.
82
     * @return string the title of the vocabulary
83
     */
84
    public function getTitle($lang = null)
85
    {
86
        return $this->getLiteral('dc:title', false, $lang);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
    }
88
89
    /**
90
     * Returns a boolean value set in the config.ttl config.
91
     * @return boolean
92
     */
93
    public function sortByNotation()
94
    {
95
        return $this->getBoolean('skosmos:sortByNotation');
96
    }
97
98
    /**
99
     * Returns a boolean value set in the config.ttl config.
100
     * @return boolean
101
     */
102
    public function showChangeList()
103
    {
104
        return $this->getBoolean('skosmos:showChangeList');
105
    }
106
107
    /**
108
     * get the URLs from which the vocabulary data can be downloaded
109
     * @return array Array with MIME type as key, URL as value
110
     */
111
    public function getDataURLs()
112
    {
113
        $ret = array();
114
        $urls = $this->resource->allResources("void:dataDump");
115
        foreach ($urls as $url) {
116
            // first try dc:format and dc11:format
117
            $mimetypelit = $url->getLiteral('dc:format');
118
            if ($mimetypelit === null) {
119
                $mimetypelit = $url->getLiteral('dc11:format');
120
            }
121
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
134
            $langLit = $url->getLiteral('dc:language');
135
136
            if ($langLit != null) {
137
                //when the mimetype has language variants
138
                $dataUrlLang = $langLit->getValue();
139
140
                if (!isset($ret[$mimetype])) {
141
                  $arr = array();
142
                } else {
143
                  $arr = $ret[$mimetype];
144
                }
145
                $arr[$dataUrlLang] = $url->getURI();
146
                $ret[$mimetype] = $arr;
147
            } else {
148
                $ret[$mimetype] = $url->getURI();
149
            }
150
        }
151
        return $ret;
152
    }
153
154
    /**
155
     * Returns the main Concept Scheme URI of that Vocabulary,
156
     * or null if not set.
157
     * @return string concept scheme URI or null
158
     */
159
160
    public function getMainConceptSchemeURI()
161
    {
162
        $val = $this->resource->getResource("skosmos:mainConceptScheme");
163
        if ($val) {
164
            return $val->getURI();
165
        }
166
167
        return null;
168
    }
169
170
    /**
171
     * Returns the class URI used for concept groups in this vocabulary,
172
     * or null if not set.
173
     * @return string group class URI or null
174
     */
175
176
    public function getGroupClassURI()
177
    {
178
        $val = $this->resource->getResource("skosmos:groupClass");
179
        if ($val) {
180
            return $val->getURI();
181
        }
182
183
        return null;
184
    }
185
186
    /**
187
     * Returns the class URI used for thesaurus arrays in this vocabulary,
188
     * or null if not set.
189
     * @return string array class URI or null
190
     */
191
192
    public function getArrayClassURI()
193
    {
194
        $val = $this->resource->getResource("skosmos:arrayClass");
195
        if ($val) {
196
            return $val->getURI();
197
        }
198
199
        return null;
200
    }
201
202
    /**
203
     * Returns custom properties displayed on the search page if configured.
204
     * @return string array class URI or null
205
     */
206
207 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...
208
    {
209
        $resources = $this->resource->allResources("skosmos:showPropertyInSearch");
210
        $ret = array();
211
        foreach ($resources as $res) {
212
            $prop = $res->getURI();
213
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible
214
            {
215
                $prop = EasyRdf\RdfNamespace::shorten($prop);
216
            }
217
218
            $ret[] = $prop;
219
        }
220
        return $ret;
221
    }
222
223
    /**
224
     * Queries whether the property should be shown with all the label language variations.
225
     * @param string $property
226
     * @return boolean
227
     */
228
    public function hasMultiLingualProperty($property)
229
    {
230
        $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty");
231
        foreach ($resources as $res) {
232
            $prop = $res->getURI();
233
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible
234
            {
235
                $prop = EasyRdf\RdfNamespace::shorten($prop);
236
            }
237
238
            if ($prop === $property) {
239
                return true;
240
            }
241
242
        }
243
        return false;
244
    }
245
246
    /**
247
     * Returns a boolean value set in the config.ttl config.
248
     * @return boolean
249
     */
250
    public function getShowHierarchy()
251
    {
252
        return $this->getBoolean('skosmos:showTopConcepts');
253
    }
254
255
    /**
256
     * Returns a boolean value set in the config.ttl config.
257
     * @return boolean
258
     */
259
    public function showConceptSchemesInHierarchy()
260
    {
261
        return $this->getBoolean('skosmos:conceptSchemesInHierarchy');
262
    }
263
264
    /**
265
     * Returns a boolean value set in the config.ttl config.
266
     * @return boolean defaults to true if fetching hasn't been explicitly denied.
267
     */
268
    public function getExternalResourcesLoading()
269
    {
270
        return $this->getBoolean('skosmos:loadExternalResources', true);
271
    }
272
273
    /**
274
     * Returns a boolean value set in the config.ttl config.
275
     * @return boolean
276
     */
277
    public function getShowLangCodes()
278
    {
279
        return $this->getBoolean('skosmos:explicitLanguageTags');
280
    }
281
282
    /**
283
     * Returns a boolean value set in the config.ttl config.
284
     * @return boolean
285
     */
286
    public function searchByNotation()
287
    {
288
        return $this->getBoolean('skosmos:searchByNotation');
289
    }
290
291
    /**
292
     * Returns skosmos:marcSourcecode value set in config.ttl.
293
     * @return string marcsource name
294
     */
295
    public function getMarcSourceCode($lang = null)
296
    {
297
        return $this->getLiteral('skosmos:marcSourceCode', false, $lang);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
298
    }
299
300
    /**
301
     * Returns a boolean value set in the config.ttl config.
302
     * @return array array of concept class URIs (can be empty)
303
     */
304
    public function getIndexClasses()
305
    {
306
        return $this->getResources("skosmos:indexShowClass");
307
    }
308
309
    /**
310
     * Returns skosmos:externalProperty values set in the config.ttl config.
311
     * @return array array of external property URIs (can be empty)
312
     */
313
    public function getExtProperties()
314
    {
315
        return $this->getResources("skosmos:externalProperty");
316
    }
317
318
    /**
319
     * Get the languages supported by this vocabulary
320
     * @return array languages supported by this vocabulary (as language tag strings)
321
     */
322
    public function getLanguages()
323
    {
324
        $langs = $this->resource->allLiterals('skosmos:language');
325
        $ret = array();
326
        foreach ($langs as $lang) {
327
            $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang());
328
            $ret[$langlit] = $lang->getValue();
329
        }
330
        ksort($ret);
331
332
        return $ret;
333
    }
334
335
    /**
336
     * Returns the vocabulary default sidebar view.
337
     * @return string name of the view
338
     */
339
    public function getDefaultSidebarView()
340
    {
341
        $defview = $this->resource->getLiteral('skosmos:defaultSidebarView');
342
        if ($defview) {
343
            $value = $defview->getValue();
344
            if ($value === 'groups' || $value === 'hierarchy') {
345
                return $value;
346
            }
347
348
        }
349
        if ($this->showAlphabeticalIndex() === false) {
350
            if ($this->getShowHierarchy()) {
351
                return 'hierarchy';
352
            } 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...
353
                return 'groups';
354
            }
355
        }
356
        return 'alphabetical'; // if not defined displaying the alphabetical index
357
    }
358
359
    /**
360
     * Extracts the vocabulary id string from the baseuri of the vocabulary.
361
     * @return string identifier eg. 'mesh'.
362
     */
363
    public function getId()
364
    {
365
        $uriparts = explode("#", $this->resource->getURI());
366
        if (count($uriparts) != 1)
367
        // hash namespace
368
        {
369
            return $uriparts[1];
370
        }
371
372
        // slash namespace
373
        $uriparts = explode("/", $this->resource->getURI());
374
375
        return $uriparts[count($uriparts) - 1];
376
    }
377
378
    public function getShowStatistics() {
379
        return $this->getBoolean('skosmos:showStatistics', true);
380
    }
381
382
    public function getPlugins()
383
    {
384
        return $this->plugins;
385
    }
386
387
    /**
388
     * Returns the property/properties used for visualizing concept hierarchies.
389
     * @return string array class URI or null
390
     */
391
392 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...
393
    {
394
        $resources = $this->resource->allResources("skosmos:hierarchyProperty");
395
        $ret = array();
396
        foreach ($resources as $res) {
397
            $prop = $res->getURI();
398
            if (EasyRdf\RdfNamespace::shorten($prop) !== null) // prefixing if possible
399
            {
400
                $prop = EasyRdf\RdfNamespace::shorten($prop);
401
            }
402
403
            $ret[] = $prop;
404
        }
405
        return empty($ret) ? array('skos:broader') : $ret;
406
    }
407
408
    /**
409
     * Returns a boolean value set in the config.ttl config.
410
     * @return boolean
411
     */
412
    public function showNotation()
413
    {
414
        return $this->getBoolean('skosmos:showNotation', true);
415
    }
416
417
    /**
418
     * Returns a boolean value set in the config.ttl config.
419
     * @return boolean
420
     */
421
    public function showAlphabeticalIndex()
422
    {
423
        return $this->getBoolean('skosmos:showAlphabeticalIndex', true);
424
    }
425
426
    /**
427
     * Returns the alphabetical list qualifier in this vocabulary,
428
     * or null if not set.
429
     * @return EasyRdf\Resource|null alphabetical list qualifier resource or null
430
     */
431
    public function getAlphabeticalListQualifier()
432
    {
433
        return $this->resource->getResource('skosmos:alphabeticalListQualifier');
434
    }
435
436
    /**
437
     * Returns a boolean value set in the config.ttl config.
438
     * @return boolean
439
     */
440
    public function getShowDeprecated()
441
    {
442
        return $this->getBoolean('skosmos:showDeprecated', false);
443
    }
444
445
    /**
446
     * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration.
447
     * @return array of objects or an empty array
448
     */
449
    public function getTypes($lang = null)
450
    {
451
        $resources = $this->resource->allResources("dc:type");
452
        $ret = array();
453
        foreach ($resources as $res) {
454
            $prop = $res->getURI();
455
            $label = $res->label($lang) ? $res->label($lang) : $res->label($this->getDefaultLanguage());
456
            $ret[] = array('uri' => $prop, 'prefLabel' =>  $label->getValue());
457
        }
458
        return $ret;
459
    }
460
461
    /**
462
     * Returns an array of fallback languages that is ordered by priority and
463
     * defined in the vocabulary configuration as a collection.
464
     * Additionally, the chosen content language is inserted with the highest priority
465
     * and the vocab default language is inserted with the lowest priority.
466
     * @param string $clang
467
     * @return array of language code strings
468
     */
469
    public function getLanguageOrder($clang)
470
    {
471
        if (array_key_exists($clang, $this->languageOrderCache)) {
472
            return $this->languageOrderCache[$clang];
473
        }
474
        $ret = array($clang);
475
        $fallbacks = !empty($this->resource->get('skosmos:fallbackLanguages')) ? $this->resource->get('skosmos:fallbackLanguages') : array();
476
        foreach ($fallbacks as $lang) {
477
            if (!in_array($lang, $ret)) {
478
                $ret[] = (string)$lang; // Literal to string conversion
479
            }
480
        }
481
        if (!in_array($this->getDefaultLanguage(), $ret)) {
482
            $ret[] = (string)$this->getDefaultLanguage();
483
        }
484
        foreach ($this->getLanguages() as $lang) {
485
            if (!in_array($lang, $ret)) {
486
                $ret[] = $lang;
487
            }
488
        }
489
        // store in cache so this doesn't have to be computed again
490
        $this->languageOrderCache[$clang] = $ret;
491
        return $ret;
492
    }
493
494
    /**
495
     * Returns a boolean value set in the config.ttl config.
496
     * @return boolean
497
     */
498
    public function getUseModifiedDate(): bool
499
    {
500
        return $this->getBoolean('skosmos:useModifiedDate', false);
501
    }
502
}
503