Bugzilla – Attachment 177536 Details for
Bug 20551
Add option for including deleted records in export_records.pl
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 20551: Move some code that might be reused into functions
Bug-20551-Move-some-code-that-might-be-reused-into.patch (text/plain), 6.23 KB, created by
David Gustafsson
on 2025-02-05 17:28:39 UTC
(
hide
)
Description:
Bug 20551: Move some code that might be reused into functions
Filename:
MIME Type:
Creator:
David Gustafsson
Created:
2025-02-05 17:28:39 UTC
Size:
6.23 KB
patch
obsolete
>From ae4f88492f42a0684c11177bcd3bf08070a3132f Mon Sep 17 00:00:00 2001 >From: David Gustafsson <david.gustafsson@ub.gu.se> >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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 20551
:
73916
|
73923
|
75113
|
82843
|
82914
|
82918
|
90184
|
90190
|
90220
|
93619
|
93620
|
93621
|
105403
|
116408
|
116409
|
116410
|
116411
|
123099
|
123101
|
123102
|
125211
|
125212
|
125213
|
125214
|
125215
|
125216
|
128895
|
128896
|
128897
|
128898
|
128899
|
128900
|
132936
|
132937
|
176623
|
176624
|
176625
|
176626
|
176627
|
176628
|
176629
|
176630
|
176631
| 177536