Passed
Pull Request — master (#195)
by
unknown
06:45
created

Mods::setTitle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 10
rs 10
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
49
            $modsXml = preg_replace(
50
                "/<mods:mods.*?>/",
51
                '<mods:mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
52
                .'xmlns:mods="http://www.loc.gov/mods/v3" '
53
                .'xmlns:slub="http://slub-dresden.de/" '
54
                .'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" '
55
                .'xmlns:foaf="http://xmlns.com/foaf/0.1/" '
56
                .'xmlns:person="http://www.w3.org/ns/person#" '
57
                .'xsi:schemaLocation="http://www.loc.gov/mods/v3 '
58
                .'http://www.loc.gov/standards/mods/v3/mods-3-7.xsd" version="3.7">',
59
                $modsXml
60
            );
61
62
            if (is_null(@$modsDom->loadXML($modsXml))) {
63
                throw new \Exception("Couldn't load MODS data!");
64
            }
65
        }
66
        $this->modsDom = $modsDom;
67
    }
68
69
    public function getModsXml()
70
    {
71
        return $this->modsDom->saveXML();
72
    }
73
74
    public function getModsXpath()
75
    {
76
        $xpath = \EWW\Dpf\Helper\XPath::create($this->modsDom);
77
        return $xpath;
78
    }
79
80
    public function getTitle()
81
    {
82
        $titleNode = $this->getModsXpath()->query('/data/titleInfo/title');
83
84
        if ($titleNode->length == 0) {
85
            $titleNode = $this->getModsXpath()->query("/data/titleInfo/title");
86
        }
87
        return $titleNode->item(0)->nodeValue;
88
    }
89
90
91
    public function setTitle($title)
92
    {
93
        $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title');
94
95
        if ($titleNode->length == 0) {
96
            $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title");
97
        }
98
99
        if ($titleNode->length > 0) {
100
            $titleNode->item(0)->nodeValue = $title;
101
        }
102
    }
103
104
105
    public function getAuthors()
106
    {
107
        return $this->getPersons("aut");
108
    }
109
110
    public function getPublishers()
111
    {
112
        return $this->getPersons("edt");
113
    }
114
115
    /**
116
     * Get persons of the given role
117
     *
118
     * @param string $role
119
     * @return array
120
     */
121
    public function getPersons($role = '')
122
    {
123
        $xpath = $this->getModsXpath();
124
        $personNodes = $xpath->query('/mods:mods/mods:name[@type="personal"]');
125
126
        $persons = [];
127
128
        foreach ($personNodes as $key => $personNode) {
129
130
            $familyNode = $xpath->query('mods:namePart[@type="family"]', $personNode);
131
132
            $givenNode = $xpath->query('mods:namePart[@type="given"]', $personNode);
133
134
            $roleNode = $xpath->query('mods:role/mods:roleTerm[@type="code"]', $personNode);
135
136
            $identifierNode = $xpath->query('mods:nameIdentifier[@type="FOBID"]', $personNode);
137
138
            $affiliationNodes  = $xpath->query('mods:affiliation', $personNode);
139
            $affiliationIdentifierNodes  = $xpath->query(
140
                'mods:nameIdentifier[@type="ScopusAuthorID"][@typeURI="http://www.scopus.com/authid"]',
141
                $personNode
142
            );
143
144
            $person['affiliations'] = [];
145
            foreach ($affiliationNodes as $key => $affiliationNode) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
146
                $person['affiliations'][] = $affiliationNode->nodeValue;
147
            }
148
149
            $person['affiliationIdentifiers'] = [];
150
            foreach ($affiliationIdentifierNodes as $key => $affiliationIdentifierNode) {
151
                $person['affiliationIdentifiers'][] = $affiliationIdentifierNode->nodeValue;
152
            }
153
154
            $given = '';
155
            $family = '';
156
157
            if ($givenNode->length > 0) {
158
                $given = $givenNode->item(0)->nodeValue;
159
            }
160
161
            if ($familyNode->length > 0) {
162
                $family = $familyNode->item(0)->nodeValue;
163
            }
164
165
            $person['given'] = trim($given);
166
            $person['family'] = trim($family);
167
168
            $name = [];
169
            if ($person['given']) {
170
                $name[] = $person['given'];
171
            }
172
            if ($person['family']) {
173
                $name[] = $person['family'];
174
            }
175
176
            $person['name'] = implode(' ', $name);
177
178
            $person['role'] = '';
179
            if ($roleNode->length > 0) {
180
                $person['role'] = $roleNode->item(0)->nodeValue;
181
            }
182
183
            $person['fobId'] = '';
184
            if ($identifierNode->length > 0) {
185
                $person['fobId'] = $identifierNode->item(0)->nodeValue;
186
            }
187
188
            $person['index'] = $key;
189
190
            $persons[] = $person;
191
        }
192
193
        if ($role) {
194
            $result = [];
195
            foreach ($persons as $person) {
196
                if ($person['role'] == $role)
197
                $result[] = $person;
198
            }
199
            return $result;
200
        } else {
201
            return $persons;
202
        }
203
    }
204
205
206
    /**
207
     * Get all related FOB-IDs
208
     *
209
     * @return array
210
     */
211
    public function getFobIdentifiers(): array
212
    {
213
        $xpath = $this->getModsXpath();
214
215
        $nodes = $xpath->query('/mods:mods/mods:name[@type="personal"]');
216
217
        $identifiers = [];
218
219
        foreach ($nodes as $key => $node) {
220
221
            $identifierNode = $xpath->query('mods:nameIdentifier[@type="FOBID"]', $node);
222
223
            if ($identifierNode->length > 0) {
224
                $identifiers[] = $identifierNode->item(0)->nodeValue;
225
            }
226
        }
227
228
        return $identifiers;
229
    }
230
231
232
    public function setDateIssued($date)
233
    {
234
235
        $originInfo = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]');
236
237
        if ($originInfo->length > 0) {
238
            $dateIssued = $this->getModsXpath()->query('mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]', $originInfo->item(0));
239
240
            if ($dateIssued->length == 0) {
241
                $newDateIssued = $this->modsDom->createElement('mods:dateIssued');
242
                $newDateIssued->setAttribute('encoding', 'iso8601');
243
                $newDateIssued->setAttribute('keyDate', 'yes');
244
                $newDateIssued->nodeValue = $date;
245
                $originInfo->item(0)->appendChild($newDateIssued);
246
            } else {
247
                $dateIssued->item(0)->nodeValue = $date;
248
            }
249
250
        } else {
251
252
            $rootNode = $this->getModsXpath()->query('/mods:mods');
253
254
            if ($rootNode->length == 1) {
255
                $newOriginInfo = $this->modsDom->createElement('mods:originInfo');
256
                $newOriginInfo->setAttribute('eventType', 'distribution');
257
                $rootNode->item(0)->appendChild($newOriginInfo);
258
259
                $newDateIssued = $this->modsDom->createElement('mods:dateIssued');
260
                $newDateIssued->setAttribute('encoding', 'iso8601');
261
                $newDateIssued->setAttribute('keyDate', 'yes');
262
                $newDateIssued->nodeValue = $date;
263
                $newOriginInfo->appendChild($newDateIssued);
264
            } else {
265
                throw new \Exception('Invalid xml data.');
266
            }
267
268
        }
269
270
    }
271
272
273
    public function getPublishingYear()
274
    {
275
        $year = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="publication"]/mods:dateIssued[@encoding="iso8601"]');
276
        if ($year->length > 0) {
277
            return $year->item(0)->nodeValue;
278
        }
279
280
        return null;
281
    }
282
283
    public function getOriginalSourceTitle()
284
    {
285
        $node= $this->getModsXpath()->query('/mods:mods/mods:relatedItem[@type="original"]/mods:titleInfo/mods:title');
286
        if ($node->length > 0) {
287
            return $node->item(0)->nodeValue;
288
        }
289
290
        return null;
291
    }
292
293
    public function getDateIssued()
294
    {
295
296
        $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]');
297
        if ($dateIssued->length > 0) {
298
            return $dateIssued->item(0)->nodeValue;
299
        }
300
301
        return null;
302
    }
303
304
    public function removeDateIssued()
305
    {
306
307
        $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]');
308
        if ($dateIssued->length > 0) {
309
            $dateIssued->item(0)->parentNode->removeChild($dateIssued->item(0));
0 ignored issues
show
Bug introduced by
The method removeChild() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

309
            $dateIssued->item(0)->parentNode->/** @scrutinizer ignore-call */ 
310
                                              removeChild($dateIssued->item(0));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
310
        }
311
312
    }
313
314
    public function hasQucosaUrn()
315
    {
316
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
317
318
        $hasUrn = false;
319
320
        foreach ($urnNodeList as $urnNode) {
321
            $value  = $urnNode->nodeValue;
322
            $hasUrn = $hasUrn || !empty($value);
323
        }
324
325
        return $hasUrn;
326
    }
327
328
    public function getQucosaUrn()
329
    {
330
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
331
        $urnList     = '';
332
333
        if ($urnNodeList != null) {
334
            foreach ($urnNodeList as $urnNode) {
335
                $urnList = $urnNode->nodeValue;
336
            }
337
        }
338
        return $urnList;
339
    }
340
341
    public function addQucosaUrn($urn)
342
    {
343
        $rootNode = $this->getModsXpath()->query('/mods:mods');
344
345
        if ($rootNode->length == 1) {
346
            $newUrn = $this->modsDom->createElement('mods:identifier');
347
            $newUrn->setAttribute('type', 'qucosa:urn');
348
            $newUrn->nodeValue = $urn;
349
            $rootNode->item(0)->appendChild($newUrn);
350
        } else {
351
            throw new \Exception('Invalid xml data.');
352
        }
353
354
    }
355
356
    public function clearAllUrn()
357
    {
358
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="urn"]');
359
360
        foreach ($urnNodeList as $urnNode) {
361
            $urnNode->parentNode->removeChild($urnNode);
362
        }
363
364
        $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]');
365
        foreach ($urnNodeList as $urnNode) {
366
            $urnNode->parentNode->removeChild($urnNode);
367
        }
368
    }
369
370
    /**
371
     * @return string
372
     */
373
    public function getSourceDetails()
374
    {
375
        $data = [];
376
377
        $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...
378
379
        $dataNodes[] = $this->getModsXpath()->query(
380
            '/mods:mods/mods:part[@type="article"]/mods:detail/mods:number'
381
        );
382
383
        $dataNodes[] = $this->getModsXpath()->query('/mods:mods/mods:part[@type="section"]');
384
385
        foreach ($dataNodes as $dataNode) {
386
            foreach ($dataNode as $node) {
387
                if ($node->hasChildNodes()) {
388
                    foreach ($node->childNodes as $n) {
389
                        $data[] = preg_replace('/\s+/', ' ', $n->textContent);
390
                    }
391
                } else {
392
                    $data[] = preg_replace('/\s+/', ' ', $node->textContent);
393
                }
394
            }
395
        }
396
397
        $output = trim(implode(' ', $data));
398
        $output = preg_replace('/\s+/ ', ' ', $output);
399
        return $output;
400
    }
401
}
402