Skip to content

Commit

Permalink
Initialize and use repositories in Doc classes
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrycze-volk committed May 5, 2023
1 parent 9a5ccf2 commit 078b1ff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 152 deletions.
86 changes: 18 additions & 68 deletions Classes/Common/IiifManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,46 +113,28 @@ 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()
*/
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 {
Expand Down Expand Up @@ -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)) {
Expand Down
119 changes: 35 additions & 84 deletions Classes/Common/MetsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 078b1ff

Please sign in to comment.