Skip to content

Commit

Permalink
Ensure that it is a MetsDocument when calling the musical methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizzor committed Jul 16, 2024
1 parent d9a8cef commit 3a7bfc3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 59 deletions.
72 changes: 39 additions & 33 deletions Classes/Common/DocumentAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,40 @@ function getAnnotations() {

if ($annotationTarget->getId()) {
if ($this->document->getCurrentDocument()->getFileLocation($annotationTarget->getId())) {
if (
$meiTargetPages = $this->getMeasurePagesByFileId(
$annotationTarget->getId(), $annotationTarget->getRangeValue()
)
) {
$targetPages[] = [
'target' => $annotationTarget,
'pages' => $meiTargetPages,
'verovioRelevant' => true
];

} elseif (
$audioTargetPages = $this->getAudioPagesByFileId(
$annotationTarget->getId(), $annotationTarget->getRangeValue()
)
) {
$targetPages[] = [
'target' => $annotationTarget,
'pages' => $audioTargetPages
];

} elseif ($fileIdTargetPages = $this->getPagesByFileId($annotationTarget->getId())) {
$targetPages[] = [
'target' => $annotationTarget,
'pages' => $fileIdTargetPages
];

} else {
$this->logger->warning(
' No target pages found! Annotation: "' . $annotation->getId() . '", '
. 'Target: "' . $annotationTarget->getUrl() . '"'
);
if ($this->document->getCurrentDocument() instanceof MetsDocument) {
if (
$meiTargetPages = $this->getMeasurePagesByFileId(
$annotationTarget->getId(), $annotationTarget->getRangeValue()
)
) {
$targetPages[] = [
'target' => $annotationTarget,
'pages' => $meiTargetPages,
'verovioRelevant' => true
];

} elseif (
$audioTargetPages = $this->getAudioPagesByFileId(
$annotationTarget->getId(), $annotationTarget->getRangeValue()
)
) {
$targetPages[] = [
'target' => $annotationTarget,
'pages' => $audioTargetPages
];

} elseif ($fileIdTargetPages = $this->getPagesByFileId($annotationTarget->getId())) {
$targetPages[] = [
'target' => $annotationTarget,
'pages' => $fileIdTargetPages
];

} else {
$this->logger->warning(
' No target pages found! Annotation: "' . $annotation->getId() . '", '
. 'Target: "' . $annotationTarget->getUrl() . '"'
);
}
}
} elseif ($logicalTargetPages = $this->getPagesByLogicalId($annotationTarget->getId())) {
$targetPages[] = [
Expand Down Expand Up @@ -322,6 +324,8 @@ protected function getMeasurePagesByFileId($fileId, $range = null)
$measureIndex = 1;
$startOrder = 0;
$endOrder = 0;

// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
foreach ($this->document->getCurrentDocument()->musicalStructureInfo as $key => $musicalInfo) {
if ($musicalInfo['type'] === 'measure' && is_array($musicalInfo['files'])) {
foreach ($musicalInfo['files'] as $file) {
Expand Down Expand Up @@ -368,6 +372,7 @@ protected function getMeasurePagesByFileId($fileId, $range = null)
// Get the related page numbers
$measurePages = [];
foreach ($measures as $measure) {
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$measurePages[$measure['order']] = $this->document->getCurrentDocument()->musicalStructure[$measure['order']]['page'];
}

Expand Down Expand Up @@ -405,7 +410,8 @@ protected static function loadData($document)
$conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
$apiBaseUrl = $conf['annotationServerUrl'];

if ($apiBaseUrl) {
if ($apiBaseUrl && $document->getCurrentDocument() instanceof MetsDocument) {
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$purl = $document->getCurrentDocument()->mets->xpath('//mods:mods/mods:identifier[@type="purl"]');
if (count($purl) > 0) {
$annotationRequest = new AnnotationRequest($apiBaseUrl);
Expand Down
42 changes: 25 additions & 17 deletions Classes/Controller/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Kitodo\Dlf\Controller;

use Kitodo\Dlf\Common\MetsDocument;
use Kitodo\Dlf\Domain\Model\PageSelectForm;
use TYPO3\CMS\Core\Utility\MathUtility;

Expand Down Expand Up @@ -116,24 +117,31 @@ public function mainAction(): void
}
$this->view->assign('features', $features);

if ($this->document->getCurrentDocument()->numMeasures > 0) {
$measureOptions = [];
$measurePages = [];
for ($i = 1; $i <= $this->document->getCurrentDocument()->numMeasures; $i++) {
$measureOptions[$i] = '[' . $i . ']' . ($this->document->getCurrentDocument()->musicalStructureInfo[$this->document->getCurrentDocument()->musicalStructure[$i]['measureid']]['orderlabel'] ? ' - ' . htmlspecialchars($this->document->getCurrentDocument()->musicalStructureInfo[$this->document->getCurrentDocument()->musicalStructureInfo[$i]]['orderlabel']) : '');
$measurePages[$i] = $this->document->getCurrentDocument()->musicalStructure[$i]['page'];
if ($this->document->getCurrentDocument() instanceof MetsDocument) {
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
if ($this->document->getCurrentDocument()->numMeasures > 0) {
$measureOptions = [];
$measurePages = [];
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
for ($i = 1; $i <= $this->document->getCurrentDocument()->numMeasures; $i++) {
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$measureOptions[$i] = '[' . $i . ']' . ($this->document->getCurrentDocument()->musicalStructureInfo[$this->document->getCurrentDocument()->musicalStructure[$i]['measureid']]['orderlabel'] ? ' - ' . htmlspecialchars($this->document->getCurrentDocument()->musicalStructureInfo[$this->document->getCurrentDocument()->musicalStructureInfo[$i]]['orderlabel']) : '');
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$measurePages[$i] = $this->document->getCurrentDocument()->musicalStructure[$i]['page'];
}

if (!isset($this->requestData['measure'])) {
$currentMeasure = array_search($this->requestData['page'], $measurePages);
} else {
$currentMeasure = $this->requestData['measure'];
}

$this->view->assign('currentMeasure', $currentMeasure);
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$this->view->assign('numMeasures', $this->document->getCurrentDocument()->numMeasures);
$this->view->assign('measureOptions', $measureOptions);
$this->view->assign('measurePages', $measurePages);
}

if (!isset($this->requestData['measure'])) {
$currentMeasure = array_search($this->requestData['page'], $measurePages);
} else {
$currentMeasure = $this->requestData['measure'];
}

$this->view->assign('currentMeasure', $currentMeasure);
$this->view->assign('numMeasures', $this->document->getCurrentDocument()->numMeasures);
$this->view->assign('measureOptions', $measureOptions);
$this->view->assign('measurePages', $measurePages);
}
}
}
24 changes: 15 additions & 9 deletions Classes/Controller/PageViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Kitodo\Dlf\Common\AbstractDocument;
use Kitodo\Dlf\Common\DocumentAnnotation;
use Kitodo\Dlf\Common\IiifManifest;
use Kitodo\Dlf\Common\MetsDocument;
use Kitodo\Dlf\Domain\Model\Document;
use Kitodo\Dlf\Domain\Model\FormAddDocument;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand Down Expand Up @@ -127,9 +129,10 @@ public function mainAction(): void
$this->fulltexts[1] = $this->getFulltext($this->requestData['page'] + 1);
$this->annotationContainers[1] = $this->getAnnotationContainers($this->requestData['page'] + 1);
}
$this->scores = $this->getScore($this->requestData['page']);

$this->measures = $this->getMeasures($this->requestData['page']);
if ($this->document->getCurrentDocument() instanceof MetsDocument) {
$this->scores = $this->getScore($this->requestData['page']);
$this->measures = $this->getMeasures($this->requestData['page']);
}

// Get the controls for the map.
$this->controls = explode(',', $this->settings['features']);
Expand Down Expand Up @@ -297,10 +300,11 @@ public function addDocumentAction(FormAddDocument $formAddDocument)
/**
* Get all measures from musical struct
* @param int $page
* @param ?AbstractDocument $specificDoc
* @param ?Document $specificDoc
* @param int|null $docNumber
* @return array
*/
protected function getMeasures(int $page, $specificDoc = null, $docNumber = null): array
protected function getMeasures(int $page, Document $specificDoc = null, $docNumber = null): array
{
if ($specificDoc) {
$doc = $specificDoc;
Expand All @@ -313,6 +317,7 @@ protected function getMeasures(int $page, $specificDoc = null, $docNumber = null
$measureLinks = [];
$defaultFileId = $doc->physicalStructureInfo[$currentPhysId]['files']['DEFAULT'];
if (isset($defaultFileId)) {
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$musicalStruct = $doc->musicalStructureInfo;

$i = 0;
Expand Down Expand Up @@ -358,11 +363,11 @@ protected function getMeasures(int $page, $specificDoc = null, $docNumber = null
* @access protected
*
* @param int $page: Page number
* @param ?MetsDocument $specificDoc
* @param ?Document $specificDoc
*
* @return array URL and MIME type of fulltext file
*/
protected function getScore(int $page, $specificDoc = null)
protected function getScore(int $page, Document $specificDoc = null)
{
$score = [];
$loc = '';
Expand All @@ -385,6 +390,7 @@ protected function getScore(int $page, $specificDoc = null)

if (!empty($loc)) {
$score['mimetype'] = $doc->getFileMimeType($loc);
// @phpstan-ignore-next-line It is ensured that getCurrentDocument returns a MetsDocument
$score['pagebeginning'] = $doc->getPageBeginning($pageId, $loc);
$score['url'] = $doc->getFileLocation($loc);
if ($this->settings['useInternalProxy']) {
Expand Down Expand Up @@ -602,11 +608,11 @@ protected function getAnnotationContainers(int $page): array
*
* @param int $page Page number
*
* @param ?AbstractDocument $specificDoc
* @param ?Document $specificDoc
*
* @return array URL and MIME type of image file
*/
protected function getImage(int $page, $specificDoc = null): array
protected function getImage(int $page, Document $specificDoc = null): array
{
$image = [];
// Get @USE value of METS fileGrp.
Expand Down

0 comments on commit 3a7bfc3

Please sign in to comment.