1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Rdfalite |
8
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Micrometa\Infrastructure\Parser\JsonLD; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Ports\Cache; |
40
|
|
|
use ML\JsonLD\RemoteDocument; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Vocabulary cache |
44
|
|
|
* |
45
|
|
|
* @package Jkphl\Rdfalite |
46
|
|
|
* @subpackage Jkphl\Micrometa\Infrastructure |
47
|
|
|
*/ |
48
|
|
|
class VocabularyCache |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* Document cache slot |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
const SLOT_DOC = 'jsonld.doc'; |
56
|
|
|
/** |
57
|
|
|
* Vocabulary cache slot |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
const SLOT_VOCABS = 'jsonld.vocabs'; |
62
|
|
|
/** |
63
|
|
|
* Documents |
64
|
|
|
* |
65
|
|
|
* @var RemoteDocument[] |
66
|
|
|
*/ |
67
|
|
|
protected $documents = []; |
68
|
|
|
/** |
69
|
|
|
* Vocabularies |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $vocabularies = []; |
74
|
|
|
/** |
75
|
|
|
* Vocabulary prefices |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $prefices = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return a cached document |
83
|
|
|
* |
84
|
|
|
* @param string $url URL |
85
|
|
|
* @return RemoteDocument|null Cached document |
86
|
|
|
*/ |
87
|
1 |
|
public function getDocument($url) |
88
|
|
|
{ |
89
|
1 |
|
$urlHash = $this->getCacheHash($url, self::SLOT_DOC); |
90
|
|
|
|
91
|
|
|
// Try to retrieve the document from the cache |
92
|
1 |
|
if (Cache::getAdapter()->hasItem($urlHash)) { |
93
|
1 |
|
return Cache::getAdapter()->getItem($urlHash)->get(); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create a cache hash |
101
|
|
|
* |
102
|
|
|
* @param string $str String |
103
|
|
|
* @param string $slot Slot |
104
|
|
|
* @return string URL hash |
105
|
|
|
*/ |
106
|
1 |
|
protected function getCacheHash($str, $slot) |
107
|
|
|
{ |
108
|
1 |
|
return $slot.'.'.md5($str); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Cache a document |
113
|
|
|
* |
114
|
|
|
* @param string $url URL |
115
|
|
|
* @param RemoteDocument $document Document |
116
|
|
|
* @return RemoteDocument Document |
117
|
|
|
*/ |
118
|
1 |
|
public function setDocument($url, RemoteDocument $document) |
119
|
|
|
{ |
120
|
|
|
// Process the context |
121
|
1 |
|
if (isset($document->document->{'@context'}) && is_object($document->document->{'@context'})) { |
122
|
1 |
|
$this->processContext((array)$document->document->{'@context'}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Save the document to the cache |
126
|
1 |
|
$docUrlHash = $this->getCacheHash($url, self::SLOT_DOC); |
127
|
1 |
|
$cachedDocument = Cache::getAdapter()->getItem($docUrlHash); |
128
|
1 |
|
$cachedDocument->set($document); |
129
|
1 |
|
Cache::getAdapter()->save($cachedDocument); |
130
|
|
|
|
131
|
|
|
// Return the document |
132
|
1 |
|
return $document; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Process a context vocabulary |
137
|
|
|
* |
138
|
|
|
* @param array $context Context |
139
|
|
|
*/ |
140
|
1 |
|
protected function processContext(array $context) |
141
|
|
|
{ |
142
|
1 |
|
$prefices = []; |
143
|
1 |
|
$vocabularyCache = Cache::getAdapter()->getItem(self::SLOT_VOCABS); |
144
|
1 |
|
$vocabularies = $vocabularyCache->isHit() ? $vocabularyCache->get() : []; |
145
|
|
|
|
146
|
|
|
// Run through all vocabulary terms |
147
|
1 |
|
foreach ($context as $name => $definition) { |
148
|
|
|
// Skip JSON-LD reserved terms |
149
|
1 |
|
if ($this->isReservedTokens($name, $definition)) { |
150
|
1 |
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Process this prefix / vocabulary term |
154
|
1 |
|
$this->processPrefixVocabularyTerm($name, $definition, $prefices, $vocabularies); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$vocabularyCache->set($vocabularies); |
158
|
1 |
|
Cache::getAdapter()->save($vocabularyCache); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Test if a vocabulary name or definition is a reserved term |
163
|
|
|
* |
164
|
|
|
* @param string $name Name |
165
|
|
|
* @param string $definition Definition |
166
|
|
|
* @return boolean Is reserved term |
167
|
|
|
*/ |
168
|
1 |
|
protected function isReservedTokens($name, $definition) |
169
|
|
|
{ |
170
|
1 |
|
return !strncmp('@', $name, 1) || (is_string($definition) && !strncmp('@', $definition, 1)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Process a prefix / vocabulary term |
175
|
|
|
* |
176
|
|
|
* @param string $name Prefix name |
177
|
|
|
* @param string|\stdClass $definition Definition |
178
|
|
|
* @param array $prefices Prefix register |
179
|
|
|
* @param array $vocabularies Vocabulary register |
180
|
|
|
*/ |
181
|
1 |
|
protected function processPrefixVocabularyTerm($name, $definition, array &$prefices, array &$vocabularies) |
182
|
|
|
{ |
183
|
|
|
// Register a prefix (and vocabulary) |
184
|
1 |
|
if ($this->isPrefix($name, $definition, $prefices)) { |
185
|
1 |
|
$this->processPrefix($name, strval($definition), $prefices, $vocabularies); |
186
|
|
|
|
187
|
|
|
// Else: Register vocabulary term |
188
|
1 |
|
} elseif ($this->isTerm($definition)) { |
189
|
1 |
|
$this->processVocabularyTerm((object)$definition, $prefices, $vocabularies); |
190
|
|
|
} |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Test whether this is a prefix and vocabulary definition |
195
|
|
|
* |
196
|
|
|
* @param string $name Prefix name |
197
|
|
|
* @param string|\stdClass $definition Definition |
198
|
|
|
* @param array $prefices Prefix register |
199
|
|
|
* @return bool Is a prefix and vocabulary definition |
200
|
|
|
*/ |
201
|
1 |
|
protected function isPrefix($name, $definition, array &$prefices) |
202
|
|
|
{ |
203
|
1 |
|
return is_string($definition) && !isset($prefices[$name]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Process a vocabulary prefix |
208
|
|
|
* |
209
|
|
|
* @param string $name Prefix name |
210
|
|
|
* @param string $definition Prefix definition |
211
|
|
|
* @param array $prefices Prefix register |
212
|
|
|
* @param array $vocabularies Vocabulary register |
213
|
|
|
*/ |
214
|
1 |
|
protected function processPrefix($name, $definition, array &$prefices, array &$vocabularies) |
215
|
|
|
{ |
216
|
1 |
|
$prefices[$name] = $definition; |
217
|
|
|
|
218
|
|
|
// Register the vocabulary |
219
|
1 |
|
if (!isset($vocabularies[$definition])) { |
220
|
1 |
|
$vocabularies[$definition] = []; |
221
|
|
|
} |
222
|
1 |
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Test whether this is a term definition |
226
|
|
|
* |
227
|
|
|
* @param string|\stdClass $definition Definition |
228
|
|
|
* @return bool Is a term definition |
229
|
|
|
*/ |
230
|
1 |
|
protected function isTerm($definition) |
231
|
|
|
{ |
232
|
1 |
|
return is_object($definition) && isset($definition->{'@id'}); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Process a vocabulary term |
237
|
|
|
* |
238
|
|
|
* @param \stdClass $definition Term definition |
239
|
|
|
* @param array $prefices Prefix register |
240
|
|
|
* @param array $vocabularies Vocabulary register |
241
|
|
|
*/ |
242
|
1 |
|
protected function processVocabularyTerm($definition, array &$prefices, array &$vocabularies) |
243
|
|
|
{ |
244
|
1 |
|
$prefixName = explode(':', $definition->{'@id'}, 2); |
245
|
1 |
|
if (count($prefixName) == 2) { |
246
|
1 |
|
if (isset($prefices[$prefixName[0]])) { |
247
|
1 |
|
$vocabularies[$prefices[$prefixName[0]]][$prefixName[1]] = true; |
248
|
|
|
} |
249
|
|
|
} |
250
|
1 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Create an IRI from a name considering the known vocabularies |
254
|
|
|
* |
255
|
|
|
* @param string $name Name |
256
|
|
|
* @return \stdClass IRI |
257
|
|
|
*/ |
258
|
2 |
|
public function expandIRI($name) |
259
|
|
|
{ |
260
|
2 |
|
$iri = (object)['name' => $name, 'profile' => '']; |
261
|
2 |
|
$vocabularies = Cache::getAdapter()->getItem(self::SLOT_VOCABS); |
262
|
|
|
|
263
|
|
|
// Run through all vocabularies |
264
|
2 |
|
if ($vocabularies->isHit()) { |
265
|
1 |
|
$this->matchVocabularies($name, $vocabularies->get(), $iri); |
266
|
|
|
} |
267
|
|
|
|
268
|
2 |
|
return $iri; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Match a name with the known vocabularies |
273
|
|
|
* |
274
|
|
|
* @param string $name Name |
275
|
|
|
* @param array $vocabularies Vocabularies |
276
|
|
|
* @param \stdClass $iri IRI |
277
|
|
|
*/ |
278
|
1 |
|
protected function matchVocabularies($name, array $vocabularies, &$iri) |
279
|
|
|
{ |
280
|
|
|
// Run through all vocabularies |
281
|
1 |
|
foreach ($vocabularies as $profile => $terms) { |
282
|
1 |
|
$profileLength = strlen($profile); |
283
|
|
|
|
284
|
|
|
// If the name matches the profile and the remaining string is a registered term |
285
|
1 |
|
if (!strncasecmp($profile, $name, $profileLength) && !empty($terms[substr($name, $profileLength)])) { |
286
|
1 |
|
$iri->profile = $profile; |
287
|
1 |
|
$iri->name = substr($name, $profileLength); |
288
|
1 |
|
return; |
289
|
|
|
} |
290
|
|
|
} |
291
|
1 |
|
} |
292
|
|
|
} |
293
|
|
|
|