From b4c473222645d2562f880b3c8393faf653cdeaa4 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Thu, 27 Mar 2025 00:00:21 +0100 Subject: [PATCH] Bug 20551: Return 0 instead of undef from _get_record_for_export not to break iterator loop Signed-off-by: Andrew Fuerste Henry --- Koha/Exporter/Record.pm | 37 ++++++++++++++++---------------- t/db_dependent/Exporter/Record.t | 11 +++++----- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Koha/Exporter/Record.pm b/Koha/Exporter/Record.pm index 63e6f2c6de..0d19828624 100644 --- a/Koha/Exporter/Record.pm +++ b/Koha/Exporter/Record.pm @@ -91,7 +91,7 @@ sub _get_record_for_export { } if ( !$record ) { Koha::Logger->get->warn("Record $record_id with record type $record_type could not be exported."); - return; + return 0; } if ($dont_export_fields) { @@ -114,7 +114,7 @@ sub _get_record_for_export { } } - return if $conditions && !_record_match_conditions( $record, $conditions ); + return 0 if $conditions && !_record_match_conditions( $record, $conditions ); C4::Biblio::RemoveAllNsb($record) if $clean; return $record; } @@ -125,10 +125,10 @@ sub _get_deleted_biblio_for_export { # Creating schema is expensive, so we cache it my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); - my $resultset = $memory_cache->get_from_cache('_get_deleted_biblio_for_export:resultset'); + my $resultset = $memory_cache->get_from_cache('_get_deleted_biblio_for_export:resultset'); unless ($resultset) { $resultset = Koha::Database->new()->schema()->resultset('DeletedbiblioMetadata'); - $memory_cache->set_in_cache('_get_deleted_biblio_for_export:resultset', $resultset); + $memory_cache->set_in_cache( '_get_deleted_biblio_for_export:resultset', $resultset ); } my $marc_flavour = C4::Context->preference('marcflavour'); @@ -142,7 +142,7 @@ sub _get_deleted_biblio_for_export { unless ($biblio_metadata) { Koha::Logger->get->warn("Failed to load deleted biblio with biblionumber \"$biblionumber\""); - return + return; } my $marc_xml = $biblio_metadata->metadata; @@ -243,14 +243,13 @@ sub export { } if ( $format eq 'xml' || $format eq 'iso2709' ) { - my @record_ids = reverse @{$record_ids}; + my @record_ids = reverse @{$record_ids}; my @deleted_record_ids = reverse @{$deleted_record_ids}; my $records_iterator = sub { - if (my $record_id = pop(@record_ids)) { + if ( my $record_id = pop(@record_ids) ) { return _get_record_for_export( { %{$params}, record_id => $record_id } ); - } - elsif (my $deleted_record_id = pop(@deleted_record_ids)) { + } elsif ( my $deleted_record_id = pop(@deleted_record_ids) ) { return _get_record_for_export( { %{$params}, @@ -262,9 +261,9 @@ sub export { return; }; - if ($format eq 'iso2709') { + if ( $format eq 'iso2709' ) { my $encoding_validator = sub { - my ( $record ) = @_; + my ($record) = @_; my @decoding_warnings = eval { MARC::File::USMARC->decode( $record->as_usmarc )->warnings() }; my $error = $@; @@ -276,7 +275,7 @@ sub export { if ($field) { $msg .= " " . $field->is_control_field ? $field->data : $field->subfield($id_code); } - my $warnings = join(', ', @decoding_warnings); + my $warnings = join( ', ', @decoding_warnings ); $msg .= " could not be USMARC decoded/encoded. " . ( $error // $warnings ); chomp $msg; Koha::Logger->get->warn($msg); @@ -284,25 +283,25 @@ sub export { } return 1; }; - while (defined ( my $record = $records_iterator->() )) { - if ($encoding_validator->($record)) { - print $record->as_usmarc(); - } + while ( defined( my $record = $records_iterator->() ) ) { + next unless $record && $encoding_validator->($record); + print $record->as_usmarc(); } - } elsif ($format eq 'xml') { + } elsif ( $format eq 'xml' ) { my $marcflavour = C4::Context->preference("marcflavour"); MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); print MARC::File::XML::header(); print "\n"; - while (defined ( my $record = $records_iterator->() )) { + while ( defined( my $record = $records_iterator->() ) ) { + next unless $record; print MARC::File::XML::record($record); print "\n"; } print MARC::File::XML::footer(); print "\n"; } - } elsif ($format eq 'csv') { + } elsif ( $format eq 'csv' ) { die 'There is no valid csv profile defined for this export' unless Koha::CsvProfiles->find($csv_profile_id); print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); diff --git a/t/db_dependent/Exporter/Record.t b/t/db_dependent/Exporter/Record.t index 8947a2984f..802b8532d3 100755 --- a/t/db_dependent/Exporter/Record.t +++ b/t/db_dependent/Exporter/Record.t @@ -367,7 +367,7 @@ subtest '_get_record_for_export MARC field conditions' => sub { record_type => 'bibs', } ); - is( $record, undef, "Record condition \"080a!=12345\" should not match" ); + is( $record, 0, "Record condition \"080a!=12345\" should not match" ); $record = Koha::Exporter::Record::_get_record_for_export( { @@ -394,7 +394,7 @@ subtest '_get_record_for_export MARC field conditions' => sub { record_type => 'bibs', } ); - is( $record, undef, "Record condition \"080a>123456\" should not match" ); + is( $record, 0, "Record condition \"080a>123456\" should not match" ); ## Multiple subfields @@ -414,8 +414,7 @@ subtest '_get_record_for_export MARC field conditions' => sub { record_type => 'bibs', } ); - is( $record, undef, "Record condition \"035a=TEST(1234)\" should not match" ) - ; # Since matching all subfields required + is( $record, 0, "Record condition \"035a=TEST(1234)\" should not match" ); # Since matching all subfields required ## Multiple conditions @@ -435,7 +434,7 @@ subtest '_get_record_for_export MARC field conditions' => sub { record_type => 'bibs', } ); - is( $record, undef, "Record condition \"035a!=TEST(12345),080a<1234\" should not match" ); + is( $record, 0, "Record condition \"035a!=TEST(12345),080a<1234\" should not match" ); ## exists/not_exists @@ -456,7 +455,7 @@ subtest '_get_record_for_export MARC field conditions' => sub { record_type => 'bibs', } ); - is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); + is( $record, 0, "Record condition \"not_exists(035a)\" should not match" ); }; $schema->storage->txn_rollback; -- 2.39.5