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 — dev-extbase-fluid (#718)
by Alexander
02:47
created

PageViewController::getFulltext()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 28
rs 9.0111
cc 6
nc 8
nop 1
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 TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
17
use TYPO3\CMS\Core\Database\Connection;
18
use Ubl\Iiif\Presentation\Common\Model\Resources\ManifestInterface;
19
use Ubl\Iiif\Presentation\Common\Vocabulary\Motivation;
20
21
class PageViewController extends AbstractController
22
{
23
    /**
24
     * @var
25
     */
26
    protected $extConf;
27
28
    /**
29
     * Holds the controls to add to the map
30
     *
31
     * @var array
32
     * @access protected
33
     */
34
    protected $controls = [];
35
36
    /**
37
     * The main method of the plugin
38
     *
39
     * @return void
40
     */
41
    public function mainAction()
42
    {
43
        $requestData = GeneralUtility::_GPmerged('tx_dlf');
44
        unset($requestData['__referrer'], $requestData['__trustedProperties']);
45
46
        $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
47
48
        // Load current document.
49
        $this->loadDocument($requestData);
50
        if (
51
            $this->doc === null
52
            || $this->doc->numPages < 1
53
        ) {
54
            // Quit without doing anything if required variables are not set.
55
            return;
56
        } else {
57
            if (!empty($requestData['logicalPage'])) {
58
                $requestData['page'] = $this->doc->getPhysicalPage($requestData['logicalPage']);
59
                // The logical page parameter should not appear again
60
                unset($requestData['logicalPage']);
61
            }
62
            // Set default values if not set.
63
            // $requestData['page'] may be integer or string (physical structure @ID)
64
            if ((int) $requestData['page'] > 0 || empty($requestData['page'])) {
65
                $requestData['page'] = MathUtility::forceIntegerInRange((int) $requestData['page'], 1, $this->doc->numPages, 1);
66
            } else {
67
                $requestData['page'] = array_search($requestData['page'], $this->doc->physicalStructure);
68
            }
69
            $requestData['double'] = MathUtility::forceIntegerInRange($requestData['double'], 0, 1, 0);
70
        }
71
        // Get image data.
72
        $this->images[0] = $this->getImage($requestData['page']);
0 ignored issues
show
Bug Best Practice introduced by
The property images does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
        $this->fulltexts[0] = $this->getFulltext($requestData['page']);
0 ignored issues
show
Bug Best Practice introduced by
The property fulltexts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
        $this->annotationContainers[0] = $this->getAnnotationContainers($requestData['page']);
0 ignored issues
show
Bug Best Practice introduced by
The property annotationContainers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        if ($requestData['double'] && $requestData['page'] < $this->doc->numPages) {
76
            $this->images[1] = $this->getImage($requestData['page'] + 1);
77
            $this->fulltexts[1] = $this->getFulltext($requestData['page'] + 1);
78
            $this->annotationContainers[1] = $this->getAnnotationContainers($requestData['page'] + 1);
79
        }
80
81
        // Get the controls for the map.
82
        $this->controls = explode(',', $this->settings['features']);
83
84
        $this->view->assign('forceAbsoluteUrl', $this->settings['forceAbsoluteUrl']);
85
86
        $this->addViewerJS();
87
88
        $this->view->assign('images', $this->images);
89
        $this->view->assign('docId', $requestData['id']);
90
        $this->view->assign('page', $requestData['page']);
91
    }
92
93
    /**
94
     * Get fulltext URL and MIME type
95
     *
96
     * @access protected
97
     *
98
     * @param int $page: Page number
99
     *
100
     * @return array URL and MIME type of fulltext file
101
     */
102
    protected function getFulltext($page)
103
    {
104
        $fulltext = [];
105
        // Get fulltext link.
106
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
107
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
108
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrpFulltext])) {
109
                $fulltext['url'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrpFulltext]);
110
                if ($this->settings['useInternalProxy']) {
111
                    // Configure @action URL for form.
112
                    $uri = $this->uriBuilder->reset()
113
                        ->setTargetPageUid($GLOBALS['TSFE']->id)
114
                        ->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? 1 : 0)
115
                        ->setArguments(['eID' => 'tx_dlf_pageview_proxy', 'url' => urlencode($fulltext['url'])])
116
                        ->build();
117
118
                    $fulltext['url'] = $uri;
119
                }
120
                $fulltext['mimetype'] = $this->doc->getFileMimeType($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrpFulltext]);
121
                break;
122
            } else {
123
                $this->logger->notice('No full-text file found for page "' . $page . '" in fileGrp "' . $fileGrpFulltext . '"');
1 ignored issue
show
Bug introduced by
The method notice() 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

123
                $this->logger->/** @scrutinizer ignore-call */ 
124
                               notice('No full-text file found for page "' . $page . '" in fileGrp "' . $fileGrpFulltext . '"');

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...
124
            }
125
        }
126
        if (empty($fulltext)) {
127
            $this->logger->notice('No full-text file found for page "' . $page . '" in fileGrps "' . $this->conf['settings.fileGrpFulltext'] . '"');
128
        }
129
        return $fulltext;
130
    }
131
132
    /**
133
     * Adds Viewer javascript
134
     *
135
     * @access protected
136
     *
137
     * @return void
138
     */
139
    protected function addViewerJS()
140
    {
141
        // Viewer configuration.
142
        $viewerConfiguration = '<script>
143
            $(document).ready(function() {
144
                if (dlfUtils.exists(dlfViewer)) {
145
                    tx_dlf_viewer = new dlfViewer({
146
                        controls: ["' . implode('", "', $this->controls) . '"],
147
                        div: "' . $this->settings['elementId'] . '",
148
                        images: ' . json_encode($this->images) . ',
149
                        fulltexts: ' . json_encode($this->fulltexts) . ',
150
                        annotationContainers: ' . json_encode($this->annotationContainers) . ',
151
                        useInternalProxy: ' . ($this->settings['useInternalProxy'] ? 1 : 0) . '
152
                    });
153
                }
154
            });
155
        </script>';
156
        $this->view->assign('viewerConfiguration', $viewerConfiguration);
157
    }
158
159
    /**
160
     * Get all AnnotationPages / AnnotationLists that contain text Annotations with motivation "painting"
161
     *
162
     * @access protected
163
     *
164
     * @param int $page: Page number
165
     * @return array An array containing the IRIs of the AnnotationLists / AnnotationPages as well as
166
     *               some information about the canvas.
167
     */
168
    protected function getAnnotationContainers($page)
169
    {
170
        if ($this->doc instanceof IiifManifest) {
0 ignored issues
show
Bug introduced by
The type Kitodo\Dlf\Controller\IiifManifest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
171
            $canvasId = $this->doc->physicalStructure[$page];
172
            $iiif = $this->doc->getIiif();
173
            if ($iiif instanceof ManifestInterface) {
174
                $canvas = $iiif->getContainedResourceById($canvasId);
175
                /* @var $canvas \Ubl\Iiif\Presentation\Common\Model\Resources\CanvasInterface */
176
                if ($canvas != null && !empty($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING))) {
177
                    $annotationContainers = [];
178
                    /*
179
                     *  TODO Analyzing the annotations on the server side requires loading the annotation lists / pages
180
                     *  just to determine wether they contain text annotations for painting. This will take time and lead to a bad user experience.
181
                     *  It would be better to link every annotation and analyze the data on the client side.
182
                     *
183
                     *  On the other hand, server connections are potentially better than client connections. Downloading annotation lists
184
                     */
185
                    foreach ($canvas->getPossibleTextAnnotationContainers(Motivation::PAINTING) as $annotationContainer) {
186
                        if (($textAnnotations = $annotationContainer->getTextAnnotations(Motivation::PAINTING)) != null) {
187
                            foreach ($textAnnotations as $annotation) {
188
                                if (
189
                                    $annotation->getBody()->getFormat() == 'text/plain'
190
                                    && $annotation->getBody()->getChars() != null
191
                                ) {
192
                                    $annotationListData = [];
193
                                    $annotationListData['uri'] = $annotationContainer->getId();
194
                                    $annotationListData['label'] = $annotationContainer->getLabelForDisplay();
195
                                    $annotationContainers[] = $annotationListData;
196
                                    break;
197
                                }
198
                            }
199
                        }
200
                    }
201
                    $result = [
202
                        'canvas' => [
203
                            'id' => $canvas->getId(),
204
                            'width' => $canvas->getWidth(),
205
                            'height' => $canvas->getHeight(),
206
                        ],
207
                        'annotationContainers' => $annotationContainers
208
                    ];
209
                    return $result;
210
                }
211
            }
212
        }
213
        return [];
214
    }
215
216
    /**
217
     * Get image's URL and MIME type
218
     *
219
     * @access protected
220
     *
221
     * @param int $page: Page number
222
     *
223
     * @return array URL and MIME type of image file
224
     */
225
    protected function getImage($page)
226
    {
227
        $image = [];
228
        // Get @USE value of METS fileGrp.
229
        $fileGrpsImages = GeneralUtility::trimExplode(',', $this->extConf['fileGrpImages']);
230
        while ($fileGrpImages = array_pop($fileGrpsImages)) {
231
            // Get image link.
232
            if (!empty($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrpImages])) {
233
                $image['url'] = $this->doc->getFileLocation($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrpImages]);
234
                if ($this->settings['useInternalProxy']) {
235
                    // Configure @action URL for form.
236
                    $uri = $this->uriBuilder->reset()
237
                        ->setTargetPageUid($GLOBALS['TSFE']->id)
238
                        ->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? 1 : 0)
239
                        ->setArguments(['eID' => 'tx_dlf_pageview_proxy', 'url' => urlencode($image['url'])])
240
                        ->build();
241
                    $image['url'] = $uri;
242
                }
243
                $image['mimetype'] = $this->doc->getFileMimeType($this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrpImages]);
244
                break;
245
            } else {
246
                $this->logger->notice('No image file found for page "' . $page . '" in fileGrp "' . $fileGrpImages . '"');
247
            }
248
        }
249
        if (empty($image)) {
250
            $this->logger->warning('No image file found for page "' . $page . '" in fileGrps "' . $this->settings['fileGrpImages'] . '"');
251
        }
252
        return $image;
253
    }
254
}
255