| Total Complexity | 103 |
| Total Lines | 1025 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ExternalMetadataImportController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExternalMetadataImportController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 = '') |
||
| 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(), |
||
|
|
|||
| 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(), |
||
| 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()); |
||
| 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) { |
||
| 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) { |
||
| 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); |
||
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * |
||
| 786 | */ |
||
| 787 | protected function showImportedDocuments() |
||
| 788 | { |
||
| 789 | $this->session->setStoredAction($this->getCurrentAction(), $this->getCurrentController(), |
||
| 790 | $this->uriBuilder->getRequest()->getRequestUri() |
||
| 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() |
||
| 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 = []) |
||
| 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() |
||
| 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'); |
||
| 1065 | } |
||
| 1066 | } |
||
| 1067 | } |
||
| 1068 |
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.