Skip to content

Commit 3b36a6e

Browse files
authored
[FINNA-3824] Add support for deleting unseen records after import. (#181)
1 parent f06f33f commit 3b36a6e

File tree

3 files changed

+70
-49
lines changed

3 files changed

+70
-49
lines changed

src/RecordManager/Base/Command/Records/Harvest.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -446,53 +446,4 @@ protected function doExecute(InputInterface $input, OutputInterface $output)
446446
}
447447
return $returnCode;
448448
}
449-
450-
/**
451-
* Set deleted all records that were not "seen" during harvest
452-
*
453-
* Uses the 'date' field that only gets updated when a record is received.
454-
*
455-
* @param string $source Record source
456-
* @param int $dateThreshold Date threshold for deletion
457-
*
458-
* @return void
459-
*/
460-
protected function markUnseenRecordsDeleted(
461-
string $source,
462-
int $dateThreshold
463-
): void {
464-
$this->logger->logInfo('harvest', 'Marking unseen records deleted');
465-
466-
$count = 0;
467-
$this->db->iterateRecords(
468-
[
469-
'source_id' => $source,
470-
'deleted' => false,
471-
'date' => [
472-
'$lt' =>
473-
$this->db->getTimestamp($dateThreshold),
474-
],
475-
],
476-
[],
477-
function ($record) use (&$count, $source, $dateThreshold) {
478-
if (!empty($record['oai_id'])) {
479-
$this->deleteByOaiId(
480-
$source,
481-
$record['oai_id'],
482-
$dateThreshold
483-
);
484-
} else {
485-
$this->markRecordDeleted($record);
486-
}
487-
488-
if (++$count % 1000 == 0) {
489-
$this->logger->logInfo(
490-
'harvest',
491-
"Deleted $count records"
492-
);
493-
}
494-
}
495-
);
496-
$this->logger->logInfo('harvest', "Deleted $count records");
497-
}
498449
}

src/RecordManager/Base/Command/Records/Import.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Component\Console\Output\OutputInterface;
3838

3939
use function array_slice;
40+
use function in_array;
4041
use function strlen;
4142

4243
/**
@@ -75,6 +76,12 @@ protected function configure()
7576
null,
7677
InputOption::VALUE_NONE,
7778
'Mark the imported records deleted'
79+
)->addOption(
80+
'delete-unseen',
81+
null,
82+
InputOption::VALUE_REQUIRED,
83+
'Set to true to mark deleted the records that were not in the imported file. Set to force to mark the'
84+
. ' records deleted even if no records were imported.'
7885
);
7986
}
8087

@@ -91,6 +98,9 @@ protected function doExecute(InputInterface $input, OutputInterface $output)
9198
$source = $input->getArgument('source');
9299
$files = $input->getArgument('file');
93100
$delete = $input->getOption('delete');
101+
$deleteUnseen = $input->getOption('delete-unseen');
102+
103+
$dateThreshold = time();
94104

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

127137
$this->logger->logInfo('import', "Total $count records loaded");
138+
139+
if (in_array($deleteUnseen, ['true', 'force'])) {
140+
if ($count === 0 && 'force' !== $deleteUnseen) {
141+
$this->logger->logInfo(
142+
'import',
143+
"No records imported -- skipping marking records deleted in '$source'"
144+
);
145+
} else {
146+
$this->markUnseenRecordsDeleted($source, $dateThreshold);
147+
}
148+
}
149+
128150
return Command::SUCCESS;
129151
}
130152

src/RecordManager/Base/Command/StoreRecordTrait.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,52 @@ function ($record) use (&$count, $oaiID) {
396396
);
397397
return $count;
398398
}
399+
400+
/**
401+
* Set deleted all records that were not "seen" during harvest
402+
*
403+
* Uses the 'date' field that only gets updated when a record is received.
404+
*
405+
* @param string $source Record source
406+
* @param int $dateThreshold Date threshold for deletion
407+
*
408+
* @return void
409+
*/
410+
protected function markUnseenRecordsDeleted(
411+
string $source,
412+
int $dateThreshold
413+
): void {
414+
$count = 0;
415+
$classParts = explode('\\', static::class);
416+
$funcName = strtolower(end($classParts));
417+
$this->logger->logInfo($funcName, "Marking unseen records deleted in '$source'");
418+
419+
$this->db->iterateRecords(
420+
[
421+
'source_id' => $source,
422+
'deleted' => false,
423+
'date' => [
424+
'$lt' =>
425+
$this->db->getTimestamp($dateThreshold),
426+
],
427+
],
428+
[],
429+
function ($record) use (&$count, $source, $dateThreshold, $funcName) {
430+
if (!empty($record['oai_id'])) {
431+
$this->deleteByOaiId(
432+
$source,
433+
$record['oai_id'],
434+
$dateThreshold
435+
);
436+
} else {
437+
$this->markRecordDeleted($record);
438+
}
439+
440+
if (++$count % 1000 == 0) {
441+
$this->logger->logInfo($funcName, "Deleted $count records");
442+
}
443+
}
444+
);
445+
$this->logger->logInfo($funcName, "Deleted $count records");
446+
}
399447
}

0 commit comments

Comments
 (0)