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 ( 6b982b...98311f )
by
unknown
04:19
created

ToolboxController::isFullTextEmpty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 3
eloc 6
c 4
b 2
f 0
nc 3
nop 0
dl 0
loc 10
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\AbstractDocument;
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
19
/**
20
 * Controller class for plugin 'Toolbox'.
21
 *
22
 * @package TYPO3
23
 * @subpackage dlf
24
 *
25
 * @access public
26
 */
27
class ToolboxController extends AbstractController
28
{
29
30
    /**
31
     * @access private
32
     * @var AbstractDocument This holds the current document
33
     */
34
    private AbstractDocument $currentDocument;
35
36
    /**
37
     * The main method of the plugin
38
     *
39
     * @access public
40
     *
41
     * @return void
42
     */
43
    public function mainAction(): void
44
    {
45
        // Load current document.
46
        $this->loadDocument();
47
48
        $this->view->assign('double', $this->requestData['double']);
49
50
        if (!$this->isDocMissingOrEmpty()) {
51
            $this->currentDocument = $this->document->getCurrentDocument();
52
        }
53
54
        $this->renderTools();
55
        $this->view->assign('viewData', $this->viewData);
56
    }
57
58
    /**
59
     * Renders tool in the toolbox.
60
     *
61
     * @access private
62
     *
63
     * @return void
64
     */
65
    private function renderTools(): void
66
    {
67
        if (!empty($this->settings['tools'])) {
68
69
            $tools = explode(',', $this->settings['tools']);
70
71
            foreach ($tools as $tool) {
72
                switch ($tool) {
73
                    case 'tx_dlf_annotationtool':
74
                    case 'annotationtool':
75
                        $this->renderToolByName('renderAnnotationTool');
76
                        break;
77
                    case 'tx_dlf_fulltextdownloadtool':
78
                    case 'fulltextdownloadtool':
79
                        $this->renderToolByName('renderFulltextDownloadTool');
80
                        break;
81
                    case 'tx_dlf_fulltexttool':
82
                    case 'fulltexttool':
83
                        $this->renderToolByName('renderFulltextTool');
84
                        break;
85
                    case 'tx_dlf_imagedownloadtool':
86
                    case 'imagedownloadtool':
87
                        $this->renderToolByName('renderImageDownloadTool');
88
                        break;
89
                    case 'tx_dlf_imagemanipulationtool':
90
                    case 'imagemanipulationtool':
91
                        $this->renderToolByName('renderImageManipulationTool');
92
                        break;
93
                    case 'tx_dlf_modeldownloadtool':
94
                    case 'modeldownloadtool':
95
                        $this->renderToolByName('renderModelDownloadTool');
96
                        break;
97
                    case 'tx_dlf_pdfdownloadtool':
98
                    case 'pdfdownloadtool':
99
                        $this->renderToolByName('renderPdfDownloadTool');
100
                        break;
101
                    case 'tx_dlf_searchindocumenttool':
102
                    case 'searchindocumenttool':
103
                        $this->renderToolByName('renderSearchInDocumentTool');
104
                        break;
105
                    case 'scoretool':
106
                        $this->renderToolByName('renderScoreTool');
107
                        break;
108
                    default:
109
                        $this->logger->warning('Incorrect tool configuration: "' . $this->settings['tools'] . '". Tool "' . $tool . '" does not exist.');
110
                }
111
            }
112
        }
113
    }
114
115
    /**
116
     * Renders tool by the name in the toolbox.
117
     *
118
     * @access private
119
     *
120
     * @param string $tool name
121
     *
122
     * @return void
123
     */
124
    private function renderToolByName(string $tool): void
125
    {
126
        $this->$tool();
127
        $this->view->assign($tool, true);
128
    }
129
130
    /**
131
     * Get image's URL and MIME type information's.
132
     *
133
     * @access private
134
     *
135
     * @param int $page Page number
136
     *
137
     * @return array Array of image information's.
138
     */
139
    public function getImage(int $page): array
140
    {
141
        // Get @USE value of METS fileGroup.
142
        $image = $this->getFile($page, GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']));
143
        switch ($image['mimetype']) {
144
            case 'image/jpeg':
145
                $image['mimetypeLabel'] = ' (JPG)';
146
                break;
147
            case 'image/tiff':
148
                $image['mimetypeLabel'] = ' (TIFF)';
149
                break;
150
            default:
151
                $image['mimetypeLabel'] = '';
152
        }
153
        return $image;
154
    }
155
156
    /**
157
     * Renders the annotation tool (used in template)
158
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
159
     *
160
     * @access private
161
     *
162
     * @return void
163
     */
164
    private function renderAnnotationTool(): void
165
    {
166
        if ($this->isDocMissingOrEmpty()) {
167
            // Quit without doing anything if required variables are not set.
168
            return;
169
        }
170
171
        $this->setPage();
172
173
        $annotationContainers = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$this->requestData['page']]]['annotationContainers'];
174
        if (
175
            $annotationContainers != null
176
            && count($annotationContainers) > 0
177
        ) {
178
            $this->view->assign('annotationTool', true);
179
        } else {
180
            $this->view->assign('annotationTool', false);
181
        }
182
    }
183
184
    /**
185
     * Renders the fulltext download tool (used in template)
186
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
187
     *
188
     * @access private
189
     *
190
     * @return void
191
     */
192
    private function renderFulltextDownloadTool(): void
193
    {
194
        if (
195
            $this->isDocMissingOrEmpty()
196
            || empty($this->extConf['files']['fileGrpFulltext'])
197
        ) {
198
            // Quit without doing anything if required variables are not set.
199
            return;
200
        }
201
202
        $this->setPage();
203
204
        // Get text download.
205
        $this->view->assign('fulltextDownload', !$this->isFullTextEmpty());
206
    }
207
208
    /**
209
     * Renders the fulltext tool (used in template)
210
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
211
     *
212
     * @access private
213
     *
214
     * @return void
215
     */
216
    private function renderFulltextTool(): void
217
    {
218
        if (
219
            $this->isDocMissingOrEmpty()
220
            || empty($this->extConf['files']['fileGrpFulltext'])
221
        ) {
222
            // Quit without doing anything if required variables are not set.
223
            return;
224
        }
225
226
        $this->setPage();
227
228
        if (!$this->isFullTextEmpty()) {
229
            $this->view->assign('fulltext', true);
230
            $this->view->assign('activateFullTextInitially', MathUtility::forceIntegerInRange($this->settings['activateFullTextInitially'], 0, 1, 0));
231
        } else {
232
            $this->view->assign('fulltext', false);
233
        }
234
    }
235
236
    /**
237
     * Renders the score tool
238
     *
239
     * @return void
240
     */
241
    public function renderScoreTool()
242
    {
243
        if (
244
            $this->isDocMissingOrEmpty()
245
            || empty($this->extConf['files']['fileGrpScore'])
246
        ) {
247
            // Quit without doing anything if required variables are not set.
248
            return;
249
        }
250
251
        if ($this->requestData['page']) {
252
            $currentPhysPage = $this->document->getCurrentDocument()->physicalStructure[$this->requestData['page']];
253
        } else {
254
            $currentPhysPage = $this->document->getCurrentDocument()->physicalStructure[1];
255
        }
256
257
        $fileGrpsScores = GeneralUtility::trimExplode(',', $this->extConf['files']['fileGrpScore']);
258
        foreach ($fileGrpsScores as $fileGrpScore) {
259
            if ($this->document->getCurrentDocument()->physicalStructureInfo[$currentPhysPage]['files'][$fileGrpScore]) {
260
                $scoreFile = $this->document->getCurrentDocument()->physicalStructureInfo[$currentPhysPage]['files'][$fileGrpScore];
261
            }
262
        }
263
        if (!empty($scoreFile)) {
264
            $this->view->assign('score', true);
265
            $this->view->assign('activateScoreInitially', MathUtility::forceIntegerInRange($this->settings['activateScoreInitially'], 0, 1, 0));
266
        } else {
267
            $this->view->assign('score', false);
268
        }
269
    }
270
271
    /**
272
     * Renders the image download tool
273
     * Renders the image download tool (used in template)
274
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
275
     *
276
     * @access private
277
     *
278
     * @return void
279
     */
280
    private function renderImageDownloadTool(): void
281
    {
282
        if (
283
            $this->isDocMissingOrEmpty()
284
            || empty($this->settings['fileGrpsImageDownload'])
285
        ) {
286
            // Quit without doing anything if required variables are not set.
287
            return;
288
        }
289
290
        $this->setPage();
291
292
        $imageArray = [];
293
        // Get left or single page download.
294
        $imageArray[0] = $this->getImage($this->requestData['page']);
295
        if ($this->requestData['double'] == 1) {
296
            $imageArray[1] = $this->getImage($this->requestData['page'] + 1);
297
        }
298
299
        $this->view->assign('imageDownload', $imageArray);
300
    }
301
302
    /**
303
     * Get file's URL and MIME type
304
     *
305
     * @access private
306
     *
307
     * @param int $page Page number
308
     *
309
     * @return array Array of file information
310
     */
311
    private function getFile(int $page, array $fileGrps): array
312
    {
313
        $file = [];
314
        while ($fileGrp = @array_pop($fileGrps)) {
315
            $physicalStructureInfo = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$page]];
316
            $fileId = $physicalStructureInfo['files'][$fileGrp];
317
            if (!empty($fileId)) {
318
                $file['url'] = $this->currentDocument->getDownloadLocation($fileId);
319
                $file['mimetype'] = $this->currentDocument->getFileMimeType($fileId);
320
            } else {
321
                $this->logger->warning('File not found in fileGrp "' . $fileGrp . '"');
322
            }
323
        }
324
        return $file;
325
    }
326
327
    /**
328
     * Renders the image manipulation tool (used in template)
329
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
330
     *
331
     * @access private
332
     *
333
     * @return void
334
     */
335
    private function renderImageManipulationTool(): void
336
    {
337
        // Set parent element for initialization.
338
        $parentContainer = !empty($this->settings['parentContainer']) ? $this->settings['parentContainer'] : '.tx-dlf-imagemanipulationtool';
339
340
        $this->view->assign('imageManipulation', true);
341
        $this->view->assign('parentContainer', $parentContainer);
342
    }
343
344
    /**
345
     * Renders the model download tool
346
     * Renders the model download tool (used in template)
347
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
348
     *
349
     * @access private
350
     *
351
     * @return void
352
     */
353
    private function renderModelDownloadTool(): void
354
    {
355
        if (
356
            $this->isDocMissingOrEmpty()
357
            || empty($this->settings['fileGrpsModelDownload'])
358
        ) {
359
            // Quit without doing anything if required variables are not set.
360
            return;
361
        }
362
363
        $this->setPage();
364
365
        $this->view->assign('modelDownload', $this->getFile($this->requestData['page'], GeneralUtility::trimExplode(',', $this->settings['fileGrpsModelDownload'])));
366
    }
367
368
    /**
369
     * Renders the PDF download tool (used in template)
370
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
371
     *
372
     * @access private
373
     *
374
     * @return void
375
     */
376
    private function renderPdfDownloadTool(): void
377
    {
378
        if (
379
            $this->isDocMissingOrEmpty()
380
            || empty($this->extConf['files']['fileGrpDownload'])
381
        ) {
382
            // Quit without doing anything if required variables are not set.
383
            return;
384
        }
385
386
        $this->setPage();
387
388
        // Get single page downloads.
389
        $this->view->assign('pageLinks', $this->getPageLink());
390
        // Get work download.
391
        $this->view->assign('workLink', $this->getWorkLink());
392
    }
393
394
    /**
395
     * Get page's download link
396
     *
397
     * @access private
398
     *
399
     * @return array Link to downloadable page
400
     */
401
    private function getPageLink(): array
402
    {
403
        $firstPageLink = '';
404
        $secondPageLink = '';
405
        $pageLinkArray = [];
406
        $pageNumber = $this->requestData['page'];
407
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['files']['fileGrpDownload']);
408
        // Get image link.
409
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
410
            $firstFileGroupDownload = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$pageNumber]]['files'][$fileGrpDownload];
411
            if (!empty($firstFileGroupDownload)) {
412
                $firstPageLink = $this->currentDocument->getFileLocation($firstFileGroupDownload);
413
                // Get second page, too, if double page view is activated.
414
                $secondFileGroupDownload = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$pageNumber + 1]]['files'][$fileGrpDownload];
415
                if (
416
                    $this->requestData['double']
417
                    && $pageNumber < $this->currentDocument->numPages
418
                    && !empty($secondFileGroupDownload)
419
                ) {
420
                    $secondPageLink = $this->currentDocument->getFileLocation($secondFileGroupDownload);
421
                }
422
                break;
423
            }
424
        }
425
        if (
426
            empty($firstPageLink)
427
            && empty($secondPageLink)
428
        ) {
429
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['files']['fileGrpDownload'] . '"');
430
        }
431
432
        if (!empty($firstPageLink)) {
433
            $pageLinkArray[0] = $firstPageLink;
434
        }
435
        if (!empty($secondPageLink)) {
436
            $pageLinkArray[1] = $secondPageLink;
437
        }
438
        return $pageLinkArray;
439
    }
440
441
    /**
442
     * Get work's download link
443
     *
444
     * @access private
445
     *
446
     * @return string Link to downloadable work
447
     */
448
    private function getWorkLink(): string
449
    {
450
        $workLink = '';
451
        $fileGrpsDownload = GeneralUtility::trimExplode(',', $this->extConf['files']['fileGrpDownload']);
452
        // Get work link.
453
        while ($fileGrpDownload = array_shift($fileGrpsDownload)) {
454
            $fileGroupDownload = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[0]]['files'][$fileGrpDownload];
455
            if (!empty($fileGroupDownload)) {
456
                $workLink = $this->currentDocument->getFileLocation($fileGroupDownload);
457
                break;
458
            } else {
459
                $details = $this->currentDocument->getLogicalStructure($this->currentDocument->toplevelId);
460
                if (!empty($details['files'][$fileGrpDownload])) {
461
                    $workLink = $this->currentDocument->getFileLocation($details['files'][$fileGrpDownload]);
462
                    break;
463
                }
464
            }
465
        }
466
        if (empty($workLink)) {
467
            $this->logger->warning('File not found in fileGrps "' . $this->extConf['files']['fileGrpDownload'] . '"');
468
        }
469
        return $workLink;
470
    }
471
472
    /**
473
     * Renders the searchInDocument tool (used in template)
474
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
475
     *
476
     * @access private
477
     *
478
     * @return void
479
     */
480
    private function renderSearchInDocumentTool(): void
481
    {
482
        if (
483
            $this->isDocMissingOrEmpty()
484
            || empty($this->extConf['files']['fileGrpFulltext'])
485
            || empty($this->settings['solrcore'])
486
        ) {
487
            // Quit without doing anything if required variables are not set.
488
            return;
489
        }
490
491
        $this->setPage();
492
493
        // Quit if no fulltext file is present
494
        if ($this->isFullTextEmpty()) {
495
            return;
496
        }
497
498
        $viewArray = [
499
            'labelQueryUrl' => $this->settings['queryInputName'],
500
            'labelStart' => $this->settings['startInputName'],
501
            'labelId' => $this->settings['idInputName'],
502
            'labelPid' => $this->settings['pidInputName'],
503
            'labelPageUrl' => $this->settings['pageInputName'],
504
            'labelHighlightWord' => $this->settings['highlightWordInputName'],
505
            'labelEncrypted' => $this->settings['encryptedInputName'],
506
            'documentId' => $this->getCurrentDocumentId(),
507
            'documentPageId' => $this->document->getPid(),
508
            'solrEncrypted' => $this->getEncryptedCoreName() ? : ''
509
        ];
510
511
        $this->view->assign('searchInDocument', $viewArray);
512
    }
513
514
    /**
515
     * Get current document id. As default the uid will be used.
516
     * In case there is defined documentIdUrlSchema then the id will
517
     * extracted from this URL.
518
     *
519
     * @access private
520
     *
521
     * @return string with current document id
522
     */
523
    private function getCurrentDocumentId(): string
524
    {
525
        $id = $this->document->getUid();
0 ignored issues
show
Bug introduced by
The method getUid() 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

525
        /** @scrutinizer ignore-call */ 
526
        $id = $this->document->getUid();

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...
526
527
        if ($id !== null && $id > 0) {
528
            // we found the document uid
529
            return (string) $id;
530
        } else {
531
            $id = $this->requestData['id'];
532
            if (!GeneralUtility::isValidUrl($id)) {
533
                // we found no valid URI --> something unexpected we cannot search within.
534
                return '';
535
            }
536
        }
537
538
        // example: https://host.de/items/*id*/record
539
        if (!empty($this->settings['documentIdUrlSchema'])) {
540
            $arr = explode('*', $this->settings['documentIdUrlSchema']);
541
542
            if (count($arr) == 2) {
543
                $id = explode($arr[0], $id)[0];
544
            } else if (count($arr) == 3) {
545
                $sub = substr($id, strpos($id, $arr[0]) + strlen($arr[0]), strlen($id));
546
                $id = substr($sub, 0, strpos($sub, $arr[2]));
547
            }
548
        }
549
        return $id;
550
    }
551
552
    /**
553
     * Get the encrypted Solr core name
554
     *
555
     * @access private
556
     *
557
     * @return string with encrypted core name
558
     */
559
    private function getEncryptedCoreName(): string
560
    {
561
        // Get core name.
562
        $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores');
563
        // Encrypt core name.
564
        if (!empty($name)) {
565
            $name = Helper::encrypt($name);
566
        }
567
        return $name;
568
    }
569
570
    /**
571
     * Check if the full text is empty.
572
     *
573
     * @access private
574
     *
575
     * @return bool true if empty, false otherwise
576
     */
577
    private function isFullTextEmpty(): bool
578
    {
579
        $fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['files']['fileGrpFulltext']);
580
        while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
581
            $files = $this->currentDocument->physicalStructureInfo[$this->currentDocument->physicalStructure[$this->requestData['page']]]['files'];
582
            if (!empty($files[$fileGrpFulltext])) {
583
                return false;
584
            }
585
        }
586
        return true;
587
    }
588
}
589