15 |
15 |
16 /** |
16 /** |
17 * Model class for Document. Inherit from EasyRd\Resource |
17 * Model class for Document. Inherit from EasyRd\Resource |
18 * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}} |
18 * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}} |
19 */ |
19 */ |
20 class Document extends RdfModelResource implements JsonSerializable { |
20 class Document extends DocumentResult { |
21 |
21 |
22 public function __construct($uri, $graph = null) { |
22 public function __construct($uri, $graph = null) { |
23 //print($graph->dump('html')); |
23 //print($graph->dump('html')); |
24 parent::__construct($uri, $graph); |
24 parent::__construct($uri, $graph); |
25 } |
25 } |
26 |
26 |
27 private $id = null; |
|
28 |
|
29 // memoization |
|
30 private $providedCHO = null; |
|
31 private $title = false; |
|
32 private $lang = null; |
|
33 private $langResolved = null; |
|
34 private $publishers = null; |
27 private $publishers = null; |
35 private $mediaArray = null; |
28 private $mediaArray = null; |
36 private $issued = null; |
29 private $transcript = false; |
37 private $modified = null; |
|
38 private $contributors = null; |
30 private $contributors = null; |
39 private $subjects = null; |
31 private $subjects = null; |
40 |
32 |
41 public function getProvidedCHO() { |
33 protected function clearMemoizationCache() { |
42 if(is_null($this->providedCHO)) { |
34 parent::clearMemoizationCache(); |
43 $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>"); |
|
44 } |
|
45 return $this->providedCHO; |
|
46 } |
|
47 |
|
48 private function clearMemoizationCache() { |
|
49 $this->providedCHO = null; |
|
50 $this->title = false; |
|
51 $this->lang = null; |
|
52 $this->langResolved = null; |
|
53 $this->publishers = null; |
35 $this->publishers = null; |
54 $this->mediaArray = null; |
36 $this->mediaArray = null; |
55 $this->issued = null; |
|
56 $this->modified = null; |
|
57 $this->contributors = null; |
37 $this->contributors = null; |
58 $this->subjects = null; |
38 $this->subjects = null; |
59 } |
39 $this->transcript = false; |
60 |
40 } |
61 public function getId() { |
41 |
62 if(is_null($this->id)) { |
|
63 $ids = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/identifier>'); |
|
64 foreach ($ids as $id) { |
|
65 if($id instanceof Literal && strpos($id->getValue(), config('corpusparole.corpus_id_scheme')) === 0) { |
|
66 $this->id = $id->getValue(); |
|
67 } |
|
68 } |
|
69 if(is_null($this->id)) { |
|
70 $this->id = CocoonUtils::getIdFromCorpusUri($this->uri); |
|
71 } |
|
72 } |
|
73 return $this->id; |
|
74 } |
|
75 |
|
76 public function getLanguage() { |
|
77 if(is_null($this->lang)) { |
|
78 try { |
|
79 $langs = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/language>'); |
|
80 if(count($langs) > 0) { |
|
81 $this->lang = $langs[0]; |
|
82 } |
|
83 } catch(\Exception $e) { |
|
84 $this->lang = null; |
|
85 } |
|
86 } |
|
87 return $this->lang; |
|
88 } |
|
89 |
|
90 public function getLanguageValue() { |
|
91 $lang = $this->getLanguage(); |
|
92 if($lang instanceof Resource) { |
|
93 return $lang->getUri(); |
|
94 } else if($lan instanceof Literal) { |
|
95 return $lang->getValue(); |
|
96 } |
|
97 return null; |
|
98 } |
|
99 |
|
100 public function getLanguageResolved() { |
|
101 return $this->langResolved; |
|
102 } |
|
103 public function setLanguageResolved($languageResolved) { |
|
104 $this->langResolved = $languageResolved; |
|
105 } |
|
106 |
|
107 |
|
108 public function getTitle() { |
|
109 if($this->title === false) { |
|
110 try { |
|
111 $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>'); |
|
112 } catch(\Exception $e) { |
|
113 $this->title = null; |
|
114 } |
|
115 } |
|
116 return $this->title; |
|
117 } |
|
118 |
|
119 public function setTitle($value, $lang="fr") { |
|
120 $oldTitle = $this->getTitle(); |
|
121 if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) { |
|
122 $literalTitle = new Literal($value, $lang, null); |
|
123 $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle); |
|
124 //clear cache |
|
125 $this->title = false; |
|
126 } |
|
127 } |
|
128 |
|
129 public function getTitleValue() { |
|
130 $title = $this->getTitle(); |
|
131 return is_null($title)?null:$title->getValue(); |
|
132 } |
|
133 |
42 |
134 public function getPublishers() { |
43 public function getPublishers() { |
135 if(is_null($this->publishers)) { |
44 if(is_null($this->publishers)) { |
136 try { |
45 try { |
137 $this->publishers = $this->getProvidedCHO()->all('dc11:publisher'); |
46 $this->publishers = $this->getProvidedCHO()->all('dc11:publisher'); |
140 } |
49 } |
141 } |
50 } |
142 return $this->publishers; |
51 return $this->publishers; |
143 } |
52 } |
144 |
53 |
145 public function getIssued() { |
54 private function parseWebResources() { |
146 if(is_null($this->issued)) { |
55 |
147 try { |
56 $this->mediaArray = []; |
148 $this->issued = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/issued>"); |
57 $this->transcript = null; |
149 } catch(\Exception $e) { |
58 |
150 $this->issued = null; |
59 $master = $this->get('<http://www.europeana.eu/schemas/edm/isShownBy>'); |
151 } |
60 $masterUrl = is_null($master)?null:$master->getUri(); |
152 } |
61 |
153 return $this->issued; |
62 foreach($this->graph->allOfType("<http://www.europeana.eu/schemas/edm/WebResource>") as $webResource) { |
154 } |
63 $formatLit = $webResource->getLiteral("dc11:format"); |
155 |
64 $format = is_null($formatLit)?null:$formatLit->getValue(); |
156 public function getIssuedValue() { |
65 if(is_null($format)) { |
157 $issued = $this->getIssued(); |
66 throw new ModelsException("parseWebResources: No dc:11 format on web resource"); |
158 return is_null($issued)?null:$issued->getValue(); |
67 } |
159 } |
68 |
160 |
69 if(0 === strpos($format, 'audio/') || |
161 public function getModified() { |
70 0 === strpos($format, 'video/') || |
162 if(is_null($this->modified)) { |
71 0 === strpos($format, 'Sampling:') ) { |
163 try { |
72 array_push( |
164 $this->modified = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/modified>"); |
73 $this->mediaArray, |
165 if(is_null($this->modified)) { |
74 new MediaResource( |
166 $this->modified = $this->getIssued(); |
75 $webResource->getUri(), |
167 } |
76 $this->graph, |
168 } catch(\Exception $e) { |
77 (($webResource->getUri() === $masterUrl)?true:false)) |
169 $this->modified = null; |
78 ); |
170 } |
79 } else if( |
171 } |
80 0 === strpos($format, 'application/xml') || |
172 return $this->modified; |
81 0 === strpos($format, 'application/pdf') ) { |
173 } |
82 $this->transcript = new TranscriptResource($webResource->getUri(), $this->graph); |
174 |
83 } |
175 public function getModifiedValue() { |
84 else { |
176 $modified = $this->getModified(); |
85 throw new ModelsException("parseWebResources: unknown format"); |
177 return is_null($modified)?null:$modified->getValue(); |
86 } |
178 } |
87 } |
179 |
88 |
180 public function setModified($value = null) { |
|
181 if(is_null($value)) { |
|
182 $value = gmdate(\DateTime::ATOM); |
|
183 } elseif ($value instanceof \DateTime) { |
|
184 $value = $value->format(\DateTime::ATOM); |
|
185 } |
|
186 $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value); |
|
187 |
|
188 $modified = $this->getModified(); |
|
189 if($value && (!$modified || $modified->getValue() !== $value) ) { |
|
190 |
|
191 $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF"); |
|
192 $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified); |
|
193 |
|
194 $this->modified = null; |
|
195 } |
|
196 } |
89 } |
197 |
90 |
198 public function getMediaArray() { |
91 public function getMediaArray() { |
199 |
92 |
200 if(is_null($this->mediaArray)) { |
93 if(is_null($this->mediaArray)) { |
201 //TODO: add media type |
94 $this->parseWebResources(); |
202 $this->mediaArray = []; |
|
203 |
|
204 $master = $this->get('<http://www.europeana.eu/schemas/edm/isShownBy>'); |
|
205 $masterUrl = is_null($master)?null:$master->getUri(); |
|
206 |
|
207 foreach($this->graph->allOfType("<http://www.europeana.eu/schemas/edm/WebResource>") as $webResource) { |
|
208 $extent = $webResource->getLiteral("<http://purl.org/dc/terms/extent>"); |
|
209 $extent = is_null($extent)?null:$extent->getValue(); |
|
210 $extent_ms = Utils::iso8601IntervalToMillis($extent); |
|
211 $format = $webResource->getLiteral("dc11:format"); |
|
212 |
|
213 $this->mediaArray[$webResource->getUri()] = [ |
|
214 'url' => $webResource->getUri(), |
|
215 'format' => is_null($format)?null:$format->getValue(), |
|
216 'extent' => $extent, |
|
217 'extent_ms' => $extent_ms, |
|
218 'master' => (($webResource->getUri() === $masterUrl)?true:false) |
|
219 ]; |
|
220 } |
|
221 } |
95 } |
222 return $this->mediaArray; |
96 return $this->mediaArray; |
223 } |
97 } |
224 |
98 |
225 public function getTypes() { |
99 public function getTypes() { |
238 })); |
112 })); |
239 return $res; |
113 return $res; |
240 } |
114 } |
241 |
115 |
242 public function getTranscript() { |
116 public function getTranscript() { |
243 $res = null; |
117 if($this->transcript === false) { |
244 foreach($this->graph->allOfType("<http://www.europeana.eu/schemas/edm/WebResource>") as $webResource) { |
118 $this->parseWebResources(); |
245 $format = $webResource->getLiteral("dc11:format")->getValue(); |
119 } |
246 |
120 return $this->transcript; |
247 if((0 === strpos($format, 'application/xml')) || |
|
248 (0 === strpos($format, 'application/pdf')) ) { |
|
249 $conformsTo = $webResource->getResource("<http://purl.org/dc/terms/conformsTo>"); |
|
250 $res = [ |
|
251 'url' => $webResource->getUri(), |
|
252 'format' => $format, |
|
253 'conforms-to' => $conformsTo?$conformsTo->getUri():null, |
|
254 ]; |
|
255 } |
|
256 } |
|
257 return $res; |
|
258 } |
121 } |
259 |
122 |
260 public function getContributors() { |
123 public function getContributors() { |
261 if(is_null($this->contributors)) { |
124 if(is_null($this->contributors)) { |
262 $this->contributors = array_reduce( |
125 $this->contributors = array_reduce( |
391 /* |
254 /* |
392 * Clone document. |
255 * Clone document. |
393 * clone also the innerDocumenent |
256 * clone also the innerDocumenent |
394 */ |
257 */ |
395 public function __clone() { |
258 public function __clone() { |
396 |
259 if(!is_null($this->graph)) { |
397 $this->graph = new Graph($this->graph->getUri(), $this->graph->toRdfPhp()); |
260 $this->graph = new Graph($this->graph->getUri(), $this->graph->toRdfPhp()); |
|
261 } |
398 } |
262 } |
399 |
263 |
400 public function jsonSerialize() { |
264 public function jsonSerialize() { |
401 if(!$this->graph) { |
265 |
402 return [ |
266 $res = parent::jsonSerialize(); |
403 'id' => $this->getId(), |
267 if($this->graph) { |
404 ]; |
268 |
405 } else { |
269 $mediaArray = is_null($this->getMediaArray())? |
406 $mediaArray = array_map( |
270 null: |
407 function($m) { |
271 array_combine( |
408 $f = Utils::processLiteralResourceOrString($m['format']); |
272 array_map(function($m) { return $m->getUrl(); }, $this->getMediaArray()), |
409 $res = $m; |
273 array_map( |
410 $res['format'] = $f; |
274 function($m) { |
411 return $res;}, |
275 return $m->jsonSerialize();}, |
412 $this->getMediaArray() |
276 $this->getMediaArray() |
413 ); |
277 ) |
|
278 ); |
|
279 |
|
280 $transcript = is_null($this->getTranscript())?null:$this->getTranscript()->jsonSerialize(); |
414 |
281 |
415 $publishers = array_map( |
282 $publishers = array_map( |
416 function($v) { return Utils::processLiteralResourceOrString($v); }, |
283 function($v) { return Utils::processLiteralResourceOrString($v); }, |
417 $this->getPublishers() |
284 $this->getPublishers() |
418 ); |
285 ); |
425 $subjects = array_map( |
292 $subjects = array_map( |
426 function($s) { return Utils::processLiteralResourceOrString($s); }, |
293 function($s) { return Utils::processLiteralResourceOrString($s); }, |
427 $this->getSubjects() |
294 $this->getSubjects() |
428 ); |
295 ); |
429 |
296 |
430 $res = [ |
297 $res = array_merge($res, [ |
431 'id' => $this->getId(), |
|
432 'uri' => $this->getUri(), |
|
433 'title' => $this->getTitleValue(), |
|
434 'language' => $this->getLanguageValue(), |
|
435 'modified' => $this->getModifiedValue(), |
|
436 'publishers' => $publishers, |
298 'publishers' => $publishers, |
437 'contributors' => $contributors, |
299 'contributors' => $contributors, |
438 'subjects' => $subjects, |
300 'subjects' => $subjects, |
439 'transcript' => $this->getTranscript(), |
301 'transcript' => $transcript, |
440 'mediaArray'=> $mediaArray |
302 'mediaArray'=> $mediaArray |
441 ]; |
303 ]); |
442 |
304 |
443 if($this->language_resolved) { |
305 } |
444 $res['language_resolved'] = $this->getLanguageResolved(); |
306 return $res; |
445 } |
307 |
446 |
|
447 return $res; |
|
448 } |
|
449 } |
308 } |
450 |
309 |
451 } |
310 } |