1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
7
|
|
|
* |
8
|
|
|
* @license GNU General Public License version 3 or later. |
9
|
|
|
* For the full copyright and license information, please read the |
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Kitodo\Dlf\Command; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\AbstractDocument; |
16
|
|
|
use Kitodo\Dlf\Command\BaseCommand; |
17
|
|
|
use Kitodo\Dlf\Common\Indexer; |
18
|
|
|
use Kitodo\Dlf\Domain\Model\Document; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
24
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* CLI Command for deleting single document from database and Solr. |
28
|
|
|
* |
29
|
|
|
* @package TYPO3 |
30
|
|
|
* @subpackage dlf |
31
|
|
|
* |
32
|
|
|
* @access public |
33
|
|
|
*/ |
34
|
|
|
class DeleteCommand extends BaseCommand |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Configure the command by defining the name, options and arguments |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function configure(): void |
45
|
|
|
{ |
46
|
|
|
$this |
47
|
|
|
->setDescription('Delete single document from database and Solr.') |
48
|
|
|
->setHelp('') |
49
|
|
|
->addOption( |
50
|
|
|
'doc', |
51
|
|
|
'd', |
52
|
|
|
InputOption::VALUE_REQUIRED, |
53
|
|
|
'UID or URL of the document.' |
54
|
|
|
) |
55
|
|
|
->addOption( |
56
|
|
|
'pid', |
57
|
|
|
'p', |
58
|
|
|
InputOption::VALUE_REQUIRED, |
59
|
|
|
'UID of the page the document should be added to.' |
60
|
|
|
) |
61
|
|
|
->addOption( |
62
|
|
|
'solr', |
63
|
|
|
's', |
64
|
|
|
InputOption::VALUE_REQUIRED, |
65
|
|
|
'[UID|index_name] of the Solr core the document should be added to.' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Executes the command to delete the given document to DB and SOLR. |
71
|
|
|
* |
72
|
|
|
* @access protected |
73
|
|
|
* |
74
|
|
|
* @param InputInterface $input The input parameters |
75
|
|
|
* @param OutputInterface $output The Symfony interface for outputs on console |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
80
|
|
|
{ |
81
|
|
|
$io = new SymfonyStyle($input, $output); |
82
|
|
|
$io->title($this->getDescription()); |
83
|
|
|
|
84
|
|
|
$this->initializeRepositories((int) $input->getOption('pid')); |
85
|
|
|
|
86
|
|
|
if ($this->storagePid == 0) { |
87
|
|
|
$io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
88
|
|
|
return BaseCommand::FAILURE; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ( |
92
|
|
|
!empty($input->getOption('solr')) |
93
|
|
|
&& !is_array($input->getOption('solr')) |
94
|
|
|
) { |
95
|
|
|
$allSolrCores = $this->getSolrCores($this->storagePid); |
96
|
|
|
$solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr')); |
97
|
|
|
|
98
|
|
|
// Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
99
|
|
|
if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
100
|
|
|
$outputSolrCores = []; |
101
|
|
|
foreach ($allSolrCores as $indexName => $uid) { |
102
|
|
|
$outputSolrCores[] = $uid . ' : ' . $indexName; |
103
|
|
|
} |
104
|
|
|
if (empty($outputSolrCores)) { |
105
|
|
|
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n"); |
106
|
|
|
return BaseCommand::FAILURE; |
107
|
|
|
} else { |
108
|
|
|
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n"); |
109
|
|
|
return BaseCommand::FAILURE; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
$io->error('ERROR: Required parameter --solr|-s is missing or array.'); |
114
|
|
|
return BaseCommand::FAILURE; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( |
118
|
|
|
empty($input->getOption('doc')) |
119
|
|
|
|| is_array($input->getOption('doc')) |
120
|
|
|
|| ( |
121
|
|
|
!MathUtility::canBeInterpretedAsInteger($input->getOption('doc')) |
122
|
|
|
&& !GeneralUtility::isValidUrl($input->getOption('doc')) |
123
|
|
|
) |
124
|
|
|
) { |
125
|
|
|
$io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.'); |
126
|
|
|
return BaseCommand::FAILURE; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->deleteFromDatabase($input, $io); |
130
|
|
|
$this->deleteFromSolr($input, $io, $solrCoreUid); |
131
|
|
|
|
132
|
|
|
return BaseCommand::SUCCESS; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Delete document from database. |
137
|
|
|
* |
138
|
|
|
* @access private |
139
|
|
|
* |
140
|
|
|
* @param InputInterface $input The input parameters |
141
|
|
|
* @param SymfonyStyle $io |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
private function deleteFromDatabase($input, $io): void |
146
|
|
|
{ |
147
|
|
|
$document = $this->getDocument($input); |
148
|
|
|
|
149
|
|
|
if ($document === null) { |
150
|
|
|
$io->info('INFO: Document with UID "' . $input->getOption('doc') . '" could not be found on PID ' . $this->storagePid . '. It is probably already deleted from DB.'); |
151
|
|
|
} else { |
152
|
|
|
if ($io->isVerbose()) { |
153
|
|
|
$io->section('Deleting ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . '.'); |
154
|
|
|
} |
155
|
|
|
$this->documentRepository->remove($document); |
156
|
|
|
$this->persistenceManager->persistAll(); |
157
|
|
|
if ($io->isVerbose()) { |
158
|
|
|
$io->success('Deleted ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . '.'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Delete document from SOLR. |
165
|
|
|
* |
166
|
|
|
* @access private |
167
|
|
|
* |
168
|
|
|
* @param InputInterface $input The input parameters |
169
|
|
|
* @param SymfonyStyle $io |
170
|
|
|
* @param int $solrCoreUid |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
private function deleteFromSolr($input, $io, $solrCoreUid): void |
175
|
|
|
{ |
176
|
|
|
if ($io->isVerbose()) { |
177
|
|
|
$io->section('Deleting ' . $input->getOption('doc') . ' on Solr core ' . $solrCoreUid . '.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$isDeleted = false; |
181
|
|
|
if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) { |
182
|
|
|
$isDeleted = Indexer::delete($input, 'uid', $solrCoreUid); |
183
|
|
|
|
184
|
|
|
} elseif (GeneralUtility::isValidUrl($input->getOption('doc'))) { |
185
|
|
|
$isDeleted = Indexer::delete($input, 'location', $solrCoreUid); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ($isDeleted) { |
189
|
|
|
if ($io->isVerbose()) { |
190
|
|
|
$io->success('Deleted ' . $input->getOption('doc') . ' on Solr core ' . $solrCoreUid . '.'); |
191
|
|
|
} |
192
|
|
|
$io->success('All done!'); |
193
|
|
|
} else { |
194
|
|
|
$io->error('Document was not deleted - check log file for more details!'); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get document from given URL. Find it in database, if not found create the new one. |
200
|
|
|
* |
201
|
|
|
* @access private |
202
|
|
|
* |
203
|
|
|
* @param InputInterface $input The input parameters |
204
|
|
|
* |
205
|
|
|
* @return ?Document |
206
|
|
|
*/ |
207
|
|
|
private function getDocument($input): ?Document |
208
|
|
|
{ |
209
|
|
|
$document = null; |
210
|
|
|
|
211
|
|
|
if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) { |
212
|
|
|
$document = $this->documentRepository->findByUid($input->getOption('doc')); |
213
|
|
|
} elseif (GeneralUtility::isValidUrl($input->getOption('doc'))) { |
214
|
|
|
$doc = AbstractDocument::getInstance($input->getOption('doc'), ['storagePid' => $this->storagePid], true); |
215
|
|
|
|
216
|
|
|
if ($doc->recordId) { |
217
|
|
|
$document = $this->documentRepository->findOneByRecordId($doc->recordId); |
|
|
|
|
218
|
|
|
} else { |
219
|
|
|
$document = $this->documentRepository->findOneByLocation($input->getOption('doc')); |
|
|
|
|
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $document; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|