Skip to content

Commit

Permalink
[MAINTENANCE] Adjustments and minor fixes in Hooks (kitodo#1266)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer authored Jul 5, 2024
1 parent aac8af0 commit 231f76c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 85 deletions.
187 changes: 108 additions & 79 deletions Classes/Hooks/DataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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');
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions Classes/Hooks/ItemsProcFunc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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
)
Expand Down
6 changes: 5 additions & 1 deletion Classes/Hooks/KitodoProductionHacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down

0 comments on commit 231f76c

Please sign in to comment.