Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions src/RecordManager/Base/Command/Records/Harvest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,53 +446,4 @@ protected function doExecute(InputInterface $input, OutputInterface $output)
}
return $returnCode;
}

/**
* Set deleted all records that were not "seen" during harvest
*
* Uses the 'date' field that only gets updated when a record is received.
*
* @param string $source Record source
* @param int $dateThreshold Date threshold for deletion
*
* @return void
*/
protected function markUnseenRecordsDeleted(
string $source,
int $dateThreshold
): void {
$this->logger->logInfo('harvest', 'Marking unseen records deleted');

$count = 0;
$this->db->iterateRecords(
[
'source_id' => $source,
'deleted' => false,
'date' => [
'$lt' =>
$this->db->getTimestamp($dateThreshold),
],
],
[],
function ($record) use (&$count, $source, $dateThreshold) {
if (!empty($record['oai_id'])) {
$this->deleteByOaiId(
$source,
$record['oai_id'],
$dateThreshold
);
} else {
$this->markRecordDeleted($record);
}

if (++$count % 1000 == 0) {
$this->logger->logInfo(
'harvest',
"Deleted $count records"
);
}
}
);
$this->logger->logInfo('harvest', "Deleted $count records");
}
}
22 changes: 22 additions & 0 deletions src/RecordManager/Base/Command/Records/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Symfony\Component\Console\Output\OutputInterface;

use function array_slice;
use function in_array;
use function strlen;

/**
Expand Down Expand Up @@ -75,6 +76,12 @@ protected function configure()
null,
InputOption::VALUE_NONE,
'Mark the imported records deleted'
)->addOption(
'delete-unseen',
null,
InputOption::VALUE_REQUIRED,
'Set to true to mark deleted the records that were not in the imported file. Set to force to mark the'
. ' records deleted even if no records were imported.'
);
}

Expand All @@ -91,6 +98,9 @@ protected function doExecute(InputInterface $input, OutputInterface $output)
$source = $input->getArgument('source');
$files = $input->getArgument('file');
$delete = $input->getOption('delete');
$deleteUnseen = $input->getOption('delete-unseen');

$dateThreshold = time();

if (!($settings = $this->dataSourceConfig[$source] ?? null)) {
$this->logger->logFatal(
Expand Down Expand Up @@ -125,6 +135,18 @@ protected function doExecute(InputInterface $input, OutputInterface $output)
}

$this->logger->logInfo('import', "Total $count records loaded");

if (in_array($deleteUnseen, ['true', 'force'])) {
if ($count === 0 && 'force' !== $deleteUnseen) {
$this->logger->logInfo(
'import',
"No records imported -- skipping marking records deleted in '$source'"
);
} else {
$this->markUnseenRecordsDeleted($source, $dateThreshold);
}
}

return Command::SUCCESS;
}

Expand Down
48 changes: 48 additions & 0 deletions src/RecordManager/Base/Command/StoreRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,52 @@ function ($record) use (&$count, $oaiID) {
);
return $count;
}

/**
* Set deleted all records that were not "seen" during harvest
*
* Uses the 'date' field that only gets updated when a record is received.
*
* @param string $source Record source
* @param int $dateThreshold Date threshold for deletion
*
* @return void
*/
protected function markUnseenRecordsDeleted(
string $source,
int $dateThreshold
): void {
$count = 0;
$classParts = explode('\\', static::class);
$funcName = strtolower(end($classParts));
$this->logger->logInfo($funcName, "Marking unseen records deleted in '$source'");

$this->db->iterateRecords(
[
'source_id' => $source,
'deleted' => false,
'date' => [
'$lt' =>
$this->db->getTimestamp($dateThreshold),
],
],
[],
function ($record) use (&$count, $source, $dateThreshold, $funcName) {
if (!empty($record['oai_id'])) {
$this->deleteByOaiId(
$source,
$record['oai_id'],
$dateThreshold
);
} else {
$this->markRecordDeleted($record);
}

if (++$count % 1000 == 0) {
$this->logger->logInfo($funcName, "Deleted $count records");
}
}
);
$this->logger->logInfo($funcName, "Deleted $count records");
}
}