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 (#705)
by Alexander
02:44
created

Metadata   F

Complexity

Total Complexity 87

Size/Duplication

Total Lines 371
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 87
eloc 208
c 1
b 0
f 0
dl 0
loc 371
rs 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
F main() 0 51 15
A getMetadataForIds() 0 14 5
F printMetadata() 0 154 49
A getCollections() 0 15 2
B getMetadataFromDatabase() 0 37 8
B getMetadata() 0 27 8

How to fix   Complexity   

Complex Class

Complex classes like Metadata often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Metadata, and based on these observations, apply Extract Interface, too.

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\Plugin;
14
15
use Kitodo\Dlf\Common\Document;
16
use Kitodo\Dlf\Common\Helper;
17
use Kitodo\Dlf\Common\IiifManifest;
18
use TYPO3\CMS\Core\Database\ConnectionPool;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use Ubl\Iiif\Context\IRI;
21
22
/**
23
 * Plugin 'Metadata' for the 'dlf' extension
24
 *
25
 * @author Sebastian Meyer <[email protected]>
26
 * @author Siegfried Schweizer <[email protected]>
27
 * @package TYPO3
28
 * @subpackage dlf
29
 * @access public
30
 */
31
class Metadata extends \Kitodo\Dlf\Common\AbstractPlugin
32
{
33
    public $scriptRelPath = 'Classes/Plugin/Metadata.php';
34
35
    /**
36
     * This holds the hook objects
37
     *
38
     * @var array
39
     * @access protected
40
     */
41
    protected $hookObjects = [];
42
43
    /**
44
     * The main method of the PlugIn
45
     *
46
     * @access public
47
     *
48
     * @param string $content: The PlugIn content
49
     * @param array $conf: The PlugIn configuration
50
     *
51
     * @return string The content that is displayed on the website
52
     */
53
    public function main($content, $conf)
54
    {
55
        $this->init($conf);
56
        // Turn cache on.
57
        $this->setCache(true);
58
        // Load current document.
59
        $this->loadDocument();
60
        if ($this->doc === null) {
61
            // Quit without doing anything if required variables are not set.
62
            return $content;
63
        } else {
64
            // Set default values if not set.
65
            if (!isset($this->conf['rootline'])) {
66
                $this->conf['rootline'] = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property conf does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
            }
68
            if (!isset($this->conf['originalIiifMetadata'])) {
69
                $this->conf['originalIiifMetadata'] = 0;
70
            }
71
            if (!isset($this->conf['displayIiifDescription'])) {
72
                $this->conf['displayIiifDescription'] = 1;
73
            }
74
            if (!isset($this->conf['displayIiifRights'])) {
75
                $this->conf['displayIiifRights'] = 1;
76
            }
77
            if (!isset($this->conf['displayIiifLinks'])) {
78
                $this->conf['displayIiifLinks'] = 1;
79
            }
80
        }
81
        $useOriginalIiifManifestMetadata = $this->conf['originalIiifMetadata'] == 1 && $this->doc instanceof IiifManifest;
82
        $metadata = $this->getMetadata();
83
        // Get titledata?
84
        if (empty($metadata) || ($this->conf['rootline'] == 1 && $metadata[0]['_id'] != $this->doc->toplevelId)) {
85
            $data = $useOriginalIiifManifestMetadata ? $this->doc->getManifestMetadata($this->doc->toplevelId, $this->conf['pages']) : $this->doc->getTitleData($this->conf['pages']);
86
            $data['_id'] = $this->doc->toplevelId;
87
            array_unshift($metadata, $data);
88
        }
89
        if (empty($metadata)) {
90
            $this->logger->warning('No metadata found for document with UID ' . $this->doc->uid);
1 ignored issue
show
Bug introduced by
The method warning() does not exist on null. ( Ignorable by Annotation )

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

90
            $this->logger->/** @scrutinizer ignore-call */ 
91
                           warning('No metadata found for document with UID ' . $this->doc->uid);

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...
91
            return $content;
92
        }
93
        ksort($metadata);
94
        // Get hook objects.
95
        $this->hookObjects = Helper::getHookObjects($this->scriptRelPath);
96
        // Hook for getting a customized title bar (requested by SBB).
97
        foreach ($this->hookObjects as $hookObj) {
98
            if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) {
99
                $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata);
100
            }
101
        }
102
        $content .= $this->printMetadata($metadata, $useOriginalIiifManifestMetadata);
103
        return $this->pi_wrapInBaseClass($content);
104
    }
105
106
    /**
107
     * Prepares the metadata array for output
108
     *
109
     * @access protected
110
     *
111
     * @param array $metadataArray: The metadata array
112
     * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs?
113
     *
114
     * @return string The metadata array ready for output
115
     */
116
    protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false)
117
    {
118
        // Load template file.
119
        $this->getTemplate();
120
        $output = '';
121
        $subpart['block'] = $this->templateService->getSubpart($this->template, '###BLOCK###');
122
        // Save original data array.
123
        $cObjData = $this->cObj->data;
124
        // Get list of metadata to show.
125
        $metaList = [];
126
        if ($useOriginalIiifManifestMetadata) {
127
            if ($this->conf['iiifMetadataWrap']) {
128
                $iiifwrap = $this->parseTS($this->conf['iiifMetadataWrap']);
129
            } else {
130
                $iiifwrap['key.']['wrap'] = '<dt>|</dt>';
131
                $iiifwrap['value.']['required'] = 1;
132
                $iiifwrap['value.']['wrap'] = '<dd>|</dd>';
133
            }
134
            $iiifLink = [];
135
            $iiifLink['key.']['wrap'] = '<dt>|</dt>';
136
            $iiifLink['value.']['required'] = 1;
137
            $iiifLink['value.']['setContentToCurrent'] = 1;
138
            $iiifLink['value.']['typolink.']['parameter.']['current'] = 1;
139
            $iiifLink['value.']['typolink.']['forceAbsoluteUrl'] = !empty($this->conf['forceAbsoluteUrl']) ? 1 : 0;
140
            $iiifLink['value.']['typolink.']['forceAbsoluteUrl.']['scheme'] = !empty($this->conf['forceAbsoluteUrl']) && !empty($this->conf['forceAbsoluteUrlHttps']) ? 'https' : 'http';
141
            $iiifLink['value.']['wrap'] = '<dd>|</dd>';
142
            foreach ($metadataArray as $metadata) {
143
                foreach ($metadata as $key => $group) {
144
                    $markerArray['###METADATA###'] = '<span class="tx-dlf-metadata-group">' . $this->pi_getLL($key) . '</span>';
145
                    // Reset content object's data array.
146
                    $this->cObj->data = $cObjData;
147
                    if (!is_array($group)) {
148
                        if ($key == '_id') {
149
                            continue;
150
                        }
151
                        $this->cObj->data[$key] = $group;
152
                        if (
153
                            IRI::isAbsoluteIri($this->cObj->data[$key])
154
                            && (($scheme = (new IRI($this->cObj->data[$key]))->getScheme()) == 'http' || $scheme == 'https')
155
                        ) {
156
                            $field = $this->cObj->stdWrap('', $iiifLink['key.']);
157
                            $field .= $this->cObj->stdWrap($this->cObj->data[$key], $iiifLink['value.']);
158
                        } else {
159
                            $field = $this->cObj->stdWrap('', $iiifwrap['key.']);
160
                            $field .= $this->cObj->stdWrap($this->cObj->data[$key], $iiifwrap['value.']);
161
                        }
162
                        $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $iiifwrap['all.']);
163
                    } else {
164
                        // Load all the metadata values into the content object's data array.
165
                        foreach ($group as $label => $value) {
166
                            if ($label == '_id') {
167
                                continue;
168
                            }
169
                            if (is_array($value)) {
170
                                $this->cObj->data[$label] = implode($this->conf['separator'], $value);
171
                            } else {
172
                                $this->cObj->data[$label] = $value;
173
                            }
174
                            if (IRI::isAbsoluteIri($this->cObj->data[$label]) && (($scheme = (new IRI($this->cObj->data[$label]))->getScheme()) == 'http' || $scheme == 'https')) {
175
                                $nolabel = $this->cObj->data[$label] == $label;
176
                                $field = $this->cObj->stdWrap($nolabel ? '' : htmlspecialchars($label), $iiifLink['key.']);
177
                                $field .= $this->cObj->stdWrap($this->cObj->data[$label], $iiifLink['value.']);
178
                            } else {
179
                                $field = $this->cObj->stdWrap(htmlspecialchars($label), $iiifwrap['key.']);
180
                                $field .= $this->cObj->stdWrap($this->cObj->data[$label], $iiifwrap['value.']);
181
                            }
182
                            $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $iiifwrap['all.']);
183
                        }
184
                    }
185
                    $output .= $this->templateService->substituteMarkerArray($subpart['block'], $markerArray);
186
                }
187
            }
188
        } else {
189
            $metaList = $this->getMetadataFromDatabase($metaList);
190
            $collList = $this->getCollections();
191
            // Parse the metadata arrays.
192
            foreach ($metadataArray as $metadata) {
193
                $markerArray['###METADATA###'] = '';
194
                // Reset content object's data array.
195
                $this->cObj->data = $cObjData;
196
                // Load all the metadata values into the content object's data array.
197
                foreach ($metadata as $index_name => $value) {
198
                    if (is_array($value)) {
199
                        $this->cObj->data[$index_name] = implode($this->conf['separator'], $value);
200
                    } else {
201
                        $this->cObj->data[$index_name] = $value;
202
                    }
203
                }
204
                // Process each metadate.
205
                foreach ($metaList as $index_name => $metaConf) {
206
                    $parsedValue = '';
207
                    $fieldwrap = $this->parseTS($metaConf['wrap']);
208
                    do {
209
                        $value = @array_shift($metadata[$index_name]);
210
                        if ($index_name == 'title') {
211
                            // Get title of parent document if needed.
212
                            if (empty($value) && $this->conf['getTitle'] && $this->doc->parentId) {
213
                                $superiorTitle = Document::getTitle($this->doc->parentId, true);
214
                                if (!empty($superiorTitle)) {
215
                                    $value = '[' . $superiorTitle . ']';
216
                                }
217
                            }
218
                            if (!empty($value)) {
219
                                $value = htmlspecialchars($value);
220
                                // Link title to pageview.
221
                                if ($this->conf['linkTitle'] && $metadata['_id']) {
222
                                    $details = $this->doc->getLogicalStructure($metadata['_id']);
223
                                    $value = $this->pi_linkTP($value, [$this->prefixId => ['id' => $this->doc->uid, 'page' => (!empty($details['points']) ? intval($details['points']) : 1)]], true, $this->conf['targetPid']);
224
                                }
225
                            }
226
                        } elseif ($index_name == 'owner' && !empty($value)) {
227
                            // Translate name of holding library.
228
                            $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
229
                        } elseif ($index_name == 'type' && !empty($value)) {
230
                            // Translate document type.
231
                            $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
232
                        } elseif ($index_name == 'collection' && !empty($value)) {
233
                            // Check if collections isn't hidden.
234
                            if (in_array($value, $collList)) {
235
                                // Translate collection.
236
                                $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_collections', $this->conf['pages']));
237
                            } else {
238
                                $value = '';
239
                            }
240
                        } elseif ($index_name == 'language' && !empty($value)) {
241
                            // Translate ISO 639 language code.
242
                            $value = htmlspecialchars(Helper::getLanguageName($value));
243
                        } elseif (!empty($value)) {
244
                            // Sanitize value for output.
245
                            $value = htmlspecialchars($value);
246
                        }
247
                        // Hook for getting a customized value (requested by SBB).
248
                        foreach ($this->hookObjects as $hookObj) {
249
                            if (method_exists($hookObj, 'printMetadata_customizeMetadata')) {
250
                                $hookObj->printMetadata_customizeMetadata($value);
251
                            }
252
                        }
253
                        // $value might be empty for aggregation metadata fields including other "hidden" fields.
254
                        $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
255
                        if (!empty($value)) {
256
                            $parsedValue .= $value;
257
                        }
258
                    } while (is_array($metadata[$index_name]) && count($metadata[$index_name]) > 0);
259
260
                    if (!empty($parsedValue)) {
261
                        $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
262
                        $field .= $parsedValue;
263
                        $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
264
                    }
265
                }
266
                $output .= $this->templateService->substituteMarkerArray($subpart['block'], $markerArray);
267
            }
268
        }
269
        return $this->templateService->substituteSubpart($this->template, '###BLOCK###', $output, true);
270
    }
271
272
    /**
273
     * Get list of collections to show.
274
     *
275
     * @access private
276
     *
277
     * @return array list of collections
278
     */
279
    private function getCollections() {
280
        $collections = [];
281
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
282
                ->getQueryBuilderForTable('tx_dlf_collections');
283
        $result = $queryBuilder
284
            ->select('tx_dlf_collections.index_name AS index_name')
285
            ->from('tx_dlf_collections')
286
            ->where(
287
                $queryBuilder->expr()->eq('tx_dlf_collections.pid', intval($this->conf['pages']))
288
            )
289
            ->execute();
290
        while ($resArray = $result->fetch()) {
291
            $collections[] = $resArray['index_name'];
292
        }
293
        return $collections;
294
    }
295
296
    /**
297
     * Get list of metadata from database.
298
     *
299
     * @access private
300
     *
301
     * @return array list of metadata
302
     */
303
    private function getMetadataFromDatabase($metadata) {
304
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
305
            ->getQueryBuilderForTable('tx_dlf_metadata');
306
        $result = $queryBuilder
307
            ->select(
308
                'tx_dlf_metadata.index_name AS index_name',
309
                'tx_dlf_metadata.is_listed AS is_listed',
310
                'tx_dlf_metadata.wrap AS wrap',
311
                'tx_dlf_metadata.sys_language_uid AS sys_language_uid'
312
            )
313
            ->from('tx_dlf_metadata')
314
            ->where(
315
                $queryBuilder->expr()->andX(
316
                    $queryBuilder->expr()->orX(
317
                        $queryBuilder->expr()->in('tx_dlf_metadata.sys_language_uid', [-1, 0]),
318
                        $queryBuilder->expr()->eq('tx_dlf_metadata.sys_language_uid', $GLOBALS['TSFE']->sys_language_uid)
319
                    ),
320
                    $queryBuilder->expr()->eq('tx_dlf_metadata.l18n_parent', 0)
321
                ),
322
                $queryBuilder->expr()->eq('tx_dlf_metadata.pid', intval($this->conf['pages']))
323
            )
324
            ->orderBy('tx_dlf_metadata.sorting')
325
            ->execute();
326
        while ($resArray = $result->fetch()) {
327
            if (is_array($resArray) && $resArray['sys_language_uid'] != $GLOBALS['TSFE']->sys_language_content && $GLOBALS['TSFE']->sys_language_contentOL) {
328
                $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_metadata', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
329
            }
330
            if ($resArray) {
331
                if ($this->conf['showFull'] || $resArray['is_listed']) {
332
                    $metadata[$resArray['index_name']] = [
333
                        'wrap' => $resArray['wrap'],
334
                        'label' => Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages'])
335
                    ];
336
                }
337
            }
338
        }
339
        return $metadata;
340
    }
341
342
    /**
343
     * Get metadata for given id array.
344
     *
345
     * @access private
346
     *
347
     * @return array metadata
348
     */
349
    private function getMetadata() {
350
        $metadata = [];
351
        if ($this->conf['rootline'] < 2) {
352
            // Get current structure's @ID.
353
            $ids = [];
354
            $physicalStructure = $this->doc->physicalStructure[$this->piVars['page']];
355
            if (!empty($physicalStructure) && !empty($this->doc->smLinks['p2l'][$physicalStructure])) {
356
                foreach ($this->doc->smLinks['p2l'][$physicalStructure] as $logId) {
357
                    $count = $this->doc->getStructureDepth($logId);
358
                    $ids[$count][] = $logId;
359
                }
360
            }
361
            ksort($ids);
362
            reset($ids);
363
            // Check if we should display all metadata up to the root.
364
            if ($this->conf['rootline'] == 1) {
365
                foreach ($ids as $id) {
366
                    $metadata = $this->getMetadataForIds($id, $metadata);
367
                }
368
            } else {
369
                $id = array_pop($ids);
370
                if (is_array($id)) {
371
                    $metadata = $this->getMetadataForIds($id, $metadata);
372
                }
373
            }
374
        }
375
        return $metadata;
376
    }
377
378
    /**
379
     * Get metadata for given id array.
380
     *
381
     * @access private
382
     *
383
     * @param array $id: An array with ids
384
     * @param array $metadata: An array with metadata
385
     *
386
     * @return array metadata
387
     */
388
    private function getMetadataForIds($id, $metadata) {
389
        $useOriginalIiifManifestMetadata = $this->conf['originalIiifMetadata'] == 1 && $this->doc instanceof IiifManifest;
390
        foreach ($id as $sid) {
391
            if ($useOriginalIiifManifestMetadata) {
392
                $data = $this->doc->getManifestMetadata($sid, $this->conf['pages']);
393
            } else {
394
                $data = $this->doc->getMetadata($sid, $this->conf['pages']);
395
            }
396
            if (!empty($data)) {
397
                $data['_id'] = $sid;
398
                $metadata[] = $data;
399
            }
400
        }
401
        return $metadata;
402
    }
403
}
404