1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
6
|
|
|
* |
7
|
|
|
* @license GNU General Public License version 3 or later. |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kitodo\Dlf\Controller; |
13
|
|
|
|
14
|
|
|
use Kitodo\Dlf\Common\Doc; |
15
|
|
|
use Kitodo\Dlf\Common\Helper; |
16
|
|
|
use Kitodo\Dlf\Common\IiifManifest; |
17
|
|
|
use Kitodo\Dlf\Domain\Model\Collection; |
18
|
|
|
use Kitodo\Dlf\Domain\Model\Metadata; |
19
|
|
|
use Kitodo\Dlf\Domain\Repository\CollectionRepository; |
20
|
|
|
use Kitodo\Dlf\Domain\Repository\MetadataRepository; |
21
|
|
|
use Kitodo\Dlf\Domain\Repository\StructureRepository; |
22
|
|
|
use Ubl\Iiif\Context\IRI; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Controller class for the plugin 'Metadata'. |
26
|
|
|
* |
27
|
|
|
* @author Sebastian Meyer <[email protected]> |
28
|
|
|
* @package TYPO3 |
29
|
|
|
* @subpackage dlf |
30
|
|
|
* @access public |
31
|
|
|
*/ |
32
|
|
|
class MetadataController extends AbstractController |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var CollectionRepository |
36
|
|
|
*/ |
37
|
|
|
protected $collectionRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param CollectionRepository $collectionRepository |
41
|
|
|
*/ |
42
|
|
|
public function injectCollectionRepository(CollectionRepository $collectionRepository) |
43
|
|
|
{ |
44
|
|
|
$this->collectionRepository = $collectionRepository; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var MetadataRepository |
49
|
|
|
*/ |
50
|
|
|
protected $metadataRepository; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param MetadataRepository $metadataRepository |
54
|
|
|
*/ |
55
|
|
|
public function injectMetadataRepository(MetadataRepository $metadataRepository) |
56
|
|
|
{ |
57
|
|
|
$this->metadataRepository = $metadataRepository; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var StructureRepository |
62
|
|
|
*/ |
63
|
|
|
protected $structureRepository; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param StructureRepository $structureRepository |
67
|
|
|
*/ |
68
|
|
|
public function injectStructureRepository(StructureRepository $structureRepository) |
69
|
|
|
{ |
70
|
|
|
$this->structureRepository = $structureRepository; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string|void |
75
|
|
|
*/ |
76
|
|
|
public function mainAction() |
77
|
|
|
{ |
78
|
|
|
$this->cObj = $this->configurationManager->getContentObject(); |
|
|
|
|
79
|
|
|
|
80
|
|
|
// Load current document. |
81
|
|
|
$this->loadDocument($this->requestData); |
82
|
|
|
if ( |
83
|
|
|
$this->document === null |
84
|
|
|
|| $this->document->getDoc() === null |
85
|
|
|
) { |
86
|
|
|
// Quit without doing anything if required variables are not set. |
87
|
|
|
return ''; |
88
|
|
|
} else { |
89
|
|
|
// Set default values if not set. |
90
|
|
|
if (!isset($this->settings['rootline'])) { |
91
|
|
|
$this->settings['rootline'] = 0; |
92
|
|
|
} |
93
|
|
|
if (!isset($this->settings['originalIiifMetadata'])) { |
94
|
|
|
$this->settings['originalIiifMetadata'] = 0; |
95
|
|
|
} |
96
|
|
|
if (!isset($this->settings['displayIiifDescription'])) { |
97
|
|
|
$this->settings['displayIiifDescription'] = 1; |
98
|
|
|
} |
99
|
|
|
if (!isset($this->settings['displayIiifRights'])) { |
100
|
|
|
$this->settings['displayIiifRights'] = 1; |
101
|
|
|
} |
102
|
|
|
if (!isset($this->settings['displayIiifLinks'])) { |
103
|
|
|
$this->settings['displayIiifLinks'] = 1; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest; |
107
|
|
|
$metadata = $this->getMetadata(); |
108
|
|
|
// Get titledata? |
109
|
|
|
if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->document->getDoc()->toplevelId)) { |
110
|
|
|
$data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitledata($this->settings['storagePid']); |
111
|
|
|
$data['_id'] = $this->document->getDoc()->toplevelId; |
112
|
|
|
$data['_active'] = true; |
113
|
|
|
array_unshift($metadata, $data); |
114
|
|
|
} |
115
|
|
|
if (empty($metadata)) { |
116
|
|
|
$this->logger->warning('No metadata found for document with UID ' . $this->document->getUid()); |
117
|
|
|
return ''; |
118
|
|
|
} |
119
|
|
|
ksort($metadata); |
120
|
|
|
|
121
|
|
|
$this->printMetadata($metadata, $useOriginalIiifManifestMetadata); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Prepares the metadata array for output |
126
|
|
|
* |
127
|
|
|
* @access protected |
128
|
|
|
* |
129
|
|
|
* @param array $metadata: The metadata array |
130
|
|
|
* @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
131
|
|
|
* |
132
|
|
|
* @return string The metadata array ready for output |
133
|
|
|
*/ |
134
|
|
|
protected function printMetadata(array $metadata, $useOriginalIiifManifestMetadata = false) |
135
|
|
|
{ |
136
|
|
|
if ($useOriginalIiifManifestMetadata) { |
137
|
|
|
$iiifData = []; |
138
|
|
|
foreach ($metadata as $row) { |
139
|
|
|
foreach ($row as $key => $group) { |
140
|
|
|
if ($key == '_id' || $key === '_active') { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
if (!is_array($group)) { |
144
|
|
|
if ( |
145
|
|
|
IRI::isAbsoluteIri($group) |
146
|
|
|
&& (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https') |
147
|
|
|
) { |
148
|
|
|
// Build link |
149
|
|
|
$iiifData[$key] = [ |
150
|
|
|
'label' => $key, |
151
|
|
|
'value' => $group, |
152
|
|
|
'buildUrl' => true, |
153
|
|
|
]; |
154
|
|
|
} else { |
155
|
|
|
// Data output |
156
|
|
|
$iiifData[$key] = [ |
157
|
|
|
'label' => $key, |
158
|
|
|
'value' => $group, |
159
|
|
|
'buildUrl' => false, |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
} else { |
163
|
|
|
foreach ($group as $label => $value) { |
164
|
|
|
if ($label === '_id' || $label === '_active') { |
165
|
|
|
continue; |
166
|
|
|
} |
167
|
|
|
if (is_array($value)) { |
168
|
|
|
$value = implode($this->settings['separator'], $value); |
169
|
|
|
} |
170
|
|
|
// NOTE: Labels are to be escaped in Fluid template |
171
|
|
|
if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) { |
172
|
|
|
$nolabel = $value == $label; |
173
|
|
|
$iiifData[$key]['data'][] = [ |
174
|
|
|
'label' => $nolabel ? '' : $label, |
175
|
|
|
'value' => $value, |
176
|
|
|
'buildUrl' => true, |
177
|
|
|
]; |
178
|
|
|
} else { |
179
|
|
|
$iiifData[$key]['data'][] = [ |
180
|
|
|
'label' => $label, |
181
|
|
|
'value' => $value, |
182
|
|
|
'buildUrl' => false, |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
$this->view->assign('useIiif', true); |
188
|
|
|
$this->view->assign('iiifData', $iiifData); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} else { |
192
|
|
|
// findBySettings also sorts entries by the `sorting` field |
193
|
|
|
$metadataResult = $this->metadataRepository->findBySettings([ |
194
|
|
|
'is_listed' => !$this->settings['showFull'], |
195
|
|
|
]); |
196
|
|
|
|
197
|
|
|
// Collect raw metadata into an array that will be passed as data to cObj. |
198
|
|
|
// This lets metadata wraps reference (own or foreign) values via TypoScript "field". |
199
|
|
|
$metaCObjData = []; |
200
|
|
|
|
201
|
|
|
$buildUrl = []; |
202
|
|
|
$externalUrl = []; |
203
|
|
|
$i = 0; |
204
|
|
|
foreach ($metadata as $section) { |
205
|
|
|
$metaCObjData[$i] = []; |
206
|
|
|
|
207
|
|
|
foreach ($section as $name => $value) { |
208
|
|
|
// NOTE: Labels are to be escaped in Fluid template |
209
|
|
|
|
210
|
|
|
$metaCObjData[$i][$name] = is_array($value) |
211
|
|
|
? implode($this->settings['separator'], $value) |
212
|
|
|
: $value; |
213
|
|
|
|
214
|
|
|
if ($name == 'title') { |
215
|
|
|
// Get title of parent document if needed. |
216
|
|
|
if (empty(implode('', $value)) && $this->settings['getTitle'] && $this->document->getPartof()) { |
217
|
|
|
$superiorTitle = Doc::getTitle($this->document->getPartof(), true); |
218
|
|
|
if (!empty($superiorTitle)) { |
219
|
|
|
$metadata[$i][$name] = ['[' . $superiorTitle . ']']; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
if (!empty($value)) { |
223
|
|
|
$metadata[$i][$name][0] = $metadata[$i][$name][0]; |
224
|
|
|
// Link title to pageview. |
225
|
|
|
if ($this->settings['linkTitle'] && $section['_id']) { |
226
|
|
|
$details = $this->document->getDoc()->getLogicalStructure($section['_id']); |
227
|
|
|
$buildUrl[$i][$name]['buildUrl'] = [ |
228
|
|
|
'id' => $this->document->getUid(), |
229
|
|
|
'page' => (!empty($details['points']) ? intval($details['points']) : 1), |
230
|
|
|
'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0), |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} elseif (($name == 'author' || $name == 'holder') && !empty($value) && !empty($value[0]['url'])) { |
235
|
|
|
$externalUrl[$i][$name]['externalUrl'] = $value[0]; |
236
|
|
|
} elseif (($name == 'geonames' || $name == 'wikidata' || $name == 'wikipedia') && !empty($value)) { |
237
|
|
|
$externalUrl[$i][$name]['externalUrl'] = [ |
238
|
|
|
'name' => $value[0], |
239
|
|
|
'url' => $value[0] |
240
|
|
|
]; |
241
|
|
|
} elseif ($name == 'owner' && empty($value)) { |
242
|
|
|
// no owner is found by metadata records --> take the one associated to the document |
243
|
|
|
$library = $this->document->getOwner(); |
244
|
|
|
if ($library) { |
245
|
|
|
$metadata[$i][$name][0] = $library->getLabel(); |
246
|
|
|
} |
247
|
|
|
} elseif ($name == 'type' && !empty($value)) { |
248
|
|
|
// Translate document type. |
249
|
|
|
$structure = $this->structureRepository->findOneByIndexName($metadata[$i][$name][0]); |
|
|
|
|
250
|
|
|
if ($structure) { |
251
|
|
|
$metadata[$i][$name][0] = $structure->getLabel(); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
} elseif ($name == 'collection' && !empty($value)) { |
254
|
|
|
// Check if collections isn't hidden. |
255
|
|
|
$j = 0; |
256
|
|
|
foreach ($value as $entry) { |
257
|
|
|
$collection = $this->collectionRepository->findOneByIndexName($entry); |
|
|
|
|
258
|
|
|
if ($collection) { |
259
|
|
|
$metadata[$i][$name][$j] = $collection->getLabel() ? : ''; |
260
|
|
|
$j++; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} elseif ($name == 'language' && !empty($value)) { |
264
|
|
|
// Translate ISO 639 language code. |
265
|
|
|
foreach ($metadata[$i][$name] as &$langValue) { |
266
|
|
|
$langValue = Helper::getLanguageName($langValue); |
267
|
|
|
} |
268
|
|
|
} elseif (!empty($value)) { |
269
|
|
|
$metadata[$i][$name][0] = $metadata[$i][$name][0]; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if (is_array($metadata[$i][$name])) { |
273
|
|
|
$metadata[$i][$name] = array_values(array_filter($metadata[$i][$name], function($metadataValue) |
274
|
|
|
{ |
275
|
|
|
return !empty($metadataValue); |
276
|
|
|
})); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
$i++; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$this->view->assign('buildUrl', $buildUrl); |
283
|
|
|
$this->view->assign('externalUrl', $externalUrl); |
284
|
|
|
$this->view->assign('documentMetadataSections', $metadata); |
285
|
|
|
$this->view->assign('configMetadata', $metadataResult); |
286
|
|
|
$this->view->assign('separator', $this->settings['separator']); |
287
|
|
|
$this->view->assign('metaCObjData', $metaCObjData); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get metadata for given id array. |
293
|
|
|
* |
294
|
|
|
* @access private |
295
|
|
|
* |
296
|
|
|
* @return array metadata |
297
|
|
|
*/ |
298
|
|
|
private function getMetadata() |
299
|
|
|
{ |
300
|
|
|
$metadata = []; |
301
|
|
|
if ($this->settings['rootline'] < 2) { |
302
|
|
|
// Get current structure's @ID. |
303
|
|
|
$ids = $this->document->getDoc()->getLogicalSectionsOnPage((int) $this->requestData['page']); |
304
|
|
|
|
305
|
|
|
// Check if we should display all metadata up to the root. |
306
|
|
|
if ($this->settings['prerenderAllSections'] ?? false) { |
307
|
|
|
// Collect IDs of all logical structures. This is a flattened tree, so the |
308
|
|
|
// order also works for rootline configurations. |
309
|
|
|
$allIds = []; |
310
|
|
|
function getIds($toc, &$output) { |
311
|
|
|
foreach ($toc as $entry) { |
312
|
|
|
$output[$entry['id']] = true; |
313
|
|
|
if (is_array($entry['children'])) { |
314
|
|
|
getIds($entry['children'], $output); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
getIds($this->document->getDoc()->tableOfContents, $allIds); |
319
|
|
|
|
320
|
|
|
$idIsActive = []; |
321
|
|
|
foreach ($ids as $id) { |
322
|
|
|
foreach ($id as $sid) { |
323
|
|
|
$idIsActive[$sid] = true; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$metadata = $this->getMetadataForIds(array_keys($allIds), $metadata); |
328
|
|
|
foreach ($metadata as &$entry) { |
329
|
|
|
$entry['_active'] = isset($idIsActive[$entry['_id']]); |
330
|
|
|
} |
331
|
|
|
} elseif ($this->settings['rootline'] == 1) { |
332
|
|
|
foreach ($ids as $id) { |
333
|
|
|
$metadata = $this->getMetadataForIds($id, $metadata); |
334
|
|
|
} |
335
|
|
|
} else { |
336
|
|
|
$id = array_pop($ids); |
337
|
|
|
if (is_array($id)) { |
338
|
|
|
$metadata = $this->getMetadataForIds($id, $metadata); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
return $metadata; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Get metadata for given id array. |
347
|
|
|
* |
348
|
|
|
* @access private |
349
|
|
|
* |
350
|
|
|
* @param array $id: An array with ids |
351
|
|
|
* @param array $metadata: An array with metadata |
352
|
|
|
* |
353
|
|
|
* @return array metadata |
354
|
|
|
*/ |
355
|
|
|
private function getMetadataForIds($id, $metadata) |
356
|
|
|
{ |
357
|
|
|
$useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest; |
358
|
|
|
foreach ($id as $sid) { |
359
|
|
|
if ($useOriginalIiifManifestMetadata) { |
360
|
|
|
$data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']); |
361
|
|
|
} else { |
362
|
|
|
$data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']); |
363
|
|
|
} |
364
|
|
|
if (!empty($data)) { |
365
|
|
|
$data['_id'] = $sid; |
366
|
|
|
$data['_active'] = true; |
367
|
|
|
$metadata[] = $data; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
return $metadata; |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|