Skip to content

Commit

Permalink
[MAINTENANCE] Simplify reading of use groups in document classes (#1404)
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 Dec 18, 2024
1 parent 89c4dd9 commit 01456c4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 103 deletions.
53 changes: 53 additions & 0 deletions Classes/Common/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
70 changes: 7 additions & 63 deletions Classes/Common/IiifManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*/
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down
61 changes: 21 additions & 40 deletions Classes/Common/MetsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1347,46 +1345,30 @@ 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,
];
}
}
}
}
}

// 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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 01456c4

Please sign in to comment.