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 ( d8afc6...7d8f03 )
by
unknown
04:31
created

ToolboxController::mainAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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