From 6049df99188170ff681f23179154fbbd6b426ba0 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Wed, 5 Feb 2025 18:13:13 +0100 Subject: [PATCH] Bug 23009: Add -set_deleted_marc_conditions to export_records Add -set_deleted_marc_conditions argument to export_records script for setting deleted flag on records if condition matches. To test: 1) Run tests in t/db_dependent/Exporter/Record.t Sponsored-by: Gothenburg University Library --- Koha/Exporter/Record.pm | 18 ++++++++++++++++-- misc/export_records.pl | 13 +++++++++++++ t/db_dependent/Exporter/Record.t | 32 +++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Koha/Exporter/Record.pm b/Koha/Exporter/Record.pm index bda57efa6a4..d0766307bb3 100644 --- a/Koha/Exporter/Record.pm +++ b/Koha/Exporter/Record.pm @@ -78,6 +78,7 @@ sub _get_record_for_export { my $record_type = $params->{record_type}; my $record_id = $params->{record_id}; my $conditions = $params->{record_conditions}; + my $set_deleted_conditions = $params->{set_deleted_record_conditions}; my $dont_export_fields = $params->{dont_export_fields}; my $clean = $params->{clean}; @@ -115,8 +116,16 @@ sub _get_record_for_export { } } } - return if $conditions && !_record_match_conditions($record, $conditions); + + if ( + $record_type ne 'deleted_bibs' && + $set_deleted_conditions && + _record_match_conditions($record, $set_deleted_conditions) + ) { + _record_set_deleted($record) + } + C4::Biblio::RemoveAllNsb($record) if $clean; return $record; } @@ -144,9 +153,14 @@ sub _get_deleted_biblio_for_export { Koha::Logger->get->warn("Failed to load MARCXML for deleted biblio with biblionumber \"$biblionumber\": $@"); return; } + _record_set_deleted($record); + return $record; +} - # Set deleted flag (record status, position 05) +sub _record_set_deleted { + my ($record) = @_; my $leader = $record->leader; + # Set deleted flag (record status, position 05) substr $leader, 5, 1, 'd'; $record->leader($leader); return $record; diff --git a/misc/export_records.pl b/misc/export_records.pl index 817ed47a819..7111ed6609d 100755 --- a/misc/export_records.pl +++ b/misc/export_records.pl @@ -59,6 +59,7 @@ my ( $start_accession, $end_accession, $marc_conditions, + $set_deleted_marc_conditions, $embed_see_from_headings, $report_id, @report_params, @@ -91,6 +92,7 @@ GetOptions( 'start_accession=s' => \$start_accession, 'end_accession=s' => \$end_accession, 'marc_conditions=s' => \$marc_conditions, + 'set_deleted_marc_conditions=s' => \$set_deleted_marc_conditions, 'embed_see_from_headings' => \$embed_see_from_headings, 'report_id=s' => \$report_id, 'report_param=s' => \@report_params, @@ -181,6 +183,10 @@ my @marc_conditions; if ($marc_conditions) { @marc_conditions = _parse_marc_conditions($marc_conditions); } +my @set_deleted_marc_conditions; +if ($set_deleted_marc_conditions) { + @set_deleted_marc_conditions = _parse_marc_conditions($set_deleted_marc_conditions); +} my $dbh = C4::Context->dbh; @@ -373,6 +379,7 @@ if ($deleted_barcodes) { record_type => $record_type, record_ids => \@record_ids, record_conditions => @marc_conditions ? \@marc_conditions : undef, + set_deleted_record_conditions => @set_deleted_marc_conditions ? \@set_deleted_marc_conditions : undef, deleted_record_ids => \@deleted_record_ids, format => $output_format, csv_profile_id => $csv_profile_id, @@ -519,6 +526,12 @@ Print a brief help message. "exists()" will include marc records where no exists. +=item B<--set_deleted_marc_conditions> + + --set_deleted_marc_conditions=CONDITIONS Set record deleted flag for biblios with MARC + data matching CONDITIONS.Only include biblios with MARC data matching CONDITIONS. + See --marc_conditions for more information about the CONDITIONS format. + =item B<--embed_see_from_headings> --embed_see_from_headings Embed see from (non-preferred form) headings in bibliographic record. diff --git a/t/db_dependent/Exporter/Record.t b/t/db_dependent/Exporter/Record.t index 8ffbaf50b78..553f9f4e588 100755 --- a/t/db_dependent/Exporter/Record.t +++ b/t/db_dependent/Exporter/Record.t @@ -319,7 +319,7 @@ subtest '_get_biblio_for_export' => sub { }; subtest '_get_record_for_export MARC field conditions' => sub { - plan tests => 11; + plan tests => 13; my $biblio = $builder->build_sample_biblio( { @@ -444,6 +444,36 @@ subtest '_get_record_for_export MARC field conditions' => sub { } ); is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); + + ## Deleted conditions + + $record = Koha::Exporter::Record::_get_record_for_export( + { + record_id => $biblionumber, + set_deleted_record_conditions => [['080', 'a', '=', '12345']], + record_type => 'bibs', + } + ); + is( + substr($record->leader, 5, 1), + 'd', + "Record deleted condition \"080a=12345\" should match and deleted flag should be set" + ); + + $record = Koha::Exporter::Record::_get_record_for_export( + { + record_id => $biblionumber, + set_deleted_record_conditions => [['080', 'a', '!=', '12345']], + record_type => 'bibs', + } + ); + # Position 5, record status, should be 'n' but Koha record created by + # Koha (build_sample_biblio) for some reaso has it set it to ' ' + isnt( + substr($record->leader, 5, 1), + 'd', + "Record deleted condition \"080a!=12345\" should not match and deleted flag should not be set" + ); }; $schema->storage->txn_rollback; -- 2.48.1