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 (#715)
by Alexander
03:36
created

ToolboxController::fulltextdownloadtool()   B

Complexity

Conditions 10
Paths 25

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 24
c 2
b 0
f 0
nc 25
nop 0
dl 0
loc 38
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helper;
15
use TYPO3\CMS\Core\Utility\MathUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18
class ToolboxController extends AbstractController
19
{
20
    /**
21
     * The main method of the plugin
22
     *
23
     * @return void
24
     */
25
    public function mainAction()
26
    {
27
        // Load current document.
28
        $this->loadDocument($this->requestData);
29
30
        $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
31
        $this->view->assign('double', $this->requestData['double']);
32
33
        $tools = explode(',', $this->settings['tools']);
34
        // Add the tools to the toolbox.
35
        foreach ($tools as $tool) {
36
            $tool = trim(str_replace('tx_dlf_', '', $tool));
37
            $this->$tool();
38
            $this->view->assign($tool, true);
39
        }
40
    }
41
42
    /**
43
     * Renders the annotation tool
44
     *
45
     * @return void
46
     */
47
    public function annotationtool()
48
    {
49
        if (
50
            $this->document === null
51
            || $this->document->getDoc()->numPages < 1
52
        ) {
53
            // Quit without doing anything if required variables are not set.
54
            return;
55
        } else {
56
            if (!empty($this->requestData['logicalPage'])) {
57
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
58
                // The logical page parameter should not appear again
59
                unset($this->requestData['logicalPage']);
60
            }
61
            // Set default values if not set.
62
            // $this->requestData['page'] may be integer or string (physical structure @ID)
63
            if (
64
                (int) $this->requestData['page'] > 0
65
                || empty($this->requestData['page'])
66
            ) {
67
                $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
68
            } else {
69
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
70
            }
71
        }
72
73
        $annotationContainers = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['annotationContainers'];
74
        if (
75
            $annotationContainers != null
76
            && sizeof($annotationContainers) > 0
77
        ) {
78
            $this->view->assign('annotationTool', true);
79
        } else {
80
            $this->view->assign('annotationTool', false);
81
        }
82
    }
83
84
    /**
85
     * Renders the fulltext download tool
86
     *
87
     * @return void
88
     */
89
    public function fulltextdownloadtool()
90
    {
91
        if (
92
            $this->document === null
93
            || $this->document->getDoc()->numPages < 1
94
            || empty($this->extConf['fileGrpFulltext'])
95
        ) {
96
            // Quit without doing anything if required variables are not set.
97
            return;
98
        } else {
99
            if (!empty($this->requestData['logicalPage'])) {
100
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
101
                // The logical page parameter should not appear again
102
                unset($this->requestData['logicalPage']);
103
            }
104
            // Set default values if not set.
105
            // $this->requestData['page'] may be integer or string (physical structure @ID)
106
            if (
107
                (int) $this->requestData['page'] > 0
108
                || empty($this->requestData['page'])
109
            ) {
110
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
111
            } else {
112
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
113
            }
114
        }
115
        // Get text download.
116
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
117
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
118
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
119
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
120
                break;
121
            }
122
        }
123
        if (!empty($fullTextFile)) {
124
            $this->view->assign('fulltextDownload', true);
125
        } else {
126
            $this->view->assign('fulltextDownload', false);
127
        }
128
    }
129
130
    /**
131
     * Renders the fulltext tool
132
     *
133
     * @return void
134
     */
135
    public function fulltexttool()
136
    {
137
        if (
138
            $this->document === null
139
            || $this->document->getDoc()->numPages < 1
140
            || empty($this->extConf['fileGrpFulltext'])
141
        ) {
142
            // Quit without doing anything if required variables are not set.
143
            return;
144
        } else {
145
            if (!empty($this->requestData['logicalPage'])) {
146
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
147
                // The logical page parameter should not appear again
148
                unset($this->requestData['logicalPage']);
149
            }
150
            // Set default values if not set.
151
            // $this->requestData['page'] may be integer or string (physical structure @ID)
152
            if (
153
                (int) $this->requestData['page'] > 0
154
                || empty($this->requestData['page'])
155
            ) {
156
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
157
            } else {
158
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
159
            }
160
        }
161
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
162
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
163
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
164
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
165
                break;
166
            }
167
        }
168
        if (!empty($fullTextFile)) {
169
            $this->view->assign('fulltext', true);
170
            $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0));
171
        } else {
172
            $this->view->assign('fulltext', false);
173
        }
174
    }
175
176
    /**
177
     * Renders the image download tool
178
     *
179
     * @return void
180
     */
181
    public function imagedownloadtool()
182
    {
183
        if (
184
            $this->document === null
185
            || $this->document->getDoc()->numPages < 1
186
            || empty($this->settings['fileGrpsImageDownload'])
187
        ) {
188
            // Quit without doing anything if required variables are not set.
189
            return;
190
        } else {
191
            if (!empty($this->requestData['logicalPage'])) {
192
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
193
                // The logical page parameter should not appear again
194
                unset($this->requestData['logicalPage']);
195
            }
196
            // Set default values if not set.
197
            // $this->requestData['page'] may be integer or string (physical structure @ID)
198
            if (
199
                (int) $this->requestData['page'] > 0
200
                || empty($this->requestData['page'])
201
            ) {
202
                $this->requestData['page'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
203
            } else {
204
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
205
            }
206
        }
207
        $imageArray = [];
208
        // Get left or single page download.
209
        $imageArray[0] = $this->getImage($this->requestData['page']);
210
        if ($this->requestData['double'] == 1) {
211
            $imageArray[1] = $this->getImage($this->requestData['page'] + 1);
212
        }
213
        $this->view->assign('imageDownload', $imageArray);
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 Array of image links and image format information
224
     */
225
    protected function getImage($page)
226
    {
227
        $image = [];
228
        // Get @USE value of METS fileGrp.
229
        $fileGrps = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
230
        while ($fileGrp = @array_pop($fileGrps)) {
231
            // Get image link.
232
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp])) {
233
                $image['url'] = $this->document->getDoc()->getDownloadLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]);
234
                $image['mimetype'] = $this->document->getDoc()->getFileMimeType($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$page]]['files'][$fileGrp]);
235
                switch ($image['mimetype']) {
236
                    case 'image/jpeg':
237
                        $mimetypeLabel = ' (JPG)';
238
                        break;
239
                    case 'image/tiff':
240
                        $mimetypeLabel = ' (TIFF)';
241
                        break;
242
                    default:
243
                        $mimetypeLabel = '';
244
                }
245
                $image['mimetypeLabel'] = $mimetypeLabel;
246
                break;
247
            } else {
248
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
249
            }
250
        }
251
        return $image;
252
    }
253
254
    /**
255
     * Renders the image manipulation tool
256
     *
257
     * @return void
258
     */
259
    public function imagemanipulationtool()
260
    {
261
        // Set parent element for initialization.
262
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
263
264
        $this->view->assign('imageManipulation', true);
265
        $this->view->assign('parentContainer', $parentContainer);
266
    }
267
268
    /**
269
     * Renders the PDF download tool
270
     *
271
     * @return void
272
     */
273
    public function pdfdownloadtool()
274
    {
275
        if (
276
            $this->document === null
277
            || $this->document->getDoc() === null
278
            || $this->document->getDoc()->numPages < 1
279
            || empty($this->extConf['fileGrpDownload'])
280
        ) {
281
            // Quit without doing anything if required variables are not set.
282
            return;
283
        } else {
284
            if (!empty($this->requestData['logicalPage'])) {
285
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
286
                // The logical page parameter should not appear again
287
                unset($this->requestData['logicalPage']);
288
            }
289
            // Set default values if not set.
290
            // $this->requestData['page'] may be integer or string (physical structure @ID)
291
            if (
292
                (int) $this->requestData['page'] > 0
293
                || empty($this->requestData['page'])
294
            ) {
295
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
296
            } else {
297
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
298
            }
299
        }
300
        // Get single page downloads.
301
        $this->view->assign('pageLinks', $this->getPageLink());
302
        // Get work download.
303
        $this->view->assign('workLink', $this->getWorkLink());
304
    }
305
306
    /**
307
     * Get page's download link
308
     *
309
     * @access protected
310
     *
311
     * @return array Link to downloadable page
312
     */
313
    protected function getPageLink()
314
    {
315
        $page1Link = '';
316
        $page2Link = '';
317
        $pageLinkArray = [];
318
        $pageNumber = $this->requestData['page'];
319
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
320
        // Get image link.
321
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
322
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload])) {
323
                $page1Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber]]['files'][$fileGrpDownload]);
324
                // Get second page, too, if double page view is activated.
325
                if (
326
                    $this->requestData['double']
327
                    && $pageNumber < $this->document->getDoc()->numPages
328
                    && !empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload])
329
                ) {
330
                    $page2Link = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload]);
331
                }
332
                break;
333
            }
334
        }
335
        if (
336
            empty($page1Link)
337
            && empty($page2Link)
338
        ) {
339
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
340
        }
341
342
        if (!empty($page1Link)) {
343
            $pageLinkArray[0] = $page1Link;
344
        }
345
        if (!empty($page2Link)) {
346
            $pageLinkArray[1] = $page2Link;
347
        }
348
        return $pageLinkArray;
349
    }
350
351
    /**
352
     * Get work's download link
353
     *
354
     * @access protected
355
     *
356
     * @return string Link to downloadable work
357
     */
358
    protected function getWorkLink()
359
    {
360
        $workLink = '';
361
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']);
362
        // Get work link.
363
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
364
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[0]]['files'][$fileGrpDownload])) {
365
                $workLink = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[0]]['files'][$fileGrpDownload]);
366
                break;
367
            } else {
368
                $details = $this->document->getDoc()->getLogicalStructure($this->document->getDoc()->toplevelId);
369
                if (!empty($details['files'][$fileGrpDownload])) {
370
                    $workLink = $this->document->getDoc()->getFileLocation($details['files'][$fileGrpDownload]);
371
                    break;
372
                }
373
            }
374
        }
375
        if (!empty($workLink)) {
376
            $workLink = $workLink;
377
        } else {
378
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['fileGrpDownload'] . '"');
379
        }
380
        return $workLink;
381
    }
382
383
    /**
384
     * Renders the searchInDocument tool
385
     *
386
     * @return void
387
     */
388
    public function searchindocumenttool()
389
    {
390
        if (
391
            $this->document === null
392
            || $this->document->getDoc()->numPages < 1
393
            || empty($this->extConf['fileGrpFulltext'])
394
            || empty($this->settings['solrcore'])
395
        ) {
396
            // Quit without doing anything if required variables are not set.
397
            return;
398
        } else {
399
            if (!empty($this->requestData['logicalPage'])) {
400
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
401
                // The logical page parameter should not appear again
402
                unset($this->requestData['logicalPage']);
403
            }
404
            // Set default values if not set.
405
            // $this->requestData['page'] may be integer or string (physical structure @ID)
406
            if (
407
                (int) $this->requestData['page'] > 0
408
                || empty($this->requestData['page'])
409
            ) {
410
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
411
            } else {
412
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
413
            }
414
        }
415
416
        // Quit if no fulltext file is present
417
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
418
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
419
            if (!empty($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext])) {
420
                $fullTextFile = $this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpFulltext];
421
                break;
422
            }
423
        }
424
        if (empty($fullTextFile)) {
425
            return;
426
        }
427
428
        // Fill markers.
429
        $viewArray = [
430
            'LABEL_QUERY_URL' => $this->settings['queryInputName'],
431
            'LABEL_START' => $this->settings['startInputName'],
432
            'LABEL_ID' => $this->settings['idInputName'],
433
            'LABEL_PAGE_URL' => $this->settings['pageInputName'],
434
            'LABEL_HIGHLIGHT_WORD' => $this->settings['highlightWordInputName'],
435
            'LABEL_ENCRYPTED' => $this->settings['encryptedInputName'],
436
            'CURRENT_DOCUMENT' => $this->getCurrentDocumentId(),
437
            'SOLR_ENCRYPTED' => $this->getEncryptedCoreName() ? : ''
438
        ];
439
440
        $this->view->assign('searchInDocument', $viewArray);
441
    }
442
443
    /**
444
     * Get current document id. As default the uid will be used.
445
     * In case there is defined documentIdUrlSchema then the id will
446
     * extracted from this URL.
447
     *
448
     * @access protected
449
     *
450
     * @return string with current document id
451
     */
452
    protected function getCurrentDocumentId()
453
    {
454
        $id = $this->document->getUid();
455
456
        if ($id !== null && $id > 0) {
457
            // we found the document uid
458
            return (string) $id;
459
        } else {
460
            $id = $this->requestData['id'];
461
            if (!GeneralUtility::isValidUrl($id)) {
462
                // we found no valid URI --> something unexpected we cannot search within.
463
                return '';
464
            }
465
        }
466
467
        // example: https://host.de/items/*id*/record
468
        if (!empty($this->settings['documentIdUrlSchema'])) {
469
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
470
471
            if (count($arr) == 2) {
472
                $id = explode($arr[0], $id)[0];
473
            } else if (count($arr) == 3) {
474
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
475
                $id = substr($sub, 0, strpos($sub, $arr[2]));
476
            }
477
        }
478
        return $id;
479
    }
480
481
    /**
482
     * Get the encrypted Solr core name
483
     *
484
     * @access protected
485
     *
486
     * @return string with encrypted core name
487
     */
488
    protected function getEncryptedCoreName()
489
    {
490
        // Get core name.
491
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
492
        // Encrypt core name.
493
        if (!empty($name)) {
494
            $name = Helper::encrypt($name);
495
        }
496
        return $name;
497
    }
498
}
499