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

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

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

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