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 (#838)
by Beatrycze
03:51
created

Mods::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
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->getAuthorsFromOrcidApi($identifier, $authors, $i);
95
                } else {
96
                    $this->getAuthorsFromXml($authors, $i);
97
                }
98
            }
99
        }
100
    }
101
102
    private function getAuthorsFromOrcidApi($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 getAuthorsFromXml($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->getHoldersFromViafApi($identifier,$holders, $i);
191
                } else {
192
                    $this->getHoldersFromXml($holders, $i);
193
                }
194
            }
195
        }
196
    }
197
198
    private function getHoldersFromViafApi($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
            $displayForm = $holders[$i]->xpath('./mods:displayForm');
210
            if ($displayForm) {
211
                $this->metadata['holder'][$i] = (string) $displayForm[0];
212
            }
213
        }
214
    }
215
216
    private function getHoldersFromXml($holders, $i) {
217
        // Check if there is a display form.
218
        $displayForm = $holders[$i]->xpath('./mods:displayForm');
219
        if ($displayForm) {
220
            $this->metadata['holder'][$i] = (string) $displayForm[0];
221
        }
222
        // Append "valueURI" to name using Unicode unit separator.
223
        if (isset($holders[$i]['valueURI'])) {
224
            $this->metadata['holder'][$i] .= chr(31) . (string) $holders[$i]['valueURI'];
225
        }
226
    }
227
228
    /**
229
     * Get "place" and "place_sorting".
230
     *
231
     * @access private
232
     *
233
     * @return void
234
     */
235
    private function getPlaces() {
236
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
237
        // Get "place" and "place_sorting" again if that was to sophisticated.
238
        if (empty($places)) {
239
            // Get all places and assume these are places of publication.
240
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
241
        }
242
        if (!empty($places)) {
243
            foreach ($places as $place) {
244
                $this->metadata['place'][] = (string) $place;
245
                if (!$this->metadata['place_sorting'][0]) {
246
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
247
                }
248
            }
249
        }
250
    }
251
252
    /**
253
     * Get "year" and "year_sorting".
254
     *
255
     * @access private
256
     *
257
     * @return void
258
     */
259
    private function getYears() {
260
        // Get "year_sorting".
261
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
262
            foreach ($years_sorting as $year_sorting) {
263
                $this->metadata['year_sorting'][0] = intval($year_sorting);
264
            }
265
        }
266
        // Get "year" and "year_sorting" if not specified separately.
267
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
268
        // Get "year" and "year_sorting" again if that was to sophisticated.
269
        if (empty($years)) {
270
            // Get all dates and assume these are dates of publication.
271
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
272
        }
273
        if (!empty($years)) {
274
            foreach ($years as $year) {
275
                $this->metadata['year'][] = (string) $year;
276
                if (!$this->metadata['year_sorting'][0]) {
277
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
278
                    if (
279
                        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

279
                        strpos(/** @scrutinizer ignore-type */ $year_sorting, '.')
Loading history...
280
                        || 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

280
                        || strlen(/** @scrutinizer ignore-type */ $year_sorting) < 3
Loading history...
281
                    ) {
282
                        $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

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