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 ( a8ce9a...6f9851 )
by
unknown
04:16
created

MetadataController::setDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 3
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 CollectionRepository
36
     */
37
    protected $collectionRepository;
38
39
    /**
40
     * @param CollectionRepository $collectionRepository
41
     */
42
    public function injectCollectionRepository(CollectionRepository $collectionRepository)
43
    {
44
        $this->collectionRepository = $collectionRepository;
45
    }
46
47
    /**
48
     * @var MetadataRepository
49
     */
50
    protected $metadataRepository;
51
52
    /**
53
     * @param MetadataRepository $metadataRepository
54
     */
55
    public function injectMetadataRepository(MetadataRepository $metadataRepository)
56
    {
57
        $this->metadataRepository = $metadataRepository;
58
    }
59
60
    /**
61
     * @var StructureRepository
62
     */
63
    protected $structureRepository;
64
65
    /**
66
     * @param StructureRepository $structureRepository
67
     */
68
    public function injectStructureRepository(StructureRepository $structureRepository)
69
    {
70
        $this->structureRepository = $structureRepository;
71
    }
72
73
    /**
74
     * @return string|void
75
     */
76
    public function mainAction()
77
    {
78
        $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...
79
80
        // Load current document.
81
        $this->loadDocument();
82
        if ($this->isDocMissing()) {
83
            // Quit without doing anything if required variables are not set.
84
            return '';
85
        } else {
86
            // Set default values if not set.
87
            $this->setDefault('rootline', 0);
88
            $this->setDefault('originalIiifMetadata', 0);
89
            $this->setDefault('displayIiifDescription', 1);
90
            $this->setDefault('displayIiifRights', 1);
91
            $this->setDefault('displayIiifLinks', 1);
92
        }
93
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest;
94
        $metadata = $this->getMetadata();
95
        // Get titledata?
96
        if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->document->getDoc()->toplevelId)) {
97
            $data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitledata($this->settings['storagePid']);
98
            $data['_id'] = $this->document->getDoc()->toplevelId;
99
            array_unshift($metadata, $data);
100
        }
101
        if (empty($metadata)) {
102
            $this->logger->warning('No metadata found for document with UID ' . $this->document->getUid());
103
            return '';
104
        }
105
        ksort($metadata);
106
107
        $this->printMetadata($metadata, $useOriginalIiifManifestMetadata);
108
    }
109
110
    /**
111
     * Prepares the metadata array for output
112
     *
113
     * @access protected
114
     *
115
     * @param array $metadata: The metadata array
116
     * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs?
117
     *
118
     * @return string The metadata array ready for output
119
     */
120
    protected function printMetadata(array $metadata, $useOriginalIiifManifestMetadata = false)
121
    {
122
        if ($useOriginalIiifManifestMetadata) {
123
            $iiifData = [];
124
            foreach ($metadata as $row) {
125
                foreach ($row as $key => $group) {
126
                    if ($key == '_id') {
127
                        continue;
128
                    }
129
                    if (!is_array($group)) {
130
                        if (
131
                            IRI::isAbsoluteIri($group)
132
                            && (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https')
133
                        ) {
134
                            // Build link
135
                            $iiifData[$key] = [
136
                                'label' => $key,
137
                                'value' => $group,
138
                                'buildUrl' => true,
139
                            ];
140
                        } else {
141
                            // Data output
142
                            $iiifData[$key] = [
143
                                'label' => $key,
144
                                'value' => $group,
145
                                'buildUrl' => false,
146
                            ];
147
                        }
148
                    } else {
149
                        foreach ($group as $label => $value) {
150
                            if ($label == '_id') {
151
                                continue;
152
                            }
153
                            if (is_array($value)) {
154
                                $value = implode($this->settings['separator'], $value);
155
                            }
156
                            // NOTE: Labels are to be escaped in Fluid template
157
                            if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) {
158
                                $nolabel = $value == $label;
159
                                $iiifData[$key]['data'][] = [
160
                                    'label' => $nolabel ? '' : $label,
161
                                    'value' => $value,
162
                                    'buildUrl' => true,
163
                                ];
164
                            } else {
165
                                $iiifData[$key]['data'][] = [
166
                                    'label' => $label,
167
                                    'value' => $value,
168
                                    'buildUrl' => false,
169
                                ];
170
                            }
171
                        }
172
                    }
173
                    $this->view->assign('useIiif', true);
174
                    $this->view->assign('iiifData', $iiifData);
175
                }
176
            }
177
        } else {
178
            // findBySettings also sorts entries by the `sorting` field
179
            $metadataResult = $this->metadataRepository->findBySettings([
180
                'is_listed' => !$this->settings['showFull'],
181
            ]);
182
183
            // Collect raw metadata into an array that will be passed as data to cObj.
184
            // This lets metadata wraps reference (own or foreign) values via TypoScript "field".
185
            $metaCObjData = [];
186
187
            $buildUrl = [];
188
            $externalUrl = [];
189
            $i = 0;
190
            foreach ($metadata as $section) {
191
                $metaCObjData[$i] = [];
192
193
                foreach ($section as $name => $value) {
194
                    // NOTE: Labels are to be escaped in Fluid template
195
196
                    $metaCObjData[$i][$name] = is_array($value)
197
                        ? implode($this->settings['separator'], $value)
198
                        : $value;
199
200
                    if ($name == 'title') {
201
                        // Get title of parent document if needed.
202
                        if (empty(implode('', $value)) && $this->settings['getTitle'] && $this->document->getPartof()) {
203
                            $superiorTitle = Doc::getTitle($this->document->getPartof(), true);
204
                            if (!empty($superiorTitle)) {
205
                                $metadata[$i][$name] = ['[' . $superiorTitle . ']'];
206
                            }
207
                        }
208
                        if (!empty($value)) {
209
                            $metadata[$i][$name][0] = $metadata[$i][$name][0];
210
                            // Link title to pageview.
211
                            if ($this->settings['linkTitle'] && $section['_id']) {
212
                                $details = $this->document->getDoc()->getLogicalStructure($section['_id']);
213
                                $buildUrl[$i][$name]['buildUrl'] = [
214
                                    'id' => $this->document->getUid(),
215
                                    'page' => (!empty($details['points']) ? intval($details['points']) : 1),
216
                                    'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0),
217
                                ];
218
                            }
219
                        }
220
                    } elseif (($name == 'author' || $name == 'holder') && !empty($value) && !empty($value[0]['url'])) {
221
                        $externalUrl[$i][$name]['externalUrl'] = $value[0];
222
                    } elseif (($name == 'geonames' || $name == 'wikidata' || $name == 'wikipedia') && !empty($value)) {
223
                        $externalUrl[$i][$name]['externalUrl'] = [
224
                            'name' => $value[0],
225
                            'url' => $value[0]
226
                        ];
227
                    } elseif ($name == 'owner' && empty($value)) {
228
                        // no owner is found by metadata records --> take the one associated to the document
229
                        $library = $this->document->getOwner();
230
                        if ($library) {
231
                            $metadata[$i][$name][0] = $library->getLabel();
232
                        }
233
                    } elseif ($name == 'type' && !empty($value)) {
234
                        // Translate document type.
235
                        $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

235
                        /** @scrutinizer ignore-call */ 
236
                        $structure = $this->structureRepository->findOneByIndexName($metadata[$i][$name][0]);
Loading history...
236
                        if ($structure) {
237
                            $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

237
                            /** @scrutinizer ignore-call */ 
238
                            $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...
238
                        }
239
                    } elseif ($name == 'collection' && !empty($value)) {
240
                        // Check if collections isn't hidden.
241
                        $j = 0;
242
                        foreach ($value as $entry) {
243
                            $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

243
                            /** @scrutinizer ignore-call */ 
244
                            $collection = $this->collectionRepository->findOneByIndexName($entry);
Loading history...
244
                            if ($collection) {
245
                                $metadata[$i][$name][$j] = $collection->getLabel() ? : '';
246
                                $j++;
247
                            }
248
                        }
249
                    } elseif ($name == 'language' && !empty($value)) {
250
                        // Translate ISO 639 language code.
251
                        foreach ($metadata[$i][$name] as &$langValue) {
252
                            $langValue = Helper::getLanguageName($langValue);
253
                        }
254
                    }
255
256
                    if (is_array($metadata[$i][$name])) {
257
                        $metadata[$i][$name] = array_values(array_filter($metadata[$i][$name], function($metadataValue)
258
                        {
259
                            return !empty($metadataValue);
260
                        }));
261
                    }
262
                }
263
                $i++;
264
            }
265
266
            $this->view->assign('buildUrl', $buildUrl);
267
            $this->view->assign('externalUrl', $externalUrl);
268
            $this->view->assign('documentMetadataSections', $metadata);
269
            $this->view->assign('configMetadata', $metadataResult);
270
            $this->view->assign('separator', $this->settings['separator']);
271
            $this->view->assign('metaCObjData', $metaCObjData);
272
        }
273
    }
274
275
    /**
276
     * Get metadata for given id array.
277
     *
278
     * @access private
279
     *
280
     * @return array metadata
281
     */
282
    private function getMetadata()
283
    {
284
        $metadata = [];
285
        if ($this->settings['rootline'] < 2) {
286
            // Get current structure's @ID.
287
            $ids = [];
288
            if (!empty($this->document->getDoc()->physicalStructure[$this->requestData['page']]) && !empty($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]])) {
289
                foreach ($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]] as $logId) {
290
                    $count = $this->document->getDoc()->getStructureDepth($logId);
291
                    $ids[$count][] = $logId;
292
                }
293
            }
294
            ksort($ids);
295
            reset($ids);
296
            // Check if we should display all metadata up to the root.
297
            if ($this->settings['rootline'] == 1) {
298
                foreach ($ids as $id) {
299
                    $metadata = $this->getMetadataForIds($id, $metadata);
300
                }
301
            } else {
302
                $id = array_pop($ids);
303
                if (is_array($id)) {
304
                    $metadata = $this->getMetadataForIds($id, $metadata);
305
                }
306
            }
307
        }
308
        return $metadata;
309
    }
310
311
    /**
312
     * Get metadata for given id array.
313
     *
314
     * @access private
315
     *
316
     * @param array $id: An array with ids
317
     * @param array $metadata: An array with metadata
318
     *
319
     * @return array metadata
320
     */
321
    private function getMetadataForIds($id, $metadata)
322
    {
323
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest;
324
        foreach ($id as $sid) {
325
            if ($useOriginalIiifManifestMetadata) {
326
                $data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']);
327
            } else {
328
                $data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']);
329
            }
330
            if (!empty($data)) {
331
                $data['_id'] = $sid;
332
                $metadata[] = $data;
333
            }
334
        }
335
        return $metadata;
336
    }
337
338
    /**
339
     * Sets default value for setting if not yet set.
340
     *
341
     * @access private
342
     *
343
     * @param string $setting name of setting
344
     * @param int $value 0 or 1
345
     *
346
     * @return void
347
     */
348
    private function setDefault($setting, $value) {
349
        if (!isset($this->settings[$setting])) {
350
            $this->settings[$setting] = $value;
351
        }
352
    }
353
}
354