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 Symfony\Component\Console\Input\InputInterface; |
||
19 | use Symfony\Component\Console\Input\InputOption; |
||
20 | use Symfony\Component\Console\Output\OutputInterface; |
||
21 | use Symfony\Component\Console\Style\SymfonyStyle; |
||
22 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
23 | use TYPO3\CMS\Core\Utility\MathUtility; |
||
24 | |||
25 | /** |
||
26 | * CLI Command for re-indexing collections into database and Solr. |
||
27 | * |
||
28 | * @package TYPO3 |
||
29 | * @subpackage dlf |
||
30 | * |
||
31 | * @access public |
||
32 | */ |
||
33 | class ReindexCommand extends BaseCommand |
||
34 | { |
||
35 | /** |
||
36 | * Configure the command by defining the name, options and arguments |
||
37 | * |
||
38 | * @access public |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function configure(): void |
||
43 | { |
||
44 | $this |
||
45 | ->setDescription('Reindex a collection into database and Solr.') |
||
46 | ->setHelp('') |
||
47 | ->addOption( |
||
48 | 'dry-run', |
||
49 | null, |
||
50 | InputOption::VALUE_NONE, |
||
51 | 'If this option is set, the files will not actually be processed but the location URI is shown.' |
||
52 | ) |
||
53 | ->addOption( |
||
54 | 'coll', |
||
55 | 'c', |
||
56 | InputOption::VALUE_REQUIRED, |
||
57 | 'UID of the collection.' |
||
58 | ) |
||
59 | ->addOption( |
||
60 | 'pid', |
||
61 | 'p', |
||
62 | InputOption::VALUE_REQUIRED, |
||
63 | 'UID of the page the documents should be added to.' |
||
64 | ) |
||
65 | ->addOption( |
||
66 | 'solr', |
||
67 | 's', |
||
68 | InputOption::VALUE_REQUIRED, |
||
69 | '[UID|index_name] of the Solr core the document should be added to.' |
||
70 | ) |
||
71 | ->addOption( |
||
72 | 'owner', |
||
73 | 'o', |
||
74 | InputOption::VALUE_OPTIONAL, |
||
75 | '[UID|index_name] of the Library which should be set as owner of the documents.' |
||
76 | ) |
||
77 | ->addOption( |
||
78 | 'all', |
||
79 | 'a', |
||
80 | InputOption::VALUE_NONE, |
||
81 | 'Reindex all documents on the given page.' |
||
82 | ) |
||
83 | ->addOption( |
||
84 | 'index-limit', |
||
85 | 'l', |
||
86 | InputOption::VALUE_OPTIONAL, |
||
87 | 'Reindex the given amount of documents on the given page.' |
||
88 | ) |
||
89 | ->addOption( |
||
90 | 'index-begin', |
||
91 | 'b', |
||
92 | InputOption::VALUE_OPTIONAL, |
||
93 | 'Reindex documents on the given page starting from the given value.' |
||
94 | ) |
||
95 | ->addOption( |
||
96 | 'softCommit', |
||
97 | null, |
||
98 | InputOption::VALUE_NONE, |
||
99 | 'If this option is set, documents are just added to the index with a soft commit.' |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Executes the command to index the given document to DB and SOLR. |
||
105 | * |
||
106 | * @access protected |
||
107 | * |
||
108 | * @param InputInterface $input The input parameters |
||
109 | * @param OutputInterface $output The Symfony interface for outputs on console |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
114 | { |
||
115 | $dryRun = $input->getOption('dry-run') != false ? true : false; |
||
116 | |||
117 | $io = new SymfonyStyle($input, $output); |
||
118 | $io->title($this->getDescription()); |
||
119 | |||
120 | $this->initializeRepositories((int) $input->getOption('pid')); |
||
121 | |||
122 | if ($this->storagePid == 0) { |
||
123 | $io->error('ERROR: No valid PID (' . $this->storagePid . ') given.'); |
||
124 | return BaseCommand::FAILURE; |
||
125 | } |
||
126 | |||
127 | if ( |
||
128 | !empty($input->getOption('solr')) |
||
129 | && !is_array($input->getOption('solr')) |
||
130 | ) { |
||
131 | $allSolrCores = $this->getSolrCores($this->storagePid); |
||
132 | $solrCoreUid = $this->getSolrCoreUid($allSolrCores, $input->getOption('solr')); |
||
133 | |||
134 | // Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
||
135 | if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
||
136 | $outputSolrCores = []; |
||
137 | foreach ($allSolrCores as $indexName => $uid) { |
||
138 | $outputSolrCores[] = $uid . ' : ' . $indexName; |
||
139 | } |
||
140 | if (empty($outputSolrCores)) { |
||
141 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n"); |
||
142 | return BaseCommand::FAILURE; |
||
143 | } else { |
||
144 | $io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n"); |
||
145 | return BaseCommand::FAILURE; |
||
146 | } |
||
147 | } |
||
148 | } else { |
||
149 | $io->error('ERROR: Required parameter --solr|-s is missing or array.'); |
||
150 | return BaseCommand::FAILURE; |
||
151 | } |
||
152 | |||
153 | if (!empty($input->getOption('owner'))) { |
||
154 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('owner'))) { |
||
155 | $this->owner = $this->libraryRepository->findByUid(MathUtility::forceIntegerInRange((int) $input->getOption('owner'), 1)); |
||
156 | } else { |
||
157 | $this->owner = $this->libraryRepository->findOneByIndexName((string) $input->getOption('owner')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
158 | } |
||
159 | } else { |
||
160 | $this->owner = null; |
||
161 | } |
||
162 | |||
163 | if (!empty($input->getOption('all'))) { |
||
164 | if ( |
||
165 | !empty($input->getOption('index-limit')) |
||
166 | && $input->getOption('index-begin') >= 0 |
||
167 | ) { |
||
168 | // Get all documents for given limit and start. |
||
169 | $documents = $this->documentRepository->findAll() |
||
170 | ->getQuery() |
||
171 | ->setLimit((int) $input->getOption('index-limit')) |
||
172 | ->setOffset((int) $input->getOption('index-begin')) |
||
173 | ->execute(); |
||
174 | $io->writeln($input->getOption('index-limit') . ' documents starting from ' . $input->getOption('index-begin') . ' will be indexed.'); |
||
175 | } else { |
||
176 | // Get all documents. |
||
177 | $documents = $this->documentRepository->findAll(); |
||
178 | } |
||
179 | } elseif ( |
||
180 | !empty($input->getOption('coll')) |
||
181 | && !is_array($input->getOption('coll')) |
||
182 | ) { |
||
183 | $collections = GeneralUtility::intExplode(',', $input->getOption('coll'), true); |
||
184 | // "coll" may be a single integer or a comma-separated list of integers. |
||
185 | if (empty(array_filter($collections))) { |
||
186 | $io->error('ERROR: Parameter --coll|-c is not a valid comma-separated list of collection UIDs.'); |
||
187 | return BaseCommand::FAILURE; |
||
188 | } |
||
189 | |||
190 | if ( |
||
191 | !empty($input->getOption('index-limit')) |
||
192 | && $input->getOption('index-begin') >= 0 |
||
193 | ) { |
||
194 | $documents = $this->documentRepository->findAllByCollectionsLimited($collections, (int) $input->getOption('index-limit'), (int) $input->getOption('index-begin')); |
||
195 | |||
196 | $io->writeln($input->getOption('index-limit') . ' documents starting from ' . $input->getOption('index-begin') . ' will be indexed.'); |
||
197 | } else { |
||
198 | // Get all documents of given collections. |
||
199 | $documents = $this->documentRepository->findAllByCollectionsLimited($collections, 0); |
||
200 | } |
||
201 | } else { |
||
202 | $io->error('ERROR: One of parameters --all|-a or --coll|-c must be given.'); |
||
203 | return BaseCommand::FAILURE; |
||
204 | } |
||
205 | |||
206 | foreach ($documents as $id => $document) { |
||
207 | $doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $this->storagePid], true); |
||
208 | |||
209 | if ($doc === null) { |
||
210 | $io->warning('WARNING: Document "' . $document->getLocation() . '" could not be loaded. Skip to next document.'); |
||
211 | continue; |
||
212 | } |
||
213 | |||
214 | if ($dryRun) { |
||
215 | $io->writeln('DRY RUN: Would index ' . ($id + 1) . '/' . count($documents) . ' with UID "' . $document->getUid() . '" ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
216 | } else { |
||
217 | if ($io->isVerbose()) { |
||
218 | $io->writeln(date('Y-m-d H:i:s') . ' Indexing ' . ($id + 1) . '/' . count($documents) . ' with UID "' . $document->getUid() . '" ("' . $document->getLocation() . '") on PID ' . $this->storagePid . ' and Solr core ' . $solrCoreUid . '.'); |
||
219 | } |
||
220 | $document->setCurrentDocument($doc); |
||
221 | // save to database |
||
222 | $this->saveToDatabase($document, $input->getOption('softCommit')); |
||
223 | // add to index |
||
224 | Indexer::add($document, $this->documentRepository, $input->getOption('softCommit')); |
||
225 | } |
||
226 | // Clear document cache to prevent memory exhaustion. |
||
227 | AbstractDocument::clearDocumentCache(); |
||
228 | } |
||
229 | |||
230 | // Clear state of persistence manager to prevent memory exhaustion. |
||
231 | $this->persistenceManager->clearState(); |
||
232 | |||
233 | $io->success('All done!'); |
||
234 | |||
235 | return BaseCommand::SUCCESS; |
||
236 | } |
||
237 | } |
||
238 |