From 00d4dc80ccae72a823a3bdb54ca96c7ac8951f52 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Thu, 4 Jan 2024 15:33:28 +0100 Subject: [PATCH] Respect deleted records policy --- src/Console/CsvImportCommand.php | 2 +- src/Database.php | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Console/CsvImportCommand.php b/src/Console/CsvImportCommand.php index 06417e3..311a388 100644 --- a/src/Console/CsvImportCommand.php +++ b/src/Console/CsvImportCommand.php @@ -24,7 +24,6 @@ namespace OCC\OaiPmh2\Console; use DateTime; use OCC\OaiPmh2\Database; -use OCC\OaiPmh2\Database\Record; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -160,6 +159,7 @@ class CsvImportCommand extends Command } } Database::getInstance()->flush(true); + Database::getInstance()->pruneOrphanSets(); $output->writeln([ '', diff --git a/src/Database.php b/src/Database.php index 21f4a83..e56a73f 100644 --- a/src/Database.php +++ b/src/Database.php @@ -129,21 +129,27 @@ class Database $format = $this->entityManager->getReference(Format::class, $format); } $record = $this->entityManager->find(Record::class, ['identifier' => $identifier, 'format' => $format]); - if (isset($record)) { - try { - $record->setContent($data); - $record->setLastChanged($lastChanged); - } catch (ValidationFailedException $exception) { - throw $exception; + if (!isset($data) && Configuration::getInstance()->deletedRecords === 'no') { + if (isset($record)) { + $this->entityManager->remove($record); } } else { - try { - $record = new Record($identifier, $format, $data, $lastChanged); - } catch (ValidationFailedException $exception) { - throw $exception; + if (isset($record)) { + try { + $record->setContent($data); + $record->setLastChanged($lastChanged); + } catch (ValidationFailedException $exception) { + throw $exception; + } + } else { + try { + $record = new Record($identifier, $format, $data, $lastChanged); + } catch (ValidationFailedException $exception) { + throw $exception; + } } + $this->entityManager->persist($record); } - $this->entityManager->persist($record); if (!$bulkMode) { $this->entityManager->flush(); }