We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 183 |
| Total Lines | 1084 |
| 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 | $xml = $this->loadXMLLocation($location); |
||
| 718 | // Set some basic properties. |
||
| 719 | if ($xml !== false) { |
||
| 720 | $this->xml = $xml; |
||
| 721 | return true; |
||
| 722 | } |
||
| 723 | return false; |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * {@inheritDoc} |
||
| 728 | * @see FullTextDocument::ensureHasFullTextIsSet() |
||
| 729 | */ |
||
| 730 | protected function ensureHasFullTextIsSet() |
||
| 735 | } |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * {@inheritDoc} |
||
| 740 | * @see Document::getParentDocumentUid() |
||
| 741 | */ |
||
| 742 | protected function getParentDocumentUidForSaving($pid, $core, $owner) |
||
| 743 | { |
||
| 744 | $partof = 0; |
||
| 745 | // Get the closest ancestor of the current document which has a MPTR child. |
||
| 746 | $parentMptr = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@ID="' . $this->_getToplevelId() . '"]/ancestor::mets:div[./mets:mptr][1]/mets:mptr'); |
||
| 747 | if (!empty($parentMptr)) { |
||
| 748 | $parentLocation = (string) $parentMptr[0]->attributes('http://www.w3.org/1999/xlink')->href; |
||
| 749 | if ($parentLocation != $this->location) { |
||
| 750 | $parentDoc = self::getInstance($parentLocation, $pid); |
||
| 751 | if ($parentDoc->ready) { |
||
| 752 | if ($parentDoc->pid != $pid) { |
||
| 753 | $parentDoc->save($pid, $core, $owner); |
||
| 754 | } |
||
| 755 | $partof = $parentDoc->uid; |
||
| 756 | } |
||
| 757 | } |
||
| 758 | } |
||
| 759 | return $partof; |
||
| 760 | } |
||
| 761 | |||
| 762 | /** |
||
| 763 | * {@inheritDoc} |
||
| 764 | * @see Document::setPreloadedDocument() |
||
| 765 | */ |
||
| 766 | protected function setPreloadedDocument($preloadedDocument) |
||
| 767 | { |
||
| 768 | |||
| 769 | if ($preloadedDocument instanceof \SimpleXMLElement) { |
||
| 770 | $this->xml = $preloadedDocument; |
||
| 771 | return true; |
||
| 772 | } |
||
| 773 | return false; |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * {@inheritDoc} |
||
| 778 | * @see Document::getDocument() |
||
| 779 | */ |
||
| 780 | protected function getDocument() |
||
| 781 | { |
||
| 782 | return $this->mets; |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * This builds an array of the document's dmdSecs |
||
| 787 | * |
||
| 788 | * @access protected |
||
| 789 | * |
||
| 790 | * @return array Array of dmdSecs with their IDs as array key |
||
| 791 | */ |
||
| 792 | protected function _getDmdSec() |
||
| 793 | { |
||
| 794 | if (!$this->dmdSecLoaded) { |
||
| 795 | // Get available data formats. |
||
| 796 | $this->loadFormats(); |
||
| 797 | // Get dmdSec nodes from METS. |
||
| 798 | $dmdIds = $this->mets->xpath('./mets:dmdSec/@ID'); |
||
| 799 | if (!empty($dmdIds)) { |
||
| 800 | foreach ($dmdIds as $dmdId) { |
||
| 801 | if ($type = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[not(@MDTYPE="OTHER")]/@MDTYPE')) { |
||
| 802 | if (!empty($this->formats[(string) $type[0]])) { |
||
| 803 | $type = (string) $type[0]; |
||
| 804 | $xml = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[@MDTYPE="' . $type . '"]/mets:xmlData/' . strtolower($type) . ':' . $this->formats[$type]['rootElement']); |
||
| 805 | } |
||
| 806 | } elseif ($type = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[@MDTYPE="OTHER"]/@OTHERMDTYPE')) { |
||
| 807 | if (!empty($this->formats[(string) $type[0]])) { |
||
| 808 | $type = (string) $type[0]; |
||
| 809 | $xml = $this->mets->xpath('./mets:dmdSec[@ID="' . (string) $dmdId . '"]/mets:mdWrap[@MDTYPE="OTHER"][@OTHERMDTYPE="' . $type . '"]/mets:xmlData/' . strtolower($type) . ':' . $this->formats[$type]['rootElement']); |
||
| 810 | } |
||
| 811 | } |
||
| 812 | if (!empty($xml)) { |
||
| 813 | $this->dmdSec[(string) $dmdId]['type'] = $type; |
||
| 814 | $this->dmdSec[(string) $dmdId]['xml'] = $xml[0]; |
||
| 815 | $this->registerNamespaces($this->dmdSec[(string) $dmdId]['xml']); |
||
| 816 | } |
||
| 817 | } |
||
| 818 | } |
||
| 819 | $this->dmdSecLoaded = true; |
||
| 820 | } |
||
| 821 | return $this->dmdSec; |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * This builds the file ID -> USE concordance |
||
| 826 | * |
||
| 827 | * @access protected |
||
| 828 | * |
||
| 829 | * @return array Array of file use groups with file IDs |
||
| 830 | */ |
||
| 831 | protected function _getFileGrps() |
||
| 832 | { |
||
| 833 | if (!$this->fileGrpsLoaded) { |
||
| 834 | // Get configured USE attributes. |
||
| 835 | $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey); |
||
| 836 | $useGrps = GeneralUtility::trimExplode(',', $extConf['fileGrpImages']); |
||
| 837 | if (!empty($extConf['fileGrpThumbs'])) { |
||
| 838 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs'])); |
||
| 839 | } |
||
| 840 | if (!empty($extConf['fileGrpDownload'])) { |
||
| 841 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpDownload'])); |
||
| 842 | } |
||
| 843 | if (!empty($extConf['fileGrpFulltext'])) { |
||
| 844 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext'])); |
||
| 845 | } |
||
| 846 | if (!empty($extConf['fileGrpAudio'])) { |
||
| 847 | $useGrps = array_merge($useGrps, GeneralUtility::trimExplode(',', $extConf['fileGrpAudio'])); |
||
| 848 | } |
||
| 849 | // Get all file groups. |
||
| 850 | $fileGrps = $this->mets->xpath('./mets:fileSec/mets:fileGrp'); |
||
| 851 | if (!empty($fileGrps)) { |
||
| 852 | // Build concordance for configured USE attributes. |
||
| 853 | foreach ($fileGrps as $fileGrp) { |
||
| 854 | if (in_array((string) $fileGrp['USE'], $useGrps)) { |
||
| 855 | foreach ($fileGrp->children('http://www.loc.gov/METS/')->file as $file) { |
||
| 856 | $this->fileGrps[(string) $file->attributes()->ID] = (string) $fileGrp['USE']; |
||
| 857 | } |
||
| 858 | } |
||
| 859 | } |
||
| 860 | } |
||
| 861 | // Are there any full text files available? |
||
| 862 | if ( |
||
| 863 | !empty($extConf['fileGrpFulltext']) |
||
| 864 | && array_intersect(GeneralUtility::trimExplode(',', $extConf['fileGrpFulltext']), $this->fileGrps) !== [] |
||
| 865 | ) { |
||
| 866 | $this->hasFullText = true; |
||
| 867 | } |
||
| 868 | $this->fileGrpsLoaded = true; |
||
| 869 | } |
||
| 870 | return $this->fileGrps; |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * {@inheritDoc} |
||
| 875 | * @see Document::prepareMetadataArray() |
||
| 876 | */ |
||
| 877 | protected function prepareMetadataArray($cPid) |
||
| 884 | } |
||
| 885 | } |
||
| 886 | // Set current PID for metadata definitions. |
||
| 887 | } |
||
| 888 | |||
| 889 | /** |
||
| 890 | * This returns $this->mets via __get() |
||
| 891 | * |
||
| 892 | * @access protected |
||
| 893 | * |
||
| 894 | * @return \SimpleXMLElement The XML's METS part as \SimpleXMLElement object |
||
| 895 | */ |
||
| 896 | protected function _getMets() |
||
| 897 | { |
||
| 898 | return $this->mets; |
||
| 899 | } |
||
| 900 | |||
| 901 | /** |
||
| 902 | * {@inheritDoc} |
||
| 903 | * @see Document::_getPhysicalStructure() |
||
| 904 | */ |
||
| 905 | protected function _getPhysicalStructure() |
||
| 906 | { |
||
| 907 | // Is there no physical structure array yet? |
||
| 908 | if (!$this->physicalStructureLoaded) { |
||
| 909 | // Does the document have a structMap node of type "PHYSICAL"? |
||
| 910 | $elementNodes = $this->mets->xpath('./mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]/mets:div'); |
||
| 911 | if (!empty($elementNodes)) { |
||
| 912 | // Get file groups. |
||
| 913 | $fileUse = $this->_getFileGrps(); |
||
| 914 | // Get the physical sequence's metadata. |
||
| 915 | $physNode = $this->mets->xpath('./mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]'); |
||
| 916 | $physSeq[0] = (string) $physNode[0]['ID']; |
||
| 917 | $this->physicalStructureInfo[$physSeq[0]]['id'] = (string) $physNode[0]['ID']; |
||
| 918 | $this->physicalStructureInfo[$physSeq[0]]['dmdId'] = (isset($physNode[0]['DMDID']) ? (string) $physNode[0]['DMDID'] : ''); |
||
| 919 | $this->physicalStructureInfo[$physSeq[0]]['order'] = (isset($physNode[0]['ORDER']) ? (string) $physNode[0]['ORDER'] : ''); |
||
| 920 | $this->physicalStructureInfo[$physSeq[0]]['label'] = (isset($physNode[0]['LABEL']) ? (string) $physNode[0]['LABEL'] : ''); |
||
| 921 | $this->physicalStructureInfo[$physSeq[0]]['orderlabel'] = (isset($physNode[0]['ORDERLABEL']) ? (string) $physNode[0]['ORDERLABEL'] : ''); |
||
| 922 | $this->physicalStructureInfo[$physSeq[0]]['type'] = (string) $physNode[0]['TYPE']; |
||
| 923 | $this->physicalStructureInfo[$physSeq[0]]['contentIds'] = (isset($physNode[0]['CONTENTIDS']) ? (string) $physNode[0]['CONTENTIDS'] : ''); |
||
| 924 | // Get the file representations from fileSec node. |
||
| 925 | foreach ($physNode[0]->children('http://www.loc.gov/METS/')->fptr as $fptr) { |
||
| 926 | // Check if file has valid @USE attribute. |
||
| 927 | if (!empty($fileUse[(string) $fptr->attributes()->FILEID])) { |
||
| 928 | $this->physicalStructureInfo[$physSeq[0]]['files'][$fileUse[(string) $fptr->attributes()->FILEID]] = (string) $fptr->attributes()->FILEID; |
||
| 929 | } |
||
| 930 | } |
||
| 931 | // Build the physical elements' array from the physical structMap node. |
||
| 932 | foreach ($elementNodes as $elementNode) { |
||
| 933 | $elements[(int) $elementNode['ORDER']] = (string) $elementNode['ID']; |
||
| 934 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['id'] = (string) $elementNode['ID']; |
||
| 935 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['dmdId'] = (isset($elementNode['DMDID']) ? (string) $elementNode['DMDID'] : ''); |
||
| 936 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['order'] = (isset($elementNode['ORDER']) ? (string) $elementNode['ORDER'] : ''); |
||
| 937 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['label'] = (isset($elementNode['LABEL']) ? (string) $elementNode['LABEL'] : ''); |
||
| 938 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['orderlabel'] = (isset($elementNode['ORDERLABEL']) ? (string) $elementNode['ORDERLABEL'] : ''); |
||
| 939 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['type'] = (string) $elementNode['TYPE']; |
||
| 940 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['contentIds'] = (isset($elementNode['CONTENTIDS']) ? (string) $elementNode['CONTENTIDS'] : ''); |
||
| 941 | // Get the file representations from fileSec node. |
||
| 942 | foreach ($elementNode->children('http://www.loc.gov/METS/')->fptr as $fptr) { |
||
| 943 | // Check if file has valid @USE attribute. |
||
| 944 | if (!empty($fileUse[(string) $fptr->attributes()->FILEID])) { |
||
| 945 | $this->physicalStructureInfo[$elements[(int) $elementNode['ORDER']]]['files'][$fileUse[(string) $fptr->attributes()->FILEID]] = (string) $fptr->attributes()->FILEID; |
||
| 946 | } |
||
| 947 | } |
||
| 948 | } |
||
| 949 | // Sort array by keys (= @ORDER). |
||
| 950 | if (ksort($elements)) { |
||
| 951 | // Set total number of pages/tracks. |
||
| 952 | $this->numPages = count($elements); |
||
| 953 | // Merge and re-index the array to get nice numeric indexes. |
||
| 954 | $this->physicalStructure = array_merge($physSeq, $elements); |
||
| 955 | } |
||
| 956 | } |
||
| 957 | $this->physicalStructureLoaded = true; |
||
| 958 | } |
||
| 959 | return $this->physicalStructure; |
||
| 960 | } |
||
| 961 | |||
| 962 | /** |
||
| 963 | * {@inheritDoc} |
||
| 964 | * @see Document::_getSmLinks() |
||
| 965 | */ |
||
| 966 | protected function _getSmLinks() |
||
| 979 | } |
||
| 980 | |||
| 981 | /** |
||
| 982 | * {@inheritDoc} |
||
| 983 | * @see Document::_getThumbnail() |
||
| 984 | */ |
||
| 985 | protected function _getThumbnail($forceReload = false) |
||
| 986 | { |
||
| 987 | if ( |
||
| 988 | !$this->thumbnailLoaded |
||
| 989 | || $forceReload |
||
| 990 | ) { |
||
| 991 | // Retain current PID. |
||
| 992 | $cPid = ($this->cPid ? $this->cPid : $this->pid); |
||
| 993 | if (!$cPid) { |
||
| 994 | $this->logger->error('Invalid PID ' . $cPid . ' for structure definitions'); |
||
| 995 | $this->thumbnailLoaded = true; |
||
| 996 | return $this->thumbnail; |
||
| 997 | } |
||
| 998 | // Load extension configuration. |
||
| 999 | $extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey); |
||
| 1000 | if (empty($extConf['fileGrpThumbs'])) { |
||
| 1001 | $this->logger->warning('No fileGrp for thumbnails specified'); |
||
| 1002 | $this->thumbnailLoaded = true; |
||
| 1003 | return $this->thumbnail; |
||
| 1004 | } |
||
| 1005 | $strctId = $this->_getToplevelId(); |
||
| 1006 | $metadata = $this->getTitleData($cPid); |
||
| 1007 | |||
| 1008 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 1009 | ->getQueryBuilderForTable('tx_dlf_structures'); |
||
| 1010 | |||
| 1011 | // Get structure element to get thumbnail from. |
||
| 1012 | $result = $queryBuilder |
||
| 1013 | ->select('tx_dlf_structures.thumbnail AS thumbnail') |
||
| 1014 | ->from('tx_dlf_structures') |
||
| 1015 | ->where( |
||
| 1016 | $queryBuilder->expr()->eq('tx_dlf_structures.pid', intval($cPid)), |
||
| 1017 | $queryBuilder->expr()->eq('tx_dlf_structures.index_name', $queryBuilder->expr()->literal($metadata['type'][0])), |
||
| 1018 | Helper::whereExpression('tx_dlf_structures') |
||
| 1019 | ) |
||
| 1020 | ->setMaxResults(1) |
||
| 1021 | ->execute(); |
||
| 1022 | |||
| 1023 | $allResults = $result->fetchAll(); |
||
| 1024 | |||
| 1025 | if (count($allResults) == 1) { |
||
| 1026 | $resArray = $allResults[0]; |
||
| 1027 | // Get desired thumbnail structure if not the toplevel structure itself. |
||
| 1028 | if (!empty($resArray['thumbnail'])) { |
||
| 1029 | $strctType = Helper::getIndexNameFromUid($resArray['thumbnail'], 'tx_dlf_structures', $cPid); |
||
| 1030 | // Check if this document has a structure element of the desired type. |
||
| 1031 | $strctIds = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@TYPE="' . $strctType . '"]/@ID'); |
||
| 1032 | if (!empty($strctIds)) { |
||
| 1033 | $strctId = (string) $strctIds[0]; |
||
| 1034 | } |
||
| 1035 | } |
||
| 1036 | // Load smLinks. |
||
| 1037 | $this->_getSmLinks(); |
||
| 1038 | // Get thumbnail location. |
||
| 1039 | $fileGrpsThumb = GeneralUtility::trimExplode(',', $extConf['fileGrpThumbs']); |
||
| 1040 | while ($fileGrpThumb = array_shift($fileGrpsThumb)) { |
||
| 1041 | if ( |
||
| 1042 | $this->_getPhysicalStructure() |
||
| 1043 | && !empty($this->smLinks['l2p'][$strctId]) |
||
| 1044 | && !empty($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$fileGrpThumb]) |
||
| 1045 | ) { |
||
| 1046 | $this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->smLinks['l2p'][$strctId][0]]['files'][$fileGrpThumb]); |
||
| 1047 | break; |
||
| 1048 | } elseif (!empty($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb])) { |
||
| 1049 | $this->thumbnail = $this->getFileLocation($this->physicalStructureInfo[$this->physicalStructure[1]]['files'][$fileGrpThumb]); |
||
| 1050 | break; |
||
| 1051 | } |
||
| 1052 | } |
||
| 1053 | } else { |
||
| 1054 | $this->logger->error('No structure of type "' . $metadata['type'][0] . '" found in database'); |
||
| 1055 | } |
||
| 1056 | $this->thumbnailLoaded = true; |
||
| 1057 | } |
||
| 1058 | return $this->thumbnail; |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * {@inheritDoc} |
||
| 1063 | * @see Document::_getToplevelId() |
||
| 1064 | */ |
||
| 1065 | protected function _getToplevelId() |
||
| 1066 | { |
||
| 1067 | if (empty($this->toplevelId)) { |
||
| 1068 | // Get all logical structure nodes with metadata, but without associated METS-Pointers. |
||
| 1069 | $divs = $this->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@DMDID and not(./mets:mptr)]'); |
||
| 1070 | if (!empty($divs)) { |
||
| 1071 | // Load smLinks. |
||
| 1072 | $this->_getSmLinks(); |
||
| 1073 | foreach ($divs as $div) { |
||
| 1074 | $id = (string) $div['ID']; |
||
| 1075 | // Are there physical structure nodes for this logical structure? |
||
| 1076 | if (array_key_exists($id, $this->smLinks['l2p'])) { |
||
| 1077 | // Yes. That's what we're looking for. |
||
| 1078 | $this->toplevelId = $id; |
||
| 1079 | break; |
||
| 1080 | } elseif (empty($this->toplevelId)) { |
||
| 1081 | // No. Remember this anyway, but keep looking for a better one. |
||
| 1082 | $this->toplevelId = $id; |
||
| 1083 | } |
||
| 1084 | } |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | return $this->toplevelId; |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * This magic method is executed prior to any serialization of the object |
||
| 1092 | * @see __wakeup() |
||
| 1093 | * |
||
| 1094 | * @access public |
||
| 1095 | * |
||
| 1096 | * @return array Properties to be serialized |
||
| 1097 | */ |
||
| 1098 | public function __sleep() |
||
| 1099 | { |
||
| 1100 | // \SimpleXMLElement objects can't be serialized, thus save the XML as string for serialization |
||
| 1101 | $this->asXML = $this->xml->asXML(); |
||
| 1102 | return ['uid', 'pid', 'recordId', 'parentId', 'asXML']; |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * This magic method is used for setting a string value for the object |
||
| 1107 | * |
||
| 1108 | * @access public |
||
| 1109 | * |
||
| 1110 | * @return string String representing the METS object |
||
| 1111 | */ |
||
| 1112 | public function __toString() |
||
| 1113 | { |
||
| 1114 | $xml = new \DOMDocument('1.0', 'utf-8'); |
||
| 1115 | $xml->appendChild($xml->importNode(dom_import_simplexml($this->mets), true)); |
||
| 1116 | $xml->formatOutput = true; |
||
| 1117 | return $xml->saveXML(); |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * This magic method is executed after the object is deserialized |
||
| 1122 | * @see __sleep() |
||
| 1123 | * |
||
| 1124 | * @access public |
||
| 1125 | * |
||
| 1126 | * @return void |
||
| 1127 | */ |
||
| 1128 | public function __wakeup() |
||
| 1138 | } |
||
| 1139 | } |
||
| 1140 | } |
||
| 1141 |