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

Mods::getYears()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 20
nop 0
dl 0
loc 26
rs 8.0555
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
                    $orcidIdParts = explode('/', (string) $identifier[0]);
95
                    $orcidId = trim(end($orcidIdParts));
96
                    if (!str_contains($orcidId, 'orcid=')) {
97
                        $profile = new OrcidProfile($orcidId);
98
                        $this->metadata['author'][$i] = $profile->getFullName();
99
                    }
100
                } else {
101
                    // Check if there is a display form.
102
                    if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
103
                        $this->metadata['author'][$i] = (string) $displayForm[0];
104
                    } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
105
                        $name = [];
106
                        $k = 4;
107
                        foreach ($nameParts as $namePart) {
108
                            if (
109
                                isset($namePart['type'])
110
                                && (string) $namePart['type'] == 'family'
111
                            ) {
112
                                $name[0] = (string) $namePart;
113
                            } elseif (
114
                                isset($namePart['type'])
115
                                && (string) $namePart['type'] == 'given'
116
                            ) {
117
                                $name[1] = (string) $namePart;
118
                            } elseif (
119
                                isset($namePart['type'])
120
                                && (string) $namePart['type'] == 'termsOfAddress'
121
                            ) {
122
                                $name[2] = (string) $namePart;
123
                            } elseif (
124
                                isset($namePart['type'])
125
                                && (string) $namePart['type'] == 'date'
126
                            ) {
127
                                $name[3] = (string) $namePart;
128
                            } else {
129
                                $name[$k] = (string) $namePart;
130
                            }
131
                            $k++;
132
                        }
133
                        ksort($name);
134
                        $this->metadata['author'][$i] = trim(implode(', ', $name));
135
                    }
136
                    // Append "valueURI" to name using Unicode unit separator.
137
                    if (isset($authors[$i]['valueURI'])) {
138
                        $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
139
                    }
140
                }
141
            }
142
        }
143
    }
144
145
    /**
146
     * Get holder.
147
     *
148
     * @access private
149
     *
150
     * @return void
151
     */
152
    private function getHolders() {
153
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
154
155
        if (!empty($holders)) {
156
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
157
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
158
159
                $identifier = $holders[$i]->xpath('./mods:nameIdentifier');
160
                if ($identifier[0]['type'] == 'viaf') {
161
                    $viafUrl = (string) $identifier[0];
162
                    $profile = new ViafProfile($viafUrl);
163
                    $this->metadata['holder'][$i] = $profile->getFullName();
164
                } else {
165
                    // Check if there is a display form.
166
                    if (($displayForm = $holders[$i]->xpath('./mods:displayForm'))) {
167
                        $this->metadata['holder'][$i] = (string) $displayForm[0];
168
                    } elseif (($nameParts = $holders[$i]->xpath('./mods:namePart'))) {
169
                        $name = [];
170
                        $k = 4;
171
                        foreach ($nameParts as $namePart) {
172
                            if (
173
                                isset($namePart['type'])
174
                                && (string) $namePart['type'] == 'family'
175
                            ) {
176
                                $name[0] = (string) $namePart;
177
                            } elseif (
178
                                isset($namePart['type'])
179
                                && (string) $namePart['type'] == 'given'
180
                            ) {
181
                                $name[1] = (string) $namePart;
182
                            } elseif (
183
                                isset($namePart['type'])
184
                                && (string) $namePart['type'] == 'termsOfAddress'
185
                            ) {
186
                                $name[2] = (string) $namePart;
187
                            } elseif (
188
                                isset($namePart['type'])
189
                                && (string) $namePart['type'] == 'date'
190
                            ) {
191
                                $name[3] = (string) $namePart;
192
                            } else {
193
                                $name[$k] = (string) $namePart;
194
                            }
195
                            $k++;
196
                        }
197
                        ksort($name);
198
                        $this->metadata['holder'][$i] = trim(implode(', ', $name));
199
                    }
200
                    // Append "valueURI" to name using Unicode unit separator.
201
                    if (isset($authors[$i]['valueURI'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $authors seems to never exist and therefore isset should always be false.
Loading history...
202
                        $this->metadata['holder'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
203
                    }
204
                }
205
            }
206
        }
207
    }
208
209
    /**
210
     * Get "place" and "place_sorting".
211
     *
212
     * @access private
213
     *
214
     * @return void
215
     */
216
    private function getPlaces() {
217
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
218
        // Get "place" and "place_sorting" again if that was to sophisticated.
219
        if (empty($places)) {
220
            // Get all places and assume these are places of publication.
221
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
222
        }
223
        if (!empty($places)) {
224
            foreach ($places as $place) {
225
                $this->metadata['place'][] = (string) $place;
226
                if (!$this->metadata['place_sorting'][0]) {
227
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
228
                }
229
            }
230
        }
231
    }
232
233
    /**
234
     * Get "year" and "year_sorting".
235
     *
236
     * @access private
237
     *
238
     * @return void
239
     */
240
    private function getYears() {
241
        // Get "year_sorting".
242
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
243
            foreach ($years_sorting as $year_sorting) {
244
                $this->metadata['year_sorting'][0] = intval($year_sorting);
245
            }
246
        }
247
        // Get "year" and "year_sorting" if not specified separately.
248
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
249
        // Get "year" and "year_sorting" again if that was to sophisticated.
250
        if (empty($years)) {
251
            // Get all dates and assume these are dates of publication.
252
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
253
        }
254
        if (!empty($years)) {
255
            foreach ($years as $year) {
256
                $this->metadata['year'][] = (string) $year;
257
                if (!$this->metadata['year_sorting'][0]) {
258
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
259
                    if (
260
                        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

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

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

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