diff --git a/Classes/Common/IiifManifest.php b/Classes/Common/IiifManifest.php index 9b3a9ae301..92892e4c0f 100644 --- a/Classes/Common/IiifManifest.php +++ b/Classes/Common/IiifManifest.php @@ -13,9 +13,8 @@ namespace Kitodo\Dlf\Common; use Flow\JSONPath\JSONPath; +use Kitodo\Dlf\Domain\Repository\MetadataRepository; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; -use TYPO3\CMS\Core\Database\ConnectionPool; -use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction; use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Utility\GeneralUtility; use Ubl\Iiif\Presentation\Common\Model\Resources\AnnotationContainerInterface; @@ -114,6 +113,19 @@ final class IiifManifest extends Doc */ public static $extKey = 'dlf'; + /** + * @var MetadataRepository + */ + protected $metadataRepository; + + /** + * @param MetadataRepository $metadataRepository + */ + public function injectMetadataRepository(MetadataRepository $metadataRepository) + { + $this->metadataRepository = $metadataRepository; + } + /** * {@inheritDoc} * @see Doc::establishRecordId() @@ -121,39 +133,8 @@ final class IiifManifest extends Doc protected function establishRecordId($pid) { if ($this->iiif !== null) { - /* - * FIXME This will not consistently work because we can not be sure to have the pid at hand. It may miss - * if the plugin that actually loads the manifest allows content from other pages. - * Up until now the cPid is only set after the document has been initialized. We need it before to - * check the configuration. - * TODO Saving / indexing should still work - check! - */ - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) - ->getQueryBuilderForTable('tx_dlf_metadata'); - // Get hidden records, too. - $queryBuilder - ->getRestrictions() - ->removeByType(HiddenRestriction::class); - $result = $queryBuilder - ->select('tx_dlf_metadataformat.xpath AS querypath') - ->from('tx_dlf_metadata') - ->from('tx_dlf_metadataformat') - ->from('tx_dlf_formats') - ->where( - $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($pid)), - $queryBuilder->expr()->eq('tx_dlf_metadataformat.pid', intval($pid)), - $queryBuilder->expr()->orX( - $queryBuilder->expr()->andX( - $queryBuilder->expr()->eq('tx_dlf_metadata.uid', 'tx_dlf_metadataformat.parent_id'), - $queryBuilder->expr()->eq('tx_dlf_metadataformat.encoded', 'tx_dlf_formats.uid'), - $queryBuilder->expr()->eq('tx_dlf_metadata.index_name', $queryBuilder->createNamedParameter('record_id')), - $queryBuilder->expr()->eq('tx_dlf_formats.type', $queryBuilder->createNamedParameter($this->getIiifVersion())) - ), - $queryBuilder->expr()->eq('tx_dlf_metadata.format', 0) - ) - ) - ->execute(); - while ($resArray = $result->fetch()) { + $allResults = $this->metadataRepository->findQueryPath(intval($pid), $this->getIiifVersion()); + foreach ($allResults as $resArray) { $recordIdPath = $resArray['querypath']; if (!empty($recordIdPath)) { try { @@ -617,40 +598,9 @@ public function getMetadata($id, $cPid = 0) } $metadata = $this->initializeMetadata('IIIF'); - - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) - ->getQueryBuilderForTable('tx_dlf_metadata'); - // Get hidden records, too. - $queryBuilder - ->getRestrictions() - ->removeByType(HiddenRestriction::class); - $result = $queryBuilder - ->select( - 'tx_dlf_metadata.index_name AS index_name', - 'tx_dlf_metadataformat.xpath AS xpath', - 'tx_dlf_metadataformat.xpath_sorting AS xpath_sorting', - 'tx_dlf_metadata.is_sortable AS is_sortable', - 'tx_dlf_metadata.default_value AS default_value', - 'tx_dlf_metadata.format AS format' - ) - ->from('tx_dlf_metadata') - ->from('tx_dlf_metadataformat') - ->from('tx_dlf_formats') - ->where( - $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($cPid)), - $queryBuilder->expr()->eq('tx_dlf_metadataformat.pid', intval($cPid)), - $queryBuilder->expr()->orX( - $queryBuilder->expr()->andX( - $queryBuilder->expr()->eq('tx_dlf_metadata.uid', 'tx_dlf_metadataformat.parent_id'), - $queryBuilder->expr()->eq('tx_dlf_metadataformat.encoded', 'tx_dlf_formats.uid'), - $queryBuilder->expr()->eq('tx_dlf_formats.type', $queryBuilder->createNamedParameter($this->getIiifVersion())) - ), - $queryBuilder->expr()->eq('tx_dlf_metadata.format', 0) - ) - ) - ->execute(); + $allResults = $this->metadataRepository->findForIiif(intval($cPid), $this->getIiifVersion()); $iiifResource = $this->iiif->getContainedResourceById($id); - while ($resArray = $result->fetch()) { + foreach ($allResults as $resArray) { // Set metadata field's value(s). if ($resArray['format'] > 0 && !empty($resArray['xpath']) && ($values = $iiifResource->jsonPath($resArray['xpath'])) != null) { if (is_string($values)) { diff --git a/Classes/Common/MetsDocument.php b/Classes/Common/MetsDocument.php index 3015948626..a15b22f361 100644 --- a/Classes/Common/MetsDocument.php +++ b/Classes/Common/MetsDocument.php @@ -12,13 +12,13 @@ namespace Kitodo\Dlf\Common; +use Kitodo\Dlf\Domain\Repository\MetadataRepository; +use Kitodo\Dlf\Domain\Repository\StructureRepository; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; -use TYPO3\CMS\Core\Database\ConnectionPool; -use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction; +use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Utility\GeneralUtility; use Ubl\Iiif\Tools\IiifHelper; use Ubl\Iiif\Services\AbstractImageService; -use TYPO3\CMS\Core\Log\LogManager; /** * MetsDocument class for the 'dlf' extension. @@ -166,6 +166,32 @@ final class MetsDocument extends Doc */ protected $parentHref; + /** + * @var MetadataRepository + */ + protected $metadataRepository; + + /** + * @var StructureRepository + */ + protected $structureRepository; + + /** + * @param MetadataRepository $metadataRepository + */ + public function injectMetadataRepository(MetadataRepository $metadataRepository) + { + $this->metadataRepository = $metadataRepository; + } + + /** + * @param StructureRepository $structureRepository + */ + public function injectStructureRepository(StructureRepository $structureRepository) + { + $this->structureRepository = $structureRepository; + } + /** * This adds metadata from METS structural map to metadata array. * @@ -493,72 +519,10 @@ class_exists($class) continue; } // Get the additional metadata from database. - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) - ->getQueryBuilderForTable('tx_dlf_metadata'); - // Get hidden records, too. - $queryBuilder - ->getRestrictions() - ->removeByType(HiddenRestriction::class); - // Get all metadata with configured xpath and applicable format first. - $resultWithFormat = $queryBuilder - ->select( - 'tx_dlf_metadata.index_name AS index_name', - 'tx_dlf_metadataformat_joins.xpath AS xpath', - 'tx_dlf_metadataformat_joins.xpath_sorting AS xpath_sorting', - 'tx_dlf_metadata.is_sortable AS is_sortable', - 'tx_dlf_metadata.default_value AS default_value', - 'tx_dlf_metadata.format AS format' - ) - ->from('tx_dlf_metadata') - ->innerJoin( - 'tx_dlf_metadata', - 'tx_dlf_metadataformat', - 'tx_dlf_metadataformat_joins', - $queryBuilder->expr()->eq( - 'tx_dlf_metadataformat_joins.parent_id', - 'tx_dlf_metadata.uid' - ) - ) - ->innerJoin( - 'tx_dlf_metadataformat_joins', - 'tx_dlf_formats', - 'tx_dlf_formats_joins', - $queryBuilder->expr()->eq( - 'tx_dlf_formats_joins.uid', - 'tx_dlf_metadataformat_joins.encoded' - ) - ) - ->where( - $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($cPid)), - $queryBuilder->expr()->eq('tx_dlf_metadata.l18n_parent', 0), - $queryBuilder->expr()->eq('tx_dlf_metadataformat_joins.pid', intval($cPid)), - $queryBuilder->expr()->eq('tx_dlf_formats_joins.type', $queryBuilder->createNamedParameter($this->mdSec[$dmdId]['type'])) - ) - ->execute(); - // Get all metadata without a format, but with a default value next. - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) - ->getQueryBuilderForTable('tx_dlf_metadata'); - // Get hidden records, too. - $queryBuilder - ->getRestrictions() - ->removeByType(HiddenRestriction::class); - $resultWithoutFormat = $queryBuilder - ->select( - 'tx_dlf_metadata.index_name AS index_name', - 'tx_dlf_metadata.is_sortable AS is_sortable', - 'tx_dlf_metadata.default_value AS default_value', - 'tx_dlf_metadata.format AS format' - ) - ->from('tx_dlf_metadata') - ->where( - $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($cPid)), - $queryBuilder->expr()->eq('tx_dlf_metadata.l18n_parent', 0), - $queryBuilder->expr()->eq('tx_dlf_metadata.format', 0), - $queryBuilder->expr()->neq('tx_dlf_metadata.default_value', $queryBuilder->createNamedParameter('')) - ) - ->execute(); + $resultWithFormat = $this->metadataRepository->findWithFormat(intval($cPid), $this->mdSec[$dmdId]['type']); + $resultWithoutFormat = $this->metadataRepository->findWithoutFormat(intval($cPid)); // Merge both result sets. - $allResults = array_merge($resultWithFormat->fetchAll(), $resultWithoutFormat->fetchAll()); + $allResults = array_merge($resultWithFormat, $resultWithoutFormat); // We need a \DOMDocument here, because SimpleXML doesn't support XPath functions properly. $domNode = dom_import_simplexml($this->mdSec[$dmdId]['xml']); $domXPath = new \DOMXPath($domNode->ownerDocument); @@ -729,6 +693,8 @@ public function getStructureDepth($logId) protected function init($location) { $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(get_class($this)); + $this->metadataRepository = GeneralUtility::makeInstance(MetadataRepository::class); + $this->structureRepository = GeneralUtility::makeInstance(StructureRepository::class); // Get METS node from XML file. $this->registerNamespaces($this->xml); $mets = $this->xml->xpath('//mets:mets'); @@ -1103,22 +1069,7 @@ protected function _getThumbnail($forceReload = false) $strctId = $this->_getToplevelId(); $metadata = $this->getTitledata($cPid); - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) - ->getQueryBuilderForTable('tx_dlf_structures'); - - // Get structure element to get thumbnail from. - $result = $queryBuilder - ->select('tx_dlf_structures.thumbnail AS thumbnail') - ->from('tx_dlf_structures') - ->where( - $queryBuilder->expr()->eq('tx_dlf_structures.pid', intval($cPid)), - $queryBuilder->expr()->eq('tx_dlf_structures.index_name', $queryBuilder->expr()->literal($metadata['type'][0])), - Helper::whereExpression('tx_dlf_structures') - ) - ->setMaxResults(1) - ->execute(); - - $allResults = $result->fetchAll(); + $allResults = $this->structureRepository->findThumbnail(intval($cPid), $metadata['type'][0]); if (count($allResults) == 1) { $resArray = $allResults[0];