From 01456c4bbc7eacc59d4a74d47998b112f2c5676d Mon Sep 17 00:00:00 2001 From: Beatrycze Volk Date: Wed, 18 Dec 2024 08:51:04 +0100 Subject: [PATCH] [MAINTENANCE] Simplify reading of use groups in document classes (#1404) Co-authored-by: Sebastian Meyer --- Classes/Common/AbstractDocument.php | 53 ++++++++++++++++++++++ Classes/Common/IiifManifest.php | 70 +++-------------------------- Classes/Common/MetsDocument.php | 61 +++++++++---------------- 3 files changed, 81 insertions(+), 103 deletions(-) diff --git a/Classes/Common/AbstractDocument.php b/Classes/Common/AbstractDocument.php index 8166ce0a8..7b41c3dc4 100644 --- a/Classes/Common/AbstractDocument.php +++ b/Classes/Common/AbstractDocument.php @@ -274,6 +274,14 @@ abstract class AbstractDocument */ protected string $toplevelId = ''; + /** + * Holds the configured useGrps as array. + * + * @var array + * @access protected + */ + protected array $useGroups = []; + /** * @access protected * @var \SimpleXMLElement This holds the whole XML file as \SimpleXMLElement object @@ -613,6 +621,20 @@ public function getPhysicalPage(string $logicalPage): int return 1; } + /** + * Get the configuration for given use group. + * + * @access protected + * + * @param string $use + * + * @return array|string + */ + protected function getUseGroup(string $use) + { + return array_key_exists($use, $this->getUseGroups()) ? $this->useGroups[$use] : []; + } + /** * This determines a title for the given document * @@ -739,6 +761,37 @@ public function getStructureDepth(string $logId) return $this->getTreeDepth($this->magicGetTableOfContents(), 1, $logId); } + /** + * Get the configuration for use groups. + * + * @access protected + * + * @return array + */ + protected function getUseGroups(): array + { + if (empty($this->useGroups)) { + // Get configured USE attributes. + $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'files'); + + $configKeys = [ + 'fileGrpImages', + 'fileGrpThumbs', + 'fileGrpDownload', + 'fileGrpFulltext', + 'fileGrpAudio', + 'fileGrpScore' + ]; + + foreach ($configKeys as $key) { + if (!empty($extConf[$key])) { + $this->useGroups[$key] = GeneralUtility::trimExplode(',', $extConf[$key]); + } + } + } + return $this->useGroups; + } + /** * Load XML file / IIIF resource from URL * diff --git a/Classes/Common/IiifManifest.php b/Classes/Common/IiifManifest.php index b5924f85a..47045815f 100644 --- a/Classes/Common/IiifManifest.php +++ b/Classes/Common/IiifManifest.php @@ -201,59 +201,6 @@ public function getIiifVersion(): string return $this->iiifVersion; } - /** - * True if getUseGroups() has been called and $this->useGrps is loaded - * - * @var bool - * @access protected - */ - protected bool $useGrpsLoaded = false; - - /** - * Holds the configured useGrps as array. - * - * @var array - * @access protected - */ - protected array $useGrps = []; - - /** - * IiifManifest also populates the physical structure array entries for matching - * 'fileGrp's. To do that, the configuration has to be loaded; afterwards configured - * 'fileGrp's for thumbnails, downloads, audio, fulltext and the 'fileGrp's for images - * can be requested with this method. - * - * @access protected - * - * @param string $use - * - * @return array|string - */ - protected function getUseGroups(string $use) - { - if (!$this->useGrpsLoaded) { - // Get configured USE attributes. - $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'files'); - if (!empty($extConf['fileGrpImages'])) { - $this->useGrps['fileGrpImages'] = GeneralUtility::trimExplode(',', $extConf['fileGrpImages']); - } - if (!empty($extConf['fileGrpThumbs'])) { - $this->useGrps['fileGrpThumbs'] = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']); - } - if (!empty($extConf['fileGrpDownload'])) { - $this->useGrps['fileGrpDownload'] = GeneralUtility::trimExplode(',', $extConf['fileGrpDownload']); - } - if (!empty($extConf['fileGrpFulltext'])) { - $this->useGrps['fileGrpFulltext'] = GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']); - } - if (!empty($extConf['fileGrpAudio'])) { - $this->useGrps['fileGrpAudio'] = GeneralUtility::trimExplode(',', $extConf['fileGrpAudio']); - } - $this->useGrpsLoaded = true; - } - return array_key_exists($use, $this->useGrps) ? $this->useGrps[$use] : []; - } - /** * @see AbstractDocument::magicGetPhysicalStructure() */ @@ -276,8 +223,8 @@ protected function magicGetPhysicalStructure(): array $this->setFileUseDownload($iiifId, $this->iiif); $this->setFileUseFulltext($iiifId, $this->iiif); - $fileUseThumbs = $this->getUseGroups('fileGrpThumbs'); - $fileUses = $this->getUseGroups('fileGrpImages'); + $fileUseThumbs = $this->getUseGroup('fileGrpThumbs'); + $fileUses = $this->getUseGroup('fileGrpImages'); if (!empty($this->iiif->getDefaultCanvases())) { // canvases have not order property, but the context defines canveses as @list with a specific order, so we can provide an alternative @@ -495,10 +442,7 @@ protected function getLogicalStructureInfo(IiifResourceInterface $resource, bool $details['points'] = $startCanvasIndex + 1; } } - $useGroups = $this->getUseGroups('fileGrpImages'); - if (is_string($useGroups)) { - $useGroups = [$useGroups]; - } + // Keep for later usage. $this->logicalUnits[$details['id']] = $details; // Walk the structure recursively? And are there any children of the current element? @@ -754,8 +698,8 @@ public function getFullText(string $id): string $this->magicGetPhysicalStructure(); // ... and extension configuration. $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey); - $fileGrpsFulltext = GeneralUtility::trimExplode(',', $extConf['files']['fileGrpFulltext']); - + $fileGrpsFulltext = $this->getUseGroup('fileGrpFulltext'); + $physicalStructureNode = $this->physicalStructureInfo[$id]; if (!empty($physicalStructureNode)) { while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) { @@ -952,7 +896,7 @@ private function getAnnotationTexts($annotationContainerIds, $iiifId): array */ private function setFileUseDownload(string $iiifId, $iiif): void { - $fileUseDownload = $this->getUseGroups('fileGrpDownload'); + $fileUseDownload = $this->getUseGroup('fileGrpDownload'); if (!empty($fileUseDownload)) { $docPdfRendering = $iiif->getRenderingUrlsForFormat('application/pdf'); @@ -974,7 +918,7 @@ private function setFileUseDownload(string $iiifId, $iiif): void */ private function setFileUseFulltext(string $iiifId, $iiif): void { - $fileUseFulltext = $this->getUseGroups('fileGrpFulltext'); + $fileUseFulltext = $this->getUseGroup('fileGrpFulltext'); if (!empty($fileUseFulltext)) { $alto = $iiif->getSeeAlsoUrlsForFormat('application/alto+xml'); diff --git a/Classes/Common/MetsDocument.php b/Classes/Common/MetsDocument.php index 4f13c606c..6e464e530 100644 --- a/Classes/Common/MetsDocument.php +++ b/Classes/Common/MetsDocument.php @@ -480,9 +480,7 @@ private function getPage(array &$details, ?SimpleXMLElement $metsPointers): void */ private function getThumbnail(string $id = '') { - // Load plugin configuration. - $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'files'); - $fileGrpsThumb = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']); + $fileGrpsThumb = $this->getUseGroup('fileGrpThumbs'); $thumbnail = null; @@ -1347,37 +1345,21 @@ protected function processMdSec(SimpleXMLElement $element): ?array protected function magicGetFileGrps(): array { if (!$this->fileGrpsLoaded) { - // Get configured USE attributes. - $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'files'); - $useGrps = GeneralUtility::trimExplode(',', $extConf['fileGrpImages']); - - $configKeys = [ - 'fileGrpThumbs', - 'fileGrpDownload', - 'fileGrpFulltext', - 'fileGrpAudio', - 'fileGrpScore' - ]; - - foreach ($configKeys as $key) { - if (!empty($extConf[$key])) { - $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf[$key])); - } - } - - foreach ($useGrps as $useGrp) { - // Perform XPath query for each configured USE attribute - $fileGrps = $this->mets->xpath("./mets:fileSec/mets:fileGrp[@USE='$useGrp']"); - if (!empty($fileGrps)) { - foreach ($fileGrps as $fileGrp) { - foreach ($fileGrp->children('http://www.loc.gov/METS/')->file as $file) { - $fileId = (string) $file->attributes()->ID; - $this->fileGrps[$fileId] = $useGrp; - $this->fileInfos[$fileId] = [ - 'fileGrp' => $useGrp, - 'admId' => (string) $file->attributes()->ADMID, - 'dmdId' => (string) $file->attributes()->DMDID, - ]; + foreach (array_values($this->getUseGroups()) as $useGroups) { + foreach ($useGroups as $useGroup) { + // Perform XPath query for each configured USE attribute + $fileGrps = $this->mets->xpath("./mets:fileSec/mets:fileGrp[@USE='$useGroup']"); + if (!empty($fileGrps)) { + foreach ($fileGrps as $fileGrp) { + foreach ($fileGrp->children('http://www.loc.gov/METS/')->file as $file) { + $fileId = (string) $file->attributes()->ID; + $this->fileGrps[$fileId] = $useGroup; + $this->fileInfos[$fileId] = [ + 'fileGrp' => $useGroup, + 'admId' => (string) $file->attributes()->ADMID, + 'dmdId' => (string) $file->attributes()->DMDID, + ]; + } } } } @@ -1385,8 +1367,8 @@ protected function magicGetFileGrps(): array // Are there any fulltext files available? if ( - !empty($extConf['fileGrpFulltext']) - && array_intersect(GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']), $this->fileGrps) !== [] + !empty($this->getUseGroup('fileGrpFulltext')) + && array_intersect($this->getUseGroup('fileGrpFulltext'), $this->fileGrps) !== [] ) { $this->hasFulltext = true; } @@ -1585,9 +1567,8 @@ protected function magicGetThumbnail(bool $forceReload = false): string $this->thumbnailLoaded = true; return $this->thumbnail; } - // Load extension configuration. - $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'files'); - if (empty($extConf['fileGrpThumbs'])) { + + if (empty($this->getUseGroup('fileGrpThumbs'))) { $this->logger->warning('No fileGrp for thumbnails specified'); $this->thumbnailLoaded = true; return $this->thumbnail; @@ -1626,7 +1607,7 @@ protected function magicGetThumbnail(bool $forceReload = false): string // Load smLinks. $this->magicGetSmLinks(); // Get thumbnail location. - $fileGrpsThumb = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']); + $fileGrpsThumb = $this->getUseGroup('fileGrpThumbs'); while ($fileGrpThumb = array_shift($fileGrpsThumb)) { if ( $this->magicGetPhysicalStructure()