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
Push — master ( 7d8f03...4635ba )
by
unknown
03:12
created

MetadataController::buildMetaCObjData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
rs 10
1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\Doc;
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\IiifManifest;
17
use Kitodo\Dlf\Domain\Model\Collection;
18
use Kitodo\Dlf\Domain\Model\Metadata;
19
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
20
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
21
use Kitodo\Dlf\Domain\Repository\StructureRepository;
22
use Ubl\Iiif\Context\IRI;
23
24
/**
25
 * Controller class for the plugin 'Metadata'.
26
 *
27
 * @author Sebastian Meyer <[email protected]>
28
 * @package TYPO3
29
 * @subpackage dlf
30
 * @access public
31
 */
32
class MetadataController extends AbstractController
33
{
34
    /**
35
     * @var Doc
36
     */
37
    private $doc;
38
39
    /**
40
     * @var CollectionRepository
41
     */
42
    protected $collectionRepository;
43
44
    /**
45
     * @param CollectionRepository $collectionRepository
46
     */
47
    public function injectCollectionRepository(CollectionRepository $collectionRepository)
48
    {
49
        $this->collectionRepository = $collectionRepository;
50
    }
51
52
    /**
53
     * @var MetadataRepository
54
     */
55
    protected $metadataRepository;
56
57
    /**
58
     * @param MetadataRepository $metadataRepository
59
     */
60
    public function injectMetadataRepository(MetadataRepository $metadataRepository)
61
    {
62
        $this->metadataRepository = $metadataRepository;
63
    }
64
65
    /**
66
     * @var StructureRepository
67
     */
68
    protected $structureRepository;
69
70
    /**
71
     * @param StructureRepository $structureRepository
72
     */
73
    public function injectStructureRepository(StructureRepository $structureRepository)
74
    {
75
        $this->structureRepository = $structureRepository;
76
    }
77
78
    /**
79
     * @return string|void
80
     */
81
    public function mainAction()
82
    {
83
        $this->cObj = $this->configurationManager->getContentObject();
0 ignored issues
show
Bug Best Practice introduced by
The property cObj does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
85
        // Load current document.
86
        $this->loadDocument();
87
        if ($this->isDocMissing()) {
88
            // Quit without doing anything if required variables are not set.
89
            return '';
90
        } else {
91
            // Set default values if not set.
92
            $this->setDefault('rootline', 0);
93
            $this->setDefault('originalIiifMetadata', 0);
94
            $this->setDefault('displayIiifDescription', 1);
95
            $this->setDefault('displayIiifRights', 1);
96
            $this->setDefault('displayIiifLinks', 1);
97
        }
98
99
        $this->doc = $this->document->getDoc();
100
101
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->doc instanceof IiifManifest;
102
        $metadata = $this->getMetadata();
103
        // Get titledata?
104
        if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->doc->toplevelId)) {
105
            $data = $useOriginalIiifManifestMetadata ? $this->doc->getManifestMetadata($this->doc->toplevelId, $this->settings['storagePid']) : $this->doc->getTitledata($this->settings['storagePid']);
106
            $data['_id'] = $this->doc->toplevelId;
107
            array_unshift($metadata, $data);
108
        }
109
        if (empty($metadata)) {
110
            $this->logger->warning('No metadata found for document with UID ' . $this->document->getUid());
111
            return '';
112
        }
113
        ksort($metadata);
114
115
        $this->printMetadata($metadata, $useOriginalIiifManifestMetadata);
116
    }
117
118
    /**
119
     * Prepares the metadata array for output
120
     *
121
     * @access protected
122
     *
123
     * @param array $metadata: The metadata array
124
     * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs?
125
     *
126
     * @return string The metadata array ready for output
127
     */
128
    protected function printMetadata(array $metadata, $useOriginalIiifManifestMetadata = false)
129
    {
130
        if ($useOriginalIiifManifestMetadata) {
131
            $iiifData = $this->buildIiifData($metadata);
132
            $this->view->assign('useIiif', true);
133
            $this->view->assign('iiifData', $iiifData);
134
        } else {
135
            // findBySettings also sorts entries by the `sorting` field
136
            $metadataResult = $this->metadataRepository->findBySettings([
137
                'is_listed' => !$this->settings['showFull'],
138
            ]);
139
140
            foreach ($metadata as $i => $section) {
141
142
                foreach ($section as $name => $value) {
143
                    // NOTE: Labels are to be escaped in Fluid template
144
145
                    $this->parseMetadata($i, $name, $value, $metadata);
146
147
                    if (is_array($metadata[$i][$name])) {
148
                        $metadata[$i][$name] = array_values(array_filter($metadata[$i][$name], function($metadataValue)
149
                        {
150
                            return !empty($metadataValue);
151
                        }));
152
                    }
153
                }
154
            }
155
156
            $this->view->assign('buildUrl', $this->buildUrlFromMetadata($metadata));
157
            $this->view->assign('externalUrl', $this->buildExternalUrlFromMetadata($metadata));
158
            $this->view->assign('documentMetadataSections', $metadata);
159
            $this->view->assign('configMetadata', $metadataResult);
160
            $this->view->assign('separator', $this->settings['separator']);
161
            $this->view->assign('metaCObjData', $this->buildMetaCObjData($metadata));
162
        }
163
    }
164
165
    /**
166
     * Builds the IIIF data array from metadata array
167
     *
168
     * @access private
169
     *
170
     * @param array $metadata The metadata array
171
     *
172
     * @return array The IIIF data array ready for output
173
     */
174
    private function buildIiifData(array $metadata): array
175
    {
176
        $iiifData = [];
177
178
        foreach ($metadata as $row) {
179
            foreach ($row as $key => $group) {
180
                if ($key == '_id') {
181
                    continue;
182
                }
183
184
                if (!is_array($group)) {
185
                    $iiifData[$key] = $this->buildIiifDataGroup($key, $group);
186
                } else {
187
                    foreach ($group as $label => $value) {
188
                        if ($label == '_id') {
189
                            continue;
190
                        }
191
                        if (is_array($value)) {
192
                            $value = implode($this->settings['separator'], $value);
193
                        }
194
195
                        $iiifData[$key]['data'][] = $this->buildIiifDataGroup($label, $value);
196
                    }
197
                }
198
            }
199
        }
200
201
        return $iiifData;
202
    }
203
204
    /**
205
     * Builds the IIIF data array from label and value
206
     *
207
     * @access private
208
     *
209
     * @param string $label The label string
210
     * @param string $value The value string
211
     *
212
     * @return array The IIIF data array ready for output
213
     */
214
    private function buildIiifDataGroup(string $label, string $value): array
215
    {
216
        // NOTE: Labels are to be escaped in Fluid template
217
        if (IRI::isAbsoluteIri($value) && ($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $scheme does not seem to be defined for all execution paths leading up to this point.
Loading history...
introduced by
Consider adding parentheses for clarity. Current Interpretation: (Ubl\Iiif\Context\IRI::i...) || $scheme == 'https', Probably Intended Meaning: Ubl\Iiif\Context\IRI::is... || $scheme == 'https')
Loading history...
218
            //TODO: should really label be converted to empty string if equal to value?
219
            $label = $value == $label ? '' : $label;
220
            $buildUrl = true;
221
        } else {
222
            $buildUrl = false;
223
        }
224
225
        return [
226
            'label' => $label,
227
            'value' => $value,
228
            'buildUrl' => $buildUrl,
229
        ];
230
    }
231
232
    /**
233
     * Collects raw metadata into an array that will be passed as data to cObj.
234
     * This lets metadata wraps reference (own or foreign) values via TypoScript "field".
235
     *
236
     * @access private
237
     *
238
     * @param array $metadata The metadata array
239
     *
240
     * @return array The raw metadata array ready for output
241
     */
242
    private function buildMetaCObjData(array $metadata)
243
    {
244
        $metaCObjData = [];
245
246
        foreach ($metadata as $i => $section) {
247
            $metaCObjData[$i] = [];
248
249
            foreach ($section as $name => $value) {
250
                $metaCObjData[$i][$name] = is_array($value)
251
                    ? implode($this->settings['separator'], $value)
252
                    : $value;
253
            }
254
        }
255
256
        return $metaCObjData;
257
    }
258
259
    /**
260
     * Builds URLs array for given metadata array.
261
     *
262
     * @access private
263
     *
264
     * @param array $metadata The metadata array
265
     *
266
     * @return array URLs
267
     */
268
    private function buildUrlFromMetadata(array $metadata)
269
    {
270
        $buildUrl = [];
271
272
        foreach ($metadata as $i => $section) {
273
            if ($this->settings['linkTitle'] && $section['_id'] && isset($section['title']) && !empty($section['title'])) {
274
                $details = $this->doc->getLogicalStructure($section['_id']);
275
                $buildUrl[$i]['title'] = [
276
                    'id' => $this->document->getUid(),
277
                    'page' => (!empty($details['points']) ? intval($details['points']) : 1),
278
                    'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0),
279
                ];
280
            }
281
        }
282
283
        return $buildUrl;
284
    }
285
286
    /**
287
     * Builds external URLs array for given metadata array.
288
     *
289
     * @access private
290
     *
291
     * @param array $metadata The metadata array
292
     *
293
     * @return array external URLs
294
     */
295
    private function buildExternalUrlFromMetadata(array $metadata)
296
    {
297
        $externalUrl = [];
298
299
        foreach ($metadata as $i => $section) {
300
            foreach ($section as $name => $value) {
301
                if (($name == 'author' || $name == 'holder') && !empty($value) && !empty($value[0]['url'])) {
302
                    $externalUrl[$i][$name]['externalUrl'] = $value[0];
303
                } elseif (($name == 'geonames' || $name == 'wikidata' || $name == 'wikipedia') && !empty($value)) {
304
                    $externalUrl[$i][$name]['externalUrl'] = [
305
                        'name' => $value[0],
306
                        'url' => $value[0]
307
                    ];
308
                }
309
            }
310
        }
311
312
        return $externalUrl;
313
    }
314
315
    /**
316
     * Parses metadata.
317
     *
318
     * @access private
319
     *
320
     * @param int $i The index of metadata array
321
     * @param string $name The name of section in metadata array
322
     * @param mixed $value The value of section in metadata array
323
     * @param array $metadata The metadata array passed as reference
324
     *
325
     * @return void
326
     */
327
    private function parseMetadata(int $i, string $name, $value, array &$metadata) : void {
328
        if ($name == 'title') {
329
            // Get title of parent document if needed.
330
            if (empty(implode('', $value)) && $this->settings['getTitle'] && $this->document->getPartof()) {
331
                $superiorTitle = Doc::getTitle($this->document->getPartof(), true);
332
                if (!empty($superiorTitle)) {
333
                    $metadata[$i][$name] = ['[' . $superiorTitle . ']'];
334
                }
335
            }
336
        } elseif ($name == 'owner' && empty($value)) {
337
            // no owner is found by metadata records --> take the one associated to the document
338
            $library = $this->document->getOwner();
339
            if ($library) {
0 ignored issues
show
introduced by
$library is of type Kitodo\Dlf\Domain\Model\Library, thus it always evaluated to true.
Loading history...
340
                $metadata[$i][$name][0] = $library->getLabel();
341
            }
342
        } elseif ($name == 'type' && !empty($value)) {
343
            // Translate document type.
344
            $structure = $this->structureRepository->findOneByIndexName($metadata[$i][$name][0]);
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\StructureRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

344
            /** @scrutinizer ignore-call */ 
345
            $structure = $this->structureRepository->findOneByIndexName($metadata[$i][$name][0]);
Loading history...
345
            if ($structure) {
346
                $metadata[$i][$name][0] = $structure->getLabel();
0 ignored issues
show
Bug introduced by
The method getLabel() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

346
                /** @scrutinizer ignore-call */ 
347
                $metadata[$i][$name][0] = $structure->getLabel();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
347
            }
348
        } elseif ($name == 'collection' && !empty($value)) {
349
            // Check if collections isn't hidden.
350
            $j = 0;
351
            foreach ($value as $entry) {
352
                $collection = $this->collectionRepository->findOneByIndexName($entry);
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\CollectionRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

352
                /** @scrutinizer ignore-call */ 
353
                $collection = $this->collectionRepository->findOneByIndexName($entry);
Loading history...
353
                if ($collection) {
354
                    $metadata[$i][$name][$j] = $collection->getLabel() ? : '';
355
                    $j++;
356
                }
357
            }
358
        } elseif ($name == 'language' && !empty($value)) {
359
            // Translate ISO 639 language code.
360
            foreach ($metadata[$i][$name] as &$langValue) {
361
                $langValue = Helper::getLanguageName($langValue);
362
            }
363
        }
364
    }
365
366
    /**
367
     * Get metadata for given id array.
368
     *
369
     * @access private
370
     *
371
     * @return array metadata
372
     */
373
    private function getMetadata()
374
    {
375
        $metadata = [];
376
        if ($this->settings['rootline'] < 2) {
377
            // Get current structure's @ID.
378
            $ids = [];
379
            if (!empty($this->doc->physicalStructure[$this->requestData['page']]) && !empty($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->requestData['page']]])) {
380
                foreach ($this->doc->smLinks['p2l'][$doc->physicalStructure[$this->requestData['page']]] as $logId) {
381
                    $count = $doc->getStructureDepth($logId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $doc seems to be never defined.
Loading history...
382
                    $ids[$count][] = $logId;
383
                }
384
            }
385
            ksort($ids);
386
            reset($ids);
387
            // Check if we should display all metadata up to the root.
388
            if ($this->settings['rootline'] == 1) {
389
                foreach ($ids as $id) {
390
                    $metadata = $this->getMetadataForIds($id, $metadata);
391
                }
392
            } else {
393
                $id = array_pop($ids);
394
                if (is_array($id)) {
395
                    $metadata = $this->getMetadataForIds($id, $metadata);
396
                }
397
            }
398
        }
399
        return $metadata;
400
    }
401
402
    /**
403
     * Get metadata for given id array.
404
     *
405
     * @access private
406
     *
407
     * @param array $id: An array with ids
408
     * @param array $metadata: An array with metadata
409
     *
410
     * @return array metadata
411
     */
412
    private function getMetadataForIds($id, $metadata)
413
    {
414
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->doc instanceof IiifManifest;
415
        foreach ($id as $sid) {
416
            if ($useOriginalIiifManifestMetadata) {
417
                $data = $this->doc->getManifestMetadata($sid, $this->settings['storagePid']);
418
            } else {
419
                $data = $this->doc->getMetadata($sid, $this->settings['storagePid']);
420
            }
421
            if (!empty($data)) {
422
                $data['_id'] = $sid;
423
                $metadata[] = $data;
424
            }
425
        }
426
        return $metadata;
427
    }
428
429
    /**
430
     * Sets default value for setting if not yet set.
431
     *
432
     * @access private
433
     *
434
     * @param string $setting name of setting
435
     * @param int $value 0 or 1
436
     *
437
     * @return void
438
     */
439
    private function setDefault($setting, $value) {
440
        if (!isset($this->settings[$setting])) {
441
            $this->settings[$setting] = $value;
442
        }
443
    }
444
}
445