We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 184 |
| Total Lines | 1088 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MetsDocument often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetsDocument, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | final class MetsDocument extends FullTextDocument |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * This holds the whole XML file as string for serialization purposes |
||
| 58 | * @see __sleep() / __wakeup() |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | * @access protected |
||
| 62 | */ |
||
| 63 | protected $asXML = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * This holds the XML file's dmdSec parts with their IDs as array key |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | * @access protected |
||
| 70 | */ |
||
| 71 | protected $dmdSec = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Are the METS file's dmdSecs loaded? |
||
| 75 | * @see $dmdSec |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | * @access protected |
||
| 79 | */ |
||
| 80 | protected $dmdSecLoaded = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The extension key |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | * @access public |
||
| 87 | */ |
||
| 88 | public static $extKey = 'dlf'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * This holds the file ID -> USE concordance |
||
| 92 | * @see _getFileGrps() |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | * @access protected |
||
| 96 | */ |
||
| 97 | protected $fileGrps = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Are the image file groups loaded? |
||
| 101 | * @see $fileGrps |
||
| 102 | * |
||
| 103 | * @var bool |
||
| 104 | * @access protected |
||
| 105 | */ |
||
| 106 | protected $fileGrpsLoaded = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * This holds the XML file's METS part as \SimpleXMLElement object |
||
| 110 | * |
||
| 111 | * @var \SimpleXMLElement |
||
| 112 | * @access protected |
||
| 113 | */ |
||
| 114 | protected $mets; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * This adds metadata from METS structural map to metadata array. |
||
| 118 | * |
||
| 119 | * @access public |
||
| 120 | * |
||
| 121 | * @param array &$metadata: The metadata array to extend |
||
| 122 | * @param string $id: The @ID attribute of the logical structure node |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function addMetadataFromMets(&$metadata, $id) |
||
| 127 | { |
||
| 128 | $details = $this->getLogicalStructure($id); |
||
| 129 | if (!empty($details)) { |
||
| 130 | $metadata['mets_order'][0] = $details['order']; |
||
| 131 | $metadata['mets_label'][0] = $details['label']; |
||
| 132 | $metadata['mets_orderlabel'][0] = $details['orderlabel']; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * |
||
| 138 | * {@inheritDoc} |
||
| 139 | * @see Document::establishRecordId() |
||
| 140 | */ |
||
| 141 | protected function establishRecordId($pid) |
||
| 142 | { |
||
| 143 | // Check for METS object @ID. |
||
| 144 | if (!empty($this->mets['OBJID'])) { |
||
| 145 | $this->recordId = (string) $this->mets['OBJID']; |
||
|
|
|||
| 146 | } |
||
| 147 | // Get hook objects. |
||
| 148 | $hookObjects = Helper::getHookObjects('Classes/Common/Document/MetsDocument.php'); |
||
| 149 | // Apply hooks. |
||
| 150 | foreach ($hookObjects as $hookObj) { |
||
| 151 | if (method_exists($hookObj, 'construct_postProcessRecordId')) { |
||
| 152 | $hookObj->construct_postProcessRecordId($this->xml, $this->recordId); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * |
||
| 159 | * {@inheritDoc} |
||
| 160 | * @see Document::getDownloadLocation() |
||
| 161 | */ |
||
| 162 | public function getDownloadLocation($id) |
||
| 163 | { |
||
| 164 | $fileMimeType = $this->getFileMimeType($id); |
||
| 165 | $fileLocation = $this->getFileLocation($id); |
||
| 166 | if ($fileMimeType === 'application/vnd.kitodo.iiif') { |
||
| 167 | $fileLocation = (strrpos($fileLocation, 'info.json') === strlen($fileLocation) - 9) ? $fileLocation : (strrpos($fileLocation, '/') === strlen($fileLocation) ? $fileLocation . 'info.json' : $fileLocation . '/info.json'); |
||
| 168 | $conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey); |
||
| 169 | IiifHelper::setUrlReader(IiifUrlReader::getInstance()); |
||
| 170 | IiifHelper::setMaxThumbnailHeight($conf['iiifThumbnailHeight']); |
||
| 171 | IiifHelper::setMaxThumbnailWidth($conf['iiifThumbnailWidth']); |
||
| 172 | $service = IiifHelper::loadIiifResource($fileLocation); |
||
| 173 | if ($service !== null && $service instanceof AbstractImageService) { |
||
| 174 | return $service->getImageUrl(); |
||
| 175 | } |
||
| 176 | } elseif ($fileMimeType === 'application/vnd.netfpx') { |
||
| 177 | $baseURL = $fileLocation . (strpos($fileLocation, '?') === false ? '?' : ''); |
||
| 178 | // TODO CVT is an optional IIP server capability; in theory, capabilities should be determined in the object request with '&obj=IIP-server' |
||
| 179 | return $baseURL . '&CVT=jpeg'; |
||
| 180 | } |
||
| 181 | return $fileLocation; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritDoc} |
||
| 186 | * @see Document::getFileLocation() |
||
| 187 | */ |
||
| 188 | public function getFileLocation($id) |
||
| 189 | { |
||
| 190 | $location = $this->mets->xpath('./mets:fileSec/mets:fileGrp/mets:file[@ID="' . $id . '"]/mets:FLocat[@LOCTYPE="URL"]'); |
||
| 191 | if ( |
||
| 192 | !empty($id) |
||
| 193 | && !empty($location) |
||
| 194 | ) { |
||
| 195 | return (string) $location[0]->attributes('http://www.w3.org/1999/xlink')->href; |
||
| 196 | } else { |
||
| 197 | $this->logger->warning('There is no file node with @ID "' . $id . '"'); |
||
|
1 ignored issue
–
show
|
|||
| 198 | return ''; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritDoc} |
||
| 204 | * @see Document::getFileMimeType() |
||
| 205 | */ |
||
| 206 | public function getFileMimeType($id) |
||
| 207 | { |
||
| 208 | $mimetype = $this->mets->xpath('./mets:fileSec/mets:fileGrp/mets:file[@ID="' . $id . '"]/@MIMETYPE'); |
||
| 209 | if ( |
||
| 210 | !empty($id) |
||
| 211 | && !empty($mimetype) |
||
| 212 | ) { |
||
| 213 | return (string) $mimetype[0]; |
||
| 214 | } else { |
||
| 215 | $this->logger->warning('There is no file node with @ID "' . $id . '" or no MIME type specified'); |
||
| 216 | return ''; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritDoc} |
||
| 222 | * @see Document::getLogicalStructure() |
||
| 223 | */ |
||
| 224 | public function getLogicalStructure($id, $recursive = false) |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * This gets details about a logical structure element |
||
| 257 | * |
||
| 258 | * @access protected |
||
| 259 | * |
||
| 260 | * @param \SimpleXMLElement $structure: The logical structure node |
||
| 261 | * @param bool $recursive: Whether to include the child elements |
||
| 262 | * |
||
| 263 | * @return array Array of the element's id, label, type and physical page indexes/mptr link |
||
| 264 | */ |
||
| 265 | protected function getLogicalStructureInfo(\SimpleXMLElement $structure, $recursive = false) |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritDoc} |
||
| 362 | * @see Document::getMetadata() |
||
| 363 | */ |
||
| 364 | public function getMetadata($id, $cPid = 0) |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * {@inheritDoc} |
||
| 665 | * @see FullTextDocument::getFullText() |
||
| 666 | */ |
||
| 667 | public function getFullText($id) |
||
| 668 | { |
||
| 669 | $fullText = ''; |
||
| 670 | |||
| 671 | // Load fileGrps and check for full text files. |
||
| 672 | $this->_getFileGrps(); |
||
| 673 | if ($this->hasFullText) { |
||
| 674 | $fullText = $this->getFullTextFromXml($id); |
||
| 675 | } |
||
| 676 | return $fullText; |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * {@inheritDoc} |
||
| 681 | * @see Document::getStructureDepth() |
||
| 682 | */ |
||
| 683 | public function getStructureDepth($logId) |
||
| 684 | { |
||
| 685 | $ancestors = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@ID="' . $logId . '"]/ancestor::*'); |
||
| 686 | if (!empty($ancestors)) { |
||
| 687 | return count($ancestors); |
||
| 688 | } else { |
||
| 689 | return 0; |
||
| 690 | } |
||
| 691 | } |
||
| 692 | |||
| 693 | /** |
||
| 694 | * {@inheritDoc} |
||
| 695 | * @see Document::init() |
||
| 696 | */ |
||
| 697 | protected function init() |
||
| 708 | } |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * {@inheritDoc} |
||
| 713 | * @see Document::loadLocation() |
||
| 714 | */ |
||
| 715 | protected function loadLocation($location) |
||
| 716 | { |
||
| 717 | $fileResource = GeneralUtility::getUrl($location); |
||
| 718 | if ($fileResource !== false) { |
||
| 719 | $xml = Helper::getXmlFileAsString($fileResource); |
||
| 720 | // Set some basic properties. |
||
| 721 | if ($xml !== false) { |
||
| 722 | $this->xml = $xml; |
||
| 723 | return true; |
||
| 724 | } |
||
| 725 | } |
||
| 726 | $this->logger->error('Could not load XML file from "' . $location . '"'); |
||
| 727 | return false; |
||
| 728 | } |
||
| 729 | |||
| 730 | /** |
||
| 731 | * {@inheritDoc} |
||
| 732 | * @see FullTextDocument::ensureHasFullTextIsSet() |
||
| 733 | */ |
||
| 734 | protected function ensureHasFullTextIsSet() |
||
| 735 | { |
||
| 736 | // Are the fileGrps already loaded? |
||
| 737 | if (!$this->fileGrpsLoaded) { |
||
| 738 | $this->_getFileGrps(); |
||
| 739 | } |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * {@inheritDoc} |
||
| 744 | * @see Document::getParentDocumentUid() |
||
| 745 | */ |
||
| 746 | protected function getParentDocumentUidForSaving($pid, $core, $owner) |
||
| 747 | { |
||
| 748 | $partof = 0; |
||
| 749 | // Get the closest ancestor of the current document which has a MPTR child. |
||
| 750 | $parentMptr = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@ID="' . $this->_getToplevelId() . '"]/ancestor::mets:div[./mets:mptr][1]/mets:mptr'); |
||
| 751 | if (!empty($parentMptr)) { |
||
| 752 | $parentLocation = (string) $parentMptr[0]->attributes('http://www.w3.org/1999/xlink')->href; |
||
| 753 | if ($parentLocation != $this->location) { |
||
| 754 | $parentDoc = self::getInstance($parentLocation, $pid); |
||
| 755 | if ($parentDoc->ready) { |
||
| 756 | if ($parentDoc->pid != $pid) { |
||
| 757 | $parentDoc->save($pid, $core, $owner); |
||
| 758 | } |
||
| 759 | $partof = $parentDoc->uid; |
||
| 760 | } |
||
| 761 | } |
||
| 762 | } |
||
| 763 | return $partof; |
||
| 764 | } |
||
| 765 | |||
| 766 | /** |
||
| 767 | * {@inheritDoc} |
||
| 768 | * @see Document::setPreloadedDocument() |
||
| 769 | */ |
||
| 770 | protected function setPreloadedDocument($preloadedDocument) |
||
| 771 | { |
||
| 772 | |||
| 773 | if ($preloadedDocument instanceof \SimpleXMLElement) { |
||
| 774 | $this->xml = $preloadedDocument; |
||
| 775 | return true; |
||
| 776 | } |
||
| 777 | return false; |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * {@inheritDoc} |
||
| 782 | * @see Document::getDocument() |
||
| 783 | */ |
||
| 784 | protected function getDocument() |
||
| 785 | { |
||
| 786 | return $this->mets; |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * This builds an array of the document's dmdSecs |
||
| 791 | * |
||
| 792 | * @access protected |
||
| 793 | * |
||
| 794 | * @return array Array of dmdSecs with their IDs as array key |
||
| 795 | */ |
||
| 796 | protected function _getDmdSec() |
||
| 797 | { |
||
| 798 | if (!$this->dmdSecLoaded) { |
||
| 799 | // Get available data formats. |
||
| 800 | $this->loadFormats(); |
||
| 801 | // Get dmdSec nodes from METS. |
||
| 802 | $dmdIds = $this->mets->xpath('./mets:dmdSec/@ID'); |
||
| 803 | if (!empty($dmdIds)) { |
||
| 804 | foreach ($dmdIds as $dmdId) { |
||
| 805 | if ($type = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[not(@MDTYPE="OTHER")]/@MDTYPE')) { |
||
| 806 | if (!empty($this->formats[(string) $type[0]])) { |
||
| 807 | $type = (string) $type[0]; |
||
| 808 | $xml = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[@MDTYPE="' . $type . '"]/mets:xmlData/' . strtolower($type) . ':' . $this->formats[$type]['rootElement']); |
||
| 809 | } |
||
| 810 | } elseif ($type = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[@MDTYPE="OTHER"]/@OTHERMDTYPE')) { |
||
| 811 | if (!empty($this->formats[(string) $type[0]])) { |
||
| 812 | $type = (string) $type[0]; |
||
| 813 | $xml = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[@MDTYPE="OTHER"][@OTHERMDTYPE="' . $type . '"]/mets:xmlData/' . strtolower($type) . ':' . $this->formats[$type]['rootElement']); |
||
| 814 | } |
||
| 815 | } |
||
| 816 | if (!empty($xml)) { |
||
| 817 | $this->dmdSec[(string) $dmdId]['type'] = $type; |
||
| 818 | $this->dmdSec[(string) $dmdId]['xml'] = $xml[0]; |
||
| 819 | $this->registerNamespaces($this->dmdSec[(string) $dmdId]['xml']); |
||
| 820 | } |
||
| 821 | } |
||
| 822 | } |
||
| 823 | $this->dmdSecLoaded = true; |
||
| 824 | } |
||
| 825 | return $this->dmdSec; |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * This builds the file ID -> USE concordance |
||
| 830 | * |
||
| 831 | * @access protected |
||
| 832 | * |
||
| 833 | * @return array Array of file use groups with file IDs |
||
| 834 | */ |
||
| 835 | protected function _getFileGrps() |
||
| 836 | { |
||
| 837 | if (!$this->fileGrpsLoaded) { |
||
| 838 | // Get configured USE attributes. |
||
| 839 | $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey); |
||
| 840 | $useGrps = GeneralUtility::trimExplode(',', $extConf['fileGrpImages']); |
||
| 841 | if (!empty($extConf['fileGrpThumbs'])) { |
||
| 842 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs'])); |
||
| 843 | } |
||
| 844 | if (!empty($extConf['fileGrpDownload'])) { |
||
| 845 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpDownload'])); |
||
| 846 | } |
||
| 847 | if (!empty($extConf['fileGrpFulltext'])) { |
||
| 848 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext'])); |
||
| 849 | } |
||
| 850 | if (!empty($extConf['fileGrpAudio'])) { |
||
| 851 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpAudio'])); |
||
| 852 | } |
||
| 853 | // Get all file groups. |
||
| 854 | $fileGrps = $this->mets->xpath('./mets:fileSec/mets:fileGrp'); |
||
| 855 | if (!empty($fileGrps)) { |
||
| 856 | // Build concordance for configured USE attributes. |
||
| 857 | foreach ($fileGrps as $fileGrp) { |
||
| 858 | if (in_array((string) $fileGrp['USE'], $useGrps)) { |
||
| 859 | foreach ($fileGrp->children('http://www.loc.gov/METS/')->file as $file) { |
||
| 860 | $this->fileGrps[(string) $file->attributes()->ID] = (string) $fileGrp['USE']; |
||
| 861 | } |
||
| 862 | } |
||
| 863 | } |
||
| 864 | } |
||
| 865 | // Are there any full text files available? |
||
| 866 | if ( |
||
| 867 | !empty($extConf['fileGrpFulltext']) |
||
| 868 | && array_intersect(GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']), $this->fileGrps) !== [] |
||
| 869 | ) { |
||
| 870 | $this->hasFullText = true; |
||
| 871 | } |
||
| 872 | $this->fileGrpsLoaded = true; |
||
| 873 | } |
||
| 874 | return $this->fileGrps; |
||
| 875 | } |
||
| 876 | |||
| 877 | /** |
||
| 878 | * {@inheritDoc} |
||
| 879 | * @see Document::prepareMetadataArray() |
||
| 880 | */ |
||
| 881 | protected function prepareMetadataArray($cPid) |
||
| 888 | } |
||
| 889 | } |
||
| 890 | // Set current PID for metadata definitions. |
||
| 891 | } |
||
| 892 | |||
| 893 | /** |
||
| 894 | * This returns $this->mets via __get() |
||
| 895 | * |
||
| 896 | * @access protected |
||
| 897 | * |
||
| 898 | * @return \SimpleXMLElement The XML's METS part as \SimpleXMLElement object |
||
| 899 | */ |
||
| 900 | protected function _getMets() |
||
| 901 | { |
||
| 902 | return $this->mets; |
||
| 903 | } |
||
| 904 | |||
| 905 | /** |
||
| 906 | * {@inheritDoc} |
||
| 907 | * @see Document::_getPhysicalStructure() |
||
| 908 | */ |
||
| 909 | protected function _getPhysicalStructure() |
||
| 910 | { |
||
| 911 | // Is there no physical structure array yet? |
||
| 912 | if (!$this->physicalStructureLoaded) { |
||
| 913 | // Does the document have a structMap node of type "PHYSICAL"? |
||
| 914 | $elementNodes = $this->mets->xpath('./mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]/mets:div'); |
||
| 915 | if (!empty($elementNodes)) { |
||
| 916 | // Get file groups. |
||
| 917 | $fileUse = $this->_getFileGrps(); |
||
| 918 | // Get the physical sequence's metadata. |
||
| 919 | $physNode = $this->mets->xpath('./mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]'); |
||
| 920 | $physSeq[0] = (string) $physNode[0]['ID']; |
||
| 921 | $this->physicalStructureInfo[$physSeq[0]]['id'] = (string) $physNode[0]['ID']; |
||
| 922 | $this->physicalStructureInfo[$physSeq[0]]['dmdId'] = (isset($physNode[0]['DMDID']) ? (string) $physNode[0]['DMDID'] : ''); |
||
| 923 | $this->physicalStructureInfo[$physSeq[0]]['order'] = (isset($physNode[0]['ORDER']) ? (string) $physNode[0]['ORDER'] : ''); |
||
| 924 | $this->physicalStructureInfo[$physSeq[0]]['label'] = (isset($physNode[0]['LABEL']) ? (string) $physNode[0]['LABEL'] : ''); |
||
| 925 | $this->physicalStructureInfo[$physSeq[0]]['orderlabel'] = (isset($physNode[0]['ORDERLABEL']) ? (string) $physNode[0]['ORDERLABEL'] : ''); |
||
| 926 | $this->physicalStructureInfo[$physSeq[0]]['type'] = (string) $physNode[0]['TYPE']; |
||
| 927 | $this->physicalStructureInfo[$physSeq[0]]['contentIds'] = (isset($physNode[0]['CONTENTIDS']) ? (string) $physNode[0]['CONTENTIDS'] : ''); |
||
| 928 | // Get the file representations from fileSec node. |
||
| 929 | foreach ($physNode[0]->children('http://www.loc.gov/METS/')->fptr as $fptr) { |
||
| 930 | // Check if file has valid @USE attribute. |
||
| 931 | if (!empty($fileUse[(string) $fptr->attributes()->FILEID])) { |
||
| 932 | $this->physicalStructureInfo[$physSeq[0]]['files'][$fileUse[(string) $fptr->attributes()->FILEID]] = (string) $fptr->attributes()->FILEID; |
||
| 933 | } |
||
| 934 | } |
||
| 935 | // Build the physical elements' array from the physical structMap node. |
||
| 936 | foreach ($elementNodes as $elementNode) { |
||
| 937 | $elements[(int) $elementNode['ORDER']] = (string) $elementNode['ID']; |
||
| 938 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['id'] = (string) $elementNode['ID']; |
||
| 939 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['dmdId'] = (isset($elementNode['DMDID']) ? (string) $elementNode['DMDID'] : ''); |
||
| 940 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['order'] = (isset($elementNode['ORDER']) ? (string) $elementNode['ORDER'] : ''); |
||
| 941 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['label'] = (isset($elementNode['LABEL']) ? (string) $elementNode['LABEL'] : ''); |
||
| 942 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['orderlabel'] = (isset($elementNode['ORDERLABEL']) ? (string) $elementNode['ORDERLABEL'] : ''); |
||
| 943 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['type'] = (string) $elementNode['TYPE']; |
||
| 944 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['contentIds'] = (isset($elementNode['CONTENTIDS']) ? (string) $elementNode['CONTENTIDS'] : ''); |
||
| 945 | // Get the file representations from fileSec node. |
||
| 946 | foreach ($elementNode->children('http://www.loc.gov/METS/')->fptr as $fptr) { |
||
| 947 | // Check if file has valid @USE attribute. |
||
| 948 | if (!empty($fileUse[(string) $fptr->attributes()->FILEID])) { |
||
| 949 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['files'][$fileUse[(string) $fptr->attributes()->FILEID]] = (string) $fptr->attributes()->FILEID; |
||
| 950 | } |
||
| 951 | } |
||
| 952 | } |
||
| 953 | // Sort array by keys (= @ORDER). |
||
| 954 | if (ksort($elements)) { |
||
| 955 | // Set total number of pages/tracks. |
||
| 956 | $this->numPages = count($elements); |
||
| 957 | // Merge and re-index the array to get nice numeric indexes. |
||
| 958 | $this->physicalStructure = array_merge($physSeq, $elements); |
||
| 959 | } |
||
| 960 | } |
||
| 961 | $this->physicalStructureLoaded = true; |
||
| 962 | } |
||
| 963 | return $this->physicalStructure; |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * {@inheritDoc} |
||
| 968 | * @see Document::_getSmLinks() |
||
| 969 | */ |
||
| 970 | protected function _getSmLinks() |
||
| 983 | } |
||
| 984 | |||
| 985 | /** |
||
| 986 | * {@inheritDoc} |
||
| 987 | * @see Document::_getThumbnail() |
||
| 988 | */ |
||
| 989 | protected function _getThumbnail($forceReload = false) |
||
| 990 | { |
||
| 991 | if ( |
||
| 992 | !$this->thumbnailLoaded |
||
| 993 | || $forceReload |
||
| 994 | ) { |
||
| 995 | // Retain current PID. |
||
| 996 | $cPid = ($this->cPid ? $this->cPid : $this->pid); |
||
| 997 | if (!$cPid) { |
||
| 998 | $this->logger->error('Invalid PID ' . $cPid . ' for structure definitions'); |
||
| 999 | $this->thumbnailLoaded = true; |
||
| 1000 | return $this->thumbnail; |
||
| 1001 | } |
||
| 1002 | // Load extension configuration. |
||
| 1003 | $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey); |
||
| 1004 | if (empty($extConf['fileGrpThumbs'])) { |
||
| 1005 | $this->logger->warning('No fileGrp for thumbnails specified'); |
||
| 1006 | $this->thumbnailLoaded = true; |
||
| 1007 | return $this->thumbnail; |
||
| 1008 | } |
||
| 1009 | $strctId = $this->_getToplevelId(); |
||
| 1010 | $metadata = $this->getTitleData($cPid); |
||
| 1011 | |||
| 1012 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 1013 | ->getQueryBuilderForTable('tx_dlf_structures'); |
||
| 1014 | |||
| 1015 | // Get structure element to get thumbnail from. |
||
| 1016 | $result = $queryBuilder |
||
| 1017 | ->select('tx_dlf_structures.thumbnail AS thumbnail') |
||
| 1018 | ->from('tx_dlf_structures') |
||
| 1019 | ->where( |
||
| 1020 | $queryBuilder->expr()->eq('tx_dlf_structures.pid', intval($cPid)), |
||
| 1021 | $queryBuilder->expr()->eq('tx_dlf_structures.index_name', $queryBuilder->expr()->literal($metadata['type'][0])), |
||
| 1022 | Helper::whereExpression('tx_dlf_structures') |
||
| 1023 | ) |
||
| 1024 | ->setMaxResults(1) |
||
| 1025 | ->execute(); |
||
| 1026 | |||
| 1027 | $allResults = $result->fetchAll(); |
||
| 1028 | |||
| 1029 | if (count($allResults) == 1) { |
||
| 1030 | $resArray = $allResults[0]; |
||
| 1031 | // Get desired thumbnail structure if not the toplevel structure itself. |
||
| 1032 | if (!empty($resArray['thumbnail'])) { |
||
| 1033 | $strctType = Helper::getIndexNameFromUid($resArray['thumbnail'], 'tx_dlf_structures', $cPid); |
||
| 1034 | // Check if this document has a structure element of the desired type. |
||
| 1035 | $strctIds = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@TYPE="' . $strctType . '"]/@ID'); |
||
| 1036 | if (!empty($strctIds)) { |
||
| 1037 | $strctId = (string) $strctIds[0]; |
||
| 1038 | } |
||
| 1039 | } |
||
| 1040 | // Load smLinks. |
||
| 1041 | $this->_getSmLinks(); |
||
| 1042 | // Get thumbnail location. |
||
| 1043 | $fileGrpsThumb = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']); |
||
| 1044 | while ($fileGrpThumb = array_shift($fileGrpsThumb)) { |
||
| 1045 | if ( |
||
| 1046 | $this->_getPhysicalStructure() |
||
| 1047 | && !empty($this->smLinks['l2p'][$strctId]) |
||
| 1048 | && !empty($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$fileGrpThumb]) |
||
| 1049 | ) { |
||
| 1050 | $this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$fileGrpThumb]); |
||
| 1051 | break; |
||
| 1052 | } elseif (!empty($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb])) { |
||
| 1053 | $this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb]); |
||
| 1054 | break; |
||
| 1055 | } |
||
| 1056 | } |
||
| 1057 | } else { |
||
| 1058 | $this->logger->error('No structure of type "' . $metadata['type'][0] . '" found in database'); |
||
| 1059 | } |
||
| 1060 | $this->thumbnailLoaded = true; |
||
| 1061 | } |
||
| 1062 | return $this->thumbnail; |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * {@inheritDoc} |
||
| 1067 | * @see Document::_getToplevelId() |
||
| 1068 | */ |
||
| 1069 | protected function _getToplevelId() |
||
| 1070 | { |
||
| 1071 | if (empty($this->toplevelId)) { |
||
| 1072 | // Get all logical structure nodes with metadata, but without associated METS-Pointers. |
||
| 1073 | $divs = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@DMDID and not(./mets:mptr)]'); |
||
| 1074 | if (!empty($divs)) { |
||
| 1075 | // Load smLinks. |
||
| 1076 | $this->_getSmLinks(); |
||
| 1077 | foreach ($divs as $div) { |
||
| 1078 | $id = (string) $div['ID']; |
||
| 1079 | // Are there physical structure nodes for this logical structure? |
||
| 1080 | if (array_key_exists($id, $this->smLinks['l2p'])) { |
||
| 1081 | // Yes. That's what we're looking for. |
||
| 1082 | $this->toplevelId = $id; |
||
| 1083 | break; |
||
| 1084 | } elseif (empty($this->toplevelId)) { |
||
| 1085 | // No. Remember this anyway, but keep looking for a better one. |
||
| 1086 | $this->toplevelId = $id; |
||
| 1087 | } |
||
| 1088 | } |
||
| 1089 | } |
||
| 1090 | } |
||
| 1091 | return $this->toplevelId; |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * This magic method is executed prior to any serialization of the object |
||
| 1096 | * @see __wakeup() |
||
| 1097 | * |
||
| 1098 | * @access public |
||
| 1099 | * |
||
| 1100 | * @return array Properties to be serialized |
||
| 1101 | */ |
||
| 1102 | public function __sleep() |
||
| 1103 | { |
||
| 1104 | // \SimpleXMLElement objects can't be serialized, thus save the XML as string for serialization |
||
| 1105 | $this->asXML = $this->xml->asXML(); |
||
| 1106 | return ['uid', 'pid', 'recordId', 'parentId', 'asXML']; |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * This magic method is used for setting a string value for the object |
||
| 1111 | * |
||
| 1112 | * @access public |
||
| 1113 | * |
||
| 1114 | * @return string String representing the METS object |
||
| 1115 | */ |
||
| 1116 | public function __toString() |
||
| 1117 | { |
||
| 1118 | $xml = new \DOMDocument('1.0', 'utf-8'); |
||
| 1119 | $xml->appendChild($xml->importNode(dom_import_simplexml($this->mets), true)); |
||
| 1120 | $xml->formatOutput = true; |
||
| 1121 | return $xml->saveXML(); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * This magic method is executed after the object is deserialized |
||
| 1126 | * @see __sleep() |
||
| 1127 | * |
||
| 1128 | * @access public |
||
| 1129 | * |
||
| 1130 | * @return void |
||
| 1131 | */ |
||
| 1132 | public function __wakeup() |
||
| 1142 | } |
||
| 1143 | } |
||
| 1144 | } |
||
| 1145 |