You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1015 lines
43 KiB

  1. <?php
  2. /**
  3. * (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
  4. *
  5. * This file is part of the Kitodo and TYPO3 projects.
  6. *
  7. * @license GNU General Public License version 3 or later.
  8. * For the full copyright and license information, please read the
  9. * LICENSE.txt file that was distributed with this source code.
  10. */
  11. namespace Kitodo\Dlf\Common;
  12. use Flow\JSONPath\JSONPath;
  13. use TYPO3\CMS\Core\Database\ConnectionPool;
  14. use TYPO3\CMS\Core\Utility\GeneralUtility;
  15. use Ubl\Iiif\Presentation\Common\Model\Resources\AnnotationContainerInterface;
  16. use Ubl\Iiif\Presentation\Common\Model\Resources\AnnotationInterface;
  17. use Ubl\Iiif\Presentation\Common\Model\Resources\CanvasInterface;
  18. use Ubl\Iiif\Presentation\Common\Model\Resources\ContentResourceInterface;
  19. use Ubl\Iiif\Presentation\Common\Model\Resources\IiifResourceInterface;
  20. use Ubl\Iiif\Presentation\Common\Model\Resources\ManifestInterface;
  21. use Ubl\Iiif\Presentation\Common\Model\Resources\RangeInterface;
  22. use Ubl\Iiif\Presentation\Common\Vocabulary\Motivation;
  23. use Ubl\Iiif\Presentation\V1\Model\Resources\AbstractIiifResource1;
  24. use Ubl\Iiif\Presentation\V2\Model\Resources\AbstractIiifResource2;
  25. use Ubl\Iiif\Presentation\V3\Model\Resources\AbstractIiifResource3;
  26. use Ubl\Iiif\Services\AbstractImageService;
  27. use Ubl\Iiif\Services\Service;
  28. use Ubl\Iiif\Tools\IiifHelper;
  29. /**
  30. * IiifManifest class for the 'dlf' extension.
  31. *
  32. * @author Lutz Helm <helm@ub.uni-leipzig.de>
  33. * @package TYPO3
  34. * @subpackage dlf
  35. * @access public
  36. * @property-write int $cPid This holds the PID for the configuration
  37. * @property-read bool $hasFulltext Are there any fulltext files available?
  38. * @property-read string $location This holds the documents location
  39. * @property-read array $metadataArray This holds the documents' parsed metadata array
  40. * @property-read int $numPages The holds the total number of pages
  41. * @property-read int $parentId This holds the UID of the parent document or zero if not multi-volumed
  42. * @property-read array $physicalStructure This holds the physical structure
  43. * @property-read array $physicalStructureInfo This holds the physical structure metadata
  44. * @property-read int $pid This holds the PID of the document or zero if not in database
  45. * @property-read bool $ready Is the document instantiated successfully?
  46. * @property-read string $recordId The IIIF manifest's record identifier
  47. * @property-read int $rootId This holds the UID of the root document or zero if not multi-volumed
  48. * @property-read array $smLinks This holds the connections between resources and canvases
  49. * @property-read array $tableOfContents This holds the logical structure
  50. * @property-read string $thumbnail This holds the document's thumbnail location
  51. * @property-read string $toplevelId This holds the toplevel manifest's @id
  52. * @property-read mixed $uid This holds the UID or the URL of the document
  53. */
  54. final class IiifManifest extends Document
  55. {
  56. /**
  57. * This holds the manifest file as string for serialization purposes
  58. * @see __sleep() / __wakeup()
  59. *
  60. * @var string
  61. * @access protected
  62. */
  63. protected $asJson = '';
  64. /**
  65. * A PHP object representation of a IIIF manifest.
  66. * @var ManifestInterface
  67. * @access protected
  68. */
  69. protected $iiif;
  70. /**
  71. * 'IIIF1', 'IIIF2' or 'IIIF3', depending on the API $this->iiif confrms to:
  72. * IIIF Metadata API 1, IIIF Presentation API 2 or 3
  73. * @var string
  74. * @access protected
  75. */
  76. protected $iiifVersion;
  77. /**
  78. * Document has already been analyzed if it contains fulltext for the Solr index
  79. * @var bool
  80. * @access protected
  81. */
  82. protected $hasFulltextSet = false;
  83. /**
  84. * This holds the original manifest's parsed metadata array with their corresponding
  85. * resource (Manifest / Sequence / Range) ID as array key
  86. *
  87. * @var array
  88. * @access protected
  89. */
  90. protected $originalMetadataArray = [];
  91. /**
  92. * Holds the mime types of linked resources in the manifest (extreacted during parsing) for later use.
  93. * @var array
  94. * @access protected
  95. */
  96. protected $mimeTypes = [];
  97. /**
  98. * The extension key
  99. *
  100. * @var string
  101. * @static
  102. * @access public
  103. */
  104. public static $extKey = 'dlf';
  105. /**
  106. * {@inheritDoc}
  107. * @see Document::establishRecordId()
  108. */
  109. protected function establishRecordId($pid)
  110. {
  111. if ($this->iiif !== null) {
  112. /*
  113. * FIXME This will not consistently work because we can not be sure to have the pid at hand. It may miss
  114. * if the plugin that actually loads the manifest allows content from other pages.
  115. * Up until now the cPid is only set after the document has been initialized. We need it before to
  116. * check the configuration.
  117. * TODO Saving / indexing should still work - check!
  118. */
  119. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
  120. ->getQueryBuilderForTable('tx_dlf_metadata');
  121. // Get hidden records, too.
  122. $queryBuilder
  123. ->getRestrictions()
  124. ->removeByType(HiddenRestriction::class);
  125. $result = $queryBuilder
  126. ->select('tx_dlf_metadataformat.xpath AS querypath')
  127. ->from('tx_dlf_metadata')
  128. ->from('tx_dlf_metadataformat')
  129. ->from('tx_dlf_formats')
  130. ->where(
  131. $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($pid)),
  132. $queryBuilder->expr()->eq('tx_dlf_metadataformat.pid', intval($pid)),
  133. $queryBuilder->expr()->orX(
  134. $queryBuilder->expr()->andX(
  135. $queryBuilder->expr()->eq('tx_dlf_metadata.uid', 'tx_dlf_metadataformat.parent_id'),
  136. $queryBuilder->expr()->eq('tx_dlf_metadataformat.encoded', 'tx_dlf_formats.uid'),
  137. $queryBuilder->expr()->eq('tx_dlf_metadata.index_name', $queryBuilder->createNamedParameter('record_id')),
  138. $queryBuilder->expr()->eq('tx_dlf_formats.type', $queryBuilder->createNamedParameter($this->getIiifVersion()))
  139. ),
  140. $queryBuilder->expr()->eq('tx_dlf_metadata.format', 0)
  141. )
  142. )
  143. ->execute();
  144. while ($resArray = $result->fetch()) {
  145. $recordIdPath = $resArray['querypath'];
  146. if (!empty($recordIdPath)) {
  147. $this->recordId = $this->iiif->jsonPath($recordIdPath);
  148. }
  149. }
  150. // For now, it's a hardcoded ID, not only as a fallback
  151. if (!isset($this->recordId)) {
  152. $this->recordId = $this->iiif->getId();
  153. }
  154. }
  155. }
  156. /**
  157. * {@inheritDoc}
  158. * @see Document::getDocument()
  159. */
  160. protected function getDocument()
  161. {
  162. return $this->iiif;
  163. }
  164. /**
  165. * Returns a string representing the Metadata / Presentation API version which the IIIF resource
  166. * conforms to. This is used for example to extract metadata according to configured patterns.
  167. *
  168. * @access public
  169. *
  170. * @return string 'IIIF1' if the resource is a Metadata API 1 resource, 'IIIF2' / 'IIIF3' if
  171. * the resource is a Presentation API 2 / 3 resource
  172. */
  173. public function getIiifVersion()
  174. {
  175. if (!isset($this->iiifVersion)) {
  176. if ($this->iiif instanceof AbstractIiifResource1) {
  177. $this->iiifVersion = 'IIIF1';
  178. } elseif ($this->iiif instanceof AbstractIiifResource2) {
  179. $this->iiifVersion = 'IIIF2';
  180. } elseif ($this->iiif instanceof AbstractIiifResource3) {
  181. $this->iiifVersion = 'IIIF3';
  182. }
  183. }
  184. return $this->iiifVersion;
  185. }
  186. /**
  187. * True if getUseGroups() has been called and $this->useGrps is loaded
  188. *
  189. * @var bool
  190. * @access protected
  191. */
  192. protected $useGrpsLoaded = false;
  193. /**
  194. * Holds the configured useGrps as array.
  195. *
  196. * @var array
  197. * @access protected
  198. */
  199. protected $useGrps = [];
  200. /**
  201. * IiifManifest also populates the physical stucture array entries for matching
  202. * 'fileGrp's. To do that, the configuration has to be loaded; afterwards configured
  203. * 'fileGrp's for thumbnails, downloads, audio, fulltext and the 'fileGrp's for images
  204. * can be requested with this method.
  205. *
  206. * @access protected
  207. *
  208. * @param string $use
  209. *
  210. * @return array|string
  211. */
  212. protected function getUseGroups($use)
  213. {
  214. if (!$this->useGrpsLoaded) {
  215. // Get configured USE attributes.
  216. $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
  217. if (!empty($extConf['fileGrpImages'])) {
  218. $this->useGrps['fileGrpImages'] = GeneralUtility::trimExplode(',', $extConf['fileGrpImages']);
  219. }
  220. if (!empty($extConf['fileGrpThumbs'])) {
  221. $this->useGrps['fileGrpThumbs'] = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']);
  222. }
  223. if (!empty($extConf['fileGrpDownload'])) {
  224. $this->useGrps['fileGrpDownload'] = GeneralUtility::trimExplode(',', $extConf['fileGrpDownload']);
  225. }
  226. if (!empty($extConf['fileGrpFulltext'])) {
  227. $this->useGrps['fileGrpFulltext'] = GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']);
  228. }
  229. if (!empty($extConf['fileGrpAudio'])) {
  230. $this->useGrps['fileGrpAudio'] = GeneralUtility::trimExplode(',', $extConf['fileGrpAudio']);
  231. }
  232. $this->useGrpsLoaded = true;
  233. }
  234. return array_key_exists($use, $this->useGrps) ? $this->useGrps[$use] : [];
  235. }
  236. /**
  237. * {@inheritDoc}
  238. * @see Document::_getPhysicalStructure()
  239. */
  240. protected function _getPhysicalStructure()
  241. {
  242. // Is there no physical structure array yet?
  243. if (!$this->physicalStructureLoaded) {
  244. if ($this->iiif == null || !($this->iiif instanceof ManifestInterface)) {
  245. return null;
  246. }
  247. $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
  248. $iiifId = $this->iiif->getId();
  249. $physSeq[0] = $iiifId;
  250. $this->physicalStructureInfo[$physSeq[0]]['id'] = $iiifId;
  251. $this->physicalStructureInfo[$physSeq[0]]['dmdId'] = $iiifId;
  252. $this->physicalStructureInfo[$physSeq[0]]['label'] = $this->iiif->getLabelForDisplay();
  253. $this->physicalStructureInfo[$physSeq[0]]['orderlabel'] = $this->iiif->getLabelForDisplay();
  254. $this->physicalStructureInfo[$physSeq[0]]['type'] = 'physSequence';
  255. $this->physicalStructureInfo[$physSeq[0]]['contentIds'] = null;
  256. $fileUseDownload = $this->getUseGroups('fileGrpDownload');
  257. $fileUseFulltext = $this->getUseGroups('fileGrpFulltext');
  258. $fileUseThumbs = $this->getUseGroups('fileGrpThumbs');
  259. $fileUses = $this->getUseGroups('fileGrpImages');
  260. if (!empty($fileUseDownload)) {
  261. $docPdfRendering = $this->iiif->getRenderingUrlsForFormat('application/pdf');
  262. if (!empty($docPdfRendering)) {
  263. $this->physicalStructureInfo[$physSeq[0]]['files'][$fileUseDownload[0]] = $docPdfRendering[0];
  264. }
  265. }
  266. if (!empty($fileUseFulltext)) {
  267. $iiifAlto = $this->iiif->getSeeAlsoUrlsForFormat('application/alto+xml');
  268. if (empty($iiifAlto)) {
  269. $iiifAlto = $this->iiif->getSeeAlsoUrlsForProfile('http://www.loc.gov/standards/alto/', true);
  270. }
  271. if (!empty($iiifAlto)) {
  272. $this->mimeTypes[$iiifAlto[0]] = 'application/alto+xml';
  273. $this->physicalStructureInfo[$physSeq[0]]['files'][$fileUseFulltext[0]] = $iiifAlto[0];
  274. $this->hasFulltext = true;
  275. $this->hasFulltextSet = true;
  276. }
  277. }
  278. if (!empty($this->iiif->getDefaultCanvases())) {
  279. // canvases have not order property, but the context defines canveses as @list with a specific order, so we can provide an alternative
  280. $canvasOrder = 0;
  281. foreach ($this->iiif->getDefaultCanvases() as $canvas) {
  282. $canvasOrder++;
  283. $thumbnailUrl = $canvas->getThumbnailUrl();
  284. // put thumbnails in thumbnail filegroup
  285. if (
  286. !empty($thumbnailUrl)
  287. && empty($this->physicalStructureInfo[$physSeq[0]]['files'][$fileUseThumbs[0]])
  288. ) {
  289. $this->physicalStructureInfo[$physSeq[0]]['files'][$fileUseThumbs[0]] = $thumbnailUrl;
  290. }
  291. // populate structural metadata info
  292. $elements[$canvasOrder] = $canvas->getId();
  293. $this->physicalStructureInfo[$elements[$canvasOrder]]['id'] = $canvas->getId();
  294. $this->physicalStructureInfo[$elements[$canvasOrder]]['dmdId'] = null;
  295. $this->physicalStructureInfo[$elements[$canvasOrder]]['label'] = $canvas->getLabelForDisplay();
  296. $this->physicalStructureInfo[$elements[$canvasOrder]]['orderlabel'] = $canvas->getLabelForDisplay();
  297. // assume that a canvas always represents a page
  298. $this->physicalStructureInfo[$elements[$canvasOrder]]['type'] = 'page';
  299. $this->physicalStructureInfo[$elements[$canvasOrder]]['contentIds'] = null;
  300. $this->physicalStructureInfo[$elements[$canvasOrder]]['annotationContainers'] = null;
  301. if (!empty($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING))) {
  302. $this->physicalStructureInfo[$elements[$canvasOrder]]['annotationContainers'] = [];
  303. foreach ($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING) as $annotationContainer) {
  304. $this->physicalStructureInfo[$elements[$canvasOrder]]['annotationContainers'][] = $annotationContainer->getId();
  305. if ($extConf['indexAnnotations']) {
  306. $this->hasFulltext = true;
  307. $this->hasFulltextSet = true;
  308. }
  309. }
  310. }
  311. if (!empty($fileUseFulltext)) {
  312. $alto = $canvas->getSeeAlsoUrlsForFormat('application/alto+xml');
  313. if (empty($alto)) {
  314. $alto = $canvas->getSeeAlsoUrlsForProfile('http://www.loc.gov/standards/alto/', true);
  315. }
  316. if (!empty($alto)) {
  317. $this->mimeTypes[$alto[0]] = 'application/alto+xml';
  318. $this->physicalStructureInfo[$elements[$canvasOrder]]['files'][$fileUseFulltext[0]] = $alto[0];
  319. $this->hasFulltext = true;
  320. $this->hasFulltextSet = true;
  321. }
  322. }
  323. if (!empty($fileUses)) {
  324. $image = $canvas->getImageAnnotations()[0];
  325. foreach ($fileUses as $fileUse) {
  326. if ($image->getBody() !== null && $image->getBody() instanceof ContentResourceInterface) {
  327. $this->physicalStructureInfo[$elements[$canvasOrder]]['files'][$fileUse] = $image->getBody()->getId();
  328. }
  329. }
  330. }
  331. if (!empty($thumbnailUrl)) {
  332. $this->physicalStructureInfo[$elements[$canvasOrder]]['files'][$fileUseThumbs] = $thumbnailUrl;
  333. }
  334. if (!empty($fileUseDownload)) {
  335. $pdfRenderingUrls = $canvas->getRenderingUrlsForFormat('application/pdf');
  336. if (!empty($pdfRenderingUrls)) {
  337. $this->physicalStructureInfo[$elements[$canvasOrder]]['files'][$fileUseDownload[0]] = $pdfRenderingUrls[0];
  338. }
  339. }
  340. }
  341. $this->numPages = $canvasOrder;
  342. // Merge and re-index the array to get nice numeric indexes.
  343. $this->physicalStructure = array_merge($physSeq, $elements);
  344. }
  345. $this->physicalStructureLoaded = true;
  346. }
  347. return $this->physicalStructure;
  348. }
  349. /**
  350. * {@inheritDoc}
  351. * @see Document::getDownloadLocation()
  352. */
  353. public function getDownloadLocation($id)
  354. {
  355. $fileLocation = $this->getFileLocation($id);
  356. $resource = $this->iiif->getContainedResourceById($fileLocation);
  357. if ($resource instanceof AbstractImageService) {
  358. return $resource->getImageUrl();
  359. }
  360. return $fileLocation;
  361. }
  362. /**
  363. * {@inheritDoc}
  364. * @see Document::getFileLocation()
  365. */
  366. public function getFileLocation($id)
  367. {
  368. if ($id == null) {
  369. return null;
  370. }
  371. $resource = $this->iiif->getContainedResourceById($id);
  372. if (isset($resource)) {
  373. if ($resource instanceof CanvasInterface) {
  374. return (!empty($resource->getImageAnnotations()) && $resource->getImageAnnotations()->getSingleService() != null) ? $resource->getImageAnnotations()[0]->getSingleService()->getId() : $id;
  375. } elseif ($resource instanceof ContentResourceInterface) {
  376. return $resource->getSingleService() != null && $resource->getSingleService() instanceof Service ? $resource->getSingleService()->getId() : $id;
  377. } elseif ($resource instanceof AbstractImageService) {
  378. return $resource->getId();
  379. } elseif ($resource instanceof AnnotationContainerInterface) {
  380. return $id;
  381. }
  382. } else {
  383. return $id;
  384. }
  385. }
  386. /**
  387. * {@inheritDoc}
  388. * @see Document::getFileMimeType()
  389. */
  390. public function getFileMimeType($id)
  391. {
  392. $fileResource = $this->iiif->getContainedResourceById($id);
  393. if ($fileResource instanceof CanvasInterface) {
  394. $format = "application/vnd.kitodo.iiif";
  395. } elseif ($fileResource instanceof AnnotationInterface) {
  396. $format = "application/vnd.kitodo.iiif";
  397. } elseif ($fileResource instanceof ContentResourceInterface) {
  398. if ($fileResource->isText() || $fileResource->isImage() && ($fileResource->getSingleService() == null || !($fileResource->getSingleService() instanceof AbstractImageService))) {
  399. // Support static images without an image service
  400. return $fileResource->getFormat();
  401. }
  402. $format = "application/vnd.kitodo.iiif";
  403. } elseif ($fileResource instanceof AbstractImageService) {
  404. $format = "application/vnd.kitodo.iiif";
  405. } else {
  406. // Assumptions: this can only be the thumbnail and the thumbnail is a jpeg - TODO determine mimetype
  407. $format = "image/jpeg";
  408. }
  409. return $format;
  410. }
  411. /**
  412. * {@inheritDoc}
  413. * @see Document::getLogicalStructure()
  414. */
  415. public function getLogicalStructure($id, $recursive = false)
  416. {
  417. $details = [];
  418. if (!$recursive && !empty($this->logicalUnits[$id])) {
  419. return $this->logicalUnits[$id];
  420. } elseif (!empty($id)) {
  421. $logUnits[] = $this->iiif->getContainedResourceById($id);
  422. } else {
  423. $logUnits[] = $this->iiif;
  424. }
  425. if (!empty($logUnits)) {
  426. if (!$recursive) {
  427. $details = $this->getLogicalStructureInfo($logUnits[0]);
  428. } else {
  429. // cache the ranges - they might occure multiple times in the structures "tree" - with full data as well as referenced as id
  430. $processedStructures = [];
  431. foreach ($logUnits as $logUnit) {
  432. if (array_search($logUnit->getId(), $processedStructures) == false) {
  433. $this->tableOfContents[] = $this->getLogicalStructureInfo($logUnit, true, $processedStructures);
  434. }
  435. }
  436. }
  437. }
  438. return $details;
  439. }
  440. /**
  441. * Get the details about a IIIF resource (manifest or range) in the logical structure
  442. *
  443. * @access protected
  444. *
  445. * @param IiifResourceInterface $resource: IIIF resource, either a manifest or range.
  446. * @param bool $recursive: Whether to include the child elements
  447. * @param array $processedStructures: IIIF resources that already have been processed
  448. * @return array Logical structure array
  449. */
  450. protected function getLogicalStructureInfo(IiifResourceInterface $resource, $recursive = false, &$processedStructures = [])
  451. {
  452. $details = [];
  453. $details['id'] = $resource->getId();
  454. $details['dmdId'] = '';
  455. $details['label'] = $resource->getLabelForDisplay() !== null ? $resource->getLabelForDisplay() : '';
  456. $details['orderlabel'] = $resource->getLabelForDisplay() !== null ? $resource->getLabelForDisplay() : '';
  457. $details['contentIds'] = '';
  458. $details['volume'] = '';
  459. $details['pagination'] = '';
  460. $cPid = ($this->cPid ? $this->cPid : $this->pid);
  461. if ($details['id'] == $this->_getToplevelId()) {
  462. $metadata = $this->getMetadata($details['id'], $cPid);
  463. if (!empty($metadata['type'][0])) {
  464. $details['type'] = $metadata['type'][0];
  465. }
  466. }
  467. $details['thumbnailId'] = $resource->getThumbnailUrl();
  468. $details['points'] = '';
  469. // Load strucural mapping
  470. $this->_getSmLinks();
  471. // Load physical structure.
  472. $this->_getPhysicalStructure();
  473. $canvases = [];
  474. if ($resource instanceof ManifestInterface) {
  475. $startCanvas = $resource->getStartCanvasOrFirstCanvas();
  476. $canvases = $resource->getDefaultCanvases();
  477. } elseif ($resource instanceof RangeInterface) {
  478. $startCanvas = $resource->getStartCanvasOrFirstCanvas();
  479. $canvases = $resource->getAllCanvases();
  480. }
  481. if ($startCanvas != null) {
  482. $details['pagination'] = $startCanvas->getLabel();
  483. $startCanvasIndex = array_search($startCanvas, $this->iiif->getDefaultCanvases());
  484. if ($startCanvasIndex !== false) {
  485. $details['points'] = $startCanvasIndex + 1;
  486. }
  487. }
  488. $useGroups = $this->getUseGroups('fileGrpImages');
  489. if (is_string($useGroups)) {
  490. $useGroups = [$useGroups];
  491. }
  492. // Keep for later usage.
  493. $this->logicalUnits[$details['id']] = $details;
  494. // Walk the structure recursively? And are there any children of the current element?
  495. if ($recursive) {
  496. $processedStructures[] = $resource->getId();
  497. $details['children'] = [];
  498. if ($resource instanceof ManifestInterface && $resource->getRootRanges() != null) {
  499. $rangesToAdd = [];
  500. $rootRanges = [];
  501. if (sizeof($this->iiif->getRootRanges()) == 1 && $this->iiif->getRootRanges()[0]->isTopRange()) {
  502. $rangesToAdd = $this->iiif->getRootRanges()[0]->getMemberRangesAndRanges();
  503. } else {
  504. $rangesToAdd = $this->iiif->getRootRanges();
  505. }
  506. foreach ($rangesToAdd as $range) {
  507. $rootRanges[] = $range;
  508. }
  509. foreach ($rootRanges as $range) {
  510. if ((array_search($range->getId(), $processedStructures) == false)) {
  511. $details['children'][] = $this->getLogicalStructureInfo($range, true, $processedStructures);
  512. }
  513. }
  514. } elseif ($resource instanceof RangeInterface) {
  515. if (!empty($resource->getAllRanges())) {
  516. foreach ($resource->getAllRanges() as $range) {
  517. if ((array_search($range->getId(), $processedStructures) == false)) {
  518. $details['children'][] = $this->getLogicalStructureInfo($range, true, $processedStructures);
  519. }
  520. }
  521. }
  522. }
  523. }
  524. return $details;
  525. }
  526. /**
  527. * Returns metadata for IIIF resources with the ID $id in there original form in
  528. * the manifest, but prepared for display to the user.
  529. *
  530. * @access public
  531. *
  532. * @param string $id: the ID of the IIIF resource
  533. * @param int $cPid: the configuration folder's id
  534. * @param bool $withDescription: add description / summary to the return value
  535. * @param bool $withRights: add attribution and license / rights and requiredStatement to the return value
  536. * @param bool $withRelated: add related links / homepage to the return value
  537. *
  538. * @return array
  539. *
  540. * @todo This method is still in experimental; the method signature may change.
  541. */
  542. public function getManifestMetadata($id, $cPid = 0, $withDescription = true, $withRights = true, $withRelated = true)
  543. {
  544. if (!empty($this->originalMetadataArray[$id])) {
  545. return $this->originalMetadataArray[$id];
  546. }
  547. $iiifResource = $this->iiif->getContainedResourceById($id);
  548. $result = [];
  549. if ($iiifResource != null) {
  550. if ($iiifResource->getLabel() != null && $iiifResource->getLabel() != "") {
  551. $result['label'] = $iiifResource->getLabel();
  552. }
  553. if (!empty($iiifResource->getMetadata())) {
  554. $result['metadata'] = [];
  555. foreach ($iiifResource->getMetadataForDisplay() as $metadata) {
  556. $result['metadata'][$metadata['label']] = $metadata['value'];
  557. }
  558. }
  559. if ($withDescription && !empty($iiifResource->getSummary())) {
  560. $result["description"] = $iiifResource->getSummaryForDisplay();
  561. }
  562. if ($withRights) {
  563. if (!empty($iiifResource->getRights())) {
  564. $result["rights"] = $iiifResource->getRights();
  565. }
  566. if (!empty($iiifResource->getRequiredStatement())) {
  567. $result["requiredStatement"] = $iiifResource->getRequiredStatementForDisplay();
  568. }
  569. }
  570. if ($withRelated && !empty($iiifResource->getWeblinksForDisplay())) {
  571. $result["weblinks"] = [];
  572. foreach ($iiifResource->getWeblinksForDisplay() as $link) {
  573. $key = array_key_exists("label", $link) ? $link["label"] : $link["@id"];
  574. $result["weblinks"][$key] = $link["@id"];
  575. }
  576. }
  577. }
  578. return $result;
  579. }
  580. /**
  581. * {@inheritDoc}
  582. * @see Document::getMetadata()
  583. */
  584. public function getMetadata($id, $cPid = 0)
  585. {
  586. if (!empty($this->metadataArray[$id]) && $this->metadataArray[0] == $cPid) {
  587. return $this->metadataArray[$id];
  588. }
  589. // Initialize metadata array with empty values.
  590. // TODO initialize metadata in abstract class
  591. $metadata = [
  592. 'title' => [],
  593. 'title_sorting' => [],
  594. 'author' => [],
  595. 'place' => [],
  596. 'year' => [],
  597. 'prod_id' => [],
  598. 'record_id' => [],
  599. 'opac_id' => [],
  600. 'union_id' => [],
  601. 'urn' => [],
  602. 'purl' => [],
  603. 'type' => [],
  604. 'volume' => [],
  605. 'volume_sorting' => [],
  606. 'license' => [],
  607. 'terms' => [],
  608. 'restrictions' => [],
  609. 'out_of_print' => [],
  610. 'rights_info' => [],
  611. 'collection' => [],
  612. 'owner' => [],
  613. 'mets_label' => [],
  614. 'mets_orderlabel' => [],
  615. 'document_format' => ['IIIF'],
  616. ];
  617. $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
  618. ->getQueryBuilderForTable('tx_dlf_metadata');
  619. // Get hidden records, too.
  620. $queryBuilder
  621. ->getRestrictions()
  622. ->removeByType(HiddenRestriction::class);
  623. $result = $queryBuilder
  624. ->select(
  625. 'tx_dlf_metadata.index_name AS index_name',
  626. 'tx_dlf_metadataformat.xpath AS xpath',
  627. 'tx_dlf_metadataformat.xpath_sorting AS xpath_sorting',
  628. 'tx_dlf_metadata.is_sortable AS is_sortable',
  629. 'tx_dlf_metadata.default_value AS default_value',
  630. 'tx_dlf_metadata.format AS format'
  631. )
  632. ->from('tx_dlf_metadata')
  633. ->from('tx_dlf_metadataformat')
  634. ->from('tx_dlf_formats')
  635. ->where(
  636. $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($pid)),
  637. $queryBuilder->expr()->eq('tx_dlf_metadataformat.pid', intval($pid)),
  638. $queryBuilder->expr()->orX(
  639. $queryBuilder->expr()->andX(
  640. $queryBuilder->expr()->eq('tx_dlf_metadata.uid', 'tx_dlf_metadataformat.parent_id'),
  641. $queryBuilder->expr()->eq('tx_dlf_metadataformat.encoded', 'tx_dlf_formats.uid'),
  642. $queryBuilder->expr()->eq('tx_dlf_formats.type', $queryBuilder->createNamedParameter($this->getIiifVersion()))
  643. ),
  644. $queryBuilder->expr()->eq('tx_dlf_metadata.format', 0)
  645. )
  646. )
  647. ->execute();
  648. $iiifResource = $this->iiif->getContainedResourceById($id);
  649. while ($resArray = $result->fetch()) {
  650. // Set metadata field's value(s).
  651. if ($resArray['format'] > 0 && !empty($resArray['xpath']) && ($values = $iiifResource->jsonPath($resArray['xpath'])) != null) {
  652. if (is_string($values)) {
  653. $metadata[$resArray['index_name']] = [trim((string) $values)];
  654. } elseif ($values instanceof JSONPath && is_array($values->data()) && count($values->data()) > 1) {
  655. $metadata[$resArray['index_name']] = [];
  656. foreach ($values->data() as $value) {
  657. $metadata[$resArray['index_name']][] = trim((string) $value);
  658. }
  659. }
  660. }
  661. // Set default value if applicable.
  662. if (empty($metadata[$resArray['index_name']][0]) && strlen($resArray['default_value']) > 0) {
  663. $metadata[$resArray['index_name']] = [$resArray['default_value']];
  664. }
  665. // Set sorting value if applicable.
  666. if (!empty($metadata[$resArray['index_name']]) && $resArray['is_sortable']) {
  667. if (
  668. $resArray['format'] > 0 && !empty($resArray['xpath_sorting'])
  669. && ($values = $iiifResource->jsonPath($resArray['xpath_sorting']) != null)
  670. ) {
  671. if (is_string($values)) {
  672. $metadata[$resArray['index_name'] . '_sorting'][0] = [trim((string) $values)];
  673. } elseif ($values instanceof JSONPath && is_array($values->data()) && count($values->data()) > 1) {
  674. $metadata[$resArray['index_name']] = [];
  675. foreach ($values->data() as $value) {
  676. $metadata[$resArray['index_name'] . '_sorting'][0] = trim((string) $value);
  677. }
  678. }
  679. }
  680. if (empty($metadata[$resArray['index_name'] . '_sorting'][0])) {
  681. $metadata[$resArray['index_name'] . '_sorting'][0] = $metadata[$resArray['index_name']][0];
  682. }
  683. }
  684. }
  685. return $metadata;
  686. }
  687. /**
  688. * {@inheritDoc}
  689. * @see Document::_getSmLinks()
  690. */
  691. protected function _getSmLinks()
  692. {
  693. if (!$this->smLinksLoaded && isset($this->iiif) && $this->iiif instanceof ManifestInterface) {
  694. if (!empty($this->iiif->getDefaultCanvases())) {
  695. foreach ($this->iiif->getDefaultCanvases() as $canvas) {
  696. $this->smLinkCanvasToResource($canvas, $this->iiif);
  697. }
  698. }
  699. if (!empty($this->iiif->getStructures())) {
  700. foreach ($this->iiif->getStructures() as $range) {
  701. $this->smLinkRangeCanvasesRecursively($range);
  702. }
  703. }
  704. $this->smLinksLoaded = true;
  705. }
  706. return $this->smLinks;
  707. }
  708. /**
  709. * Construct a link between a range and it's sub ranges and all contained canvases.
  710. *
  711. * @access private
  712. *
  713. * @param RangeInterface $range: Current range whose canvases shall be linked
  714. */
  715. private function smLinkRangeCanvasesRecursively(RangeInterface $range)
  716. {
  717. // map range's canvases including all child ranges' canvases
  718. if (!$range->isTopRange()) {
  719. foreach ($range->getAllCanvasesRecursively() as $canvas) {
  720. $this->smLinkCanvasToResource($canvas, $range);
  721. }
  722. }
  723. // recursive call for all ranges
  724. if (!empty($range->getAllRanges())) {
  725. foreach ($range->getAllRanges() as $childRange) {
  726. $this->smLinkRangeCanvasesRecursively($childRange);
  727. }
  728. }
  729. }
  730. /**
  731. * Link a single canvas to a containing range
  732. *
  733. * @access private
  734. *
  735. * @param CanvasInterface $canvas
  736. * @param IiifResourceInterface $resource
  737. */
  738. private function smLinkCanvasToResource(CanvasInterface $canvas, IiifResourceInterface $resource)
  739. {
  740. $this->smLinks['l2p'][$resource->getId()][] = $canvas->getId();
  741. if (!is_array($this->smLinks['p2l'][$canvas->getId()]) || !in_array($resource->getId(), $this->smLinks['p2l'][$canvas->getId()])) {
  742. $this->smLinks['p2l'][$canvas->getId()][] = $resource->getId();
  743. }
  744. }
  745. /**
  746. * Currently not supported for IIIF. Multivolume works _could_ be modelled
  747. * as IIIF Collections, but we can't tell them apart from actual collections.
  748. *
  749. * @access protected
  750. *
  751. * @see Document::getParentDocumentUidForSaving()
  752. */
  753. protected function getParentDocumentUidForSaving($pid, $core)
  754. {
  755. // Do nothing.
  756. }
  757. /**
  758. * {@inheritDoc}
  759. * @see Document::getRawText()
  760. */
  761. public function getRawText($id)
  762. {
  763. $rawText = '';
  764. // Get text from raw text array if available.
  765. if (!empty($this->rawTextArray[$id])) {
  766. return $this->rawTextArray[$id];
  767. }
  768. $this->ensureHasFulltextIsSet();
  769. if ($this->hasFulltext) {
  770. // Load physical structure ...
  771. $this->_getPhysicalStructure();
  772. // ... and extension configuration.
  773. $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
  774. $fileGrpsFulltext = GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']);
  775. if (!empty($this->physicalStructureInfo[$id])) {
  776. while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
  777. if (!empty($this->physicalStructureInfo[$id]['files'][$fileGrpFulltext])) {
  778. $rawText = parent::getRawTextFromXml($id);
  779. break;
  780. }
  781. }
  782. if ($extConf['indexAnnotations'] == 1) {
  783. $iiifResource = $this->iiif->getContainedResourceById($id);
  784. // Get annotation containers
  785. $annotationContainerIds = $this->physicalStructureInfo[$id]['annotationContainers'];
  786. if (!empty($annotationContainerIds)) {
  787. $annotationTexts = [];
  788. foreach ($annotationContainerIds as $annotationListId) {
  789. $annotationContainer = $this->iiif->getContainedResourceById($annotationListId);
  790. /* @var $annotationContainer \Ubl\Iiif\Presentation\Common\Model\Resources\AnnotationContainerInterface */
  791. foreach ($annotationContainer->getTextAnnotations(Motivation::PAINTING) as $annotation) {
  792. if (
  793. $annotation->getTargetResourceId() == $iiifResource->getId() &&
  794. $annotation->getBody() != null && $annotation->getBody()->getChars() != null
  795. ) {
  796. $annotationTexts[] = $annotation->getBody()->getChars();
  797. }
  798. }
  799. }
  800. $rawText .= implode(' ', $annotationTexts);
  801. }
  802. }
  803. } else {
  804. Helper::devLog('Invalid structure resource @id "' . $id . '"', DEVLOG_SEVERITY_WARNING);
  805. return $rawText;
  806. }
  807. $this->rawTextArray[$id] = $rawText;
  808. }
  809. return $rawText;
  810. }
  811. /**
  812. * Returns the underlying IiifResourceInterface.
  813. *
  814. * @access public
  815. *
  816. * @return IiifResourceInterface
  817. */
  818. public function getIiif()
  819. {
  820. return $this->iiif;
  821. }
  822. /**
  823. * {@inheritDoc}
  824. * @see Document::init()
  825. */
  826. protected function init()
  827. {
  828. // Nothing to do here, at the moment
  829. }
  830. /**
  831. * {@inheritDoc}
  832. * @see Document::loadLocation()
  833. */
  834. protected function loadLocation($location)
  835. {
  836. $fileResource = GeneralUtility::getUrl($location);
  837. if ($fileResource !== false) {
  838. $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
  839. IiifHelper::setUrlReader(IiifUrlReader::getInstance());
  840. IiifHelper::setMaxThumbnailHeight($conf['iiifThumbnailHeight']);
  841. IiifHelper::setMaxThumbnailWidth($conf['iiifThumbnailWidth']);
  842. $resource = IiifHelper::loadIiifResource($fileResource);
  843. if ($resource != null) {
  844. if ($resource instanceof ManifestInterface) {
  845. $this->iiif = $resource;
  846. return true;
  847. }
  848. }
  849. }
  850. Helper::devLog('Could not load IIIF manifest from "' . $location . '"', DEVLOG_SEVERITY_ERROR);
  851. return false;
  852. }
  853. /**
  854. * {@inheritDoc}
  855. * @see \Kitodo\Dlf\Common\Document::prepareMetadataArray()
  856. */
  857. protected function prepareMetadataArray($cPid)
  858. {
  859. $id = $this->iiif->getId();
  860. $this->metadataArray[(string) $id] = $this->getMetadata((string) $id, $cPid);
  861. }
  862. /**
  863. * {@inheritDoc}
  864. * @see Document::setPreloadedDocument()
  865. */
  866. protected function setPreloadedDocument($preloadedDocument)
  867. {
  868. if ($preloadedDocument instanceof ManifestInterface) {
  869. $this->iiif = $preloadedDocument;
  870. return true;
  871. }
  872. return false;
  873. }
  874. /**
  875. * {@inheritDoc}
  876. * @see Document::ensureHasFulltextIsSet()
  877. */
  878. protected function ensureHasFulltextIsSet()
  879. {
  880. /*
  881. * TODO Check annotations and annotation lists of canvas for ALTO documents.
  882. * Example:
  883. * https://digi.ub.uni-heidelberg.de/diglit/iiif/hirsch_hamburg1933_04_25/manifest.json links
  884. * https://digi.ub.uni-heidelberg.de/diglit/iiif/hirsch_hamburg1933_04_25/list/0001.json
  885. */
  886. if (!$this->hasFulltextSet && $this->iiif instanceof ManifestInterface) {
  887. $manifest = $this->iiif;
  888. $canvases = $manifest->getDefaultCanvases();
  889. foreach ($canvases as $canvas) {
  890. if (
  891. !empty($canvas->getSeeAlsoUrlsForFormat("application/alto+xml")) ||
  892. !empty($canvas->getSeeAlsoUrlsForProfile("http://www.loc.gov/standards/alto/"))
  893. ) {
  894. $this->hasFulltextSet = true;
  895. $this->hasFulltext = true;
  896. return;
  897. }
  898. $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
  899. if ($extConf['indexAnnotations'] == 1 && !empty($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING))) {
  900. foreach ($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING) as $annotationContainer) {
  901. if (($textAnnotations = $annotationContainer->getTextAnnotations(Motivation::PAINTING)) != null) {
  902. foreach ($textAnnotations as $annotation) {
  903. if (
  904. $annotation->getBody() != null &&
  905. $annotation->getBody()->getFormat() == "text/plain" &&
  906. $annotation->getBody()->getChars() != null
  907. ) {
  908. $this->hasFulltextSet = true;
  909. $this->hasFulltext = true;
  910. return;
  911. }
  912. }
  913. }
  914. }
  915. }
  916. }
  917. $this->hasFulltextSet = true;
  918. }
  919. }
  920. /**
  921. * {@inheritDoc}
  922. * @see \Kitodo\Dlf\Common\Document::_getThumbnail()
  923. */
  924. protected function _getThumbnail($forceReload = false)
  925. {
  926. return $this->iiif->getThumbnailUrl();
  927. }
  928. /**
  929. * {@inheritDoc}
  930. * @see \Kitodo\Dlf\Common\Document::_getToplevelId()
  931. */
  932. protected function _getToplevelId()
  933. {
  934. if (empty($this->toplevelId)) {
  935. if (isset($this->iiif)) {
  936. $this->toplevelId = $this->iiif->getId();
  937. }
  938. }
  939. return $this->toplevelId;
  940. }
  941. /**
  942. * This magic method is executed after the object is deserialized
  943. * @see __sleep()
  944. *
  945. * @access public
  946. *
  947. * @return void
  948. */
  949. public function __wakeup()
  950. {
  951. $conf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][self::$extKey]);
  952. IiifHelper::setUrlReader(IiifUrlReader::getInstance());
  953. IiifHelper::setMaxThumbnailHeight($conf['iiifThumbnailHeight']);
  954. IiifHelper::setMaxThumbnailWidth($conf['iiifThumbnailWidth']);
  955. $resource = IiifHelper::loadIiifResource($this->asJson);
  956. if ($resource != null && $resource instanceof ManifestInterface) {
  957. $this->asJson = '';
  958. $this->iiif = $resource;
  959. $this->init();
  960. } else {
  961. Helper::devLog('Could not load IIIF after deserialization', DEVLOG_SEVERITY_ERROR);
  962. }
  963. }
  964. /**
  965. *
  966. * @return string[]
  967. */
  968. public function __sleep()
  969. {
  970. // TODO implement serializiation in IIIF library
  971. $jsonArray = $this->iiif->getOriginalJsonArray();
  972. $this->asJson = json_encode($jsonArray);
  973. return ['uid', 'pid', 'recordId', 'parentId', 'asJson'];
  974. }
  975. }