forked from kitodo/kitodo-presentation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexCommand.php
More file actions
246 lines (215 loc) · 9.16 KB
/
IndexCommand.php
File metadata and controls
246 lines (215 loc) · 9.16 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
<?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\Command\BaseCommand;
use Kitodo\Dlf\Common\Indexer;
use Kitodo\Dlf\Domain\Model\Document;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
/**
* CLI Command for indexing single documents into database and Solr.
*
* @package TYPO3
* @subpackage dlf
*
* @access public
*/
class IndexCommand extends BaseCommand
{
/**
* Configure the command by defining the name, options and arguments
*
* @access public
*
* @return void
*/
public function configure(): void
{
$this
->setDescription('Index single document into database and Solr.')
->setHelp('')
->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'If this option is set, the files will not actually be processed but the location URI is shown.'
)
->addOption(
'doc',
'd',
InputOption::VALUE_REQUIRED,
'UID or URL of the document.'
)
->addOption(
'pid',
'p',
InputOption::VALUE_REQUIRED,
'UID of the page the document should be added to.'
)
->addOption(
'solr',
's',
InputOption::VALUE_REQUIRED,
'[UID|index_name] of the Solr core the document should be added to.'
)
->addOption(
'owner',
'o',
InputOption::VALUE_OPTIONAL,
'[UID|index_name] of the Library which should be set as owner of the document.'
);
}
/**
* Executes the command to index the given document to DB and SOLR.
*
* @access protected
*
* @param InputInterface $input The input parameters
* @param OutputInterface $output The Symfony interface for outputs on console
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dryRun = $input->getOption('dry-run') != false ? true : false;
$io = new SymfonyStyle($input, $output);
$io->title($this->getDescription());
$this->initializeRepositories((int) $input->getOption('pid'));
if ($this->storagePid == 0) {
$io->error('ERROR: No valid PID (' . $this->storagePid . ') given.');
return BaseCommand::FAILURE;
}
if (
!empty($input->getOption('solr'))
&& !is_array($input->getOption('solr'))
) {
$allSolrCores = $this->getSolrCores($this->storagePid);
$solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr'));
// Abort if solrCoreUid is empty or not in the array of allowed solr cores.
if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
$outputSolrCores = [];
foreach ($allSolrCores as $indexName => $uid) {
$outputSolrCores[] = $uid . ' : ' . $indexName;
}
if (empty($outputSolrCores)) {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n");
return BaseCommand::FAILURE;
} else {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n");
return BaseCommand::FAILURE;
}
}
} else {
$io->error('ERROR: Required parameter --solr|-s is missing or array.');
return BaseCommand::FAILURE;
}
if (
empty($input->getOption('doc'))
|| is_array($input->getOption('doc'))
|| (
!MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))
&& !GeneralUtility::isValidUrl($input->getOption('doc'))
)
) {
$io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.');
return BaseCommand::FAILURE;
}
if (!empty($input->getOption('owner'))) {
if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) {
$this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1));
} else {
$this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner'));
}
} else {
$this->owner = null;
}
$document = null;
$doc = null;
// Try to find existing document in database
if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) {
$document = $this->documentRepository->findByUid($input->getOption('doc'));
if ($document === null) {
$io->error('ERROR: Document with UID "' . $input->getOption('doc') . '" could not be found on PID ' . $this->storagePid . ' .');
return BaseCommand::FAILURE;
} else {
$doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $this->storagePid], true);
}
} else if (GeneralUtility::isValidUrl($input->getOption('doc'))) {
$doc = AbstractDocument::getInstance($input->getOption('doc'), ['storagePid' => $this->storagePid], true);
$document = $this->getDocumentFromUrl($doc, $input->getOption('doc'));
}
if ($doc === null) {
$io->error('ERROR: Document "' . $input->getOption('doc') . '" could not be loaded.');
return BaseCommand::FAILURE;
}
$document->setSolrcore($solrCoreUid);
if ($dryRun) {
$io->section('DRY RUN: Would index ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.');
$io->success('All done!');
return BaseCommand::SUCCESS;
} else {
$document->setCurrentDocument($doc);
if ($io->isVerbose()) {
$io->section('Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . '.');
}
$isSaved = $this->saveToDatabase($document);
if ($isSaved) {
if ($io->isVerbose()) {
$io->section('Indexing ' . $document->getUid() . ' ("' . $document->getLocation() . '") on Solr core ' . $solrCoreUid . '.');
}
$isSaved = Indexer::add($document, $this->documentRepository);
} else {
$io->error('ERROR: Document with UID "' . $document->getUid() . '" could not be indexed on PID ' . $this->storagePid . ' . There are missing mandatory fields (at least one of those: ' . $this->extConf['requiredMetadataFields'] . ') in this document.');
return BaseCommand::FAILURE;
}
if ($isSaved) {
$io->success('All done!');
return BaseCommand::SUCCESS;
}
$io->error('ERROR: Document with UID "' . $document->getUid() . '" could not be indexed on Solr core ' . $solrCoreUid . ' . There are missing mandatory fields (at least one of those: ' . $this->extConf['requiredMetadataFields'] . ') in this document.');
$io->info('INFO: Document with UID "' . $document->getUid() . '" is already in database. If you want to keep the database and index consistent you need to remove it.');
return BaseCommand::FAILURE;
}
}
/**
* Get document from given URL. Find it in database, if not found create the new one.
*
* @access private
*
* @param AbstractDocument $doc
* @param string $url
*
* @return Document
*/
private function getDocumentFromUrl($doc, string $url): Document
{
$document = null;
if ($doc->recordId) {
$document = $this->documentRepository->findOneByRecordId($doc->recordId);
} else {
$document = $this->documentRepository->findOneByLocation($url);
}
if ($document === null) {
// create new Document object
$document = GeneralUtility::makeInstance(Document::class);
}
// now there must exist a document object
if ($document) {
$document->setLocation($url);
}
return $document;
}
}