From ae4f88492f42a0684c11177bcd3bf08070a3132f Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Wed, 5 Feb 2025 17:28:16 +0100 Subject: [PATCH] Bug 20551: Move some code that might be reused into functions --- Koha/Exporter/Record.pm | 105 ++++++++++++++++++++++------------------ misc/export_records.pl | 16 ++++-- 2 files changed, 69 insertions(+), 52 deletions(-) diff --git a/Koha/Exporter/Record.pm b/Koha/Exporter/Record.pm index ed2c6cd730c..bda57efa6a4 100644 --- a/Koha/Exporter/Record.pm +++ b/Koha/Exporter/Record.pm @@ -3,6 +3,7 @@ package Koha::Exporter::Record; use Modern::Perl; use MARC::File::XML; use MARC::File::USMARC; +use Carp; use C4::AuthoritiesMarc; use C4::Biblio qw( GetMarcFromKohaField ); @@ -18,6 +19,60 @@ use List::Util qw( all any ); use MARC::Record; use MARC::File::XML; +my %marc_conditions_operators = ( + '=' => sub { + return $_[0] eq $_[1]; + }, + '!=' => sub { + return $_[0] ne $_[1]; + }, + '>' => sub { + return $_[0] gt $_[1]; + }, + '<' => sub { + return $_[0] lt $_[1]; + }, +); + +# If multiple conditions all are required to match (and) +# For matching against multiple marc targets all are also required to match +sub _record_match_conditions { + my ($record, $conditions) = @_; + + foreach my $condition (@{$conditions}) { + my ($field_tag, $subfield, $operator, $match_value) = @{$condition}; + my @fields = $record->field($field_tag); + my $no_target = 0; + + if (!@fields) { + $no_target = 1; + } + else { + if ($operator eq '?') { + return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; + } elsif ($operator eq '!?') { + return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; + } else { + my $op; + if (exists $marc_conditions_operators{$operator}) { + $op = $marc_conditions_operators{$operator}; + } else { + croak "Invalid operator: $op"; + } + my @target_values = map { $subfield ? $_->subfield($subfield) : ($_->data()) } @fields; + if (!@target_values) { + $no_target = 1; + } + else { + return unless all { $op->($_, $match_value) } @target_values; + } + } + } + return if $no_target && $operator ne '!='; + } + return 1; +} + sub _get_record_for_export { my ($params) = @_; my $record_type = $params->{record_type}; @@ -41,54 +96,6 @@ sub _get_record_for_export { return; } - # If multiple conditions all are required to match (and) - # For matching against multiple marc targets all are also required to match - my %operators = ( - '=' => sub { - return $_[0] eq $_[1]; - }, - '!=' => sub { - return $_[0] ne $_[1]; - }, - '>' => sub { - return $_[0] gt $_[1]; - }, - '<' => sub { - return $_[0] lt $_[1]; - }, - ); - if ($conditions) { - foreach my $condition ( @{$conditions} ) { - my ( $field_tag, $subfield, $operator, $match_value ) = @{$condition}; - my @fields = $record->field($field_tag); - my $no_target = 0; - - if ( !@fields ) { - $no_target = 1; - } else { - if ( $operator eq '?' ) { - return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; - } elsif ( $operator eq '!?' ) { - return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; - } else { - my $op; - if ( exists $operators{$operator} ) { - $op = $operators{$operator}; - } else { - die("Invalid operator: $op"); - } - my @target_values = map { $subfield ? $_->subfield($subfield) : ( $_->data() ) } @fields; - if ( !@target_values ) { - $no_target = 1; - } else { - return unless all { $op->( $_, $match_value ) } @target_values; - } - } - } - return if $no_target && $operator ne '!='; - } - } - if ($dont_export_fields) { for my $f ( split / /, $dont_export_fields ) { if ( $f =~ m/^(\d{3})(.)?$/ ) { @@ -108,6 +115,8 @@ sub _get_record_for_export { } } } + + return if $conditions && !_record_match_conditions($record, $conditions); C4::Biblio::RemoveAllNsb($record) if $clean; return $record; } diff --git a/misc/export_records.pl b/misc/export_records.pl index b4f1c4967d6..817ed47a819 100755 --- a/misc/export_records.pl +++ b/misc/export_records.pl @@ -34,6 +34,7 @@ use Koha::CsvProfiles; use Koha::Exporter::Record; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Reports; +use Carp; my ( $output_format, @@ -160,18 +161,25 @@ if ($report_id) { $start_accession = dt_from_string($start_accession) if $start_accession; $end_accession = dt_from_string($end_accession) if $end_accession; -# Parse marc conditions -my @marc_conditions; -if ($marc_conditions) { +sub _parse_marc_conditions { + my ($marc_conditions) = @_; + my @marc_conditions; foreach my $condition ( split( /,\s*/, $marc_conditions ) ) { if ( $condition =~ /^(\d{3})([\w\d]?)(=|(?:!=)|>|<)([^,]+)$/ ) { push @marc_conditions, [ $1, $2, $3, $4 ]; } elsif ( $condition =~ /^(exists|not_exists)\((\d{3})([\w\d]?)\)$/ ) { push @marc_conditions, [ $2, $3, $1 eq 'exists' ? '?' : '!?' ]; } else { - die("Invalid condititon: $condition"); + croak "Invalid condititon: $condition"; } } + return @marc_conditions; +} + +# Parse marc conditions +my @marc_conditions; +if ($marc_conditions) { + @marc_conditions = _parse_marc_conditions($marc_conditions); } my $dbh = C4::Context->dbh; -- 2.48.1