forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCommand.php
More file actions
522 lines (467 loc) · 17.9 KB
/
BaseCommand.php
File metadata and controls
522 lines (467 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
/**
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Kitodo\Dlf\Command;
use Kitodo\Dlf\Common\AbstractDocument;
use Kitodo\Dlf\Common\Indexer;
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
use Kitodo\Dlf\Domain\Repository\LibraryRepository;
use Kitodo\Dlf\Domain\Repository\StructureRepository;
use Kitodo\Dlf\Domain\Model\Collection;
use Kitodo\Dlf\Domain\Model\Document;
use Kitodo\Dlf\Domain\Model\Library;
use Kitodo\Dlf\Validation\DocumentValidator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
/**
* Base class for CLI Command classes.
*
* @package TYPO3
* @subpackage dlf
*
* @access public
*/
class BaseCommand extends Command
{
/**
* @access protected
* @var CollectionRepository
*/
protected CollectionRepository $collectionRepository;
/**
* @access protected
* @var DocumentRepository
*/
protected DocumentRepository $documentRepository;
/**
* @access protected
* @var LibraryRepository
*/
protected LibraryRepository $libraryRepository;
/**
* @access protected
* @var StructureRepository
*/
protected StructureRepository $structureRepository;
/**
* @access protected
* @var int
*/
protected int $storagePid;
/**
* @access protected
* @var Library|null
*/
protected ?Library $owner;
/**
* @access protected
* @var mixed[]
*/
protected array $extConf;
/**
* @access protected
* @var ConfigurationManager
*/
protected ConfigurationManager $configurationManager;
/**
* @access protected
* @var PersistenceManager
*/
protected PersistenceManager $persistenceManager;
public function __construct(
CollectionRepository $collectionRepository,
DocumentRepository $documentRepository,
LibraryRepository $libraryRepository,
StructureRepository $structureRepository,
ConfigurationManager $configurationManager,
PersistenceManager $persistenceManager
) {
parent::__construct();
$this->collectionRepository = $collectionRepository;
$this->documentRepository = $documentRepository;
$this->libraryRepository = $libraryRepository;
$this->structureRepository = $structureRepository;
$this->configurationManager = $configurationManager;
$this->persistenceManager = $persistenceManager;
}
/**
* Initialize the extbase repository based on the given storagePid.
*
* TYPO3 10+: Find a better solution e.g. based on Symfony Dependency Injection.
*
* @access protected
*
* @param int $storagePid The storage pid
*
* @return void
*/
protected function initializeRepositories(int $storagePid): void
{
$request = (new ServerRequest())->withAttribute("applicationType", SystemEnvironmentBuilder::REQUESTTYPE_BE);
$this->configurationManager->setRequest($request);
$frameworkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange($storagePid, 0);
$this->configurationManager->setConfiguration($frameworkConfiguration);
$this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
$this->storagePid = MathUtility::forceIntegerInRange($storagePid, 0);
$this->persistenceManager = GeneralUtility::makeInstance(PersistenceManager::class);
}
/**
* Return matching uid of Solr core depending on the input value.
*
* @access protected
*
* @param array<string, int> $solrCores array of the valid Solr cores
* @param bool|string|null $inputSolrId possible uid or name of Solr core
*
* @return ?int matching uid of Solr core
*/
protected function getSolrCoreUid(array $solrCores, bool|string|null $inputSolrId): ?int
{
if (MathUtility::canBeInterpretedAsInteger($inputSolrId)) {
$solrCoreUid = MathUtility::forceIntegerInRange((int) $inputSolrId, 0);
} else {
$solrCoreUid = $solrCores[$inputSolrId];
}
return $solrCoreUid;
}
/**
* Fetches all Solr cores on given page.
*
* @access protected
*
* @param int $pageId The UID of the Solr core or 0 to disable indexing
*
* @return array<string, int> Array of valid Solr cores
*/
protected function getSolrCores(int $pageId): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_solrcores');
$solrCores = [];
$result = $queryBuilder
->select('uid', 'index_name')
->from('tx_dlf_solrcores')
->where(
$queryBuilder->expr()->eq(
'pid',
$queryBuilder->createNamedParameter($pageId, Connection::PARAM_INT)
)
)
->executeQuery();
while ($record = $result->fetchAssociative()) {
$solrCores[$record['index_name']] = $record['uid'];
}
return $solrCores;
}
/**
* Set owner based on input value.
*
* @access protected
*
* @param bool|string|null $owner input owner value
*
* @return void
*/
protected function setOwner(bool|string|null $owner): void
{
if (!empty($owner)) {
if (MathUtility::canBeInterpretedAsInteger($owner)) {
$this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $owner, 1));
} else {
$this->owner = $this->libraryRepository->findOneBy(['indexName' => (string) $owner]);
}
} else {
$this->owner = null;
}
}
/**
* Update or insert document to database
*
* @access protected
*
* @param Document $document The document instance
* @param bool $softCommit If true, documents are just added to the index by a soft commit
*
* @return bool true on success, false otherwise
*/
protected function saveToDatabase(Document $document, bool $softCommit = false): bool
{
$doc = $document->getCurrentDocument();
if ($doc === null) {
return false;
}
$doc->configPid = $this->storagePid;
$metadata = $doc->getToplevelMetadata();
$validator = new DocumentValidator($metadata, explode(',', $this->extConf['general']['requiredMetadataFields']));
if ($validator->hasAllMandatoryMetadataFields()) {
// set title data
$document->setTitle($metadata['title'][0] ?? '');
$document->setTitleSorting($metadata['title_sorting'][0] ?? '');
$document->setPlace(implode('; ', $metadata['place'] ?? []));
$document->setYear(implode('; ', $metadata['year'] ?? []));
$document->setAuthor($this->getAuthors($metadata['author'] ?? []));
$document->setThumbnail($doc->thumbnail ?? '');
$document->setMetsLabel($metadata['mets_label'][0] ?? '');
$document->setMetsOrderlabel($metadata['mets_orderlabel'][0] ?? '');
$structure = $this->structureRepository->findOneBy(['indexName' => $metadata['type'][0]]);
if ($structure !== null) {
$document->setStructure($structure);
}
if (is_array($metadata['collection'])) {
$this->addCollections($document, $metadata['collection']);
}
// set identifiers
$document->setProdId($metadata['prod_id'][0] ?? '');
$document->setOpacId($metadata['opac_id'][0] ?? '');
$document->setUnionId($metadata['union_id'][0] ?? '');
$document->setRecordId($metadata['record_id'][0] ?? '');
$document->setUrn($metadata['urn'][0] ?? '');
$document->setPurl($metadata['purl'][0] ?? '');
$document->setDocumentFormat($metadata['document_format'][0] ?? '');
// set access
$document->setLicense($metadata['license'][0] ?? '');
$document->setTerms($metadata['terms'][0] ?? '');
$document->setRestrictions($metadata['restrictions'][0] ?? '');
$document->setOutOfPrint($metadata['out_of_print'][0] ?? '');
$document->setRightsInfo($metadata['rights_info'][0] ?? '');
$document->setStatus(0);
$this->setNewOwner($metadata['owner'][0] ?? '');
$document->setOwner($this->owner);
// set volume data
$document->setVolume($metadata['volume'][0] ?? '');
$document->setVolumeSorting($metadata['volume_sorting'][0] ?? $metadata['mets_order'][0] ?? '');
// Get UID of parent document.
if ($document->getDocumentFormat() === 'METS') {
$document->setPartof($this->getParentDocumentUidForSaving($document, $softCommit));
}
if ($document->getUid() === null) {
// new document
$this->documentRepository->add($document);
} else {
// update of existing document
$this->documentRepository->update($document);
}
$this->persistenceManager->persistAll();
return true;
}
return false;
}
/**
* Get the ID of the parent document if the current document has one.
* Currently only applies to METS documents.
*
* @access protected
*
* @param Document $document for which parent UID should be taken
* @param bool $softCommit If true, documents are just added to the index by a soft commit
*
* @return int The parent document's id.
*/
protected function getParentDocumentUidForSaving(Document $document, bool $softCommit = false): int
{
$doc = $document->getCurrentDocument();
if ($doc !== null && !empty($doc->parentHref)) {
// find document object by record_id of parent
$parent = AbstractDocument::getInstance($doc->parentHref, ['storagePid' => $this->storagePid], true);
if ($parent->recordId) {
$parentDocument = $this->documentRepository->findOneBy(['recordId' => $parent->recordId]);
if ($parentDocument === null) {
// create new Document object
$parentDocument = GeneralUtility::makeInstance(Document::class);
}
$parentDocument->setOwner($this->owner);
$parentDocument->setCurrentDocument($parent);
$parentDocument->setLocation($doc->parentHref);
$parentDocument->setSolrcore($document->getSolrcore());
$success = $this->saveToDatabase($parentDocument, $softCommit);
if ($success === true) {
// add to index
Indexer::add($parentDocument, $this->documentRepository, $softCommit);
return $parentDocument->getUid();
}
}
}
return 0;
}
/**
* Handle PID error output.
*
* @access protected
*
* @param SymfonyStyle $io
*
* @return int
*/
protected function handlePidError(SymfonyStyle $io): int
{
$io->error('No valid PID (' . $this->storagePid . ') given.');
return Command::FAILURE;
}
/**
* Handle Solr error output.
*
* @access protected
*
* @param array<string, int> $allSolrCores array of the valid Solr cores
* @param mixed $solr input Solr core value
* @param SymfonyStyle $io
*
* @return int
*/
protected function handleSolrError(array $allSolrCores, mixed $solr, SymfonyStyle $io): int
{
$outputSolrCores = [];
foreach ($allSolrCores as $indexName => $uid) {
$outputSolrCores[] = $uid . ' : ' . $indexName;
}
if (empty($outputSolrCores)) {
$io->error('No valid Solr core ("' . $solr . '") given. No valid cores found on PID ' . $this->storagePid . ".\n");
} else {
$io->error('No valid Solr core ("' . $solr . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n");
}
return Command::FAILURE;
}
/**
* Handle Solr missing output.
*
* @access protected
*
* @param SymfonyStyle $io
*
* @return int
*/
protected function handleSolrMissingError(SymfonyStyle $io): int
{
$io->error('Required parameter --solr|-s is missing or array.');
return Command::FAILURE;
}
/**
* Add collections.
*
* @access private
*
* @param Document &$document
* @param array<string> $collections
*
* @return void
*/
private function addCollections(Document &$document, array $collections): void
{
foreach ($collections as $collection) {
$documentCollection = $this->collectionRepository->findOneBy(['indexName' => $collection]);
if (!$documentCollection) {
// create new Collection object
$documentCollection = GeneralUtility::makeInstance(Collection::class);
$documentCollection->setIndexName($collection);
$documentCollection->setLabel($collection);
$setSpec = '';
if (!empty($this->extConf['general']['publishNewCollections'])) {
// setSpec only allows unreserved characters (rfc2396).
// alnum | "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
// Here we are a little bit more restrictive because
// some more unreserved characters are not allowed.
// Convert whitespaces to dash.
$setSpec = $collection;
$setSpec = preg_replace('/\s/', '-', $setSpec);
// Remove multiple dashes.
$setSpec = preg_replace('/-{2,}/', '-', $setSpec);
// Remove undesired characters.
$setSpec = preg_replace('/[^\w:-]/', '', $setSpec);
// A hierarchical setSpec consists of two or more
// normal setSpec which are separated by colons.
// Remove colons which don't separate hierarchical setSpec entries.
$setSpec = trim($setSpec, ':');
$setSpec = preg_replace('/:{2,}/', ':', $setSpec);
}
$documentCollection->setOaiName($setSpec);
$documentCollection->setIndexSearch('');
$documentCollection->setDescription('');
// add to CollectionRepository
$this->collectionRepository->add($documentCollection);
// persist collection to prevent duplicates
$this->persistenceManager->persistAll();
}
// add to document
$document->addCollection($documentCollection);
}
}
/**
* Get authors considering that database field can't accept
* more than 255 characters.
*
* @access private
*
* @param array<int, mixed> $metadataAuthor
*
* @return string
*/
private function getAuthors(array $metadataAuthor): string
{
// Get only authors' names for storing in database.
foreach ($metadataAuthor as $i => $author) {
if (is_array($author)) {
$metadataAuthor[$i] = $author['name'];
}
}
$authors = '';
$delimiter = '; ';
$ellipsis = 'et al.';
$count = count($metadataAuthor);
for ($i = 0; $i < $count; $i++) {
// Build the next part to add
$nextPart = ($i === 0 ? '' : $delimiter) . $metadataAuthor[$i];
// Check if adding this part and ellipsis in future would exceed the character limit
if (strlen($authors . $nextPart . $delimiter . $ellipsis) > 255) {
// Add ellipsis and stop adding more authors
$authors .= $delimiter . $ellipsis;
break;
}
// Add the part to the main string
$authors .= $nextPart;
}
return $authors;
}
/**
* If owner is not set but found by metadata, take it or take default library, if nothing found in database then create new owner.
*
* @access private
*
* @param string $owner
*
* @return void
*/
private function setNewOwner(string $owner): void
{
if (empty($this->owner)) {
// owner is not set but found by metadata --> take it or take default library
$owner = $owner ?: 'default';
$this->owner = $this->libraryRepository->findOneBy(['indexName' => $owner]);
if (empty($this->owner)) {
// create library
$this->owner = GeneralUtility::makeInstance(Library::class);
$this->owner->setLabel($owner);
$this->owner->setIndexName($owner);
$this->libraryRepository->add($this->owner);
}
}
}
}