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
|
|
|
public function getDocument($url) |
88
|
|
|
{ |
89
|
|
|
$urlHash = $this->getCacheHash($url, self::SLOT_DOC); |
90
|
|
|
|
91
|
|
|
// Try to retrieve the document from the cache |
92
|
|
|
if (Cache::getAdapter()->hasItem($urlHash)) { |
93
|
|
|
echo 'JSON-LD document cache hit'; |
94
|
|
|
return Cache::getAdapter()->getItem($urlHash)->get(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Cache a document |
102
|
|
|
* |
103
|
|
|
* @param string $url URL |
104
|
|
|
* @param RemoteDocument $document Document |
105
|
|
|
* @return RemoteDocument Document |
106
|
|
|
*/ |
107
|
|
|
public function setDocument($url, RemoteDocument $document) |
108
|
|
|
{ |
109
|
|
|
// Process the context |
110
|
|
|
if (isset($document->document) && is_object($document->document)) { |
111
|
|
|
if (isset($document->document->{'@context'}) && is_object($document->document->{'@context'})) { |
112
|
|
|
$this->processContext((array)$document->document->{'@context'}); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Save the document to the cache |
117
|
|
|
$docUrlHash = $this->getCacheHash($url, self::SLOT_DOC); |
118
|
|
|
$cachedDocument = Cache::getAdapter()->getItem($docUrlHash); |
119
|
|
|
$cachedDocument->set($document); |
120
|
|
|
Cache::getAdapter()->save($cachedDocument); |
121
|
|
|
|
122
|
|
|
// Return the document |
123
|
|
|
return $document; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Process a context vocabulary |
128
|
|
|
* |
129
|
|
|
* @param array $context Context |
130
|
|
|
*/ |
131
|
|
|
protected function processContext(array $context) |
132
|
|
|
{ |
133
|
|
|
$prefices = []; |
134
|
|
|
$vocabularyCache = Cache::getAdapter()->getItem(self::SLOT_VOCABS); |
135
|
|
|
$vocabularies = $vocabularyCache->isHit() ? $vocabularyCache->get() : []; |
136
|
|
|
|
137
|
|
|
// Run through all vocabulary terms |
138
|
|
|
foreach ($context as $name => $definition) { |
139
|
|
|
// Skip JSON-LD reserved terms |
140
|
|
|
if (!strncmp('@', $name, 1) || (is_string($definition) && !strncmp('@', $definition, 1))) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Register a prefix (and vocabulary) |
145
|
|
|
if (is_string($definition) && !isset($prefices[$name])) { |
146
|
|
|
$prefices[$name] = $definition; |
147
|
|
|
|
148
|
|
|
// Register the vocabulary |
149
|
|
|
if (!isset($vocabularies[$definition])) { |
150
|
|
|
$vocabularies[$definition] = []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Else: Register vocabulary term |
154
|
|
|
} elseif (is_object($definition) && isset($definition->{'@id'})) { |
155
|
|
|
$prefixName = explode(':', $definition->{'@id'}, 2); |
156
|
|
|
if (count($prefixName) == 2) { |
157
|
|
|
if (isset($prefices[$prefixName[0]])) { |
158
|
|
|
$vocabularies[$prefices[$prefixName[0]]][$prefixName[1]] = true; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$vocabularyCache->set($vocabularies); |
165
|
|
|
Cache::getAdapter()->save($vocabularyCache); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Create an IRI from a name considering the known vocabularies |
170
|
|
|
* |
171
|
|
|
* @param string $name Name |
172
|
|
|
* @return \stdClass IRI |
173
|
|
|
*/ |
174
|
2 |
|
public function expandIRI($name) |
175
|
|
|
{ |
176
|
2 |
|
$iri = (object)['name' => $name, 'profile' => '']; |
177
|
2 |
|
$vocabularies = Cache::getAdapter()->getItem(self::SLOT_VOCABS); |
178
|
|
|
|
179
|
|
|
// Run through all vocabularies |
180
|
2 |
|
if ($vocabularies->isHit()) { |
181
|
|
|
foreach ($vocabularies->get() as $profile => $terms) { |
182
|
|
|
$profileLength = strlen($profile); |
183
|
|
|
if (!strncasecmp($profile, $name, $profileLength) && !empty($terms[substr($name, $profileLength)])) { |
184
|
|
|
$iri->profile = $profile; |
185
|
|
|
$iri->name = substr($name, $profileLength); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
return $iri; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Create a cache hash |
195
|
|
|
* |
196
|
|
|
* @param string $str String |
197
|
|
|
* @param string $slot Slot |
198
|
|
|
* @return string URL hash |
199
|
|
|
*/ |
200
|
|
|
protected function getCacheHash($str, $slot) |
201
|
|
|
{ |
202
|
|
|
return $slot.'.'.md5($str); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|