|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kitodo\Dlf\Format; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU General Public License version 3 or later. |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Metadata MODS format class for the 'dlf' extension |
|
16
|
|
|
* |
|
17
|
|
|
* @author Sebastian Meyer <[email protected]> |
|
18
|
|
|
* @package TYPO3 |
|
19
|
|
|
* @subpackage dlf |
|
20
|
|
|
* @access public |
|
21
|
|
|
*/ |
|
22
|
|
|
class Mods implements \Kitodo\Dlf\Common\MetadataInterface { |
|
23
|
|
|
/** |
|
24
|
|
|
* This extracts the essential MODS metadata from XML |
|
25
|
|
|
* |
|
26
|
|
|
* @access public |
|
27
|
|
|
* |
|
28
|
|
|
* @param \SimpleXMLElement $xml: The XML to extract the metadata from |
|
29
|
|
|
* @param array &$metadata: The metadata array to fill |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function extractMetadata(\SimpleXMLElement $xml, array &$metadata) { |
|
34
|
|
|
$xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
|
35
|
|
|
// Get "author" and "author_sorting". |
|
36
|
|
|
$authors = $xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); |
|
37
|
|
|
// Get "author" and "author_sorting" again if that was to sophisticated. |
|
38
|
|
|
if (!$authors) { |
|
|
|
|
|
|
39
|
|
|
// Get all names which do not have any role term assigned and assume these are authors. |
|
40
|
|
|
$authors = $xml->xpath('./mods:name[not(./mods:role)]'); |
|
41
|
|
|
} |
|
42
|
|
|
if (is_array($authors)) { |
|
|
|
|
|
|
43
|
|
|
for ($i = 0, $j = count($authors); $i < $j; $i++) { |
|
44
|
|
|
$authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
|
45
|
|
|
// Check if there is a display form. |
|
46
|
|
|
if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) { |
|
47
|
|
|
$metadata['author'][$i] = (string) $displayForm[0]; |
|
48
|
|
|
} elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) { |
|
49
|
|
|
$name = []; |
|
50
|
|
|
$k = 4; |
|
51
|
|
|
foreach ($nameParts as $namePart) { |
|
52
|
|
|
if (isset($namePart['type']) |
|
53
|
|
|
&& (string) $namePart['type'] == 'family') { |
|
54
|
|
|
$name[0] = (string) $namePart; |
|
55
|
|
|
} elseif (isset($namePart['type']) |
|
56
|
|
|
&& (string) $namePart['type'] == 'given') { |
|
57
|
|
|
$name[1] = (string) $namePart; |
|
58
|
|
|
} elseif (isset($namePart['type']) |
|
59
|
|
|
&& (string) $namePart['type'] == 'termsOfAddress') { |
|
60
|
|
|
$name[2] = (string) $namePart; |
|
61
|
|
|
} elseif (isset($namePart['type']) |
|
62
|
|
|
&& (string) $namePart['type'] == 'date') { |
|
63
|
|
|
$name[3] = (string) $namePart; |
|
64
|
|
|
} else { |
|
65
|
|
|
$name[$k] = (string) $namePart; |
|
66
|
|
|
} |
|
67
|
|
|
$k++; |
|
68
|
|
|
} |
|
69
|
|
|
ksort($name); |
|
70
|
|
|
$metadata['author'][$i] = trim(implode(', ', $name)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
// Get "place" and "place_sorting". |
|
75
|
|
|
$places = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm'); |
|
76
|
|
|
// Get "place" and "place_sorting" again if that was to sophisticated. |
|
77
|
|
|
if (!$places) { |
|
|
|
|
|
|
78
|
|
|
// Get all places and assume these are places of publication. |
|
79
|
|
|
$places = $xml->xpath('./mods:originInfo/mods:place/mods:placeTerm'); |
|
80
|
|
|
} |
|
81
|
|
|
if (is_array($places)) { |
|
|
|
|
|
|
82
|
|
|
foreach ($places as $place) { |
|
83
|
|
|
$metadata['place'][] = (string) $place; |
|
84
|
|
|
if (!$metadata['place_sorting'][0]) { |
|
85
|
|
|
$metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
// Get "year_sorting". |
|
90
|
|
|
if (($years_sorting = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) { |
|
91
|
|
|
foreach ($years_sorting as $year_sorting) { |
|
92
|
|
|
$metadata['year_sorting'][0] = intval($year_sorting); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
// Get "year" and "year_sorting" if not specified separately. |
|
96
|
|
|
$years = $xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]'); |
|
97
|
|
|
// Get "year" and "year_sorting" again if that was to sophisticated. |
|
98
|
|
|
if (!$years) { |
|
|
|
|
|
|
99
|
|
|
// Get all dates and assume these are dates of publication. |
|
100
|
|
|
$years = $xml->xpath('./mods:originInfo/mods:dateIssued'); |
|
101
|
|
|
} |
|
102
|
|
|
if (is_array($years)) { |
|
|
|
|
|
|
103
|
|
|
foreach ($years as $year) { |
|
104
|
|
|
$metadata['year'][] = (string) $year; |
|
105
|
|
|
if (!$metadata['year_sorting'][0]) { |
|
106
|
|
|
$year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year)); |
|
107
|
|
|
if (strpos($year_sorting, '.') |
|
108
|
|
|
|| strlen($year_sorting) < 3) { |
|
109
|
|
|
$year_sorting = ((intval(trim($year_sorting, '.')) - 1) * 100) + 50; |
|
110
|
|
|
} |
|
111
|
|
|
$metadata['year_sorting'][0] = intval($year_sorting); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.