diff --git a/Classes/Hooks/DataHandler.php b/Classes/Hooks/DataHandler.php index cf2461210..6304efcbd 100644 --- a/Classes/Hooks/DataHandler.php +++ b/Classes/Hooks/DataHandler.php @@ -157,7 +157,8 @@ public function processDatamap_postProcessFieldArray(string $status, string $tab ->setMaxResults(1) ->execute(); - if ($resArray = $result->fetchAssociative()) { + $resArray = $result->fetchAssociative(); + if (is_array($resArray)) { // Reset storing to current. $fieldArray['index_stored'] = $resArray['is_listed']; } @@ -244,27 +245,12 @@ public function processDatamap_afterDatabaseOperations(string $status, string $t ->setMaxResults(1) ->execute(); - if ($resArray = $result->fetchAssociative()) { + $resArray = $result->fetchAssociative(); + if (is_array($resArray)) { if ($resArray['hidden']) { - // Establish Solr connection. - $solr = Solr::getInstance($resArray['core']); - if ($solr->ready) { - // Delete Solr document. - $updateQuery = $solr->service->createUpdate(); - $updateQuery->addDeleteQuery('uid:' . (int) $id); - $updateQuery->addCommit(); - $solr->service->update($updateQuery); - } + $this->deleteDocument($resArray['core'], $id); } else { - // Reindex document. - $document = $this->getDocumentRepository()->findByUid((int) $id); - $doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true); - if ($document !== null && $doc !== null) { - $document->setCurrentDocument($doc); - Indexer::add($document, $this->getDocumentRepository()); - } else { - $this->logger->error('Failed to re-index document with UID ' . (string) $id); - } + $this->reindexDocument($id); } } } @@ -321,32 +307,17 @@ public function processCmdmap_postProcess(string $command, string $table, $id): ->setMaxResults(1) ->execute(); - if ($resArray = $result->fetchAssociative()) { + $resArray = $result->fetchAssociative(); + if (is_array($resArray)) { switch ($command) { case 'move': case 'delete': - // Establish Solr connection. - $solr = Solr::getInstance($resArray['core']); - if ($solr->ready) { - // Delete Solr document. - $updateQuery = $solr->service->createUpdate(); - $updateQuery->addDeleteQuery('uid:' . (int) $id); - $updateQuery->addCommit(); - $solr->service->update($updateQuery); - if ($command == 'delete') { - break; - } + $this->deleteDocument($resArray['core'], $id); + if ($command == 'delete') { + break; } case 'undelete': - // Reindex document. - $document = $this->getDocumentRepository()->findByUid((int) $id); - $doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true); - if ($document !== null && $doc !== null) { - $document->setCurrentDocument($doc); - Indexer::add($document, $this->getDocumentRepository()); - } else { - $this->logger->error('Failed to re-index document with UID ' . (string) $id); - } + $this->reindexDocument($id); break; } } @@ -355,48 +326,106 @@ public function processCmdmap_postProcess(string $command, string $table, $id): $command === 'delete' && $table == 'tx_dlf_solrcores' ) { - // Is core deletion allowed in extension configuration? - $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf', 'solr'); - if (!empty($extConf['allowCoreDelete'])) { - // Delete core from Apache Solr as well. - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) - ->getQueryBuilderForTable('tx_dlf_solrcores'); - // Record in "tx_dlf_solrcores" is already deleted at this point. - $queryBuilder - ->getRestrictions() - ->removeByType(DeletedRestriction::class); + $this->deleteSolrCore($id); + } + } - $result = $queryBuilder - ->select( - 'tx_dlf_solrcores.index_name AS core' - ) - ->from('tx_dlf_solrcores') - ->where($queryBuilder->expr()->eq('tx_dlf_solrcores.uid', (int) $id)) - ->setMaxResults(1) - ->execute(); + /** + * Delete document from index. + * + * @access private + * + * @param mixed $core + * @param int|string $id + * + * @return void + */ + private function deleteDocument($core, $id): void + { + // Establish Solr connection. + $solr = Solr::getInstance($core); + if ($solr->ready) { + // Delete Solr document. + $updateQuery = $solr->service->createUpdate(); + $updateQuery->addDeleteQuery('uid:' . (int) $id); + $updateQuery->addCommit(); + $solr->service->update($updateQuery); + } + } - if ($resArray = $result->fetchAssociative()) { - // Establish Solr connection. - $solr = Solr::getInstance(); - if ($solr->ready) { - // Delete Solr core. - $query = $solr->service->createCoreAdmin(); - $action = $query->createUnload(); - $action->setCore($resArray['core']); - $action->setDeleteDataDir(true); - $action->setDeleteIndex(true); - $action->setDeleteInstanceDir(true); - $query->setAction($action); - try { - $response = $solr->service->coreAdmin($query); - if ($response->getWasSuccessful()) { - return; - } - } catch (\Exception $e) { - // Nothing to do here. + /** + * Reindex document. + * + * @access private + * + * @param int|string $id + * + * @return void + */ + private function reindexDocument($id):void + { + $document = $this->getDocumentRepository()->findByUid((int) $id); + $doc = AbstractDocument::getInstance($document->getLocation(), ['storagePid' => $document->getPid()], true); + if ($document !== null && $doc !== null) { + $document->setCurrentDocument($doc); + Indexer::add($document, $this->getDocumentRepository()); + } else { + $this->logger->error('Failed to re-index document with UID ' . (string) $id); + } + } + + /** + * Delete SOLR core if deletion is allowed. + * + * @access private + * + * @param int|string $id + * + * @return void + */ + private function deleteSolrCore($id): void + { + // Is core deletion allowed in extension configuration? + $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf', 'solr'); + if (!empty($extConf['allowCoreDelete'])) { + // Delete core from Apache Solr as well. + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getQueryBuilderForTable('tx_dlf_solrcores'); + // Record in "tx_dlf_solrcores" is already deleted at this point. + $queryBuilder + ->getRestrictions() + ->removeByType(DeletedRestriction::class); + + $result = $queryBuilder + ->select( + 'tx_dlf_solrcores.index_name AS core' + ) + ->from('tx_dlf_solrcores') + ->where($queryBuilder->expr()->eq('tx_dlf_solrcores.uid', (int) $id)) + ->setMaxResults(1) + ->execute(); + + $resArray = $result->fetchAssociative(); + if (is_array($resArray)) { + // Establish Solr connection. + $solr = Solr::getInstance(); + if ($solr->ready) { + // Delete Solr core. + $query = $solr->service->createCoreAdmin(); + $action = $query->createUnload(); + $action->setCore($resArray['core']); + $action->setDeleteDataDir(true); + $action->setDeleteIndex(true); + $action->setDeleteInstanceDir(true); + $query->setAction($action); + try { + $response = $solr->service->coreAdmin($query); + if ($response->getWasSuccessful() == false) { + $this->logger->warning('Core ' . $resArray['core'] . ' could not be deleted from Apache Solr'); } + } catch (\Exception $e) { + $this->logger->warning($e->getMessage()); } - $this->logger->warning('Core ' . $resArray['core'] . ' could not be deleted from Apache Solr'); } } } diff --git a/Classes/Hooks/ItemsProcFunc.php b/Classes/Hooks/ItemsProcFunc.php index c3c4277e2..f4f82c783 100644 --- a/Classes/Hooks/ItemsProcFunc.php +++ b/Classes/Hooks/ItemsProcFunc.php @@ -16,6 +16,7 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use TYPO3\CMS\Backend\Utility\BackendUtility; +use TYPO3\CMS\Core\Configuration\ConfigurationManager; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\TypoScript\TemplateService; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -50,7 +51,9 @@ class ItemsProcFunc implements LoggerAwareInterface */ public function toolList(array &$params): void { - foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['dlf/Classes/Plugin/Toolbox.php']['tools'] as $class => $label) { + $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); + $options = $configurationManager->getLocalConfigurationValueByPath('SC_OPTIONS'); + foreach ($options['dlf/Classes/Plugin/Toolbox.php']['tools'] as $class => $label) { $params['items'][] = [Helper::getLanguageService()->sL($label), $class]; } } @@ -70,9 +73,9 @@ public function getTyposcriptConfigFromPluginSiteRoot(array $params): void $pid = $params['flexParentDatabaseRow']['pid']; $rootLine = BackendUtility::BEgetRootLine($pid); $siteRootRow = []; - foreach ($rootLine as $_row) { - if ($_row['is_siteroot'] == '1') { - $siteRootRow = $_row; + foreach ($rootLine as $row) { + if ($row['is_siteroot'] == '1') { + $siteRootRow = $row; break; } } @@ -152,7 +155,7 @@ protected function generateList(array &$params, string $fields, string $table, s ->select(...explode(',', $fields)) ->from($table) ->where( - $queryBuilder->expr()->eq($table . '.pid', intval($this->storagePid)), + $queryBuilder->expr()->eq($table . '.pid', $this->storagePid), $queryBuilder->expr()->in($table . '.sys_language_uid', [-1, 0]), $andWhere ) diff --git a/Classes/Hooks/KitodoProductionHacks.php b/Classes/Hooks/KitodoProductionHacks.php index e0b247e01..526cdda20 100644 --- a/Classes/Hooks/KitodoProductionHacks.php +++ b/Classes/Hooks/KitodoProductionHacks.php @@ -39,12 +39,16 @@ public function postProcessRecordId(\SimpleXMLElement &$xml, &$recordId): void if (!$recordId) { $xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); // Get all logical structure nodes with metadata, but without associated METS-Pointers. - if (($divs = $xml->xpath('//mets:structMap[@TYPE="LOGICAL"]//mets:div[@DMDID and not(./mets:mptr)]'))) { + $divs = $xml->xpath('//mets:structMap[@TYPE="LOGICAL"]//mets:div[@DMDID and not(./mets:mptr)]'); + if (is_array($divs)) { $smLinks = $xml->xpath('//mets:structLink/mets:smLink'); if (!empty($smLinks)) { + $links = []; + foreach ($smLinks as $smLink) { $links[(string) $smLink->attributes('http://www.w3.org/1999/xlink')->from][] = (string) $smLink->attributes('http://www.w3.org/1999/xlink')->to; } + foreach ($divs as $div) { if (!empty($links[(string) $div['ID']])) { $id = (string) $div['DMDID'];