We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 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 | ->addOption( |
||||||
| 68 | 'softCommit', |
||||||
| 69 | null, |
||||||
| 70 | InputOption::VALUE_NONE, |
||||||
| 71 | 'If this option is set, documents are just deleted from the index by a soft commit.' |
||||||
| 72 | ); |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | /** |
||||||
| 76 | * Executes the command to delete the given document to DB and SOLR. |
||||||
| 77 | * |
||||||
| 78 | * @access protected |
||||||
| 79 | * |
||||||
| 80 | * @param InputInterface $input The input parameters |
||||||
| 81 | * @param OutputInterface $output The Symfony interface for outputs on console |
||||||
| 82 | * |
||||||
| 83 | * @return int |
||||||
| 84 | */ |
||||||
| 85 | protected function execute(InputInterface $input, OutputInterface $output): int |
||||||
| 86 | { |
||||||
| 87 | $io = new SymfonyStyle($input, $output); |
||||||
| 88 | $io->title($this->getDescription()); |
||||||
| 89 | |||||||
| 90 | $this->initializeRepositories((int) $input->getOption('pid')); |
||||||
| 91 | |||||||
| 92 | if ($this->storagePid == 0) { |
||||||
| 93 | $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
||||||
| 94 | return BaseCommand::FAILURE; |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | if ( |
||||||
| 98 | !empty($input->getOption('solr')) |
||||||
| 99 | && !is_array($input->getOption('solr')) |
||||||
| 100 | ) { |
||||||
| 101 | $allSolrCores = $this->getSolrCores($this->storagePid); |
||||||
| 102 | $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr')); |
||||||
| 103 | |||||||
| 104 | // Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
||||||
| 105 | if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
||||||
| 106 | $outputSolrCores = []; |
||||||
| 107 | foreach ($allSolrCores as $indexName => $uid) { |
||||||
| 108 | $outputSolrCores[] = $uid . ' : ' . $indexName; |
||||||
| 109 | } |
||||||
| 110 | if (empty($outputSolrCores)) { |
||||||
| 111 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n"); |
||||||
| 112 | return BaseCommand::FAILURE; |
||||||
| 113 | } else { |
||||||
| 114 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n"); |
||||||
| 115 | return BaseCommand::FAILURE; |
||||||
| 116 | } |
||||||
| 117 | } |
||||||
| 118 | } else { |
||||||
| 119 | $io->error('ERROR: Required parameter --solr|-s is missing or array.'); |
||||||
| 120 | return BaseCommand::FAILURE; |
||||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | if ( |
||||||
| 124 | empty($input->getOption('doc')) |
||||||
| 125 | || is_array($input->getOption('doc')) |
||||||
| 126 | || ( |
||||||
| 127 | !MathUtility::canBeInterpretedAsInteger($input->getOption('doc')) |
||||||
| 128 | && !GeneralUtility::isValidUrl($input->getOption('doc')) |
||||||
| 129 | ) |
||||||
| 130 | ) { |
||||||
| 131 | $io->error('ERROR: Required parameter --doc|-d is not a valid document UID or URL.'); |
||||||
| 132 | return BaseCommand::FAILURE; |
||||||
| 133 | } |
||||||
| 134 | |||||||
| 135 | $this->deleteFromDatabase($input, $io); |
||||||
| 136 | $this->deleteFromSolr($input, $io, $solrCoreUid); |
||||||
| 137 | |||||||
| 138 | return BaseCommand::SUCCESS; |
||||||
| 139 | } |
||||||
| 140 | |||||||
| 141 | /** |
||||||
| 142 | * Delete document from database. |
||||||
| 143 | * |
||||||
| 144 | * @access private |
||||||
| 145 | * |
||||||
| 146 | * @param InputInterface $input The input parameters |
||||||
| 147 | * @param SymfonyStyle $io |
||||||
| 148 | * |
||||||
| 149 | * @return void |
||||||
| 150 | */ |
||||||
| 151 | private function deleteFromDatabase($input, $io): void |
||||||
| 152 | { |
||||||
| 153 | $document = $this->getDocument($input); |
||||||
| 154 | |||||||
| 155 | if ($document === null) { |
||||||
| 156 | $io->info('INFO: Document with UID "' . $input->getOption('doc') . '" could not be found on PID ' . $this->storagePid . '. It is probably already deleted from DB.'); |
||||||
| 157 | } else { |
||||||
| 158 | if ($io->isVerbose()) { |
||||||
| 159 | $io->section('Deleting ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . '.'); |
||||||
| 160 | } |
||||||
| 161 | $this->documentRepository->remove($document); |
||||||
| 162 | $this->persistenceManager->persistAll(); |
||||||
| 163 | if ($io->isVerbose()) { |
||||||
| 164 | $io->success('Deleted ' . $document->getUid() . ' ("' . $document->getLocation() . '") on PID ' . $this->storagePid . '.'); |
||||||
| 165 | } |
||||||
| 166 | } |
||||||
| 167 | } |
||||||
| 168 | |||||||
| 169 | /** |
||||||
| 170 | * Delete document from SOLR. |
||||||
| 171 | * |
||||||
| 172 | * @access private |
||||||
| 173 | * |
||||||
| 174 | * @param InputInterface $input The input parameters |
||||||
| 175 | * @param SymfonyStyle $io |
||||||
| 176 | * @param int $solrCoreUid |
||||||
| 177 | * |
||||||
| 178 | * @return void |
||||||
| 179 | */ |
||||||
| 180 | private function deleteFromSolr($input, $io, $solrCoreUid): void |
||||||
| 181 | { |
||||||
| 182 | if ($io->isVerbose()) { |
||||||
| 183 | $io->section('Deleting ' . $input->getOption('doc') . ' on Solr core ' . $solrCoreUid . '.'); |
||||||
| 184 | } |
||||||
| 185 | |||||||
| 186 | $isDeleted = false; |
||||||
| 187 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) { |
||||||
| 188 | $isDeleted = Indexer::delete($input, 'uid', $solrCoreUid, $input->getOption('softCommit')); |
||||||
| 189 | |||||||
| 190 | } elseif (GeneralUtility::isValidUrl($input->getOption('doc'))) { |
||||||
| 191 | $isDeleted = Indexer::delete($input, 'location', $solrCoreUid, $input->getOption('softCommit')); |
||||||
| 192 | } |
||||||
| 193 | |||||||
| 194 | if ($isDeleted) { |
||||||
| 195 | if ($io->isVerbose()) { |
||||||
| 196 | $io->success('Deleted ' . $input->getOption('doc') . ' on Solr core ' . $solrCoreUid . '.'); |
||||||
| 197 | } |
||||||
| 198 | $io->success('All done!'); |
||||||
| 199 | } else { |
||||||
| 200 | $io->error('Document was not deleted - check log file for more details!'); |
||||||
| 201 | } |
||||||
| 202 | } |
||||||
| 203 | |||||||
| 204 | /** |
||||||
| 205 | * Get document from given URL. Find it in database, if not found create the new one. |
||||||
| 206 | * |
||||||
| 207 | * @access private |
||||||
| 208 | * |
||||||
| 209 | * @param InputInterface $input The input parameters |
||||||
| 210 | * |
||||||
| 211 | * @return ?Document |
||||||
| 212 | */ |
||||||
| 213 | private function getDocument($input): ?Document |
||||||
| 214 | { |
||||||
| 215 | $document = null; |
||||||
| 216 | |||||||
| 217 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('doc'))) { |
||||||
| 218 | $document = $this->documentRepository->findByUid($input->getOption('doc')); |
||||||
| 219 | } elseif (GeneralUtility::isValidUrl($input->getOption('doc'))) { |
||||||
| 220 | $doc = AbstractDocument::getInstance($input->getOption('doc'), ['storagePid' => $this->storagePid], true); |
||||||
| 221 | |||||||
| 222 | if ($doc->recordId) { |
||||||
| 223 | $document = $this->documentRepository->findOneByRecordId($doc->recordId); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 224 | } else { |
||||||
| 225 | $document = $this->documentRepository->findOneByLocation($input->getOption('doc')); |
||||||
|
0 ignored issues
–
show
The method
findOneByLocation() does not exist on Kitodo\Dlf\Domain\Repository\DocumentRepository. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 226 | } |
||||||
| 227 | } |
||||||
| 228 | |||||||
| 229 | return $document; |
||||||
| 230 | } |
||||||
| 231 | } |
||||||
| 232 |