This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonNamespaces.php
213 lines (187 loc) · 6.63 KB
/
CommonNamespaces.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/*
* This file is part of Saft.
*
* (c) Konrad Abicht <[email protected]>
* (c) Natanael Arndt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Saft\Rdf;
class CommonNamespaces
{
protected $cache;
protected $namespaces = [
'bibo' => 'http://purl.org/ontology/bibo/',
'cc' => 'http://creativecommons.org/ns#',
'cert' => 'http://www.w3.org/ns/auth/cert#',
'ctag' => 'http://commontag.org/ns#',
'dc' => 'http://purl.org/dc/terms/',
'dc11' => 'http://purl.org/dc/elements/1.1/',
'dcat' => 'http://www.w3.org/ns/dcat#',
'dcterms' => 'http://purl.org/dc/terms/',
'doap' => 'http://usefulinc.com/ns/doap#',
'exif' => 'http://www.w3.org/2003/12/exif/ns#',
'foaf' => 'http://xmlns.com/foaf/0.1/',
'geo' => 'http://www.w3.org/2003/01/geo/',
'gr' => 'http://purl.org/goodrelations/v1#',
'grddl' => 'http://www.w3.org/2003/g/data-view#',
'ical' => 'http://www.w3.org/2002/12/cal/icaltzd#',
'kno' => 'https://raw.githubusercontent.com/k00ni/knorke/master/knowledge/knorke.ttl#',
'ma' => 'http://www.w3.org/ns/ma-ont#',
'og' => 'http://ogp.me/ns#',
'org' => 'http://www.w3.org/ns/org#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'prov' => 'http://www.w3.org/ns/prov#',
'qb' => 'http://purl.org/linked-data/cube#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfa' => 'http://www.w3.org/ns/rdfa#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'rev' => 'http://purl.org/stuff/rev#',
'rif' => 'http://www.w3.org/2007/rif#',
'rr' => 'http://www.w3.org/ns/r2rml#',
'rss' => 'http://purl.org/rss/1.0/',
'schema' => 'http://schema.org/',
'sd' => 'http://www.w3.org/ns/sparql-service-description#',
'sioc' => 'http://rdfs.org/sioc/ns#',
'sh' => 'http://www.w3.org/ns/shacl#',
'skos' => 'http://www.w3.org/2004/02/skos/core#',
'skosxl' => 'http://www.w3.org/2008/05/skos-xl#',
'synd' => 'http://purl.org/rss/1.0/modules/syndication/',
'v' => 'http://rdf.data-vocabulary.org/#',
'vcard' => 'http://www.w3.org/2006/vcard/ns#',
'void' => 'http://rdfs.org/ns/void#',
'wdr' => 'http://www.w3.org/2007/05/powder#',
'wdrs' => 'http://www.w3.org/2007/05/powder-s#',
'wot' => 'http://xmlns.com/wot/0.1/',
'xhv' => 'http://www.w3.org/1999/xhtml/vocab#',
'xml' => 'http://www.w3.org/XML/1998/namespace',
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
];
/**
* @param array $customPrefixes Allows you to pass further prefixes on instantiation
*/
public function __construct(array $customPrefixes = [])
{
foreach ($customPrefixes as $prefix => $uri) {
$this->add($prefix, $uri);
}
$this->cache = [];
}
/**
* @param string $prefix
* @param string $uri
*/
public function add($prefix, $uri)
{
$this->namespaces[$prefix] = $uri;
$this->cache = [];
}
/**
* @param string $shortenedUri
*
* @return string
*/
public function extendUri($shortenedUri)
{
$cacheId = 'extendUri_'.$shortenedUri;
if (false == isset($this->cache[$cacheId])) {
$parts = \explode(':', $shortenedUri);
// exactly 2 parts found. one before and one after the :
// if namespace is known (by prefix $parts[0])
if (2 == \count($parts) && isset($this->namespaces[$parts[0]])) {
$this->cache[$cacheId] = \str_replace($parts[0].':', $this->namespaces[$parts[0]], $shortenedUri);
} else {
$this->cache[$cacheId] = $shortenedUri;
}
}
return $this->cache[$cacheId];
}
/**
* @return cache
*/
public function getCache()
{
return $this->cache;
}
/**
* @return array
*/
public function getNamespaces()
{
return $this->namespaces;
}
/**
* @param string $uriToMatch
*
* @return null|string
*/
public function getPrefix($uriToMatch)
{
$cacheId = 'getPrefix_'.$uriToMatch;
if (false == isset($this->cache[$cacheId])) {
foreach ($this->namespaces as $prefix => $uri) {
if ($uriToMatch == $uri) {
$this->cache[$cacheId] = $prefix;
return $prefix;
}
}
$this->cache[$cacheId] = null;
}
return $this->cache[$cacheId];
}
public function getUri($prefix)
{
return isset($this->namespaces[$prefix]) ? $this->namespaces[$prefix] : null;
}
/**
* Very basic check if a given string is a shortened URI.
*
* @param string $string
*
* @return bool
*/
public function isShortenedUri($string)
{
return false == \strpos($string, '://') && false !== \strpos($string, ':');
}
/**
* @param string $uri
*
* @return string
*/
public function shortenUri($uri)
{
$cacheId = 'getPrefix_'.$uri;
if (false == isset($this->cache[$cacheId])) {
$longestNamespaceInfo = null;
foreach ($this->namespaces as $ns => $nsUri) {
// prefix found
if (false !== \strpos($uri, $nsUri)) {
if (null == $longestNamespaceInfo) {
$longestNamespaceInfo = [
'ns' => $ns,
'nsUri' => $nsUri,
];
continue;
}
// if we found a longer one
if (\strlen($nsUri) > \strlen($longestNamespaceInfo['nsUri'])) {
$longestNamespaceInfo = [
'ns' => $ns,
'nsUri' => $nsUri,
];
}
}
}
// prefer the prefix with the longest URI to avoid results like foo:bar/fff
if (null !== $longestNamespaceInfo) {
$this->cache[$cacheId] = \str_replace($longestNamespaceInfo['nsUri'], $longestNamespaceInfo['ns'].':', $uri);
} else {
$this->cache[$cacheId] = $uri;
}
}
return $this->cache[$cacheId];
}
}