Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#839)
by Beatrycze
03:35
created

Mods::getHolderFromXmlDisplayForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 5
rs 10
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\Format;
14
15
use Kitodo\Dlf\Api\Orcid\OrcidProfile;
16
use Kitodo\Dlf\Api\Viaf\ViafProfile;
17
18
/**
19
 * Metadata MODS format class for the 'dlf' extension
20
 *
21
 * @author Sebastian Meyer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class Mods implements \Kitodo\Dlf\Common\MetadataInterface
27
{
28
    /**
29
     * The metadata XML
30
     *
31
     * @var \SimpleXMLElement
32
     **/
33
    private $xml;
34
35
    /**
36
     * The metadata array
37
     *
38
     * @var array
39
     **/
40
    private $metadata;
41
42
    /**
43
     * This extracts the essential MODS metadata from XML
44
     *
45
     * @access public
46
     *
47
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
48
     * @param array &$metadata: The metadata array to fill
49
     *
50
     * @return void
51
     */
52
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)
53
    {
54
        $this->xml = $xml;
55
        $this->metadata = $metadata;
56
57
        $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
58
59
        $this->getAuthors();
60
        $this->getHolders();
61
        $this->getPlaces();
62
        $this->getYears();
63
        $this->getDescription();
64
        $this->getIdentifier();
65
        $this->getLicense();
66
        $this->getObjectNames();
67
        $this->getObjectLocationMetadata();
68
69
        $metadata = $this->metadata;
70
    }
71
72
    /**
73
     * Get "author" and "author_sorting".
74
     *
75
     * @access private
76
     *
77
     * @return void
78
     */
79
    private function getAuthors() {
80
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
81
82
        // Get "author" and "author_sorting" again if that was too sophisticated.
83
        if (empty($authors)) {
84
            // Get all names which do not have any role term assigned and assume these are authors.
85
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
86
        }
87
88
        if (!empty($authors)) {
89
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
90
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
91
92
                $identifier = $authors[$i]->xpath('./mods:nameIdentifier');
93
                if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) {
94
                    $this->getAuthorFromOrcidApi($identifier, $authors, $i);
95
                } else {
96
                    $this->getAuthorFromXml($authors, $i);
97
                }
98
            }
99
        }
100
    }
101
102
    private function getAuthorFromOrcidApi($identifier, $authors, $i) {
103
        $orcidUrl = (string) $identifier[0];
104
        $orcidIdParts = explode('/', $orcidUrl);
105
        $orcidId = trim(end($orcidIdParts));
106
        if (!str_contains($orcidId, 'orcid=')) {
107
            $profile = new OrcidProfile($orcidId);
108
            $name = $profile->getFullName();
109
            if (!empty($name)) {
110
                $this->metadata['author'][$i] = [
111
                    'name' => $name,
112
                    'url' => $orcidUrl
113
                ];
114
            } else {
115
                //fallback into display form
116
                $this->getAuthorFromXmlDisplayForm($authors, $i);
117
            }
118
        } else {
119
            //fallback into display form
120
            $this->getAuthorFromXmlDisplayForm($authors, $i);
121
        }
122
    }
123
124
    private function getAuthorFromXml($authors, $i) {
125
        $this->getAuthorFromXmlDisplayForm($authors, $i);
126
127
        $nameParts = $authors[$i]->xpath('./mods:namePart');
128
129
        if (empty($this->metadata['author'][$i]) && $nameParts) {
130
            $name = [];
131
            $k = 4;
132
            foreach ($nameParts as $namePart) {
133
                if (
134
                    isset($namePart['type'])
135
                    && (string) $namePart['type'] == 'family'
136
                ) {
137
                    $name[0] = (string) $namePart;
138
                } elseif (
139
                    isset($namePart['type'])
140
                    && (string) $namePart['type'] == 'given'
141
                ) {
142
                    $name[1] = (string) $namePart;
143
                } elseif (
144
                    isset($namePart['type'])
145
                    && (string) $namePart['type'] == 'termsOfAddress'
146
                ) {
147
                    $name[2] = (string) $namePart;
148
                } elseif (
149
                    isset($namePart['type'])
150
                    && (string) $namePart['type'] == 'date'
151
                ) {
152
                    $name[3] = (string) $namePart;
153
                } else {
154
                    $name[$k] = (string) $namePart;
155
                }
156
                $k++;
157
            }
158
            ksort($name);
159
            $this->metadata['author'][$i] = trim(implode(', ', $name));
160
        }
161
        // Append "valueURI" to name using Unicode unit separator.
162
        if (isset($authors[$i]['valueURI'])) {
163
            $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
164
        }
165
    }
166
167
    private function getAuthorFromXmlDisplayForm($authors, $i) {
168
        $displayForm = $authors[$i]->xpath('./mods:displayForm');
169
        if ($displayForm) {
170
            $this->metadata['author'][$i] = (string) $displayForm[0];
171
        }
172
    }
173
174
    /**
175
     * Get holder.
176
     *
177
     * @access private
178
     *
179
     * @return void
180
     */
181
    private function getHolders() {
182
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
183
184
        if (!empty($holders)) {
185
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
186
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
187
188
                $identifier = $holders[$i]->xpath('./mods:nameIdentifier');
189
                if ($identifier[0]['type'] == 'viaf') {
190
                    $this->getHolderFromViafApi($identifier, $holders, $i);
191
                } else {
192
                    $this->getHolderFromXml($holders, $i);
193
                }
194
            }
195
        }
196
    }
197
198
    private function getHolderFromViafApi($identifier, $holders, $i) {
199
        $viafUrl = (string) $identifier[0];
200
        $profile = new ViafProfile($viafUrl);
201
        $name = $profile->getFullName();
202
        if (!empty($name)) {
203
            $this->metadata['holder'][$i] = [
204
                'name' => $name,
205
                'url' => $viafUrl
206
            ];
207
        } else {
208
            //fallback into display form
209
            $this->getHolderFromXmlDisplayForm($holders, $i);
210
        }
211
    }
212
213
    private function getHolderFromXml($holders, $i) {
214
        $this->getHolderFromXmlDisplayForm($holders, $i);
215
        // Append "valueURI" to name using Unicode unit separator.
216
        if (isset($holders[$i]['valueURI'])) {
217
            $this->metadata['holder'][$i] .= chr(31) . (string) $holders[$i]['valueURI'];
218
        }
219
    }
220
221
    private function getHolderFromXmlDisplayForm($holders, $i) {
222
        // Check if there is a display form.
223
        $displayForm = $holders[$i]->xpath('./mods:displayForm');
224
        if ($displayForm) {
225
            $this->metadata['holder'][$i] = (string) $displayForm[0];
226
        }
227
    }
228
229
    /**
230
     * Get "place" and "place_sorting".
231
     *
232
     * @access private
233
     *
234
     * @return void
235
     */
236
    private function getPlaces() {
237
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
238
        // Get "place" and "place_sorting" again if that was to sophisticated.
239
        if (empty($places)) {
240
            // Get all places and assume these are places of publication.
241
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
242
        }
243
        if (!empty($places)) {
244
            foreach ($places as $place) {
245
                $this->metadata['place'][] = (string) $place;
246
                if (!$this->metadata['place_sorting'][0]) {
247
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
248
                }
249
            }
250
        }
251
    }
252
253
    /**
254
     * Get "year" and "year_sorting".
255
     *
256
     * @access private
257
     *
258
     * @return void
259
     */
260
    private function getYears() {
261
        // Get "year_sorting".
262
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
263
            foreach ($years_sorting as $year_sorting) {
264
                $this->metadata['year_sorting'][0] = intval($year_sorting);
265
            }
266
        }
267
        // Get "year" and "year_sorting" if not specified separately.
268
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
269
        // Get "year" and "year_sorting" again if that was to sophisticated.
270
        if (empty($years)) {
271
            // Get all dates and assume these are dates of publication.
272
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
273
        }
274
        if (!empty($years)) {
275
            foreach ($years as $year) {
276
                $this->metadata['year'][] = (string) $year;
277
                if (!$this->metadata['year_sorting'][0]) {
278
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
279
                    if (
280
                        strpos($year_sorting, '.')
0 ignored issues
show
Bug introduced by
It seems like $year_sorting can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

280
                        strpos(/** @scrutinizer ignore-type */ $year_sorting, '.')
Loading history...
281
                        || strlen($year_sorting) < 3
0 ignored issues
show
Bug introduced by
It seems like $year_sorting can also be of type array; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

281
                        || strlen(/** @scrutinizer ignore-type */ $year_sorting) < 3
Loading history...
282
                    ) {
283
                        $year_sorting = ((intval(trim($year_sorting, '.')) - 1) * 100) + 50;
0 ignored issues
show
Bug introduced by
It seems like $year_sorting can also be of type array; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

283
                        $year_sorting = ((intval(trim(/** @scrutinizer ignore-type */ $year_sorting, '.')) - 1) * 100) + 50;
Loading history...
284
                    }
285
                    $this->metadata['year_sorting'][0] = intval($year_sorting);
286
                }
287
            }
288
        }
289
    }
290
291
    /**
292
     * Get "description" stored in recordInfoNote.
293
     *
294
     * @access private
295
     *
296
     * @return void
297
     */
298
    private function getDescription() {
299
        $this->getSingleMetadata('description', './mods:recordInfo/mods:recordInfoNote/text()');
300
    }
301
302
    /**
303
     * Get "identifier" - example: hotels (built public accommodations), AAT ID: 300007166.
304
     *
305
     * @access private
306
     *
307
     * @return void
308
     */
309
    private function getIdentifier() {
310
        $this->getSingleMetadata('identifier', './mods:identifier/text()');
311
    }
312
313
    /**
314
     * Get "license" - license on which object is published.
315
     *
316
     * @access private
317
     *
318
     * @return void
319
     */
320
    private function getLicense() {
321
        $this->getSingleMetadata('license', './mods:accessCondition/text()');
322
    }
323
324
    /**
325
     * Get names of the original object:
326
     *   - main name
327
     *   - alternative names
328
     *
329
     * @access private
330
     *
331
     * @return void
332
     */
333
    private function getObjectNames() {
334
        $this->getSingleMetadata('object_name', './mods:relatedItem/mods:titleInfo[not(@displayLabel="alternative")]/mods:title/text()');
335
        $this->getSingleMetadata('object_alternative_names', './mods:relatedItem/mods:titleInfo[@displayLabel="alternative"]/mods:title/text()');
336
    }
337
338
    /**
339
     * Get location information about the original object:
340
     *  - city (object_location)
341
     *  - geonames
342
     *  - wikidata
343
     *  - wikipedia
344
     *
345
     * @access private
346
     *
347
     * @return void
348
     */
349
    private function getObjectLocationMetadata() {
350
        $this->getSingleMetadata('object_location', './mods:relatedItem/mods:location/mods:physicalLocation/text()');
351
        $this->getSingleMetadata('geonames', './mods:relatedItem/mods:location/mods:url[@displayLabel="geonames"]/text()');
352
        $this->getSingleMetadata('wikidata', './mods:relatedItem/mods:location/mods:url[@displayLabel="wikidata"]/text()');
353
        $this->getSingleMetadata('wikipedia', './mods:relatedItem/mods:location/mods:url[@displayLabel="wikipedia"]/text()');
354
    }
355
356
    /**
357
     * Save to the metadata array the last found matching metadata.
358
     *
359
     * @access private
360
     *
361
     * @param string $metadataIndex: The index in the array by which metadata is going to be accessible
362
     * @param string $xpath: The xpath for searching metadata in MODS
363
     *
364
     * @return void
365
     */
366
    private function getSingleMetadata($metadataIndex, $xpath) {
367
        $results = $this->xml->xpath($xpath);
368
        if (!empty($results)) {
369
            foreach ($results as $result) {
370
                $this->metadata[$metadataIndex][0] = (string) $result;
371
            }
372
        }
373
    }
374
}
375