We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 24 |
| Total Lines | 261 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 1 |
| 1 | <?php |
||
| 30 | class ReindexCommand extends Command |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Configure the command by defining the name, options and arguments |
||
| 34 | */ |
||
| 35 | public function configure() |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Executes the command to index the given document to db and solr. |
||
| 74 | * |
||
| 75 | * @param InputInterface $input |
||
| 76 | * @param OutputInterface $output |
||
| 77 | */ |
||
| 78 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 79 | { |
||
| 80 | // Make sure the _cli_ user is loaded |
||
| 81 | Bootstrap::getInstance()->initializeBackendAuthentication(); |
||
| 82 | |||
| 83 | $dryRun = $input->getOption('dry-run') != false ? true : false; |
||
| 84 | |||
| 85 | $io = new SymfonyStyle($input, $output); |
||
| 86 | $io->title($this->getDescription()); |
||
| 87 | |||
| 88 | $startingPoint = 0; |
||
| 89 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('pid'))) { |
||
| 90 | $startingPoint = MathUtility::forceIntegerInRange((int)$input->getOption('pid'), 0); |
||
| 91 | } |
||
| 92 | if ($startingPoint == 0) { |
||
| 93 | $io->error('ERROR: No valid PID (' . $startingPoint . ') given.'); |
||
| 94 | exit(1); |
||
|
1 ignored issue
–
show
|
|||
| 95 | } |
||
| 96 | |||
| 97 | if ($input->getOption('solr')) { |
||
| 98 | $allSolrCores = $this->getSolrCores($startingPoint); |
||
| 99 | if (MathUtility::canBeInterpretedAsInteger($input->getOption('solr'))) { |
||
| 100 | $solrCoreUid = MathUtility::forceIntegerInRange((int)$input->getOption('solr'), 0); |
||
| 101 | } else { |
||
| 102 | $solrCoreName = $input->getOption('solr'); |
||
| 103 | $solrCoreUid = $allSolrCores[$solrCoreName]; |
||
| 104 | } |
||
| 105 | // Abort if solrCoreUid is empty or not in the array of allowed solr cores. |
||
| 106 | if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) { |
||
| 107 | foreach ($allSolrCores as $index_name => $uid) { |
||
| 108 | $output_solrCores .= ' ' . $uid . ' : ' . $index_name ."\n"; |
||
| 109 | } |
||
| 110 | if (empty($output_solrCores)) { |
||
| 111 | $io->error('ERROR: No valid solr core ("'. $input->getOption('solr') . '") given. ' . "No valid cores found on PID " . $startingPoint .".\n" . $output_solrCores); |
||
| 112 | exit(1); |
||
|
1 ignored issue
–
show
|
|||
| 113 | } else { |
||
| 114 | $io->error('ERROR: No valid solr core ("'. $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . $output_solrCores); |
||
| 115 | exit(1); |
||
|
1 ignored issue
–
show
|
|||
| 116 | } |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $io->error('ERROR: Required parameter --solr|-s is missing.'); |
||
| 120 | exit(1); |
||
|
1 ignored issue
–
show
|
|||
| 121 | } |
||
| 122 | |||
| 123 | if ($input->getOption('all')) { |
||
| 124 | // Get the document... |
||
| 125 | $documents = $this->getAllDocuments($startingPoint); |
||
| 126 | } else { |
||
| 127 | // coll may be a single integer, a list of integer |
||
| 128 | if (empty(array_filter(GeneralUtility::intExplode(',', $input->getOption('coll'), true)))) { |
||
|
1 ignored issue
–
show
|
|||
| 129 | $io->error('ERROR: "' . $input->getOption('coll') . '" is not a valid list of collection UIDs for --coll|-c.'); |
||
| 130 | exit(1); |
||
|
1 ignored issue
–
show
|
|||
| 131 | } |
||
| 132 | $documents = $this->getDocumentsToProceed($input->getOption('coll'), $startingPoint); |
||
| 133 | } |
||
| 134 | |||
| 135 | foreach ($documents as $id => $document) { |
||
| 136 | $doc = Document::getInstance($document, $startingPoint, TRUE); |
||
| 137 | if ($doc->ready) { |
||
| 138 | if ($dryRun) { |
||
| 139 | $io->writeln('DRY RUN: Would index ' . $id . '/' . count($documents) . ' ' . $doc->uid . ' ' . $doc->location . ' on UID ' . $startingPoint . ' and solr core ' . $solrCoreUid .'.'); |
||
| 140 | } else { |
||
| 141 | if ($io->isVerbose()) { |
||
| 142 | $io->writeln(date('Y-m-d H:i:s') . ' ' . $id . '/' . count($documents) . ' ' . $doc->uid . ' ' . $doc->location . ' on UID ' . $startingPoint . ' and solr core ' . $solrCoreUid .'.'); |
||
| 143 | } |
||
| 144 | // ...and save it to the database... |
||
| 145 | if (!$doc->save($startingPoint, $solrCoreUid)) { |
||
| 146 | $io->error('ERROR: Document "'.$id.'" not saved and indexed.'); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | $io->error('ERROR: Document "'.$id.'" could not be loaded.'); |
||
| 151 | } |
||
| 152 | // Clear document registry to prevent memory exhaustion. |
||
| 153 | Document::clearRegistry(); |
||
| 154 | } |
||
| 155 | |||
| 156 | $io->success('All done!'); |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Fetches all records of tx_dlf_solrcores on given page. |
||
| 162 | * |
||
| 163 | * @param int $pageId the uid of the solr record (can also be 0) |
||
| 164 | * |
||
| 165 | * @return array array of valid solr cores |
||
| 166 | */ |
||
| 167 | protected function getSolrCores(int $pageId): array |
||
| 168 | { |
||
| 169 | /** @var QueryBuilder $queryBuilder */ |
||
| 170 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 171 | ->getQueryBuilderForTable('tx_dlf_solrcores'); |
||
| 172 | |||
| 173 | $solrCores = []; |
||
| 174 | $pageId = (int)$pageId; |
||
| 175 | $result = $queryBuilder |
||
| 176 | ->select('uid', 'index_name') |
||
| 177 | ->from('tx_dlf_solrcores') |
||
| 178 | ->where( |
||
| 179 | $queryBuilder->expr()->eq( |
||
| 180 | 'pid', |
||
| 181 | $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT) |
||
| 182 | ) |
||
| 183 | ) |
||
| 184 | ->execute(); |
||
| 185 | |||
| 186 | while ($record = $result->fetch()) { |
||
| 187 | $solrCores[$record['index_name']] = $record['uid']; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $solrCores; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Fetches all documents with given collection. |
||
| 195 | * |
||
| 196 | * @param string $collId a comma separated list of collection uids |
||
| 197 | * @param int $pageId the uid of the solr record |
||
| 198 | * |
||
| 199 | * @return array array of valid solr cores |
||
| 200 | */ |
||
| 201 | protected function getDocumentsToProceed(string $collIds, int $pageId): array |
||
| 202 | { |
||
| 203 | /** @var QueryBuilder $queryBuilder */ |
||
| 204 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 205 | ->getQueryBuilderForTable('tx_dlf_documents'); |
||
| 206 | |||
| 207 | $documents = []; |
||
| 208 | $pageId = (int)$pageId; |
||
| 209 | $result = $queryBuilder |
||
| 210 | ->select('tx_dlf_documents.uid') |
||
| 211 | ->from('tx_dlf_documents') |
||
| 212 | ->join( |
||
| 213 | 'tx_dlf_documents', |
||
| 214 | 'tx_dlf_relations', |
||
| 215 | 'tx_dlf_relations_joins', |
||
| 216 | $queryBuilder->expr()->eq( |
||
| 217 | 'tx_dlf_relations_joins.uid_local', |
||
| 218 | 'tx_dlf_documents.uid' |
||
| 219 | ) |
||
| 220 | ) |
||
| 221 | ->join( |
||
| 222 | 'tx_dlf_relations_joins', |
||
| 223 | 'tx_dlf_collections', |
||
| 224 | 'tx_dlf_collections_join', |
||
| 225 | $queryBuilder->expr()->eq( |
||
| 226 | 'tx_dlf_relations_joins.uid_foreign', |
||
| 227 | 'tx_dlf_collections_join.uid' |
||
| 228 | ) |
||
| 229 | ) |
||
| 230 | -> where( |
||
| 231 | $queryBuilder->expr()->andX( |
||
| 232 | $queryBuilder->expr()->in( |
||
| 233 | 'tx_dlf_collections_join.uid', |
||
| 234 | $queryBuilder->createNamedParameter( |
||
| 235 | GeneralUtility::intExplode(',', $collIds, true), |
||
| 236 | Connection::PARAM_INT_ARRAY |
||
| 237 | ) |
||
| 238 | ), |
||
| 239 | $queryBuilder->expr()->eq( |
||
| 240 | 'tx_dlf_collections_join.pid', |
||
| 241 | $queryBuilder->createNamedParameter($pageId, Connection::PARAM_INT) |
||
| 242 | ), |
||
| 243 | $queryBuilder->expr()->eq( |
||
| 244 | 'tx_dlf_relations_joins.ident', |
||
| 245 | $queryBuilder->createNamedParameter('docs_colls') |
||
| 246 | ) |
||
| 247 | ) |
||
| 248 | ) |
||
| 249 | ->groupBy('tx_dlf_documents.uid') |
||
| 250 | ->orderBy('tx_dlf_documents.uid', 'ASC') |
||
| 251 | ->execute(); |
||
| 252 | |||
| 253 | while ($record = $result->fetch()) { |
||
| 254 | $documents[] = $record['uid']; |
||
| 255 | } |
||
| 256 | |||
| 257 | return $documents; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Fetches all documents of given page. |
||
| 262 | * |
||
| 263 | * @param int $pageId the uid of the solr record |
||
| 264 | * |
||
| 265 | * @return array array of valid solr cores |
||
| 266 | */ |
||
| 267 | protected function getAllDocuments(int $pageId): array |
||
| 291 | } |
||
| 292 | } |
||
| 293 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.