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

Mods::getObjectNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Profile;
16
17
/**
18
 * Metadata MODS format class for the 'dlf' extension
19
 *
20
 * @author Sebastian Meyer <[email protected]>
21
 * @package TYPO3
22
 * @subpackage dlf
23
 * @access public
24
 */
25
class Mods implements \Kitodo\Dlf\Common\MetadataInterface
26
{
27
    /**
28
     * The metadata XML
29
     *
30
     * @var \SimpleXMLElement
31
     **/
32
    private $xml;
33
34
    /**
35
     * The metadata array
36
     *
37
     * @var array
38
     **/
39
    private $metadata;
40
41
    /**
42
     * This extracts the essential MODS metadata from XML
43
     *
44
     * @access public
45
     *
46
     * @param \SimpleXMLElement $xml: The XML to extract the metadata from
47
     * @param array &$metadata: The metadata array to fill
48
     *
49
     * @return void
50
     */
51
    public function extractMetadata(\SimpleXMLElement $xml, array &$metadata)
52
    {
53
        $this->xml = $xml;
54
        $this->metadata = $metadata;
55
56
        $this->xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
57
58
        $this->getAuthors();
59
        $this->getHolders();
60
        $this->getPlaces();
61
        $this->getYears();
62
        $this->getDescription();
63
        $this->getIdentifier();
64
        $this->getLicense();
65
        $this->getObjectNames();
66
        $this->getObjectLocationMetadata();
67
68
        $metadata = $this->metadata;
69
    }
70
71
    /**
72
     * Get "author" and "author_sorting".
73
     *
74
     * @access private
75
     *
76
     * @return void
77
     */
78
    private function getAuthors() {
79
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
80
81
        // Get "author" and "author_sorting" again if that was too sophisticated.
82
        if (empty($authors)) {
83
            // Get all names which do not have any role term assigned and assume these are authors.
84
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
85
        }
86
87
        if (!empty($authors)) {
88
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
89
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
90
91
                $identifier = $authors[$i]->xpath('./mods:nameIdentifier');
92
                if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) {
93
                    $orcidIdParts = explode('/', (string) $identifier[0]);
94
                    $orcidId = trim(end($orcidIdParts));
95
                    $orcidEqual = 'orcid=';
96
                    if (str_contains($orcidId, $orcidEqual)) {
97
                        $orcidIdParts = explode($orcidEqual, $orcidId);
98
                        $orcidId = trim(end($orcidIdParts));
99
                    }
100
                    $profile = new Profile($orcidId);
101
                    $this->metadata['author'][$i] = $profile->getFullName();
102
                } else {
103
                    // Check if there is a display form.
104
                    if (($displayForm = $authors[$i]->xpath('./mods:displayForm'))) {
105
                        $this->metadata['author'][$i] = (string) $displayForm[0];
106
                    } elseif (($nameParts = $authors[$i]->xpath('./mods:namePart'))) {
107
                        $name = [];
108
                        $k = 4;
109
                        foreach ($nameParts as $namePart) {
110
                            if (
111
                                isset($namePart['type'])
112
                                && (string) $namePart['type'] == 'family'
113
                            ) {
114
                                $name[0] = (string) $namePart;
115
                            } elseif (
116
                                isset($namePart['type'])
117
                                && (string) $namePart['type'] == 'given'
118
                            ) {
119
                                $name[1] = (string) $namePart;
120
                            } elseif (
121
                                isset($namePart['type'])
122
                                && (string) $namePart['type'] == 'termsOfAddress'
123
                            ) {
124
                                $name[2] = (string) $namePart;
125
                            } elseif (
126
                                isset($namePart['type'])
127
                                && (string) $namePart['type'] == 'date'
128
                            ) {
129
                                $name[3] = (string) $namePart;
130
                            } else {
131
                                $name[$k] = (string) $namePart;
132
                            }
133
                            $k++;
134
                        }
135
                        ksort($name);
136
                        $this->metadata['author'][$i] = trim(implode(', ', $name));
137
                    }
138
                    // Append "valueURI" to name using Unicode unit separator.
139
                    if (isset($authors[$i]['valueURI'])) {
140
                        $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
141
                    }
142
                }
143
            }
144
        }
145
    }
146
147
    /**
148
     * Get holder.
149
     *
150
     * @access private
151
     *
152
     * @return void
153
     */
154
    private function getHolders() {
155
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
156
157
        if (!empty($holders)) {
158
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
159
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
160
161
                //$identifier = $holders[$i]->xpath('./mods:nameIdentifier');
162
                if (1 != 1) {
163
                    //TODO: reading VIAF API
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