1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Vocabulary dataobjects provide access to the vocabularies on the SPARQL endpoint. |
5
|
|
|
*/ |
6
|
|
|
class Vocabulary extends DataObject implements Modifiable |
7
|
|
|
{ |
8
|
|
|
/** cached value of URI space */ |
9
|
|
|
private $urispace = null; |
10
|
|
|
private $config; |
11
|
|
|
|
12
|
|
|
public function __construct($model, $resource) |
13
|
|
|
{ |
14
|
|
|
parent::__construct($model, $resource); |
15
|
|
|
$this->config = new VocabularyConfig($model, $resource, $model->getConfig()->getGlobalPlugins()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns the VocabularyConfig object |
20
|
|
|
* @return VocabularyConfig |
21
|
|
|
*/ |
22
|
|
|
public function getConfig() |
23
|
|
|
{ |
24
|
|
|
return $this->config; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the SPARQL endpoint URL for this vocabulary |
29
|
|
|
* |
30
|
|
|
* @return string endpoint URL |
31
|
|
|
*/ |
32
|
|
|
public function getEndpoint() |
33
|
|
|
{ |
34
|
|
|
$endpoint = $this->config->getSparqlEndpoint(); |
35
|
|
|
if ($endpoint === null) { |
36
|
|
|
$endpoint = $this->model->getConfig()->getDefaultEndpoint(); |
37
|
|
|
} |
38
|
|
|
return $endpoint; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the SPARQL graph URI for this vocabulary |
43
|
|
|
* |
44
|
|
|
* @return string|null graph URI |
45
|
|
|
*/ |
46
|
|
|
public function getGraph() |
47
|
|
|
{ |
48
|
|
|
return $this->config->getSparqlGraph(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the SPARQL implementation for this vocabulary |
53
|
|
|
* |
54
|
|
|
* @return GenericSparql SPARQL object |
55
|
|
|
*/ |
56
|
|
|
public function getSparql() |
57
|
|
|
{ |
58
|
|
|
$endpoint = $this->getEndpoint(); |
59
|
|
|
$graph = $this->getGraph(); |
60
|
|
|
$dialect = $this->config->getSparqlDialect() ?? $this->model->getConfig()->getDefaultSparqlDialect(); |
61
|
|
|
|
62
|
|
|
return $this->model->getSparqlImplementation($dialect, $endpoint, $graph); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the URI space of concepts in this vocabulary. |
67
|
|
|
* |
68
|
|
|
* @return string full URI of concept |
69
|
|
|
*/ |
70
|
|
|
public function getUriSpace() |
71
|
|
|
{ |
72
|
|
|
if ($this->urispace === null) { // initialize cache |
73
|
|
|
$urispace = $this->resource->getLiteral('void:uriSpace'); |
74
|
|
|
if ($urispace) { |
|
|
|
|
75
|
|
|
$this->urispace = $urispace->getValue(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->urispace; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return true if the URI is within the URI space of this vocabulary. |
84
|
|
|
* |
85
|
|
|
* @param string full URI of concept |
|
|
|
|
86
|
|
|
* @return bool true if URI is within URI namespace, false otherwise |
87
|
|
|
*/ |
88
|
|
|
|
89
|
|
|
public function containsURI($uri) |
90
|
|
|
{ |
91
|
|
|
return (strpos($uri, $this->getUriSpace()) === 0); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the full URI of a concept in a vocabulary. If the passed local |
96
|
|
|
* name is already a full URI, return it unchanged. |
97
|
|
|
* |
98
|
|
|
* @param $lname string local name of concept |
99
|
|
|
* @return string full URI of concept |
100
|
|
|
*/ |
101
|
|
|
public function getConceptURI($lname) |
102
|
|
|
{ |
103
|
|
|
if (strpos($lname, 'http') === 0) { |
104
|
|
|
return $lname; |
105
|
|
|
} |
106
|
|
|
// already a full URI |
107
|
|
|
return $this->getUriSpace() . $lname; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Asks the sparql implementation to make a label query for a uri. |
112
|
|
|
* @param string $uri |
113
|
|
|
* @param string $lang |
114
|
|
|
*/ |
115
|
|
|
public function getConceptLabel($uri, $lang) |
116
|
|
|
{ |
117
|
|
|
return $this->getSparql()->queryLabel($uri, $lang); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Asks the sparql implementation to make a label query for a uri. |
122
|
|
|
* @param string $uri |
123
|
|
|
* @param string $lang |
124
|
|
|
* @return array array of altLabels |
125
|
|
|
*/ |
126
|
|
|
public function getAllConceptLabels($uri, $lang) |
127
|
|
|
{ |
128
|
|
|
return $this->getSparql()->queryAllConceptLabels($uri, $lang); |
129
|
|
|
} |
130
|
|
|
/** |
131
|
|
|
* Get the localname of a concept in the vocabulary. If the URI is not |
132
|
|
|
* in the URI space of this vocabulary, return the full URI. |
133
|
|
|
* |
134
|
|
|
* @param $uri string full URI of concept |
135
|
|
|
* @return string local name of concept, or original full URI if the local name cannot be determined |
136
|
|
|
*/ |
137
|
|
|
public function getLocalName($uri) |
138
|
|
|
{ |
139
|
|
|
return str_replace($this->getUriSpace(), "", $uri); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieves all the information about the Vocabulary |
144
|
|
|
* from the SPARQL-endpoint. |
145
|
|
|
*/ |
146
|
|
|
public function getInfo($lang = null) |
147
|
|
|
{ |
148
|
|
|
$ret = array(); |
149
|
|
|
if (!$lang) { |
150
|
|
|
$lang = $this->getLang(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// get metadata (literals only e.g. name) from vocabulary configuration file |
154
|
|
|
foreach ($this->resource->properties() as $prop) { |
155
|
|
|
foreach ($this->resource->allLiterals($prop, $lang) as $val) { |
156
|
|
|
$ret[$prop][] = $val->getValue(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
try { |
161
|
|
|
// also include ConceptScheme metadata from SPARQL endpoint |
162
|
|
|
$defaultcs = $this->getDefaultConceptScheme(); |
163
|
|
|
|
164
|
|
|
// query everything the endpoint knows about the ConceptScheme |
165
|
|
|
$sparql = $this->getSparql(); |
166
|
|
|
$result = $sparql->queryConceptScheme($defaultcs); |
167
|
|
|
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) { |
168
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
169
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
170
|
|
|
} |
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
$conceptscheme = $result->resource($defaultcs); |
174
|
|
|
$this->order = array( |
175
|
|
|
"dc:title", "dc11:title", "skos:prefLabel", "rdfs:label", |
176
|
|
|
"dc:subject", "dc11:subject", |
177
|
|
|
"dc:description", "dc11:description", |
178
|
|
|
"foaf:homepage", |
179
|
|
|
"dc:publisher", "dc11:publisher", |
180
|
|
|
"dc:creator", "dc11:creator", |
181
|
|
|
"dc:contributor", |
182
|
|
|
"dc:license", |
183
|
|
|
"dc:rights", "dc11:rights", |
184
|
|
|
"dc:language", "dc11:language", |
185
|
|
|
"owl:versionInfo", |
186
|
|
|
"dc:source", "dc11:source", |
187
|
|
|
"dc:relation", "dc11:relation", |
188
|
|
|
"dc:created", |
189
|
|
|
"dc:modified", |
190
|
|
|
"dc:date", "dc11:date" |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
foreach ($conceptscheme->properties() as $prop) { |
194
|
|
|
foreach ($conceptscheme->allLiterals($prop, $lang) as $val) { |
195
|
|
|
$prop = (substr($prop, 0, 5) == 'dc11:') ? str_replace('dc11:', 'dc:', $prop) : $prop; |
196
|
|
|
$ret[$prop][$val->getValue()] = $val; |
197
|
|
|
} |
198
|
|
|
if (!isset($ret[$prop]) || sizeof($ret[$prop]) == 0) { // not found with language tag |
199
|
|
|
foreach ($conceptscheme->allLiterals($prop, null) as $val) { |
200
|
|
|
$prop = (substr($prop, 0, 5) == 'dc11:') ? str_replace('dc11:', 'dc:', $prop) : $prop; |
201
|
|
|
if ($val->getValue() instanceof DateTime) { |
202
|
|
|
$val = Punic\Calendar::formatDate($val->getValue(), 'full', $lang) . ' ' . Punic\Calendar::format($val->getValue(), 'HH:mm:ss', $lang); |
203
|
|
|
} |
204
|
|
|
$ret[$prop][] = $val; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
foreach ($conceptscheme->allResources($prop) as $val) { |
208
|
|
|
$prop = (substr($prop, 0, 5) == 'dc11:') ? str_replace('dc11:', 'dc:', $prop) : $prop; |
209
|
|
|
$exvocab = $this->model->guessVocabularyFromURI($val->getURI()); |
210
|
|
|
$exlabel = $this->getExternalLabel($exvocab, $val->getURI(), $lang); |
211
|
|
|
if (isset($exlabel)) { |
212
|
|
|
$val->add('skosmos:vocab', $exvocab->getId()); |
213
|
|
|
$val->add('skosmos:label', $exlabel); |
214
|
|
|
} |
215
|
|
|
$label = $val->label($lang) ? $val->label($lang)->getValue() : $val->getUri(); |
216
|
|
|
$ret[$prop][$exlabel ? $exlabel->getValue() : $label] = $val; |
217
|
|
|
} |
218
|
|
|
if (isset($ret[$prop])) { |
219
|
|
|
ksort($ret[$prop]); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
if (isset($ret['owl:versionInfo'])) { // if version info available for vocabulary convert it to a more readable format |
223
|
|
|
$ret['owl:versionInfo'][0] = $this->parseVersionInfo($ret['owl:versionInfo'][0]); |
224
|
|
|
} |
225
|
|
|
// remove duplicate values |
226
|
|
|
foreach (array_keys($ret) as $prop) { |
227
|
|
|
$ret[$prop] = array_unique($ret[$prop]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$ret = $this->arbitrarySort($ret); |
231
|
|
|
|
232
|
|
|
// filtering multiple labels |
233
|
|
|
if (isset($ret['dc:title'])) { |
234
|
|
|
unset($ret['dc11:title'], $ret['skos:prefLabel'], $ret['rdfs:label']); |
235
|
|
|
} elseif (isset($ret['dc11:title'])) { |
236
|
|
|
unset($ret['skos:prefLabel'], $ret['rdfs:label']); |
237
|
|
|
} elseif (isset($ret['skos:prefLabel'])) { |
238
|
|
|
unset($ret['rdfs:label']); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $ret; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Return all concept schemes in the vocabulary. |
246
|
|
|
* @return array Array with concept scheme URIs (string) as keys and labels (string) as values |
247
|
|
|
*/ |
248
|
|
|
|
249
|
|
|
public function getConceptSchemes($lang = '') |
250
|
|
|
{ |
251
|
|
|
if ($lang === '') { |
252
|
|
|
$lang = $this->getLang(); |
253
|
|
|
} |
254
|
|
|
$conceptSchemes = null; |
|
|
|
|
255
|
|
|
try { |
256
|
|
|
$conceptSchemes = $this->getSparql()->queryConceptSchemes($lang); |
257
|
|
|
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) { |
258
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
259
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
return $conceptSchemes; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Return the URI of the default concept scheme of this vocabulary. If the skosmos:mainConceptScheme property is set in the |
267
|
|
|
* vocabulary configuration, that will be returned. Otherwise an arbitrary concept scheme will be returned. |
268
|
|
|
* @return string concept scheme URI |
269
|
|
|
*/ |
270
|
|
|
|
271
|
|
|
public function getDefaultConceptScheme() |
272
|
|
|
{ |
273
|
|
|
$conceptScheme = $this->config->getMainConceptSchemeURI(); |
274
|
|
|
if ($conceptScheme) { |
275
|
|
|
return $conceptScheme; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// mainConceptScheme not explicitly set, guess it |
279
|
|
|
$conceptSchemes = $this->getConceptSchemes(); |
280
|
|
|
$conceptSchemeURIs = array_keys($conceptSchemes); |
281
|
|
|
// return the URI of the last concept scheme |
282
|
|
|
return array_pop($conceptSchemeURIs); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Returns the main Concept Scheme of that Vocabulary, or null if not set. |
287
|
|
|
* @param string $defaultConceptSchemeURI default concept scheme URI |
288
|
|
|
* @return \EasyRdf\Graph|mixed |
289
|
|
|
*/ |
290
|
|
|
public function getConceptScheme(string $defaultConceptSchemeURI) |
291
|
|
|
{ |
292
|
|
|
return $this->getSparql()->queryConceptScheme($defaultConceptSchemeURI); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Return the top concepts of a concept scheme in the vocabulary. |
297
|
|
|
* @param string $conceptScheme URI of concept scheme whose top concepts to return. If not set, |
298
|
|
|
* the default concept scheme of the vocabulary will be used. |
299
|
|
|
* @param string $lang preferred language for the concept labels, |
300
|
|
|
* @return array Array with concept URIs (string) as keys and labels (string) as values |
301
|
|
|
*/ |
302
|
|
|
|
303
|
|
|
public function getTopConcepts($conceptScheme = null, $lang = '') |
304
|
|
|
{ |
305
|
|
|
if ($lang === '') { |
306
|
|
|
$lang = $this->getLang(); |
307
|
|
|
} |
308
|
|
|
$fallback = $this->config->getDefaultLanguage(); |
309
|
|
|
|
310
|
|
|
if ($conceptScheme === null || $conceptScheme == '') { |
311
|
|
|
$conceptScheme = $this->getDefaultConceptScheme(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $this->getSparql()->queryTopConcepts($conceptScheme, $lang, $fallback); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Tries to parse version, date and time from sparql version information into a readable format. |
319
|
|
|
* @param string $version |
320
|
|
|
* @return string |
321
|
|
|
*/ |
322
|
|
|
private function parseVersionInfo($version) |
323
|
|
|
{ |
324
|
|
|
$parts = explode(' ', $version); |
325
|
|
|
if ($parts[0] != '$Id:') { |
326
|
|
|
return $version; |
327
|
|
|
} |
328
|
|
|
// don't know how to parse |
329
|
|
|
$rev = $parts[2]; |
330
|
|
|
$datestr = $parts[3] . ' ' . $parts[4]; |
331
|
|
|
|
332
|
|
|
return "$datestr (r$rev)"; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Counts the statistics of the vocabulary. |
337
|
|
|
* @return Array containing the label counts |
338
|
|
|
* @param string $array the uri of the concept array class, eg. isothes:ThesaurusArray |
339
|
|
|
* @param string $group the uri of the concept group class, eg. isothes:ConceptGroup |
340
|
|
|
*/ |
341
|
|
|
public function getStatistics($lang = '', $array = null, $group = null) |
342
|
|
|
{ |
343
|
|
|
$sparql = $this->getSparql(); |
344
|
|
|
// find the number of concepts |
345
|
|
|
return $sparql->countConcepts($lang, $array, $group); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Counts the statistics of the vocabulary. |
350
|
|
|
* @return array of the concept counts in different languages |
351
|
|
|
*/ |
352
|
|
|
public function getLabelStatistics() |
353
|
|
|
{ |
354
|
|
|
$sparql = $this->getSparql(); |
355
|
|
|
$ret = array(); |
356
|
|
|
// count the number of different types of concepts in all languages |
357
|
|
|
$ret['terms'] = $sparql->countLangConcepts($this->config->getLanguages(), $this->config->getIndexClasses()); |
358
|
|
|
|
359
|
|
|
return $ret; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Gets the parent concepts of a concept and child concepts for all of those. |
364
|
|
|
* @param string $uri |
365
|
|
|
* @param string $lang language identifier. |
366
|
|
|
*/ |
367
|
|
|
public function getConceptHierarchy($uri, $lang) |
368
|
|
|
{ |
369
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
370
|
|
|
$fallback = count($this->config->getLanguageOrder($lang)) > 1 ? $this->config->getLanguageOrder($lang)[1] : $this->config->getDefaultLanguage(); |
371
|
|
|
$props = $this->config->getHierarchyProperty(); |
372
|
|
|
return $this->getSparql()->queryParentList($uri, $lang, $fallback, $props); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Gets the child relations of a concept and whether these children have more children. |
377
|
|
|
* @param string $uri |
378
|
|
|
*/ |
379
|
|
|
public function getConceptChildren($uri, $lang) |
380
|
|
|
{ |
381
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
382
|
|
|
$fallback = count($this->config->getLanguageOrder($lang)) > 1 ? $this->config->getLanguageOrder($lang)[1] : $this->config->getDefaultLanguage(); |
383
|
|
|
$props = $this->config->getHierarchyProperty(); |
384
|
|
|
return $this->getSparql()->queryChildren($uri, $lang, $fallback, $props); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Gets the skos:narrower relations of a concept. |
389
|
|
|
* @param string $uri |
390
|
|
|
* @param string $lang language identifier. |
391
|
|
|
*/ |
392
|
|
|
public function getConceptNarrowers($uri, $lang) |
393
|
|
|
{ |
394
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
395
|
|
|
return $this->getSparql()->queryProperty($uri, 'skos:narrower', $lang); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Gets the skos:narrowerTransitive relations of a concept. |
400
|
|
|
* @param string $uri |
401
|
|
|
* @param integer $limit |
402
|
|
|
* @param string $lang language identifier. |
403
|
|
|
*/ |
404
|
|
|
public function getConceptTransitiveNarrowers($uri, $limit, $lang) |
405
|
|
|
{ |
406
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
407
|
|
|
return $this->getSparql()->queryTransitiveProperty($uri, array('skos:narrower'), $lang, $limit); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Gets the skos:broader relations of a concept. |
412
|
|
|
* @param string $uri |
413
|
|
|
* @param string $lang language identifier. |
414
|
|
|
*/ |
415
|
|
|
public function getConceptBroaders($uri, $lang) |
416
|
|
|
{ |
417
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
418
|
|
|
return $this->getSparql()->queryProperty($uri, 'skos:broader', $lang); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Gets the skos:broaderTransitive relations of a concept. |
423
|
|
|
* @param string $uri |
424
|
|
|
* @param integer $limit |
425
|
|
|
* @param boolean $any set to true if you want to have a label even in case of a correct language one missing. |
426
|
|
|
* @param string $lang language identifier. |
427
|
|
|
*/ |
428
|
|
|
public function getConceptTransitiveBroaders($uri, $limit, $any, $lang) |
429
|
|
|
{ |
430
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
431
|
|
|
$fallback = $this->config->getDefaultLanguage(); |
432
|
|
|
return $this->getSparql()->queryTransitiveProperty($uri, array('skos:broader'), $lang, $limit, $any, $fallback); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Gets all the skos:related concepts of a concept. |
437
|
|
|
* @param string $uri |
438
|
|
|
* @param string $lang language identifier. |
439
|
|
|
*/ |
440
|
|
|
public function getConceptRelateds($uri, $lang) |
441
|
|
|
{ |
442
|
|
|
$lang = $lang ? $lang : $this->getLang(); |
443
|
|
|
return $this->getSparql()->queryProperty($uri, 'skos:related', $lang); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Get all information about a single concept. |
448
|
|
|
* @param string $uri full URI of the concept |
449
|
|
|
* @param string $clang content language |
450
|
|
|
* @return Concept |
451
|
|
|
*/ |
452
|
|
|
public function getConceptInfo(string $uri, string $clang): Concept |
453
|
|
|
{ |
454
|
|
|
$sparql = $this->getSparql(); |
455
|
|
|
$conceptInfo = null; |
|
|
|
|
456
|
|
|
try { |
457
|
|
|
$conceptInfo = $sparql->queryConceptInfo([$uri], $this->config->getArrayClassURI(), array($this), $clang); |
458
|
|
|
} catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) { |
459
|
|
|
if ($this->model->getConfig()->getLogCaughtExceptions()) { |
460
|
|
|
error_log('Caught exception: ' . $e->getMessage()); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
return $conceptInfo[0]; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Lists the different concept groups available in the vocabulary. |
468
|
|
|
* @param string $clang content language parameter |
469
|
|
|
* @return array |
470
|
|
|
*/ |
471
|
|
|
public function listConceptGroups($clang = null) |
472
|
|
|
{ |
473
|
|
|
if ($clang === null || $clang == '') { |
474
|
|
|
$clang = $this->getLang(); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
$ret = array(); |
478
|
|
|
$gclass = $this->config->getGroupClassURI(); |
479
|
|
|
if ($gclass === null) { |
|
|
|
|
480
|
|
|
return $ret; |
481
|
|
|
} |
482
|
|
|
// no group class defined, so empty result |
483
|
|
|
$groups = $this->getSparql()->listConceptGroups($gclass, $clang); |
484
|
|
|
foreach ($groups as $uri => $label) { |
485
|
|
|
$ret[$uri] = $label; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
return $ret; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Lists the concepts available in the concept group. |
493
|
|
|
* @param $clname |
494
|
|
|
* @return array |
495
|
|
|
*/ |
496
|
|
|
public function listConceptGroupContents($glname, $clang) |
497
|
|
|
{ |
498
|
|
|
if (!$clang) { |
499
|
|
|
$clang = $this->getLang(); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$ret = array(); |
503
|
|
|
$gclass = $this->config->getGroupClassURI(); |
504
|
|
|
if ($gclass === null) { |
|
|
|
|
505
|
|
|
return $ret; |
506
|
|
|
} |
507
|
|
|
// no group class defined, so empty result |
508
|
|
|
$group = $this->getConceptURI($glname); |
509
|
|
|
$contents = $this->getSparql()->listConceptGroupContents($gclass, $group, $clang, $this->config->getShowDeprecated()); |
510
|
|
|
foreach ($contents as $uri => $label) { |
511
|
|
|
$ret[$uri] = $label; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
return $ret; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Returns the letters of the alphabet which have been used in this vocabulary. |
519
|
|
|
* The returned letters may also include specials such as '0-9' (digits) and '!*' (special characters). |
520
|
|
|
* @param $clang content language |
|
|
|
|
521
|
|
|
* @return array array of letters |
522
|
|
|
*/ |
523
|
|
|
public function getAlphabet($clang) |
524
|
|
|
{ |
525
|
|
|
$chars = $this->getSparql()->queryFirstCharacters($clang, $this->config->getIndexClasses()); |
526
|
|
|
$letters = array(); |
527
|
|
|
$digits = false; |
528
|
|
|
$specials = false; |
529
|
|
|
foreach ($chars as $char) { |
530
|
|
|
if (preg_match('/\p{L}/u', $char)) { |
531
|
|
|
$letters[] = $char; |
532
|
|
|
} elseif (preg_match('/\d/u', $char)) { |
533
|
|
|
$digits = true; |
534
|
|
|
} else { |
535
|
|
|
$specials = true; |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
usort($letters, 'strcoll'); |
539
|
|
|
if ($specials) { |
540
|
|
|
$letters[] = '!*'; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
if ($digits) { |
544
|
|
|
$letters[] = '0-9'; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
return $letters; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Searches for concepts with a label starting with the specified letter. |
552
|
|
|
* Also the special tokens '0-9' (digits), '!*' (special characters) and '*' |
553
|
|
|
* (everything) are supported. |
554
|
|
|
* @param $letter letter (or special token) to search for |
|
|
|
|
555
|
|
|
*/ |
556
|
|
|
public function searchConceptsAlphabetical($letter, $limit = null, $offset = null, $clang = null) |
557
|
|
|
{ |
558
|
|
|
return $this->getSparql()->queryConceptsAlphabetical($letter, $clang, $limit, $offset, $this->config->getIndexClasses(), $this->config->getShowDeprecated(), $this->config->getAlphabeticalListQualifier()); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Makes a query for the transitive broaders of a concept and returns the concepts hierarchy processed for the view. |
563
|
|
|
* @param string $lang |
564
|
|
|
* @param string $uri |
565
|
|
|
*/ |
566
|
|
|
public function getBreadCrumbs($lang, $uri) |
567
|
|
|
{ |
568
|
|
|
$broaders = $this->getConceptTransitiveBroaders($uri, 1000, true, $lang); |
569
|
|
|
$origCrumbs = $this->getCrumbs($broaders, $uri); |
570
|
|
|
return $this->combineCrumbs($origCrumbs); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Takes the crumbs as a parameter and combines the crumbs if the path they form is too long. |
575
|
|
|
* @return array |
576
|
|
|
*/ |
577
|
|
|
private function combineCrumbs($origCrumbs) |
578
|
|
|
{ |
579
|
|
|
$combined = array(); |
580
|
|
|
foreach ($origCrumbs as $pathKey => $path) { |
581
|
|
|
$firstToCombine = true; |
582
|
|
|
$combinedPath = array(); |
583
|
|
|
foreach ($path as $crumbKey => $crumb) { |
584
|
|
|
if ($crumb->getPrefLabel() === '...') { |
585
|
|
|
array_push($combinedPath, $crumb); |
586
|
|
|
if ($firstToCombine) { |
587
|
|
|
$firstToCombine = false; |
588
|
|
|
} else { |
589
|
|
|
unset($origCrumbs[$pathKey][$crumbKey]); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
$combined[] = $combinedPath; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
return array('combined' => $combined, 'breadcrumbs' => $origCrumbs); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* Recursive function for building the breadcrumb paths for the view. |
601
|
|
|
* @param array $bTresult contains the results of the broaderTransitive query. |
602
|
|
|
* @param string $uri |
603
|
|
|
* @param array $path |
604
|
|
|
*/ |
605
|
|
|
private function getCrumbs($bTresult, $uri, $path = null) |
606
|
|
|
{ |
607
|
|
|
$crumbs = array(); |
608
|
|
|
if (!isset($path)) { |
609
|
|
|
$path = array(); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
// check that there is no cycle (issue #220) |
613
|
|
|
foreach ($path as $childcrumb) { |
614
|
|
|
if ($childcrumb->getUri() == $uri) { |
615
|
|
|
// found a cycle - short-circuit and stop |
616
|
|
|
return $crumbs; |
617
|
|
|
} |
618
|
|
|
} |
619
|
|
|
if (isset($bTresult[$uri]['direct'])) { |
620
|
|
|
foreach ($bTresult[$uri]['direct'] as $broaderUri) { |
621
|
|
|
$newpath = array_merge($path, array(new Breadcrumb($uri, $bTresult[$uri]['label']))); |
622
|
|
|
if ($uri !== $broaderUri) { |
623
|
|
|
$crumbs = array_merge($crumbs, $this->getCrumbs($bTresult, $broaderUri, $newpath)); |
624
|
|
|
} |
625
|
|
|
} |
626
|
|
|
} else { // we have reached the end of a path and we need to start a new row in the 'stack' |
627
|
|
|
if (isset($bTresult[$uri])) { |
628
|
|
|
$path = array_merge($path, array(new Breadcrumb($uri, $bTresult[$uri]['label']))); |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
$index = 1; |
632
|
|
|
$length = sizeof($path); |
633
|
|
|
$limit = $length - 5; |
634
|
|
|
foreach ($path as $crumb) { |
635
|
|
|
if ($length > 5 && $index > $length - $limit) { // displays 5 concepts closest to the concept. |
636
|
|
|
$crumb->hideLabel(); |
637
|
|
|
} |
638
|
|
|
$index++; |
639
|
|
|
} |
640
|
|
|
$crumbs[] = array_reverse($path); |
641
|
|
|
} |
642
|
|
|
return $crumbs; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Verify that the requested language is supported by the vocabulary. If not, returns |
647
|
|
|
* the default language of the vocabulary. |
648
|
|
|
* @param string $lang language to check |
649
|
|
|
* @return string language tag that is supported by the vocabulary |
650
|
|
|
*/ |
651
|
|
|
|
652
|
|
|
public function verifyVocabularyLanguage($lang) |
653
|
|
|
{ |
654
|
|
|
return (in_array($lang, $this->config->getLanguages())) ? $lang : $this->config->getDefaultLanguage(); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* Returns a list of recently changed or entirely new concepts. |
659
|
|
|
* @param string $prop the property uri pointing to timestamps, eg. 'dc:modified' |
660
|
|
|
* @param string $clang content language for the labels |
661
|
|
|
* @param int $offset starting index offset |
662
|
|
|
* @param int $limit maximum number of concepts to return |
663
|
|
|
* @return Array |
664
|
|
|
*/ |
665
|
|
|
public function getChangeList($prop, $clang, $offset, $limit) |
666
|
|
|
{ |
667
|
|
|
$showDeprecated = $this->getConfig()->getShowDeprecatedChanges(); |
668
|
|
|
return $this->getSparql()->queryChangeList($prop, $clang, $offset, $limit, $showDeprecated); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
public function getTitle($lang = null) |
672
|
|
|
{ |
673
|
|
|
return $this->config->getTitle($lang); |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
public function getShortName() |
677
|
|
|
{ |
678
|
|
|
return $this->config->getShortName(); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
public function getId() |
682
|
|
|
{ |
683
|
|
|
return $this->config->getId(); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
public function getModifiedDate() |
687
|
|
|
{ |
688
|
|
|
$modifiedDate = null; |
689
|
|
|
|
690
|
|
|
$conceptSchemeURI = $this->getDefaultConceptScheme(); |
691
|
|
|
if ($conceptSchemeURI) { |
692
|
|
|
$conceptSchemeGraph = $this->getConceptScheme($conceptSchemeURI); |
693
|
|
|
if (!$conceptSchemeGraph->isEmpty()) { |
694
|
|
|
$literal = $conceptSchemeGraph->getLiteral($conceptSchemeURI, "dc:modified"); |
695
|
|
|
if ($literal) { |
|
|
|
|
696
|
|
|
$modifiedDate = $literal->getValue(); |
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
return $modifiedDate; |
|
|
|
|
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
public function isUseModifiedDate() |
705
|
|
|
{ |
706
|
|
|
return $this->getConfig()->isUseModifiedDate(); |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|