Total Complexity | 96 |
Total Lines | 634 |
Duplicated Lines | 0 % |
Changes | 7 | ||
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 $plugins; |
||
9 | private $pluginParameters; |
||
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()) |
||
67 | } |
||
68 | |||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get the SPARQL endpoint URL for this vocabulary |
||
73 | * |
||
74 | * @return string|null endpoint URL, or null if not set |
||
75 | */ |
||
76 | public function getSparqlEndpoint() |
||
77 | { |
||
78 | $endpoint = $this->resource->get('void:sparqlEndpoint'); |
||
79 | if ($endpoint) { |
||
80 | return $endpoint->getUri(); |
||
81 | } |
||
82 | return null; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get the SPARQL graph URI for this vocabulary |
||
87 | * |
||
88 | * @return string|null graph URI, or null if not set |
||
89 | */ |
||
90 | public function getSparqlGraph() |
||
91 | { |
||
92 | $graph = $this->resource->get('skosmos:sparqlGraph'); |
||
93 | if ($graph) { |
||
94 | $graph = $graph->getUri(); |
||
95 | } |
||
96 | |||
97 | return $graph; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get the SPARQL dialect for this vocabulary |
||
102 | * |
||
103 | * @return string|null dialect name |
||
104 | */ |
||
105 | public function getSparqlDialect() |
||
106 | { |
||
107 | $dialect = $this->resource->get('skosmos:sparqlDialect'); |
||
108 | if ($dialect) { |
||
109 | $dialect = $dialect->getValue(); |
||
110 | } |
||
111 | |||
112 | return $dialect; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the default language of this vocabulary |
||
117 | * @return string default language, e.g. 'en' |
||
118 | */ |
||
119 | |||
120 | public function getDefaultLanguage() |
||
121 | { |
||
122 | $deflang = $this->resource->getLiteral('skosmos:defaultLanguage'); |
||
123 | if ($deflang) { |
||
|
|||
124 | return $deflang->getValue(); |
||
125 | } |
||
126 | |||
127 | $langs = $this->getLanguages(); |
||
128 | $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric |
||
129 | if (sizeof($langs) > 1) { |
||
130 | trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING); |
||
131 | } |
||
132 | |||
133 | return $deflang; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Whether the alphabetical index is small enough to be shown all at once. |
||
138 | * @return boolean true if all concepts can be shown at once. |
||
139 | */ |
||
140 | public function getAlphabeticalFull() |
||
141 | { |
||
142 | return $this->getBoolean('skosmos:fullAlphabeticalIndex'); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
147 | * using vocabId as a fallback. |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getShortName() |
||
151 | { |
||
152 | $shortname = $this->getLiteral('skosmos:shortName'); |
||
153 | if ($shortname) |
||
154 | return $shortname; |
||
155 | |||
156 | // if no shortname exists fall back to the id |
||
157 | return $this->getId(); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get the vocabulary feedback e-mail address and return it. |
||
162 | * |
||
163 | * @return string e-mail address or null if not defined. |
||
164 | */ |
||
165 | public function getFeedbackRecipient() |
||
166 | { |
||
167 | $email = $this->resource->get('skosmos:feedbackRecipient'); |
||
168 | return isset($email) ? $email->getValue() : null; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Returns the human readable vocabulary title. |
||
173 | * @return string the title of the vocabulary |
||
174 | */ |
||
175 | public function getTitle($lang = null) |
||
176 | { |
||
177 | return $this->getLiteral('dc:title', false, $lang); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns a boolean value set in the config.ttl config. |
||
182 | * @return boolean |
||
183 | */ |
||
184 | public function sortByNotation() |
||
185 | { |
||
186 | return $this->getBoolean('skosmos:sortByNotation'); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Returns a boolean value set in the config.ttl config. |
||
191 | * @return boolean |
||
192 | */ |
||
193 | public function showChangeList() |
||
194 | { |
||
195 | return $this->getBoolean('skosmos:showChangeList'); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * get the URLs from which the vocabulary data can be downloaded |
||
200 | * @return array Array with MIME type as key, URL as value |
||
201 | */ |
||
202 | public function getDataURLs() |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Returns the main Concept Scheme URI of that Vocabulary, |
||
247 | * or null if not set. |
||
248 | * @return string concept scheme URI or null |
||
249 | */ |
||
250 | |||
251 | public function getMainConceptSchemeURI() |
||
252 | { |
||
253 | $val = $this->resource->getResource("skosmos:mainConceptScheme"); |
||
254 | if ($val) { |
||
255 | return $val->getURI(); |
||
256 | } |
||
257 | |||
258 | return null; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Returns the class URI used for concept groups in this vocabulary, |
||
263 | * or null if not set. |
||
264 | * @return string group class URI or null |
||
265 | */ |
||
266 | |||
267 | public function getGroupClassURI() |
||
268 | { |
||
269 | $val = $this->resource->getResource("skosmos:groupClass"); |
||
270 | if ($val) { |
||
271 | return $val->getURI(); |
||
272 | } |
||
273 | |||
274 | return null; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
279 | * or null if not set. |
||
280 | * @return string array class URI or null |
||
281 | */ |
||
282 | |||
283 | public function getArrayClassURI() |
||
284 | { |
||
285 | $val = $this->resource->getResource("skosmos:arrayClass"); |
||
286 | if ($val) { |
||
287 | return $val->getURI(); |
||
288 | } |
||
289 | |||
290 | return null; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Returns custom properties displayed on the search page if configured. |
||
295 | * @return array array class URI or null |
||
296 | */ |
||
297 | |||
298 | public function getAdditionalSearchProperties() |
||
299 | { |
||
300 | $resources = $this->resource->allResources("skosmos:showPropertyInSearch"); |
||
301 | $ret = array(); |
||
302 | foreach ($resources as $res) { |
||
303 | $prop = $res->getURI(); |
||
304 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible |
||
305 | { |
||
306 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
307 | } |
||
308 | |||
309 | $ret[] = $prop; |
||
310 | } |
||
311 | return $ret; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Queries whether the property should be shown with all the label language variations. |
||
316 | * @param string $property |
||
317 | * @return boolean |
||
318 | */ |
||
319 | public function hasMultiLingualProperty($property) |
||
320 | { |
||
321 | $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty"); |
||
322 | foreach ($resources as $res) { |
||
323 | $prop = $res->getURI(); |
||
324 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // shortening property labels if possible |
||
325 | { |
||
326 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
327 | } |
||
328 | |||
329 | if ($prop === $property) { |
||
330 | return true; |
||
331 | } |
||
332 | |||
333 | } |
||
334 | return false; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Returns a boolean value set in the config.ttl config. |
||
339 | * @return boolean |
||
340 | */ |
||
341 | public function getShowHierarchy() |
||
342 | { |
||
343 | return $this->getBoolean('skosmos:showTopConcepts'); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Returns a boolean value set in the config.ttl config. |
||
348 | * @return boolean |
||
349 | */ |
||
350 | public function showConceptSchemesInHierarchy() |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Returns a boolean value set in the config.ttl config. |
||
357 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
358 | */ |
||
359 | public function getExternalResourcesLoading() |
||
360 | { |
||
361 | return $this->getBoolean('skosmos:loadExternalResources', true); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Returns a boolean value set in the config.ttl config. |
||
366 | * @return boolean |
||
367 | */ |
||
368 | public function getShowLangCodes() |
||
369 | { |
||
370 | return $this->getBoolean('skosmos:explicitLanguageTags'); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Returns a boolean value set in the config.ttl config. |
||
375 | * @return boolean |
||
376 | */ |
||
377 | public function searchByNotation() |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Returns skosmos:marcSourcecode value set in config.ttl. |
||
384 | * @return string marcsource name |
||
385 | */ |
||
386 | public function getMarcSourceCode($lang = null) |
||
387 | { |
||
388 | return $this->getLiteral('skosmos:marcSourceCode', false, $lang); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Returns the boolean value of the skosmos:showNotationAsProperty setting. |
||
393 | * @return boolean |
||
394 | */ |
||
395 | public function getShowNotationAsProperty() |
||
396 | { |
||
397 | return $this->getBoolean('skosmos:showNotationAsProperty', true); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Returns a boolean value set in the config.ttl config. |
||
402 | * @return array array of concept class URIs (can be empty) |
||
403 | */ |
||
404 | public function getIndexClasses() |
||
405 | { |
||
406 | return $this->getResources("skosmos:indexShowClass"); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Returns skosmos:externalProperty values set in the config.ttl config. |
||
411 | * @return array array of external property URIs (can be empty) |
||
412 | */ |
||
413 | public function getExtProperties() |
||
414 | { |
||
415 | return $this->getResources("skosmos:externalProperty"); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Get the languages supported by this vocabulary |
||
420 | * @return array languages supported by this vocabulary (as language tag strings) |
||
421 | */ |
||
422 | public function getLanguages() |
||
423 | { |
||
424 | $langs = $this->resource->allLiterals('skosmos:language'); |
||
425 | $ret = array(); |
||
426 | foreach ($langs as $lang) { |
||
427 | $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang()); |
||
428 | $ret[$langlit] = $lang->getValue(); |
||
429 | } |
||
430 | ksort($ret); |
||
431 | |||
432 | return $ret; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Returns the plugin parameters |
||
437 | * @return string plugin parameters or null |
||
438 | */ |
||
439 | public function getPluginParameters() { |
||
440 | return json_encode($this->pluginParameters, true); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Returns the vocabulary default sidebar view. |
||
445 | * @return string name of the view |
||
446 | */ |
||
447 | public function getDefaultSidebarView() |
||
448 | { |
||
449 | $defview = $this->resource->getLiteral('skosmos:defaultSidebarView'); |
||
450 | if ($defview) { |
||
451 | $value = $defview->getValue(); |
||
452 | if ($value === 'groups' || $value === 'hierarchy') { |
||
453 | return $value; |
||
454 | } |
||
455 | |||
456 | } |
||
457 | if ($this->showAlphabeticalIndex() === false) { |
||
458 | if ($this->getShowHierarchy()) { |
||
459 | return 'hierarchy'; |
||
460 | } else if ($this->getGroupClassURI()) { |
||
461 | return 'groups'; |
||
462 | } |
||
463 | } |
||
464 | return 'alphabetical'; // if not defined displaying the alphabetical index |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
469 | * @return string identifier eg. 'mesh'. |
||
470 | */ |
||
471 | public function getId() |
||
472 | { |
||
473 | $uriparts = explode("#", $this->resource->getURI()); |
||
474 | if (count($uriparts) != 1) |
||
475 | // hash namespace |
||
476 | { |
||
477 | return $uriparts[1]; |
||
478 | } |
||
479 | |||
480 | // slash namespace |
||
481 | $uriparts = explode("/", $this->resource->getURI()); |
||
482 | |||
483 | return $uriparts[count($uriparts) - 1]; |
||
484 | } |
||
485 | |||
486 | public function getShowStatistics() { |
||
487 | return $this->getBoolean('skosmos:showStatistics', true); |
||
488 | } |
||
489 | |||
490 | public function getPlugins() |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Returns the property/properties used for visualizing concept hierarchies. |
||
497 | * @return array array class URI or null |
||
498 | */ |
||
499 | |||
500 | public function getHierarchyProperty() |
||
501 | { |
||
502 | $resources = $this->resource->allResources("skosmos:hierarchyProperty"); |
||
503 | $ret = array(); |
||
504 | foreach ($resources as $res) { |
||
505 | $prop = $res->getURI(); |
||
506 | if (EasyRdf\RdfNamespace::shorten($prop) !== null) // prefixing if possible |
||
507 | { |
||
508 | $prop = EasyRdf\RdfNamespace::shorten($prop); |
||
509 | } |
||
510 | |||
511 | $ret[] = $prop; |
||
512 | } |
||
513 | return empty($ret) ? array('skos:broader') : $ret; |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * Returns a boolean value set in the config.ttl config. |
||
518 | * @return boolean |
||
519 | */ |
||
520 | public function showNotation() |
||
521 | { |
||
522 | return $this->getBoolean('skosmos:showNotation', true); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Returns a boolean value set in the config.ttl config. |
||
527 | * @return boolean |
||
528 | */ |
||
529 | public function showAlphabeticalIndex() |
||
530 | { |
||
531 | return $this->getBoolean('skosmos:showAlphabeticalIndex', true); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * Returns the alphabetical list qualifier in this vocabulary, |
||
536 | * or null if not set. |
||
537 | * @return EasyRdf\Resource|null alphabetical list qualifier resource or null |
||
538 | */ |
||
539 | public function getAlphabeticalListQualifier() |
||
540 | { |
||
541 | return $this->resource->getResource('skosmos:alphabeticalListQualifier'); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Returns a boolean value set in the config.ttl config. |
||
546 | * @return boolean |
||
547 | */ |
||
548 | public function getShowDeprecated() |
||
549 | { |
||
550 | return $this->getBoolean('skosmos:showDeprecated', false); |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
555 | * @return array of objects or an empty array |
||
556 | */ |
||
557 | public function getTypes($lang = null) |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Returns an array of fallback languages that is ordered by priority and |
||
571 | * defined in the vocabulary configuration as a collection. |
||
572 | * Additionally, the chosen content language is inserted with the highest priority |
||
573 | * and the vocab default language is inserted with the lowest priority. |
||
574 | * @param string $clang |
||
575 | * @return array of language code strings |
||
576 | */ |
||
577 | public function getLanguageOrder($clang) |
||
578 | { |
||
579 | if (array_key_exists($clang, $this->languageOrderCache)) { |
||
580 | return $this->languageOrderCache[$clang]; |
||
581 | } |
||
582 | $ret = array($clang); |
||
583 | $fallbacks = !empty($this->resource->get('skosmos:fallbackLanguages')) ? $this->resource->get('skosmos:fallbackLanguages') : array(); |
||
584 | foreach ($fallbacks as $lang) { |
||
585 | if (!in_array($lang, $ret)) { |
||
586 | $ret[] = (string)$lang; // Literal to string conversion |
||
587 | } |
||
588 | } |
||
589 | if (!in_array($this->getDefaultLanguage(), $ret)) { |
||
590 | $ret[] = (string)$this->getDefaultLanguage(); |
||
591 | } |
||
592 | foreach ($this->getLanguages() as $lang) { |
||
593 | if (!in_array($lang, $ret)) { |
||
594 | $ret[] = $lang; |
||
595 | } |
||
596 | } |
||
597 | // store in cache so this doesn't have to be computed again |
||
598 | $this->languageOrderCache[$clang] = $ret; |
||
599 | return $ret; |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * @return boolean |
||
604 | */ |
||
605 | public function isUseModifiedDate() |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * @return array |
||
612 | */ |
||
613 | public function getPropertyOrder() |
||
614 | { |
||
615 | $order = $this->getResource()->getResource('skosmos:propertyOrder'); |
||
616 | if ($order === null) { |
||
640 | } |
||
641 | } |
||
642 |