|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU General Public License version 3 or later. |
|
9
|
|
|
* For the full copyright and license information, please read the |
|
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Kitodo\Dlf\Common\Document; |
|
14
|
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\Helper; |
|
16
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
|
17
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
18
|
|
|
|
|
19
|
|
|
trait XMLDocument |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Register all available data formats |
|
23
|
|
|
* |
|
24
|
|
|
* @access protected |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function loadFormats() |
|
29
|
|
|
{ |
|
30
|
|
|
if (!$this->formatsLoaded) { |
|
31
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
|
32
|
|
|
->getQueryBuilderForTable('tx_dlf_formats'); |
|
33
|
|
|
|
|
34
|
|
|
// Get available data formats from database. |
|
35
|
|
|
$result = $queryBuilder |
|
36
|
|
|
->select( |
|
37
|
|
|
'tx_dlf_formats.type AS type', |
|
38
|
|
|
'tx_dlf_formats.root AS root', |
|
39
|
|
|
'tx_dlf_formats.namespace AS namespace', |
|
40
|
|
|
'tx_dlf_formats.class AS class' |
|
41
|
|
|
) |
|
42
|
|
|
->from('tx_dlf_formats') |
|
43
|
|
|
->where( |
|
44
|
|
|
$queryBuilder->expr()->eq('tx_dlf_formats.pid', 0) |
|
45
|
|
|
) |
|
46
|
|
|
->execute(); |
|
47
|
|
|
|
|
48
|
|
|
while ($resArray = $result->fetch()) { |
|
49
|
|
|
// Update format registry. |
|
50
|
|
|
$this->formats[$resArray['type']] = [ |
|
|
|
|
|
|
51
|
|
|
'rootElement' => $resArray['root'], |
|
52
|
|
|
'namespaceURI' => $resArray['namespace'], |
|
53
|
|
|
'class' => $resArray['class'] |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->formatsLoaded = true; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* XML specific part of loading a location |
|
63
|
|
|
* |
|
64
|
|
|
* @access protected |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $location: The URL of the file to load |
|
67
|
|
|
* |
|
68
|
|
|
* @return string|bool string on success or false on failure |
|
69
|
|
|
* |
|
70
|
|
|
* @see Document::loadLocation($location) |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function loadXMLLocation($location) { |
|
73
|
|
|
$fileResource = GeneralUtility::getUrl($location); |
|
74
|
|
|
if ($fileResource !== false) { |
|
75
|
|
|
$xml = Helper::getXmlFileAsString($fileResource); |
|
76
|
|
|
if ($xml !== false) { |
|
77
|
|
|
return $xml; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
$this->logger->error('Could not load XML file from "' . $location . '"'); |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Register all available namespaces for a \SimpleXMLElement object |
|
86
|
|
|
* |
|
87
|
|
|
* @access public |
|
88
|
|
|
* |
|
89
|
|
|
* @param \SimpleXMLElement|\DOMXPath &$obj: \SimpleXMLElement or \DOMXPath object |
|
90
|
|
|
* |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function registerNamespaces(&$obj) |
|
94
|
|
|
{ |
|
95
|
|
|
// TODO Check usage, because it is public and may be used by extensions |
|
96
|
|
|
// TODO XML specific method does not seem to be used anywhere outside this class within the project |
|
97
|
|
|
// Changed to protected while moved to trait |
|
98
|
|
|
$this->loadFormats(); |
|
99
|
|
|
// Do we have a \SimpleXMLElement or \DOMXPath object? |
|
100
|
|
|
if ($obj instanceof \SimpleXMLElement) { |
|
101
|
|
|
$method = 'registerXPathNamespace'; |
|
102
|
|
|
} elseif ($obj instanceof \DOMXPath) { |
|
103
|
|
|
$method = 'registerNamespace'; |
|
104
|
|
|
} else { |
|
105
|
|
|
$this->logger->error('Given object is neither a SimpleXMLElement nor a DOMXPath instance'); |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
// Register metadata format's namespaces. |
|
109
|
|
|
foreach ($this->formats as $enc => $conf) { |
|
110
|
|
|
$obj->$method(strtolower($enc), $conf['namespaceURI']); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |