|
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
|
|
|
* Documents |
|
52
|
|
|
* |
|
53
|
|
|
* @var RemoteDocument[] |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $documents = []; |
|
56
|
|
|
/** |
|
57
|
|
|
* Vocabularies |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $vocabularies = []; |
|
62
|
|
|
/** |
|
63
|
|
|
* Vocabulary prefices |
|
64
|
|
|
* |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $prefices = []; |
|
68
|
|
|
/** |
|
69
|
|
|
* Document cache slot |
|
70
|
|
|
* |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
const SLOT_DOC = 'jsonld.doc'; |
|
74
|
|
|
/** |
|
75
|
|
|
* Vocabulary cache slot |
|
76
|
|
|
* |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
const SLOT_VOCABS = 'jsonld.vocabs'; |
|
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
|
|
|
* Cache a document |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $url URL |
|
103
|
|
|
* @param RemoteDocument $document Document |
|
104
|
|
|
* @return RemoteDocument Document |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function setDocument($url, RemoteDocument $document) |
|
107
|
|
|
{ |
|
108
|
|
|
// Process the context |
|
109
|
1 |
|
if (isset($document->document) && is_object($document->document)) { |
|
110
|
1 |
|
if (isset($document->document->{'@context'}) && is_object($document->document->{'@context'})) { |
|
111
|
1 |
|
$this->processContext((array)$document->document->{'@context'}); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Save the document to the cache |
|
116
|
1 |
|
$docUrlHash = $this->getCacheHash($url, self::SLOT_DOC); |
|
117
|
1 |
|
$cachedDocument = Cache::getAdapter()->getItem($docUrlHash); |
|
118
|
1 |
|
$cachedDocument->set($document); |
|
119
|
1 |
|
Cache::getAdapter()->save($cachedDocument); |
|
120
|
|
|
|
|
121
|
|
|
// Return the document |
|
122
|
1 |
|
return $document; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Process a context vocabulary |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $context Context |
|
129
|
|
|
*/ |
|
130
|
1 |
|
protected function processContext(array $context) |
|
131
|
|
|
{ |
|
132
|
1 |
|
$prefices = []; |
|
133
|
1 |
|
$vocabularyCache = Cache::getAdapter()->getItem(self::SLOT_VOCABS); |
|
134
|
1 |
|
$vocabularies = $vocabularyCache->isHit() ? $vocabularyCache->get() : []; |
|
135
|
|
|
|
|
136
|
|
|
// Run through all vocabulary terms |
|
137
|
1 |
|
foreach ($context as $name => $definition) { |
|
138
|
|
|
// Skip JSON-LD reserved terms |
|
139
|
1 |
|
if (!strncmp('@', $name, 1) || (is_string($definition) && !strncmp('@', $definition, 1))) { |
|
140
|
1 |
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// Process this prefix / vocabulary term |
|
144
|
1 |
|
$this->processPrefixVocabularyTerm($name, $definition, $prefices, $vocabularies); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
$vocabularyCache->set($vocabularies); |
|
148
|
1 |
|
Cache::getAdapter()->save($vocabularyCache); |
|
149
|
1 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Process a prefix / vocabulary term |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $name Prefix name |
|
155
|
|
|
* @param string|\stdClass $definition Definition |
|
156
|
|
|
* @param array $prefices Prefix register |
|
157
|
|
|
* @param array $vocabularies Vocabulary register |
|
158
|
|
|
*/ |
|
159
|
1 |
|
protected function processPrefixVocabularyTerm($name, $definition, array &$prefices, array &$vocabularies) |
|
160
|
|
|
{ |
|
161
|
|
|
// Register a prefix (and vocabulary) |
|
162
|
1 |
|
if (is_string($definition) && !isset($prefices[$name])) { |
|
163
|
1 |
|
$this->processPrefix($name, $definition, $prefices, $vocabularies); |
|
164
|
|
|
|
|
165
|
|
|
// Else: Register vocabulary term |
|
166
|
1 |
|
} elseif (is_object($definition) && isset($definition->{'@id'})) { |
|
167
|
1 |
|
$this->processVocabularyTerm($definition, $prefices, $vocabularies); |
|
168
|
|
|
} |
|
169
|
1 |
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Process a vocabulary prefix |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $name Prefix name |
|
175
|
|
|
* @param string $definition Prefix definition |
|
176
|
|
|
* @param array $prefices Prefix register |
|
177
|
|
|
* @param array $vocabularies Vocabulary register |
|
178
|
|
|
*/ |
|
179
|
1 |
|
protected function processPrefix($name, $definition, array &$prefices, array &$vocabularies) |
|
180
|
|
|
{ |
|
181
|
1 |
|
$prefices[$name] = $definition; |
|
182
|
|
|
|
|
183
|
|
|
// Register the vocabulary |
|
184
|
1 |
|
if (!isset($vocabularies[$definition])) { |
|
185
|
1 |
|
$vocabularies[$definition] = []; |
|
186
|
|
|
} |
|
187
|
1 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Process a vocabulary term |
|
191
|
|
|
* |
|
192
|
|
|
* @param \stdClass $definition Term definition |
|
193
|
|
|
* @param array $prefices Prefix register |
|
194
|
|
|
* @param array $vocabularies Vocabulary register |
|
195
|
|
|
*/ |
|
196
|
1 |
|
protected function processVocabularyTerm($definition, array &$prefices, array &$vocabularies) |
|
197
|
|
|
{ |
|
198
|
1 |
|
$prefixName = explode(':', $definition->{'@id'}, 2); |
|
199
|
1 |
|
if (count($prefixName) == 2) { |
|
200
|
1 |
|
if (isset($prefices[$prefixName[0]])) { |
|
201
|
1 |
|
$vocabularies[$prefices[$prefixName[0]]][$prefixName[1]] = true; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
1 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Create an IRI from a name considering the known vocabularies |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $name Name |
|
210
|
|
|
* @return \stdClass IRI |
|
211
|
|
|
*/ |
|
212
|
2 |
|
public function expandIRI($name) |
|
213
|
|
|
{ |
|
214
|
2 |
|
$iri = (object)['name' => $name, 'profile' => '']; |
|
215
|
2 |
|
$vocabularies = Cache::getAdapter()->getItem(self::SLOT_VOCABS); |
|
216
|
|
|
|
|
217
|
|
|
// Run through all vocabularies |
|
218
|
2 |
|
if ($vocabularies->isHit()) { |
|
219
|
1 |
|
foreach ($vocabularies->get() as $profile => $terms) { |
|
220
|
1 |
|
$profileLength = strlen($profile); |
|
221
|
1 |
|
if (!strncasecmp($profile, $name, $profileLength) && !empty($terms[substr($name, $profileLength)])) { |
|
222
|
1 |
|
$iri->profile = $profile; |
|
223
|
1 |
|
$iri->name = substr($name, $profileLength); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
2 |
|
return $iri; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Create a cache hash |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $str String |
|
235
|
|
|
* @param string $slot Slot |
|
236
|
|
|
* @return string URL hash |
|
237
|
|
|
*/ |
|
238
|
1 |
|
protected function getCacheHash($str, $slot) |
|
239
|
|
|
{ |
|
240
|
1 |
|
return $slot.'.'.md5($str); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|