Passed
Pull Request — master (#195)
by
unknown
19:02
created

bulkSearchCrossRefAction()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 38
c 0
b 0
f 0
nc 12
nop 1
dl 0
loc 59
rs 8.6897

How to fix   Long Method   

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
namespace EWW\Dpf\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Domain\Model\CrossRefMetadata;
18
use EWW\Dpf\Domain\Model\Document;
19
use EWW\Dpf\Domain\Model\PubMedMetadata;
20
use EWW\Dpf\Security\Security;
21
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
22
use TYPO3\CMS\Core\Messaging\AbstractMessage;
23
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
24
use EWW\Dpf\Domain\Model\ExternalMetadata;
25
use EWW\Dpf\Services\ImportExternalMetadata\Importer;
26
use EWW\Dpf\Services\ImportExternalMetadata\FileImporter;
27
use EWW\Dpf\Services\ImportExternalMetadata\CrossRefImporter;
28
use EWW\Dpf\Services\ImportExternalMetadata\DataCiteImporter;
29
use EWW\Dpf\Services\ImportExternalMetadata\PubMedImporter;
30
use EWW\Dpf\Services\ImportExternalMetadata\K10plusImporter;
31
use EWW\Dpf\Session\BulkImportSessionData;
32
use EWW\Dpf\Services\ImportExternalMetadata\BibTexFileImporter;
33
use EWW\Dpf\Services\ImportExternalMetadata\RisWosFileImporter;
34
use EWW\Dpf\Services\ImportExternalMetadata\RisReader;
35
36
37
/**
38
 * ExternalDataImportController
39
 */
40
class ExternalMetadataImportController extends AbstractController
41
{
42
    /**
43
     * ExternalMetadataRepository
44
     *
45
     * @var \EWW\Dpf\Domain\Repository\ExternalMetadataRepository
46
     * @inject
47
     */
48
    protected $externalMetadataRepository = null;
49
50
    /**
51
     * documentRepository
52
     *
53
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
54
     * @inject
55
     */
56
    protected $documentRepository = null;
57
58
    /**
59
     * documentTypeRepository
60
     *
61
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
62
     * @inject
63
     */
64
    protected $documentTypeRepository = null;
65
66
    /**
67
     * persistence manager
68
     *
69
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
70
     * @inject
71
     */
72
    protected $persistenceManager;
73
74
    /**
75
     * elasticSearch
76
     *
77
     * @var \EWW\Dpf\Services\ElasticSearch\ElasticSearch
78
     * @inject
79
     */
80
    protected $elasticSearch = null;
81
82
    /**
83
     * queryBuilder
84
     *
85
     * @var \EWW\Dpf\Services\ElasticSearch\QueryBuilder
86
     * @inject
87
     */
88
    protected $queryBuilder = null;
89
90
    /**
91
     * bookmarkRepository
92
     *
93
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
94
     * @inject
95
     */
96
    protected $bookmarkRepository = null;
97
98
    /**
99
     * workflow
100
     *
101
     * @var \EWW\Dpf\Domain\Workflow\DocumentWorkflow
102
     */
103
    protected $workflow;
104
105
    /**
106
     * metadataGroupRepository
107
     *
108
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
109
     * @inject
110
     */
111
    protected $metadataGroupRepository;
112
113
114
    /**
115
     * DocumentController constructor.
116
     */
117
    public function __construct()
118
    {
119
        parent::__construct();
120
    }
121
122
    /**
123
     * @param string $crossRefQuery
124
     * @param string $pubMedQuery
125
     */
126
    public function bulkStartAction($crossRefQuery = '', $pubMedQuery = '')
127
    {
128
        /** @var BulkImportSessionData $bulkImportSessionData */
129
        $bulkImportSessionData = $this->session->getBulkImportData();
130
131
        $crossRefAuthorSearch = $bulkImportSessionData->getCrossRefSearchField() === 'author';
132
        $pubMedAuthorSearch = $bulkImportSessionData->getPubMedSearchField() === 'author';
133
134
        $this->externalMetadataRepository->clearExternalMetadataByFeUserUid($this->security->getUser()->getUid());
135
        $this->view->assign('crossRefAuthorSearch', $crossRefAuthorSearch);
136
        $this->view->assign('pubMedAuthorSearch', $pubMedAuthorSearch);
137
        $this->view->assign('crossRefQuery', $crossRefQuery);
138
        $this->view->assign('pubMedQuery', $pubMedQuery);
139
    }
140
141
    /**
142
     * @param string $query
143
     */
144
    public function bulkSearchCrossRefAction($query = '')
145
    {
146
        /** @var BulkImportSessionData $bulkImportSessionData */
147
        $bulkImportSessionData = $this->session->getBulkImportData();
148
149
        $currentPage = null;
150
        $pagination = $this->getParametersSafely('@widget_0');
151
        if ($pagination) {
152
            $currentPage = $pagination['currentPage'];
153
            $query = $bulkImportSessionData->getCrossRefQuery();
154
        } else {
155
            if (empty($query)) {
156
                $this->redirect('bulkStart');
157
            }
158
159
            $bulkImportSessionData->setCrossRefQuery($query);
160
            $currentPage = 1;
161
        }
162
163
        $offset = empty($currentPage)? 0 : ($currentPage-1) * $this->itemsPerPage();
164
165
        /** @var Importer $importer */
166
        $importer = $this->objectManager->get(CrossRefImporter::class);
167
        $results = $importer->search(
168
            $query,
169
            $this->itemsPerPage(),
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Services\ImportE...data\Importer::search() has too many arguments starting with $this->itemsPerPage(). ( Ignorable by Annotation )

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

169
        /** @scrutinizer ignore-call */ 
170
        $results = $importer->search(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
170
            $offset,
171
            $bulkImportSessionData->getCrossRefSearchField()
172
        );
173
174
        $bulkImportSessionData->setCurrentMetadataItems(($results? $results['items'] : []));
175
        $this->session->setBulkImportData($bulkImportSessionData);
176
177
        if ($results) {
178
            $this->forward(
179
                'bulkResults',
180
                null,
181
                null,
182
                [
183
                    'results' => $results,
184
                    'query' => $query,
185
                    'importSourceName' => 'Crossref',
186
                    'currentPage' => $currentPage
187
                ]
188
            );
189
        } else {
190
191
            $message = LocalizationUtility::translate(
192
                'manager.importMetadata.nothingFound', 'dpf'
193
            );
194
195
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
196
197
            $this->forward(
198
                'bulkStart',
199
                null,
200
                null,
201
                [
202
                    'crossRefQuery' => $bulkImportSessionData->getCrossRefQuery()
203
                ]
204
            );
205
        }
206
    }
207
208
    /**
209
     * @param string $query
210
     */
211
    public function bulkSearchPubMedAction($query = '')
212
    {
213
        /** @var BulkImportSessionData $bulkImportSessionData */
214
        $bulkImportSessionData = $this->session->getBulkImportData();
215
216
        $currentPage = null;
217
        $pagination = $this->getParametersSafely('@widget_0');
218
        if ($pagination) {
219
            $currentPage = $pagination['currentPage'];
220
            $query = $bulkImportSessionData->getPubMedQuery();
221
        } else {
222
            if (empty($query)) {
223
                $this->redirect('bulkStart');
224
            }
225
226
            $bulkImportSessionData->setPubMedQuery($query);
227
            $currentPage = 1;
228
        }
229
230
        $offset = empty($currentPage)? 0 : ($currentPage-1) * $this->itemsPerPage();
231
232
        /** @var Importer $importer */
233
        $importer = $this->objectManager->get(PubMedImporter::class);
234
        $results = $importer->search(
235
            $query,
236
            $this->itemsPerPage(),
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Services\ImportE...data\Importer::search() has too many arguments starting with $this->itemsPerPage(). ( Ignorable by Annotation )

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

236
        /** @scrutinizer ignore-call */ 
237
        $results = $importer->search(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
237
            $offset,
238
            $bulkImportSessionData->getPubMedSearchField()
239
        );
240
241
        $bulkImportSessionData->setCurrentMetadataItems(($results? $results['items'] : []));
242
        $this->session->setBulkImportData($bulkImportSessionData);
243
244
        if ($results) {
245
            $this->forward(
246
                'bulkResults',
247
                null,
248
                null,
249
                [
250
                    'results' => $results,
251
                    'query' => $query,
252
                    'importSourceName' => 'PubMed',
253
                    'currentPage' => $currentPage
254
                ]
255
            );
256
        } else {
257
258
            $message = LocalizationUtility::translate(
259
                'manager.importMetadata.nothingFound', 'dpf'
260
            );
261
262
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
263
264
            $this->forward(
265
                'bulkStart',
266
                null,
267
                null,
268
                [
269
                    'pubMedQuery' => $bulkImportSessionData->getPubMedQuery()
270
                ]
271
            );
272
        }
273
    }
274
275
    /**
276
     * @param string $importSourceName
277
     * @param string $query
278
     * @param array $results
279
     * @param int $currentPage
280
     */
281
    public function bulkResultsAction($importSourceName, $query, $results = null, $currentPage = 1)
282
    {
283
        $externalMetadata = $this->externalMetadataRepository->findByFeUser($this->security->getUser()->getUid());
0 ignored issues
show
Bug introduced by
The method findByFeUser() does not exist on EWW\Dpf\Domain\Repositor...ernalMetadataRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

283
        /** @scrutinizer ignore-call */ 
284
        $externalMetadata = $this->externalMetadataRepository->findByFeUser($this->security->getUser()->getUid());
Loading history...
284
        $checkedPublicationIdentifiers = [];
285
286
        /** @var ExternalMetadata $data */
287
        foreach ($externalMetadata as $data) {
288
            $checkedPublicationIdentifiers[] = $data->getPublicationIdentifier();
289
        }
290
291
        $this->view->assign('importSourceName', $importSourceName);
292
        $this->view->assign('totalResults', $results['total-results']);
293
        $this->view->assign('itemsPerPage', $this->itemsPerPage());
294
        $this->view->assign('currentPage', $currentPage);
295
        $this->view->assign('query', $query);
296
        $this->view->assign('checkedPublicationIdentifiers', $checkedPublicationIdentifiers);
297
        $this->view->assign('results', $results);
298
    }
299
300
    /**
301
     *
302
     */
303
    function bulkImportAction()
304
    {
305
        $importCounter = ['imported' => 0, 'bookmarked' => 0, 'total' => 0];
306
307
        try {
308
            $externalMetadata = $this->externalMetadataRepository->findByFeUser($this->security->getUser()->getUid());
309
310
            $importedDocuments = [];
311
            $importedDocumentIdentifiers = [];
312
313
            /** @var ExternalMetadata $externalMetadataItem */
314
            foreach ($externalMetadata as $externalMetadataItem) {
315
316
                /** @var  Importer $importer */
317
                $importer = $this->objectManager->get($externalMetadataItem->getSource());
318
319
                // Check if the publication already exists in kitodo
320
                if ($this->findDocumentInKitodo($externalMetadataItem->getPublicationIdentifier())) {
321
                    $existingWorkspaceDocument = $this->findDocumentInWorkspace(
322
                        $externalMetadataItem->getPublicationIdentifier()
323
                    );
324
                    if (empty($existingWorkspaceDocument)) {
325
                        $this->bookmarkRepository->addBookmark(
326
                            $existingWorkspaceDocument['_id'],
327
                            $this->security->getUser()->getUid()
328
                        );
329
                        $importCounter['bookmarked'] += 1;
330
                    }
331
332
                } else {
333
334
                    if (!$this->findDocumentInWorkspace($externalMetadataItem->getPublicationIdentifier())) {
335
                        /** @var Document $newDocument */
336
                        $newDocument = $importer->import($externalMetadataItem);
337
338
                        if ($newDocument instanceof Document) {
339
                            $this->documentRepository->add($newDocument);
340
                            $this->externalMetadataRepository->remove($externalMetadataItem);
341
                            $importedDocuments[] = $newDocument;
342
                            $importCounter['imported'] += 1;
343
                        }
344
                    }
345
                }
346
            }
347
348
            $this->persistenceManager->persistAll();
349
350
            // Documents can only be indexed after they have been persisted as we need a valid UID.
351
            /** @var Document $importedDocument */
352
            foreach ($importedDocuments as $importedDocument) {
353
                // index the document
354
                $this->signalSlotDispatcher->dispatch(
355
                    AbstractController::class, 'indexDocument', [$importedDocument]
356
                );
357
                $importedDocumentIdentifiers[] = $importedDocument->getDocumentIdentifier();
358
            }
359
360
            /** @var BulkImportSessionData $bulkImportSessionData */
361
            $bulkImportSessionData = $this->session->getBulkImportData();
362
            $bulkImportSessionData->setLatestImportIdentifiers($importedDocumentIdentifiers);
363
            $this->session->setBulkImportData($bulkImportSessionData);
364
365
        } catch(\Throwable $throwable) {
366
            $this->logger->error($throwable->getMessage());
367
368
            $message = LocalizationUtility::translate(
369
                'manager.importMetadata.publicationNotImported', 'dpf'
370
            );
371
372
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
373
        }
374
375
        $this->redirect(
376
            'bulkImportedDocuments',
377
            null,
378
            null,
379
            ['from' => 0, 'importCounter' => $importCounter]);
380
    }
381
382
    /**
383
     * Cancels the bulk import result list view.
384
     *
385
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
386
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
387
     */
388
    function cancelBulkImportAction()
389
    {
390
        $this->redirect('bulkStart');
391
    }
392
393
394
    /**
395
     * Shows the form to find a publication by an identifier
396
     * @param string $identifier
397
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
398
     */
399
    public function findAction($identifier = '')
400
    {
401
        $this->externalMetadataRepository->clearExternalMetadataByFeUserUid($this->security->getUser()->getUid());
402
        $this->view->assign('identifier', $identifier);
403
    }
404
405
    /**
406
     * Retrieves and caches the the metadata related to the given identifier.
407
     *
408
     * @param string $identifier
409
     */
410
    public function retrieveAction($identifier)
411
    {
412
        $identifier = trim($identifier);
413
414
        if (empty($identifier)) {
415
            $this->redirect('find');
416
        }
417
418
        // Check if the document alreday exists in the workspace or my publications
419
        if ($this->findDocumentInWorkspace($identifier)) {
420
            if ($this->security->getUser()->getUserRole() == Security::ROLE_LIBRARIAN) {
421
                $message = LocalizationUtility::translate(
422
                    'manager.importMetadata.alreadyInWorkspace', 'dpf'
423
                );
424
            } else {
425
                $message = LocalizationUtility::translate(
426
                    'manager.importMetadata.alreadyInMyPublications', 'dpf'
427
                );
428
            }
429
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
430
431
            $this->redirect('find', null, null, ['identifier' => $identifier]);
432
        }
433
434
        // Check if the document already exists in kitodo.
435
        /** @var array $existingDocument */
436
        if ($existingDocument = $this->findDocumentInKitodo($identifier)) {
437
438
            $this->bookmarkRepository->addBookmark(
439
                $existingDocument['_id'],
440
                $this->security->getUser()->getUid()
441
            );
442
443
            if ($this->security->getUser()->getUserRole() == Security::ROLE_LIBRARIAN) {
444
                $message = LocalizationUtility::translate(
445
                    'manager.importMetadata.alreadyInSystemWorkspace', 'dpf'
446
                );
447
            } else {
448
                $message = LocalizationUtility::translate(
449
                    'manager.importMetadata.alreadyInSystemMyPublications', 'dpf'
450
                );
451
            }
452
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
453
454
            $this->redirect('find', null, null, ['identifier' => $identifier]);
455
        }
456
457
        /** @var \EWW\Dpf\Services\ImportExternalMetadata\Importer $importer */
458
        $importer = null;
459
460
        // Choose the right data provider depending on the identifier type and retrieve the metadata.
461
        $identifierType = $this->determineIdentifierType($identifier);
462
463
        if ($identifierType === 'DOI') {
464
            $importer = $this->objectManager->get(CrossRefImporter::class);
465
            $externalMetadata = $importer->findByIdentifier($identifier);
466
            if (!$externalMetadata) {
467
                $importer = $this->objectManager->get(DataCiteImporter::class);
468
                $externalMetadata = $importer->findByIdentifier($identifier);
469
            }
470
        } elseif ($identifierType === 'PMID') {
471
            $importer = $this->objectManager->get(PubMedImporter::class);
472
            $externalMetadata = $importer->findByIdentifier($identifier);
473
        } elseif ($identifierType === 'ISBN') {
474
            $importer = $this->objectManager->get(K10plusImporter::class);
475
            $externalMetadata = $importer->findByIdentifier(str_replace('- ', '', $identifier));
476
        } else {
477
            $externalMetadata = null;
478
        }
479
480
        if ($externalMetadata) {
481
            // Save the metadata for further processing
482
            $this->externalMetadataRepository->add($externalMetadata);
483
            $this->persistenceManager->persistAll();
484
        }
485
486
        if ($externalMetadata) {
487
            $this->redirect(
488
                'import',
489
                null,
490
                null,
491
                ['externalMetadata'=>$externalMetadata]
492
            );
493
        } else {
494
            $message = LocalizationUtility::translate(
495
                'manager.importMetadata.nothingFound', 'dpf'
496
            );
497
498
            $this->addFlashMessage(
499
                $message,
500
                '',
501
                AbstractMessage::ERROR
502
            );
503
            $this->redirect('find', null, null, ['identifier' => $identifier]);
504
        }
505
506
    }
507
508
    /**
509
     * The import dialog
510
     *
511
     * @param ExternalMetadata $externalMetadata
512
     */
513
    public function importAction(ExternalMetadata $externalMetadata)
514
    {
515
        $this->view->assign('identifierType', $this->determineIdentifierType($externalMetadata->getPublicationIdentifier()));
516
        $this->view->assign('externalMetadata', $externalMetadata);
517
    }
518
519
    /**
520
     * @param ExternalMetadata $externalMetadata
521
     */
522
    public function createDocumentAction(ExternalMetadata $externalMetadata)
523
    {
524
        /** @var  Importer $importer */
525
        $importer = $this->objectManager->get($externalMetadata->getSource());
526
527
528
        try {
529
            /** @var Document $newDocument */
530
            $newDocument = $importer->import($externalMetadata);
531
532
            if ($newDocument instanceof Document) {
0 ignored issues
show
introduced by
$newDocument is always a sub-type of EWW\Dpf\Domain\Model\Document.
Loading history...
533
534
                $this->documentRepository->add($newDocument);
535
                $this->persistenceManager->persistAll();
536
537
                // index the document
538
                $this->signalSlotDispatcher->dispatch(
539
                    AbstractController::class, 'indexDocument', [$newDocument]
540
                );
541
542
                $this->externalMetadataRepository->remove($externalMetadata);
543
544
                if ($this->security->getUser()->getUserRole() == Security::ROLE_LIBRARIAN) {
545
                    $message = LocalizationUtility::translate(
546
                        'manager.importMetadata.publicationAddedToWorkspace', 'dpf'
547
                    );
548
                } else {
549
                    $message = LocalizationUtility::translate(
550
                        'manager.importMetadata.publicationAddedToMyPublications', 'dpf'
551
                    );
552
                }
553
                $this->addFlashMessage($message, '', AbstractMessage::OK);
554
555
                $this->redirect('showDetails', 'Document', null, ['document' => $newDocument]);
556
557
            } else {
558
                $message = LocalizationUtility::translate(
559
                    'manager.importMetadata.publicationNotImported', 'dpf'
560
                );
561
562
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
563
                $this->redirect('find');
564
            }
565
566
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $e) {
567
            // A redirect always throws this exception, but in this case, however,
568
            // redirection is desired and should not lead to an exception handling
569
        } catch(\Throwable $throwable) {
570
571
            $this->logger->error($throwable->getMessage());
572
573
            $message = LocalizationUtility::translate(
574
                'manager.importMetadata.publicationNotImported', 'dpf'
575
            );
576
577
           $this->addFlashMessage($message, '', AbstractMessage::ERROR);
578
           $this->redirect('find');
579
        }
580
    }
581
582
    /**
583
     * Determines whether the identifier is a DOI, ISBN or PMID.
584
     *
585
     * @param $identifier
586
     * @return null|string
587
     */
588
    protected function determineIdentifierType($identifier)
589
    {
590
        // DOI
591
        if (strpos($identifier,'10.') === 0) {
592
            return 'DOI';
593
        }
594
595
        // ISBN
596
        $length = strlen(str_replace(['-',' '], '', $identifier));
597
598
        if ($length === 13) {
599
            if (strpos($identifier, '978') === 0 ||  strpos($identifier, '979') === 0) {
600
                return 'ISBN';
601
            }
602
        }
603
604
        if ($length === 10) {
605
            return 'ISBN';
606
        }
607
608
        // PMID
609
        if (is_numeric($identifier) && intval($identifier) == $identifier) {
610
            if (strlen($identifier) < 10) {
611
                return 'PMID';
612
            }
613
        }
614
615
        return null;
616
    }
617
618
    /**
619
     * Finds a document with the given $identifier in the current users "Workspace" or "My publicstions"
620
     *
621
     * @param $identifier
622
     * @return array
623
     */
624
    protected function findDocumentInWorkspace($identifier)
625
    {
626
        $bookmarkIdentifiers = [];
627
        foreach ($this->bookmarkRepository->findByFeUserUid($this->security->getUser()->getUid()) as $bookmark) {
0 ignored issues
show
Bug introduced by
The method findByFeUserUid() does not exist on EWW\Dpf\Domain\Repository\BookmarkRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

627
        foreach ($this->bookmarkRepository->/** @scrutinizer ignore-call */ findByFeUserUid($this->security->getUser()->getUid()) as $bookmark) {
Loading history...
628
            $bookmarkIdentifiers[] = $bookmark->getDocumentIdentifier();
629
        }
630
631
        if ($this->security->getUser()->getUserRole() == Security::ROLE_LIBRARIAN) {
632
            // "Workspace" of a librarian
633
            $workspaceFilter = [
634
                'bool' => [
635
                    'must' => [
636
                        [
637
                            'bool' => [
638
                                'must' => [
639
                                    [
640
                                        'term' => [
641
                                            'creator' => $this->security->getUser()->getUid()
642
                                        ]
643
                                    ],
644
                                    [
645
                                        'bool' => [
646
                                            'should' => [
647
                                                [
648
                                                    'term' => [
649
                                                        'state' => DocumentWorkflow::STATE_NEW_NONE
650
                                                    ]
651
                                                ]
652
                                            ]
653
                                        ]
654
                                    ]
655
                                ]
656
                            ]
657
                        ]
658
                    ]
659
                ]
660
            ];
661
        } else {
662
            // "My publications" of a researcher
663
            $workspaceFilter = [
664
                'bool' => [
665
                    'must' => [
666
                        [
667
                            'term' => [
668
                                'creator' => $this->security->getUser()->getUid()
669
                            ]
670
                        ]
671
                    ]
672
                ]
673
            ];
674
        }
675
676
        $query = $this->queryBuilder->buildQuery(
677
            1, $workspaceFilter, 0,
678
            $bookmarkIdentifiers, [], [], null, null,
679
            'identifier:"'.$identifier.'"'
680
        );
681
682
        try {
683
684
            $results =  $this->elasticSearch->search($query, 'object');
685
            if (is_array($results) && $results['hits']['total']['value'] > 0) {
686
                return $results['hits']['hits'][0];
687
            }
688
689
        } catch (\Exception $e) {
690
691
            $message = LocalizationUtility::translate(
692
                'manager.importMetadata.searchError', 'dpf'
693
            );
694
695
            $this->addFlashMessage(
696
                $message, '', AbstractMessage::ERROR
697
            );
698
        }
699
700
        return [];
701
    }
702
703
    /**
704
     * Finds a document with the given $identifier in the kitodo index
705
     *
706
     * @param $identifier
707
     * @return array
708
     */
709
    protected function findDocumentInKitodo($identifier) {
710
711
        $workspaceFilter = [
712
            'bool' => [
713
                'must_not' => [
714
                    [
715
                        'term' => [
716
                            'state' => DocumentWorkflow::STATE_NEW_NONE
717
                        ]
718
                    ]
719
                ]
720
            ]
721
        ];
722
723
        // Search if the document already exists in kitodo.
724
        $query = $this->queryBuilder->buildQuery(
725
            1, $workspaceFilter , 0, [], [], [], null, null, 'identifier:"'.$identifier.'"'
726
        );
727
        $results = $this->elasticSearch->search($query, 'object');
728
729
        if (is_array($results) && $results['hits']['total']['value'] > 0) {
730
            return $results['hits']['hits'][0];
731
        }
732
733
        return [];
734
    }
735
736
    /**
737
     * @param array $importCounter
738
     */
739
    public function bulkImportedDocumentsAction($importCounter = ['imported' => 0, 'bookmarked' => 0, 'total' => 0])
740
    {
741
742
        $publicationSingular = LocalizationUtility::translate('manager.importMetadata.publication.singular', 'dpf');
743
        $publicationPlural = LocalizationUtility::translate('manager.importMetadata.publication.plural', 'dpf');
744
745
        if ($this->security->getUser()->getUserRole() === Security::ROLE_LIBRARIAN) {
746
            $messageKey = 'manager.bulkImport.importMessage.libarian';
747
        } else {
748
            $messageKey = 'manager.bulkImport.importMessage.researcher';
749
        }
750
751
        $message = LocalizationUtility::translate(
752
            $messageKey,
753
            'dpf',
754
            [
755
                0 => $importCounter['imported'],
756
                1 => ($importCounter['imported'] == 1? $publicationSingular : $publicationPlural),
757
                2 => $importCounter['bookmarked'],
758
                3 => ($importCounter['bookmarked'] == 1? $publicationSingular : $publicationPlural)
759
            ]
760
        );
761
762
        if ($importCounter['imported'] > 0 || $importCounter['bookmarked'] > 0) {
763
            $severity = AbstractMessage::INFO;
764
        } else {
765
            $severity = AbstractMessage::WARNING;
766
        }
767
768
        $this->addFlashMessage(
769
            $message, '', $severity
770
        );
771
772
        if ($importCounter['imported'] > 0 || $importCounter['bookmarked'] > 0) {
773
            if ($this->security->getUser()->getUserRole() != Security::ROLE_LIBRARIAN) {
774
                $importNoteMessage = LocalizationUtility::translate('manager.bulkImport.importNote', 'dpf');
775
                $this->addFlashMessage(
776
                    $importNoteMessage, '', AbstractMessage::INFO
777
                );
778
            }
779
        }
780
781
        $this->showImportedDocuments($importCounter);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Controller\Exter...showImportedDocuments() has too many arguments starting with $importCounter. ( Ignorable by Annotation )

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

781
        $this->/** @scrutinizer ignore-call */ 
782
               showImportedDocuments($importCounter);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
782
    }
783
784
    /**
785
     *
786
     */
787
    protected function showImportedDocuments()
788
    {
789
        $this->session->setStoredAction($this->getCurrentAction(), $this->getCurrentController(),
790
            $this->uriBuilder->getRequest()->getRequestUri()
0 ignored issues
show
introduced by
The method getRequestUri() does not exist on TYPO3\CMS\Extbase\Mvc\Request. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

790
            $this->uriBuilder->getRequest()->/** @scrutinizer ignore-call */ getRequestUri()
Loading history...
791
        );
792
793
        $currentPage = null;
794
        $pagination = $this->getParametersSafely('@widget_0');
795
        if ($pagination) {
796
            $currentPage = $pagination['currentPage'];
797
        } else {
798
            $currentPage = 1;
799
        }
800
801
        // \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($test);
802
803
        /** @var BulkImportSessionData $bulkImportSessionData */
804
        $bulkImportSessionData = $this->session->getBulkImportData();
805
        $importedIdentifiers = $bulkImportSessionData->getLatestImportIdentifiers();
806
807
        $workspaceFilter = [
808
            'bool' => [
809
                'must' => [
810
                    [
811
                        'terms' => [
812
                            '_id' => array_values(array_filter($importedIdentifiers))
813
                        ]
814
                    ]
815
                ]
816
            ]
817
        ];
818
819
        $query = $this->queryBuilder->buildQuery(
820
            $this->itemsPerPage(),
821
            $workspaceFilter,
822
            (empty($currentPage)? 0 : ($currentPage-1) * $this->itemsPerPage())
823
        );
824
825
        try {
826
            $results = $this->elasticSearch->search($query, 'object');
827
            $this->view->assign('currentPage', $currentPage);
828
            $this->view->assign('documentCount', $results['hits']['total']['value']);
829
            $this->view->assign('documents', $results['hits']['hits']);
830
            $this->view->assign('itemsPerPage', $this->itemsPerPage());
831
            $this->view->assign('currentFisPersId', $this->security->getUser()->getFisPersId());
832
833
            $personGroup = $this->metadataGroupRepository->findPersonGroup();
834
            $this->view->assign('personGroup', $personGroup->getUid());
835
836
        } catch (\Throwable $e) {
837
838
            $message = LocalizationUtility::translate(
839
                'manager.importMetadata.searchError', 'dpf'
840
            );
841
842
            $this->addFlashMessage(
843
                $message, '', AbstractMessage::ERROR
844
            );
845
        }
846
    }
847
848
    /**
849
     * Returns the number of items to be shown per page.
850
     *
851
     * @return int
852
     */
853
    protected function itemsPerPage()
854
    {
855
        /** @var BulkImportSessionData $bulkImportSessionData */
856
        $bulkImportSessionData = $this->session->getBulkImportData();
857
858
        if ($bulkImportSessionData->getItemsPerPage()) {
859
            return $bulkImportSessionData->getItemsPerPage();
860
        }
861
862
        if ($this->settings['bulkImportPagination']['itemsPerPage']) {
863
            return $this->settings['bulkImportPagination']['itemsPerPage'];
864
        }
865
866
        return 10;
867
    }
868
869
    /**
870
     * @param string $error
871
     */
872
    public function uploadStartAction($error = '')
873
    {
874
        switch ($error) {
875
            case 'INVALID_FORMAT':
876
                $message = LocalizationUtility::translate(
877
                    'manager.uploadImport.invalidFormat', 'dpf'
878
                );
879
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
880
                break;
881
            case 'UPLOAD_ERROR':
882
                $message = LocalizationUtility::translate(
883
                    'manager.uploadImport.uploadError', 'dpf'
884
                );
885
                $this->addFlashMessage($message, '', AbstractMessage::ERROR);
886
                break;
887
        }
888
889
        $this->externalMetadataRepository->clearExternalMetadataByFeUserUid($this->security->getUser()->getUid());
890
    }
891
892
    /**
893
     * @param string $fileType (bibtex or riswos)
894
     * @param array $uploadFile
895
     */
896
    public function uploadImportFileAction($fileType, $uploadFile = [])
897
    {
898
        $this->externalMetadataRepository->clearExternalMetadataByFeUserUid($this->security->getUser()->getUid());
899
900
        $uploadFileUrl = new \EWW\Dpf\Helper\UploadFileUrl;
901
        $uploadFilePath = PATH_site . $uploadFileUrl->getDirectory() . "/importFile.".md5($this->security->getUser()->getUid());
902
903
        if ($uploadFile['error'] === UPLOAD_ERR_OK) {
904
            \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($uploadFile['tmp_name'], $uploadFilePath);
905
            //$finfo       = finfo_open(FILEINFO_MIME_TYPE);
906
            //$contentType = finfo_file($finfo, $uploadFilePath);
907
            //finfo_close($finfo);
908
        } elseif ($uploadFile['error'] == UPLOAD_ERR_NO_FILE) {
909
            $this->redirect('uploadStart');
910
        } else {
911
            $this->redirect('uploadStart', null, null, ['error' => 'UPLOAD_ERROR']);
912
        }
913
914
        try {
915
916
            if ($fileType == 'bibtex') {
917
                /** @var FileImporter $fileImporter */
918
                $fileImporter = $this->objectManager->get(BibTexFileImporter::class);
919
                $results = $fileImporter->loadFile($uploadFilePath, $this->settings['bibTexMandatoryFields']);
920
            } elseif ($fileType == 'riswos') {
921
                /** @var FileImporter $fileImporter */
922
                $fileImporter = $this->objectManager->get(RisWosFileImporter::class);
923
                $results = $fileImporter->loadFile($uploadFilePath, $this->settings['riswosMandatoryFields']);
924
            } else {
925
                $results = [];
926
            }
927
928
            foreach ($results as $externalMetadata) {
929
                $this->externalMetadataRepository->add($externalMetadata);
930
            }
931
932
            if ($mandatoryErrors = $fileImporter->getMandatoryErrors()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fileImporter does not seem to be defined for all execution paths leading up to this point.
Loading history...
933
                foreach (
934
                    $mandatoryErrors as $mandatoryError
935
                ) {
936
                    $message = 'Konnte die Publikation Nr. ' . $mandatoryError['index'] . ' nicht importieren';
937
                    $message .= $mandatoryError['title'] ? ' (' . $mandatoryError['title'] . ')' : '';
938
                    $message .= ', da die folgenden Felder leer sind: ' . implode(',', $mandatoryError['fields']);
939
                    $this->addFlashMessage($message, '', AbstractMessage::ERROR);
940
                }
941
            } elseif ($results) {
942
                    $this->redirect(
943
                        'importUploadedData',
944
                        null,
945
                        null,
946
                        ['uploadFilePath' => $uploadFilePath]
947
                    );
948
            } else {
949
                $this->redirect('uploadStart', null, null, ['error' => 'INVALID_FORMAT']);
950
            }
951
        } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $exception) {
952
            // A redirect always throws this exception, but in this case, however,
953
            // redirection is desired and should not lead to an exception handling
954
        } catch (\RenanBr\BibTexParser\Exception\ParserException $exception) {
0 ignored issues
show
Bug introduced by
The type RenanBr\BibTexParser\Exception\ParserException 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...
955
            $this->redirect('uploadStart', null, null, ['error' => 'INVALID_FORMAT']);
956
        }
957
958
    }
959
960
    /**
961
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
962
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
963
     */
964
    public function importUploadedDataAction()
965
    {
966
        try {
967
            $externalMetadata = $this->externalMetadataRepository->findByFeUser($this->security->getUser()->getUid());
968
969
            $importedDocuments = [];
970
            $importedDocumentIdentifiers = [];
971
972
            /** @var ExternalMetadata $externalMetadataItem */
973
            foreach ($externalMetadata as $externalMetadataItem) {
974
975
                /** @var  Importer $importer */
976
                $importer = $this->objectManager->get($externalMetadataItem->getSource());
977
978
                /** @var Document $newDocument */
979
                $newDocument = $importer->import($externalMetadataItem);
980
981
                if ($newDocument instanceof Document) {
982
                    $this->documentRepository->add($newDocument);
983
                    $this->externalMetadataRepository->remove($externalMetadataItem);
984
                    $importedDocuments[] = $newDocument;
985
                }
986
            }
987
988
            $this->persistenceManager->persistAll();
989
990
            // Documents can only be indexed after they have been persisted as we need a valid UID.
991
            /** @var Document $importedDocument */
992
            foreach ($importedDocuments as $importedDocument) {
993
                // index the document
994
                $this->signalSlotDispatcher->dispatch(
995
                    AbstractController::class, 'indexDocument', [$importedDocument]
996
                );
997
                $importedDocumentIdentifiers[] = $importedDocument->getDocumentIdentifier();
998
            }
999
1000
            /** @var BulkImportSessionData $bulkImportSessionData */
1001
            $bulkImportSessionData = $this->session->getBulkImportData();
1002
            $bulkImportSessionData->setLatestImportIdentifiers($importedDocumentIdentifiers);
1003
            $this->session->setBulkImportData($bulkImportSessionData);
1004
1005
        } catch(\Throwable $throwable) {
1006
1007
            throw $throwable;
1008
1009
            $this->logger->error($throwable->getMessage());
0 ignored issues
show
Unused Code introduced by
$this->logger->error($throwable->getMessage()) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1010
1011
            $message = LocalizationUtility::translate(
1012
                'manager.importMetadata.publicationNotImported', 'dpf'
1013
            );
1014
1015
            $this->addFlashMessage($message, '', AbstractMessage::ERROR);
1016
        }
1017
1018
        $this->redirect(
1019
            'uploadedDocuments',
1020
            null,
1021
            null,
1022
            ['from' => 0, 'importCounter' => sizeof($importedDocumentIdentifiers)]);
1023
    }
1024
1025
1026
    /**
1027
     * @param int $importCounter
1028
     */
1029
    public function uploadedDocumentsAction($importCounter = 0)
1030
    {
1031
        $publicationSingular = LocalizationUtility::translate('manager.importMetadata.publication.singular', 'dpf');
1032
        $publicationPlural = LocalizationUtility::translate('manager.importMetadata.publication.plural', 'dpf');
1033
1034
        if ($importCounter != 1) {
1035
            $messageKey = 'manager.uploadImport.importMessage.plural';
1036
        } else {
1037
            $messageKey = 'manager.uploadImport.importMessage.singular';
1038
        }
1039
1040
        $message = LocalizationUtility::translate(
1041
            $messageKey,
1042
            'dpf',
1043
            [
1044
                0 => $importCounter,
1045
                1 => ($importCounter == 1? $publicationSingular : $publicationPlural)
1046
            ]
1047
        );
1048
1049
        if ($importCounter > 0) {
1050
            $severity = AbstractMessage::INFO;
1051
            $this->addFlashMessage($message, '', $severity);
1052
1053
            if ($this->security->getUser()->getUserRole() != Security::ROLE_LIBRARIAN) {
1054
                $importNoteMessage = LocalizationUtility::translate('manager.uploadImport.importNote', 'dpf');
1055
                $this->addFlashMessage(
1056
                    $importNoteMessage, '', AbstractMessage::INFO
1057
                );
1058
            }
1059
            
1060
            $this->showImportedDocuments($importCounter);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Controller\Exter...showImportedDocuments() has too many arguments starting with $importCounter. ( Ignorable by Annotation )

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

1060
            $this->/** @scrutinizer ignore-call */ 
1061
                   showImportedDocuments($importCounter);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1061
        } else {
1062
            $severity = AbstractMessage::WARNING;
1063
            $this->addFlashMessage($message, '', $severity);
1064
            $this->redirect('uploadStart');
1065
        }
1066
    }
1067
}
1068