Skip to content

Commit

Permalink
Implement UseGroupsConfiguration class
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrycze-volk committed Jan 2, 2025
1 parent 4b3a130 commit 840103c
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 190 deletions.
53 changes: 5 additions & 48 deletions Classes/Common/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Kitodo\Dlf\Common;

use Kitodo\Dlf\Configuration\UseGroupsConfiguration;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Log\Logger;
Expand Down Expand Up @@ -275,12 +276,12 @@ abstract class AbstractDocument
protected string $toplevelId = '';

/**
* Holds the configured useGrps as array.
* Holds the configured useGroups as array.
*
* @var array
* @access protected
* @var \Kitodo\Dlf\Configuration\UseGroupsConfiguration
*/
protected array $useGroups = [];
protected UseGroupsConfiguration $useGroupsConfiguration;

/**
* @access protected
Expand Down Expand Up @@ -619,20 +620,6 @@ 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 @@ -759,37 +746,6 @@ 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 Expand Up @@ -1140,6 +1096,7 @@ protected function _setCPid(int $value): void
protected function __construct(int $pid, string $location, $preloadedDocument, array $settings = [])
{
$this->pid = $pid;
$this->useGroupsConfiguration = UseGroupsConfiguration::getInstance();
$this->setPreloadedDocument($preloadedDocument);
$this->init($location, $settings);
$this->establishRecordId($pid);
Expand Down
12 changes: 6 additions & 6 deletions Classes/Common/FullTextReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function getFromXml(string $id, array $fileLocations, $physicalStructureN
{
$fullText = '';

$fileGrpsFulltext = $this->getFullTextFileGroups();
$useGroupsFulltext = $this->getFullTextUseGroups();
$textFormat = "";
if (!empty($physicalStructureNode)) {
while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
if (!empty($physicalStructureNode['files'][$fileGrpFulltext])) {
while ($useGroupFulltext = array_shift($useGroupsFulltext)) {
if (!empty($physicalStructureNode['files'][$useGroupFulltext])) {
// Get full text file.
$fileContent = GeneralUtility::getUrl($fileLocations[$fileGrpFulltext]);
$fileContent = GeneralUtility::getUrl($fileLocations[$useGroupFulltext]);
if ($fileContent !== false) {
$textFormat = $this->getTextFormat($fileContent);
} else {
Expand Down Expand Up @@ -128,10 +128,10 @@ private function getRawTextFromClass(string $fileContent, string $textFormat): s
*
* @return array
*/
private function getFullTextFileGroups(): array
private function getFullTextUseGroups(): array
{
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf', 'files');
return GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']);
return GeneralUtility::trimExplode(',', $extConf['useGroupsFulltext']);
}

/**
Expand Down
25 changes: 12 additions & 13 deletions Classes/Common/IiifManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ protected function magicGetPhysicalStructure(): array
$this->setFileUseDownload($iiifId, $this->iiif);
$this->setFileUseFulltext($iiifId, $this->iiif);

$fileUseThumbs = $this->getUseGroup('fileGrpThumbs');
$fileUses = $this->getUseGroup('fileGrpImages');
$fileUseThumbs = $this->useGroupsConfiguration-> getThumbnail();
$fileUses = $this->useGroupsConfiguration-> getImage();

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 @@ -697,16 +697,15 @@ public function getFullText(string $id): string
// Load physical structure ...
$this->magicGetPhysicalStructure();
// ... and extension configuration.
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey);
$fileGrpsFulltext = $this->getUseGroup('fileGrpFulltext');
$useGroups = $this->useGroupsConfiguration->getFulltext();

$physicalStructureNode = $this->physicalStructureInfo[$id];
if (!empty($physicalStructureNode)) {
while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
if (!empty($physicalStructureNode['files'][$fileGrpFulltext])) {
while ($useGroup = array_shift($useGroups)) {
if (!empty($physicalStructureNode['files'][$useGroup])) {
$rawText = GeneralUtility::makeInstance(FullTextReader::class, $this->formats)->getFromXml(
$id,
[$fileGrpFulltext => $this->getFileLocation($physicalStructureNode['files'][$fileGrpFulltext])],
[$useGroup => $this->getFileLocation($physicalStructureNode['files'][$useGroup])],
$physicalStructureNode
);
break;
Expand Down Expand Up @@ -908,12 +907,12 @@ private function getIndexAnnotations(): int
*/
private function setFileUseDownload(string $iiifId, $iiif): void
{
$fileUseDownload = $this->getUseGroup('fileGrpDownload');
$useGroups = $this->useGroupsConfiguration->getDownload();

if (!empty($fileUseDownload)) {
if (!empty($useGroups)) {
$docPdfRendering = $iiif->getRenderingUrlsForFormat('application/pdf');
if (!empty($docPdfRendering)) {
$this->physicalStructureInfo[$iiifId]['files'][$fileUseDownload[0]] = $docPdfRendering[0];
$this->physicalStructureInfo[$iiifId]['files'][$useGroups[0]] = $docPdfRendering[0];
}
}
}
Expand All @@ -930,16 +929,16 @@ private function setFileUseDownload(string $iiifId, $iiif): void
*/
private function setFileUseFulltext(string $iiifId, $iiif): void
{
$fileUseFulltext = $this->getUseGroup('fileGrpFulltext');
$useGroups = $this->useGroupsConfiguration->getFulltext();

if (!empty($fileUseFulltext)) {
if (!empty($useGroups)) {
$alto = $iiif->getSeeAlsoUrlsForFormat('application/alto+xml');
if (empty($alto)) {
$alto = $iiif->getSeeAlsoUrlsForProfile('http://www.loc.gov/standards/alto/', true);
}
if (!empty($alto)) {
$this->mimeTypes[$alto[0]] = 'application/alto+xml';
$this->physicalStructureInfo[$iiifId]['files'][$fileUseFulltext[0]] = $alto[0];
$this->physicalStructureInfo[$iiifId]['files'][$useGroups[0]] = $alto[0];
$this->hasFulltext = true;
$this->hasFulltextSet = true;
}
Expand Down
8 changes: 4 additions & 4 deletions Classes/Common/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,10 @@ protected static function processPhysical(Document $document, int $page, array $
$updateQuery = self::$solr->service->createUpdate();
$solrDoc = self::getSolrDocument($updateQuery, $document, $physicalUnit, $fullText);
$solrDoc->setField('page', $page);
$fileGrpsThumb = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']);
while ($fileGrpThumb = array_shift($fileGrpsThumb)) {
if (!empty($physicalUnit['files'][$fileGrpThumb])) {
$solrDoc->setField('thumbnail', $doc->getFileLocation($physicalUnit['files'][$fileGrpThumb]));
$useGroupsThumbnail = GeneralUtility::trimExplode(',', $extConf['Thumbnail']);
while ($useGroupThumbnail = array_shift($useGroupsThumbnail)) {
if (!empty($physicalUnit['files'][$useGroupThumbnail])) {
$solrDoc->setField('thumbnail', $doc->getFileLocation($physicalUnit['files'][$useGroupThumbnail]));
break;
}
}
Expand Down
44 changes: 18 additions & 26 deletions Classes/Common/MetsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;

/**
Expand Down Expand Up @@ -480,22 +479,17 @@ private function getPage(array &$details, ?SimpleXMLElement $metsPointers): void
*/
private function getThumbnail(string $id = '')
{
$fileGrpsThumb = $this->getUseGroup('fileGrpThumbs');

$useGroups = $this->useGroupsConfiguration->getThumbnail();
$thumbnail = null;

if (!empty($this->physicalStructure)) {
// There is a physical structure (no anchor or year mets).
while ($fileGrpThumb = array_shift($fileGrpsThumb)) {
while ($useGroup = array_shift($useGroups) && $thumbnail == null) {
if (empty($id)) {
$thumbnail = $this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb] ?? null;
$thumbnail = $this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$useGroup] ?? null;
} else {
$parentId = $this->smLinks['l2p'][$id][0] ?? null;
$thumbnail = $this->physicalStructureInfo[$parentId]['files'][$fileGrpThumb] ?? null;
}

if (!empty($thumbnail)) {
break;
$thumbnail = $this->physicalStructureInfo[$parentId]['files'][$useGroup] ?? null;
}
}
}
Expand Down Expand Up @@ -1125,15 +1119,13 @@ public function getFullText(string $id): string
$this->ensureHasFulltextIsSet();

if ($this->hasFulltext) {
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'files');
$fileGrpsFulltext = GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']);

$useGroups = $this->useGroupsConfiguration->getFulltext();
$physicalStructureNode = $this->physicalStructureInfo[$id];

$fileLocations = [];

if (!empty($physicalStructureNode)) {
while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
$fileLocations[$fileGrpFulltext] = $this->getFileLocation($physicalStructureNode['files'][$fileGrpFulltext]);
while ($useGroup = array_shift($useGroups)) {
$fileLocations[$useGroup] = $this->getFileLocation($physicalStructureNode['files'][$useGroup]);
}
}

Expand Down Expand Up @@ -1206,8 +1198,8 @@ protected function ensureHasFulltextIsSet(): void
{
// Are there any fulltext files available?
if (
!empty($this->getUseGroup('fileGrpFulltext'))
&& array_intersect($this->getUseGroup('fileGrpFulltext'), $this->fileGrps) !== []
!empty($this->useGroupsConfiguration->getFulltext())
&& array_intersect($this->useGroupsConfiguration->getFulltext(), $this->fileGrps) !== []
) {
$this->hasFulltext = true;
}
Expand Down Expand Up @@ -1351,7 +1343,7 @@ protected function processMdSec(SimpleXMLElement $element): ?array
protected function magicGetFileGrps(): array
{
if (!$this->fileGrpsLoaded) {
foreach (array_values($this->getUseGroups()) as $useGroups) {
foreach (array_values($this->useGroupsConfiguration->get()) as $useGroups) {
foreach ($useGroups as $useGroup) {
// Perform XPath query for each configured USE attribute
$fileGrps = $this->mets->xpath("./mets:fileSec/mets:fileGrp[@USE='$useGroup']");
Expand Down Expand Up @@ -1564,7 +1556,7 @@ protected function magicGetThumbnail(): string
return $this->thumbnail;
}

if (empty($this->getUseGroup('fileGrpThumbs'))) {
if (empty($this->useGroupsConfiguration->getThumbnail())) {
$this->logger->warning('No fileGrp for thumbnails specified');
$this->thumbnailLoaded = true;
return $this->thumbnail;
Expand Down Expand Up @@ -1603,17 +1595,17 @@ protected function magicGetThumbnail(): string
// Load smLinks.
$this->magicGetSmLinks();
// Get thumbnail location.
$fileGrpsThumb = $this->getUseGroup('fileGrpThumbs');
while ($fileGrpThumb = array_shift($fileGrpsThumb)) {
$useGroups = $this->useGroupsConfiguration->getThumbnail();
while ($useGroup = array_shift($useGroups)) {
if (
$this->magicGetPhysicalStructure()
&& !empty($this->smLinks['l2p'][$strctId])
&& !empty($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$fileGrpThumb])
&& !empty($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$useGroup])
) {
$this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$fileGrpThumb]);
$this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$useGroup]);
break;
} elseif (!empty($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb])) {
$this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb]);
} elseif (!empty($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$useGroup])) {
$this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$useGroup]);
break;
}
}
Expand Down
Loading

0 comments on commit 840103c

Please sign in to comment.