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
10:24 queued 06:39
created

Mods::getPlaces()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 12
rs 9.6111
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
64
        $metadata = $this->metadata;
65
    }
66
67
    /**
68
     * Get "author" and "author_sorting".
69
     *
70
     * @access private
71
     *
72
     * @return void
73
     */
74
    private function getAuthors() {
75
        $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]');
76
77
        // Get "author" and "author_sorting" again if that was too sophisticated.
78
        if (empty($authors)) {
79
            // Get all names which do not have any role term assigned and assume these are authors.
80
            $authors = $this->xml->xpath('./mods:name[not(./mods:role)]');
81
        }
82
        if (!empty($authors)) {
83
            for ($i = 0, $j = count($authors); $i < $j; $i++) {
84
                $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
85
86
                $identifier = $authors[$i]->xpath('./mods:nameIdentifier');
87
                if ($identifier[0]['type'] == 'orcid' && !empty((string) $identifier[0])) {
88
                    $this->getAuthorFromOrcidApi($identifier, $authors, $i);
89
                } else {
90
                    $this->getAuthorFromXml($authors, $i);
91
                }
92
            }
93
        }
94
    }
95
96
    private function getAuthorFromOrcidApi($identifier, $authors, $i) {
97
        $orcidUrl = (string) $identifier[0];
98
        $orcidIdParts = explode('/', $orcidUrl);
99
        $orcidId = trim(end($orcidIdParts));
100
        if (!str_contains($orcidId, 'orcid=')) {
101
            $profile = new OrcidProfile($orcidId);
102
            $name = $profile->getFullName();
103
            if (!empty($name)) {
104
                $this->metadata['author'][$i] = [
105
                    'name' => $name,
106
                    'url' => $orcidUrl
107
                ];
108
            } else {
109
                //fallback into display form
110
                $this->getAuthorFromXmlDisplayForm($authors, $i);
111
            }
112
        } else {
113
            //fallback into display form
114
            $this->getAuthorFromXmlDisplayForm($authors, $i);
115
        }
116
    }
117
118
    private function getAuthorFromXml($authors, $i) {
119
        $this->getAuthorFromXmlDisplayForm($authors, $i);
120
121
        $nameParts = $authors[$i]->xpath('./mods:namePart');
122
123
        if (empty($this->metadata['author'][$i]) && $nameParts) {
124
            $name = [];
125
            $k = 4;
126
            foreach ($nameParts as $namePart) {
127
                if (
128
                    isset($namePart['type'])
129
                    && (string) $namePart['type'] == 'family'
130
                ) {
131
                    $name[0] = (string) $namePart;
132
                } elseif (
133
                    isset($namePart['type'])
134
                    && (string) $namePart['type'] == 'given'
135
                ) {
136
                    $name[1] = (string) $namePart;
137
                } elseif (
138
                    isset($namePart['type'])
139
                    && (string) $namePart['type'] == 'termsOfAddress'
140
                ) {
141
                    $name[2] = (string) $namePart;
142
                } elseif (
143
                    isset($namePart['type'])
144
                    && (string) $namePart['type'] == 'date'
145
                ) {
146
                    $name[3] = (string) $namePart;
147
                } else {
148
                    $name[$k] = (string) $namePart;
149
                }
150
                $k++;
151
            }
152
            ksort($name);
153
            $this->metadata['author'][$i] = trim(implode(', ', $name));
154
        }
155
        // Append "valueURI" to name using Unicode unit separator.
156
        if (isset($authors[$i]['valueURI'])) {
157
            $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI'];
158
        }
159
    }
160
161
    private function getAuthorFromXmlDisplayForm($authors, $i) {
162
        $displayForm = $authors[$i]->xpath('./mods:displayForm');
163
        if ($displayForm) {
164
            $this->metadata['author'][$i] = (string) $displayForm[0];
165
        }
166
    }
167
168
    /**
169
     * Get holder.
170
     *
171
     * @access private
172
     *
173
     * @return void
174
     */
175
    private function getHolders() {
176
        $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]');
177
178
        if (!empty($holders)) {
179
            for ($i = 0, $j = count($holders); $i < $j; $i++) {
180
                $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
181
182
                $identifier = $holders[$i]->xpath('./mods:nameIdentifier');
183
                if ($identifier[0]['type'] == 'viaf') {
184
                    $this->getHolderFromViafApi($identifier, $holders, $i);
185
                } else {
186
                    $this->getHolderFromXml($holders, $i);
187
                }
188
            }
189
        }
190
    }
191
192
    private function getHolderFromViafApi($identifier, $holders, $i) {
193
        $viafUrl = (string) $identifier[0];
194
        $profile = new ViafProfile($viafUrl);
195
        $name = $profile->getFullName();
196
        if (!empty($name)) {
197
            $this->metadata['holder'][$i] = [
198
                'name' => $name,
199
                'url' => $viafUrl
200
            ];
201
        } else {
202
            //fallback into display form
203
            $this->getHolderFromXmlDisplayForm($holders, $i);
204
        }
205
    }
206
207
    private function getHolderFromXml($holders, $i) {
208
        $this->getHolderFromXmlDisplayForm($holders, $i);
209
        // Append "valueURI" to name using Unicode unit separator.
210
        if (isset($holders[$i]['valueURI'])) {
211
            $this->metadata['holder'][$i] .= chr(31) . (string) $holders[$i]['valueURI'];
212
        }
213
    }
214
215
    private function getHolderFromXmlDisplayForm($holders, $i) {
216
        // Check if there is a display form.
217
        $displayForm = $holders[$i]->xpath('./mods:displayForm');
218
        if ($displayForm) {
219
            $this->metadata['holder'][$i] = (string) $displayForm[0];
220
        }
221
    }
222
223
    /**
224
     * Get "place" and "place_sorting".
225
     *
226
     * @access private
227
     *
228
     * @return void
229
     */
230
    private function getPlaces() {
231
        $places = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:place/mods:placeTerm');
232
        // Get "place" and "place_sorting" again if that was to sophisticated.
233
        if (empty($places)) {
234
            // Get all places and assume these are places of publication.
235
            $places = $this->xml->xpath('./mods:originInfo/mods:place/mods:placeTerm');
236
        }
237
        if (!empty($places)) {
238
            foreach ($places as $place) {
239
                $this->metadata['place'][] = (string) $place;
240
                if (!$this->metadata['place_sorting'][0]) {
241
                    $this->metadata['place_sorting'][0] = preg_replace('/[[:punct:]]/', '', (string) $place);
242
                }
243
            }
244
        }
245
    }
246
247
    /**
248
     * Get "year" and "year_sorting".
249
     *
250
     * @access private
251
     *
252
     * @return void
253
     */
254
    private function getYears() {
255
        // Get "year_sorting".
256
        if (($years_sorting = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateOther[@type="order" and @encoding="w3cdtf"]'))) {
257
            foreach ($years_sorting as $year_sorting) {
258
                $this->metadata['year_sorting'][0] = intval($year_sorting);
259
            }
260
        }
261
        // Get "year" and "year_sorting" if not specified separately.
262
        $years = $this->xml->xpath('./mods:originInfo[not(./mods:edition="[Electronic ed.]")]/mods:dateIssued[@keyDate="yes"]');
263
        // Get "year" and "year_sorting" again if that was to sophisticated.
264
        if (empty($years)) {
265
            // Get all dates and assume these are dates of publication.
266
            $years = $this->xml->xpath('./mods:originInfo/mods:dateIssued');
267
        }
268
        if (!empty($years)) {
269
            foreach ($years as $year) {
270
                $this->metadata['year'][] = (string) $year;
271
                if (!$this->metadata['year_sorting'][0]) {
272
                    $year_sorting = str_ireplace('x', '5', preg_replace('/[^\d.x]/i', '', (string) $year));
273
                    if (
274
                        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

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

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

277
                        $year_sorting = ((intval(trim(/** @scrutinizer ignore-type */ $year_sorting, '.')) - 1) * 100) + 50;
Loading history...
278
                    }
279
                    $this->metadata['year_sorting'][0] = intval($year_sorting);
280
                }
281
            }
282
        }
283
    }
284
}
285