Total Complexity | 104 |
Total Lines | 681 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
Complex classes like VocabularyConfig often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VocabularyConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class VocabularyConfig extends BaseConfig |
||
7 | { |
||
8 | private $pluginRegister; |
||
9 | private $pluginParameters = array(); |
||
10 | private $languageOrderCache = array(); |
||
11 | |||
12 | const DEFAULT_PROPERTY_ORDER = array("rdf:type", "dc:isReplacedBy", |
||
13 | "skos:definition", "skos:broader", "isothes:broaderGeneric", |
||
14 | "isothes:broaderPartitive", "isothes:broaderInstantial", |
||
15 | "skos:narrower", "isothes:narrowerGeneric", "isothes:narrowerPartitive", |
||
16 | "isothes:narrowerInstantial", "skos:related", "skos:altLabel", |
||
17 | "skos:note", "skos:scopeNote", "skos:historyNote", "rdfs:comment", |
||
18 | "dc11:source", "dc:source", "skosmos:memberOf", "skosmos:memberOfArray"); |
||
19 | |||
20 | const ISO25964_PROPERTY_ORDER = array("rdf:type", "dc:isReplacedBy", |
||
21 | // ISO 25964 allows placing all text fields (inc. SN and DEF) together |
||
22 | // so we will do that, except for HN, which is clearly administrative |
||
23 | "skos:note", "skos:scopeNote", "skos:definition", "rdfs:comment", |
||
24 | "dc11:source", "dc:source", "skos:altLabel", "skos:broader", |
||
25 | "isothes:broaderGeneric", "isothes:broaderPartitive", |
||
26 | "isothes:broaderInstantial", "skos:narrower", "isothes:narrowerGeneric", |
||
27 | "isothes:narrowerPartitive", "isothes:narrowerInstantial", |
||
28 | "skos:related", "skos:historyNote", "skosmos:memberOf", |
||
29 | "skosmos:memberOfArray"); |
||
30 | |||
31 | public function __construct($resource, $globalPlugins=array()) |
||
32 | { |
||
33 | $this->resource = $resource; |
||
34 | $this->globalPlugins = $globalPlugins; |
||
|
|||
35 | $this->setParameterizedPlugins(); |
||
36 | $pluginArray = $this->getPluginArray(); |
||
37 | $this->pluginRegister = new PluginRegister($pluginArray); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get an ordered array of plugin names with order configured in skosmos:vocabularyPlugins |
||
42 | * @return array of plugin names |
||
43 | */ |
||
44 | public function getPluginArray() : array |
||
45 | { |
||
46 | $pluginArray = array(); |
||
47 | |||
48 | $vocabularyPlugins = $this->resource->getResource('skosmos:vocabularyPlugins'); |
||
49 | if ($vocabularyPlugins) { |
||
50 | foreach ($vocabularyPlugins as $plugin) { |
||
51 | if ($plugin instanceof EasyRdf\Literal) { |
||
52 | $pluginName = $plugin->getValue(); |
||
53 | array_push($pluginArray, $pluginName); |
||
54 | } |
||
55 | else { |
||
56 | array_push($pluginArray, $plugin->getLiteral('skosmos:usePlugin')->getValue()); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | $pluginArray = array_merge($pluginArray, $this->globalPlugins); |
||
61 | |||
62 | $plugins = $this->resource->allLiterals('skosmos:usePlugin'); |
||
63 | if ($plugins) { |
||
64 | foreach ($plugins as $pluginlit) { |
||
65 | $pluginName = $pluginlit->getValue(); |
||
66 | array_push($pluginArray, $pluginName); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | $pluginArray = array_values(array_unique(array_merge($pluginArray, array_keys($this->pluginParameters)))); |
||
71 | |||
72 | return $pluginArray; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Sets array of parameterized plugins |
||
77 | * @param Easyrdf\Resource $pluginResource |
||
78 | * @return void |
||
79 | */ |
||
80 | private function setParameterizedPlugins() : void |
||
81 | { |
||
82 | $this->pluginParameters = array(); |
||
83 | |||
84 | $vocabularyPlugins = $this->resource->getResource('skosmos:vocabularyPlugins'); |
||
85 | if ($vocabularyPlugins) { |
||
86 | // $vocabularyPlugins has all resources |
||
87 | foreach ($vocabularyPlugins as $plugin) { |
||
88 | if ($plugin instanceof EasyRdf\Resource) { |
||
89 | $this->setPluginParameters($plugin); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | $pluginResources = $this->resource->allResources('skosmos:useParamPlugin'); |
||
94 | if ($pluginResources) { |
||
95 | foreach ($pluginResources as $pluginResource) { |
||
96 | $this->setPluginParameters($pluginResource); |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Updates array of parameterized plugins adding parameter values |
||
103 | * @param Easyrdf\Resource $pluginResource |
||
104 | * @return void |
||
105 | */ |
||
106 | private function setPluginParameters(Easyrdf\Resource $pluginResource) : void |
||
107 | { |
||
108 | $pluginName = $pluginResource->getLiteral('skosmos:usePlugin')->getValue(); |
||
109 | $this->pluginParameters[$pluginName] = array(); |
||
110 | |||
111 | $pluginParams = $pluginResource->allResources('skosmos:parameters'); |
||
112 | foreach ($pluginParams as $parameter) { |
||
113 | |||
114 | $paramLiterals = $parameter->allLiterals('schema:value'); |
||
115 | foreach ($paramLiterals as $paramLiteral) { |
||
116 | $paramName = $parameter->getLiteral('schema:propertyID')->getValue(); |
||
117 | $paramValue = $paramLiteral->getValue(); |
||
118 | $paramLang = $paramLiteral->getLang(); |
||
119 | if ($paramLang) { |
||
120 | $paramName .= '_' . $paramLang; |
||
121 | } |
||
122 | $this->pluginParameters[$pluginName][$paramName] = $paramValue; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get the SPARQL endpoint URL for this vocabulary |
||
129 | * |
||
130 | * @return string|null endpoint URL, or null if not set |
||
131 | */ |
||
132 | public function getSparqlEndpoint() |
||
133 | { |
||
134 | $endpoint = $this->resource->get('void:sparqlEndpoint'); |
||
135 | if ($endpoint) { |
||
136 | return $endpoint->getUri(); |
||
137 | } |
||
138 | return null; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get the SPARQL graph URI for this vocabulary |
||
143 | * |
||
144 | * @return string|null graph URI, or null if not set |
||
145 | */ |
||
146 | public function getSparqlGraph() |
||
147 | { |
||
148 | $graph = $this->resource->get('skosmos:sparqlGraph'); |
||
149 | if ($graph) { |
||
150 | $graph = $graph->getUri(); |
||
151 | } |
||
152 | |||
153 | return $graph; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Get the SPARQL dialect for this vocabulary |
||
158 | * |
||
159 | * @return string|null dialect name |
||
160 | */ |
||
161 | public function getSparqlDialect() |
||
162 | { |
||
163 | $dialect = $this->resource->get('skosmos:sparqlDialect'); |
||
164 | if ($dialect) { |
||
165 | $dialect = $dialect->getValue(); |
||
166 | } |
||
167 | |||
168 | return $dialect; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get the default language of this vocabulary |
||
173 | * @return string default language, e.g. 'en' |
||
174 | */ |
||
175 | |||
176 | public function getDefaultLanguage() |
||
177 | { |
||
178 | $deflang = $this->resource->getLiteral('skosmos:defaultLanguage'); |
||
179 | if ($deflang) { |
||
180 | return $deflang->getValue(); |
||
181 | } |
||
182 | |||
183 | $langs = $this->getLanguages(); |
||
184 | $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric |
||
185 | if (sizeof($langs) > 1) { |
||
186 | trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING); |
||
187 | } |
||
188 | |||
189 | return $deflang; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Whether the alphabetical index is small enough to be shown all at once. |
||
194 | * @return boolean true if all concepts can be shown at once. |
||
195 | */ |
||
196 | public function getAlphabeticalFull() |
||
197 | { |
||
198 | return $this->getBoolean('skosmos:fullAlphabeticalIndex'); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
203 | * using vocabId as a fallback. |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getShortName() |
||
207 | { |
||
208 | $shortname = $this->getLiteral('skosmos:shortName'); |
||
209 | if ($shortname) |
||
210 | return $shortname; |
||
211 | |||
212 | // if no shortname exists fall back to the id |
||
213 | return $this->getId(); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get the vocabulary feedback e-mail address and return it. |
||
218 | * |
||
219 | * @return string e-mail address or null if not defined. |
||
220 | */ |
||
221 | public function getFeedbackRecipient() |
||
222 | { |
||
223 | $email = $this->resource->get('skosmos:feedbackRecipient'); |
||
224 | return isset($email) ? $email->getValue() : null; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Returns the human readable vocabulary title. |
||
229 | * @return string the title of the vocabulary |
||
230 | */ |
||
231 | public function getTitle($lang = null) |
||
232 | { |
||
233 | return $this->getLiteral('dc:title', false, $lang); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Returns a boolean value set in the config.ttl config. |
||
238 | * @return boolean |
||
239 | */ |
||
240 | public function sortByNotation() |
||
241 | { |
||
242 | return $this->getBoolean('skosmos:sortByNotation'); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Returns a boolean value set in the config.ttl config. |
||
247 | * @return boolean |
||
248 | */ |
||
249 | public function showChangeList() |
||
250 | { |
||
251 | return $this->getBoolean('skosmos:showChangeList'); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * get the URLs from which the vocabulary data can be downloaded |
||
256 | * @return array Array with MIME type as key, URL as value |
||
257 | */ |
||
258 | public function getDataURLs() |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Returns the main Concept Scheme URI of that Vocabulary, |
||
303 | * or null if not set. |
||
304 | * @return string concept scheme URI or null |
||
305 | */ |
||
306 | |||
307 | public function getMainConceptSchemeURI() |
||
308 | { |
||
309 | $val = $this->resource->getResource("skosmos:mainConceptScheme"); |
||
310 | if ($val) { |
||
311 | return $val->getURI(); |
||
312 | } |
||
313 | |||
314 | return null; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Returns the class URI used for concept groups in this vocabulary, |
||
319 | * or null if not set. |
||
320 | * @return string group class URI or null |
||
321 | */ |
||
322 | |||
323 | public function getGroupClassURI() |
||
324 | { |
||
325 | $val = $this->resource->getResource("skosmos:groupClass"); |
||
326 | if ($val) { |
||
327 | return $val->getURI(); |
||
328 | } |
||
329 | |||
330 | return null; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
335 | * or null if not set. |
||
336 | * @return string array class URI or null |
||
337 | */ |
||
338 | |||
339 | public function getArrayClassURI() |
||
340 | { |
||
341 | $val = $this->resource->getResource("skosmos:arrayClass"); |
||
342 | if ($val) { |
||
343 | return $val->getURI(); |
||
344 | } |
||
345 | |||
346 | return null; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Returns custom properties displayed on the search page if configured. |
||
351 | * @return array array class URI or null |
||
352 | */ |
||
353 | |||
354 | public function getAdditionalSearchProperties() |
||
355 | { |
||
356 | $resources = $this->resource->allResources("skosmos:showPropertyInSearch"); |
||
357 | $ret = array(); |
||
358 | foreach ($resources as $res) { |
||
359 | $prop = $res->getURI(); |
||
360 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible |
||
361 | { |
||
362 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
363 | } |
||
364 | |||
365 | $ret[] = $prop; |
||
366 | } |
||
367 | return $ret; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Queries whether the property should be shown with all the label language variations. |
||
372 | * @param string $property |
||
373 | * @return boolean |
||
374 | */ |
||
375 | public function hasMultiLingualProperty($property) |
||
376 | { |
||
377 | $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty"); |
||
378 | foreach ($resources as $res) { |
||
379 | $prop = $res->getURI(); |
||
380 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible |
||
381 | { |
||
382 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
383 | } |
||
384 | |||
385 | if ($prop === $property) { |
||
386 | return true; |
||
387 | } |
||
388 | |||
389 | } |
||
390 | return false; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns a boolean value set in the config.ttl config. |
||
395 | * @return boolean |
||
396 | */ |
||
397 | public function getShowHierarchy() |
||
398 | { |
||
399 | return $this->getBoolean('skosmos:showTopConcepts'); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Returns a boolean value set in the config.ttl config. |
||
404 | * @return boolean |
||
405 | */ |
||
406 | public function showConceptSchemesInHierarchy() |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Returns a boolean value set in the config.ttl config. |
||
413 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
414 | */ |
||
415 | public function getExternalResourcesLoading() |
||
416 | { |
||
417 | return $this->getBoolean('skosmos:loadExternalResources', true); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Returns a boolean value set in the config.ttl config. |
||
422 | * @return boolean |
||
423 | */ |
||
424 | public function getShowLangCodes() |
||
425 | { |
||
426 | return $this->getBoolean('skosmos:explicitLanguageTags'); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Returns a boolean value set in the config.ttl config. |
||
431 | * @return boolean |
||
432 | */ |
||
433 | public function searchByNotation() |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Returns skosmos:marcSourcecode value set in config.ttl. |
||
440 | * @return string marcsource name |
||
441 | */ |
||
442 | public function getMarcSourceCode($lang = null) |
||
443 | { |
||
444 | return $this->getLiteral('skosmos:marcSourceCode', false, $lang); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Returns a boolean value set in the config.ttl config. |
||
449 | * @return array array of concept class URIs (can be empty) |
||
450 | */ |
||
451 | public function getIndexClasses() |
||
452 | { |
||
453 | return $this->getResources("skosmos:indexShowClass"); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Returns skosmos:externalProperty values set in the config.ttl config. |
||
458 | * @return array array of external property URIs (can be empty) |
||
459 | */ |
||
460 | public function getExtProperties() |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Get the languages supported by this vocabulary |
||
467 | * @return array languages supported by this vocabulary (as language tag strings) |
||
468 | */ |
||
469 | public function getLanguages() |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Returns the plugin parameters |
||
484 | * @return string plugin parameters or null |
||
485 | */ |
||
486 | public function getPluginParameters() { |
||
487 | return $this->pluginParameters; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Returns the vocabulary default sidebar view. |
||
492 | * @return string name of the view |
||
493 | */ |
||
494 | public function getDefaultSidebarView() |
||
495 | { |
||
496 | $defview = $this->resource->getLiteral('skosmos:defaultSidebarView'); |
||
497 | if ($defview) { |
||
498 | $value = $defview->getValue(); |
||
499 | if ($value === 'groups' || $value === 'hierarchy') { |
||
500 | return $value; |
||
501 | } |
||
502 | |||
503 | } |
||
504 | if ($this->showAlphabeticalIndex() === false) { |
||
505 | if ($this->getShowHierarchy()) { |
||
506 | return 'hierarchy'; |
||
507 | } else if ($this->getGroupClassURI()) { |
||
508 | return 'groups'; |
||
509 | } |
||
510 | } |
||
511 | return 'alphabetical'; // if not defined displaying the alphabetical index |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
516 | * @return string identifier eg. 'mesh'. |
||
517 | */ |
||
518 | public function getId() |
||
531 | } |
||
532 | |||
533 | public function getShowStatistics() { |
||
534 | return $this->getBoolean('skosmos:showStatistics', true); |
||
535 | } |
||
536 | |||
537 | public function getPluginRegister() |
||
538 | { |
||
539 | return $this->pluginRegister; |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Returns the property/properties used for visualizing concept hierarchies. |
||
544 | * @return array array class URI or null |
||
545 | */ |
||
546 | |||
547 | public function getHierarchyProperty() |
||
548 | { |
||
549 | $resources = $this->resource->allResources("skosmos:hierarchyProperty"); |
||
550 | $ret = array(); |
||
551 | foreach ($resources as $res) { |
||
552 | $prop = $res->getURI(); |
||
553 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // prefixing if possible |
||
554 | { |
||
555 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
556 | } |
||
557 | |||
558 | $ret[] = $prop; |
||
559 | } |
||
560 | return empty($ret) ? array('skos:broader') : $ret; |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Returns a boolean value set in the config.ttl config. |
||
565 | * @return boolean |
||
566 | */ |
||
567 | public function showNotation() |
||
568 | { |
||
569 | return $this->getBoolean('skosmos:showNotation', true); |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * Returns a boolean value set in the config.ttl config. |
||
574 | * @return boolean |
||
575 | */ |
||
576 | public function showAlphabeticalIndex() |
||
577 | { |
||
578 | return $this->getBoolean('skosmos:showAlphabeticalIndex', true); |
||
579 | } |
||
580 | |||
581 | /** |
||
582 | * Returns the alphabetical list qualifier in this vocabulary, |
||
583 | * or null if not set. |
||
584 | * @return EasyRdf\Resource|null alphabetical list qualifier resource or null |
||
585 | */ |
||
586 | public function getAlphabeticalListQualifier() |
||
587 | { |
||
588 | return $this->resource->getResource('skosmos:alphabeticalListQualifier'); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Returns a boolean value set in the config.ttl config. |
||
593 | * @return boolean |
||
594 | */ |
||
595 | public function getShowDeprecated() |
||
596 | { |
||
597 | return $this->getBoolean('skosmos:showDeprecated', false); |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
602 | * @return array of objects or an empty array |
||
603 | */ |
||
604 | public function getTypes($lang = null) |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Returns an array of fallback languages that is ordered by priority and |
||
618 | * defined in the vocabulary configuration as a collection. |
||
619 | * Additionally, the chosen content language is inserted with the highest priority |
||
620 | * and the vocab default language is inserted with the lowest priority. |
||
621 | * @param string $clang |
||
622 | * @return array of language code strings |
||
623 | */ |
||
624 | public function getLanguageOrder($clang) |
||
625 | { |
||
626 | if (array_key_exists($clang, $this->languageOrderCache)) { |
||
627 | return $this->languageOrderCache[$clang]; |
||
628 | } |
||
629 | $ret = array($clang); |
||
630 | $fallbacks = !empty($this->resource->get('skosmos:fallbackLanguages')) ? $this->resource->get('skosmos:fallbackLanguages') : array(); |
||
631 | foreach ($fallbacks as $lang) { |
||
632 | if (!in_array($lang, $ret)) { |
||
633 | $ret[] = (string)$lang; // Literal to string conversion |
||
634 | } |
||
635 | } |
||
636 | if (!in_array($this->getDefaultLanguage(), $ret)) { |
||
637 | $ret[] = (string)$this->getDefaultLanguage(); |
||
638 | } |
||
639 | foreach ($this->getLanguages() as $lang) { |
||
640 | if (!in_array($lang, $ret)) { |
||
641 | $ret[] = $lang; |
||
642 | } |
||
643 | } |
||
644 | // store in cache so this doesn't have to be computed again |
||
645 | $this->languageOrderCache[$clang] = $ret; |
||
646 | return $ret; |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * @return boolean |
||
651 | */ |
||
652 | public function isUseModifiedDate() |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * @return array |
||
659 | */ |
||
660 | public function getPropertyOrder() |
||
661 | { |
||
687 | } |
||
688 | } |
||
689 |