Completed
Push — master ( 03fc4c...95d208 )
by Ralf
15s queued 12s
created

Mods::getSourceDetails()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 4
nop 0
dl 0
loc 27
rs 9.4555
c 0
b 0
f 0
1
<?php
2
namespace EWW\Dpf\Helper;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
18
class Mods
19
{
20
    /**
21
     * metadataObjectRepository
22
     *
23
     * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository
24
     * @inject
25
     */
26
    protected $metadataObjectRepository = null;
27
28
    /**
29
     * metadatGroupRepository
30
     *
31
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
32
     * @inject
33
     */
34
    protected $metadataGroupRepository = null;
35
36
37
    protected $modsDom;
38
39
    public function __construct($modsXml)
40
    {
41
        $this->setModsXml($modsXml);
42
    }
43
44
    public function setModsXml($modsXml)
45
    {
46
        $modsDom = new \DOMDocument();
47
        if (!empty($modsXml)) {
48
            if (is_null(@$modsDom->loadXML($modsXml))) {
49
                throw new \Exception("Couldn't load MODS data!");
50
            }
51
        }
52
        $this->modsDom = $modsDom;
53
    }
54
55
    public function getModsXml()
56
    {
57
        return $this->modsDom->saveXML();
58
    }
59
60
    public function getModsXpath()
61
    {
62
        $xpath = \EWW\Dpf\Helper\XPath::create($this->modsDom);
63
        return $xpath;
64
    }
65
66
    public function getTitle()
67
    {
68
        $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title');
69
70
        if ($titleNode->length == 0) {
71
            $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title");
72
        }
73
        return $titleNode->item(0)->nodeValue;
74
    }
75
76
    public function getAuthors()
77
    {
78
        return $this->getPersons("aut");
79
    }
80
81
    public function getPublishers()
82
    {
83
        return $this->getPersons("edt");
84
    }
85
86
    /**
87
     * Get persons of the given role
88
     *
89
     * @param $role
90
     * @return array
91
     */
92
    protected function getPersons($role)
93
    {
94
        $xpath = $this->getModsXpath();
95
96
        $authorNode = $xpath->query('/mods:mods/mods:name[mods:role/mods:roleTerm[@type="code"]="'.$role.'"]');
97
98
        $authors = array();
99
100
        foreach ($authorNode as $key => $author) {
101
102
            $familyNodes = $xpath->query('mods:namePart[@type="family"]', $author);
103
104
            $givenNodes = $xpath->query('mods:namePart[@type="given"]', $author);
105
106
            $name = array();
107
108
            if ($givenNodes->length > 0) {
109
                $name[] = $givenNodes->item(0)->nodeValue;
110
            }
111
112
            if ($familyNodes->length > 0) {
113
                $name[] = $familyNodes->item(0)->nodeValue;
114
            }
115
116
            $authors[$key] = implode(" ", $name);
117
        }
118
119
        return $authors;
120
    }
121
122
    public function setDateIssued($date)
123
    {
124
125
        $originInfo = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]');
126
127
        if ($originInfo->length > 0) {
128
            $dateIssued = $this->getModsXpath()->query('mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]', $originInfo->item(0));
129
130
            if ($dateIssued->length == 0) {
131
                $newDateIssued = $this->modsDom->createElement('mods:dateIssued');
132
                $newDateIssued->setAttribute('encoding', 'iso8601');
133
                $newDateIssued->setAttribute('keyDate', 'yes');
134
                $newDateIssued->nodeValue = $date;
135
                $originInfo->item(0)->appendChild($newDateIssued);
136
            } else {
137
                $dateIssued->item(0)->nodeValue = $date;
138
            }
139
140
        } else {
141
142
            $rootNode = $this->getModsXpath()->query('/mods:mods');
143
144
            if ($rootNode->length == 1) {
145
                $newOriginInfo = $this->modsDom->createElement('mods:originInfo');
146
                $newOriginInfo->setAttribute('eventType', 'distribution');
147
                $rootNode->item(0)->appendChild($newOriginInfo);
148
149
                $newDateIssued = $this->modsDom->createElement('mods:dateIssued');
150
                $newDateIssued->setAttribute('encoding', 'iso8601');
151
                $newDateIssued->setAttribute('keyDate', 'yes');
152
                $newDateIssued->nodeValue = $date;
153
                $newOriginInfo->appendChild($newDateIssued);
154
            } else {
155
                throw new \Exception('Invalid xml data.');
156
            }
157
158
        }
159
160
    }
161
162
163
    public function getPublishingYear()
164
    {
165
        $year = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="publication"]/mods:dateIssued[@encoding="iso8601"]');
166
        if ($year->length > 0) {
167
            return $year->item(0)->nodeValue;
168
        }
169
170
        return null;
171
    }
172
173
    public function getOriginalSourceTitle()
174
    {
175
        $node= $this->getModsXpath()->query('/mods:mods/mods:relatedItem[@type="original"]/mods:titleInfo/mods:title');
176
        if ($node->length > 0) {
177
            return $node->item(0)->nodeValue;
178
        }
179
180
        return null;
181
    }
182
183
    public function getDateIssued()
184
    {
185
186
        $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]');
187
        if ($dateIssued->length > 0) {
188
            return $dateIssued->item(0)->nodeValue;
189
        }
190
191
        return null;
192
    }
193
194
    public function removeDateIssued()
195
    {
196
197
        $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]');
198
        if ($dateIssued->length > 0) {
199
            $dateIssued->item(0)->parentNode->removeChild($dateIssued->item(0));
200
        }
201
202
    }
203
204
    public function hasQucosaUrn()
205
    {
206
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
207
208
        $hasUrn = false;
209
210
        foreach ($urnNodeList as $urnNode) {
211
            $value  = $urnNode->nodeValue;
212
            $hasUrn = $hasUrn || !empty($value);
213
        }
214
215
        return $hasUrn;
216
    }
217
218
    public function getQucosaUrn()
219
    {
220
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
221
        $urnList     = '';
222
223
        if ($urnNodeList != null) {
224
            foreach ($urnNodeList as $urnNode) {
225
                $urnList = $urnNode->nodeValue;
226
            }
227
        }
228
        return $urnList;
229
    }
230
231
    public function addQucosaUrn($urn)
232
    {
233
        $rootNode = $this->getModsXpath()->query('/mods:mods');
234
235
        if ($rootNode->length == 1) {
236
            $newUrn = $this->modsDom->createElement('mods:identifier');
237
            $newUrn->setAttribute('type', 'qucosa:urn');
238
            $newUrn->nodeValue = $urn;
239
            $rootNode->item(0)->appendChild($newUrn);
240
        } else {
241
            throw new \Exception('Invalid xml data.');
242
        }
243
244
    }
245
246
    public function clearAllUrn()
247
    {
248
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="urn"]');
249
250
        foreach ($urnNodeList as $urnNode) {
251
            $urnNode->parentNode->removeChild($urnNode);
252
        }
253
254
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
255
        foreach ($urnNodeList as $urnNode) {
256
            $urnNode->parentNode->removeChild($urnNode);
257
        }
258
    }
259
260
    /**
261
     * @return string
262
     */
263
    public function getSourceDetails()
264
    {
265
        $data = [];
266
267
        $dataNodes[] = $this->getModsXpath()->query('/mods:mods/mods:relatedItem[@type="original"]');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dataNodes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dataNodes = array(); before regardless.
Loading history...
268
269
        $dataNodes[] = $this->getModsXpath()->query(
270
            '/mods:mods/mods:part[@type="article"]/mods:detail/mods:number'
271
        );
272
273
        $dataNodes[] = $this->getModsXpath()->query('/mods:mods/mods:part[@type="section"]');
274
275
        foreach ($dataNodes as $dataNode) {
276
            foreach ($dataNode as $node) {
277
                if ($node->hasChildNodes()) {
278
                    foreach ($node->childNodes as $n) {
279
                        $data[] = preg_replace('/\s+/', ' ', $n->textContent);
280
                    }
281
                } else {
282
                    $data[] = preg_replace('/\s+/', ' ', $node->textContent);
283
                }
284
            }
285
        }
286
287
        $output = trim(implode(' ', $data));
288
        $output = preg_replace('/\s+/ ', ' ', $output);
289
        return $output;
290
    }
291
}
292