From d03c9ab8cc7d2d1f4b31debb4cbf7e6f3c81f999 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Mon, 10 Feb 2020 16:58:53 +0100 Subject: [PATCH] MT26327: Allow to skip records when using bulkmarcimport.pl This patch allows to prevent records from being imported by bulkmarcimport.pl when a given value is present. Two options are added: -incomingfilter: prevent the script from importing a record when a given value is present in the record to be imported. -localfilter: prevent the script from importing a record when a given value is present in the koha record that would be replaced by the record to be imported. They both accept the following specificaton: '$=' Test plan: 1) Apply this patch 2) Launch bulkmarcimport.pl with the following option: -incomingfilter='152$b=rameau' 3) Check that the incoming records with at least a 152$b equal to 'rameau' are skipped. The following message will be displayed for each skipped record if debug is enabled: Discarding record (incoming record contains 152$b = rameau) 4) Repeat step 2 with localfilter: -localfilter='152$b=rameau' Check that incoming records are skipped when the koha record they would replace contains at least a 152$b equal to 'rameau'. 5) Launch bulkmarcimport.pl with an invalid configuration, like: -incomingfilter='incorrect_specification' or -localfilter='incorrect_specification' 6) Check that an error message is displayed and that the script exits. https://bugs.koha-community.org/show_bug.cgi?id=26235 --- misc/migration_tools/bulkmarcimport.pl | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/misc/migration_tools/bulkmarcimport.pl b/misc/migration_tools/bulkmarcimport.pl index 5bfb04c..49e00ba 100755 --- a/misc/migration_tools/bulkmarcimport.pl +++ b/misc/migration_tools/bulkmarcimport.pl @@ -26,6 +26,7 @@ use C4::Charset; use C4::Items; use C4::MarcModificationTemplates; +use Encode; use YAML; use Unicode::Normalize; use Time::HiRes qw(gettimeofday); @@ -48,6 +49,8 @@ my $framework = ''; my $localcust; my $marc_mod_template = ''; my $marc_mod_template_id = -1; +my ($incoming_filter, $local_filter); + $|=1; @@ -85,9 +88,15 @@ GetOptions( 'framework=s' => \$framework, 'custom:s' => \$localcust, 'marcmodtemplate:s' => \$marc_mod_template, + 'incomingfilter=s' => \$incoming_filter, + 'localfilter=s' => \$local_filter, ); $biblios ||= !$authorities; $insert ||= !$update; + +$incoming_filter = decode('UTF-8', $incoming_filter) if ($incoming_filter); +$local_filter = decode('UTF-8', $local_filter) if ($local_filter); + my $writemode = ($append) ? "a" : "w"; pod2usage( -msg => "\nYou must specify either --biblios or --authorities, not both.\n", -exitval ) if $biblios && $authorities; @@ -310,6 +319,23 @@ RECORD: while ( ) { # search for duplicates (based on Local-number) my $originalid; $originalid = GetRecordId( $record, $tagid, $subfieldid ); + + if ($incoming_filter) { + my ($fieldsubfieldtag, $value) = split('=', $incoming_filter); + die "Unable to get field/subfield and value to discard record (-incomingfilter=$incoming_filter)\n" unless ($fieldsubfieldtag && $value); + my ($fieldtag, $subfieldtag) = split('\$', $fieldsubfieldtag); + die "Unable to get field and subfield to discard record (-incomingfilter=$incoming_filter)\n" unless ($fieldtag && $subfieldtag); + foreach my $field ($record->field($fieldtag)) { + foreach my $subfield ($field->subfield($subfieldtag)) { + if ($subfield eq $value) { + $debug and warn "Discarding record (incoming record contains $fieldsubfieldtag = $value)\n"; + printlog({id => $id || $originalid || $match, op => "incomingfilter", status => "REJECTED"}) if ($logfile); + next RECORD; + } + } + } + } + if ($match) { require C4::Search; my $query = build_query( $match, $record ); @@ -327,6 +353,23 @@ RECORD: while ( ) { my $marcrecord = C4::Search::new_record_from_zebra( $server, $results->[0] ); SetUTF8Flag($marcrecord); $id = GetRecordId( $marcrecord, $tagid, $subfieldid ); + + if ($local_filter) { + my ($fieldsubfieldtag, $value) = split('=', $local_filter); + die "Unable to get field/subfield and value to discard record (-localfilter=$local_filter)\n" unless ($fieldsubfieldtag && $value); + my ($fieldtag, $subfieldtag) = split('\$', $fieldsubfieldtag); + die "Unable to get field and subfield to discard record (-localfilter=$local_filter)\n" unless ($fieldtag && $subfieldtag); + foreach my $field ($marcrecord->field($fieldtag)) { + foreach my $subfield ($field->subfield($subfieldtag)) { + if ($subfield eq $value) { + $debug and warn "Discarding record (koha record contains $fieldsubfieldtag = $value)\n"; + printlog({id => $id || $originalid || $match, op => "localfilter", status => "REJECTED"}) if ($logfile); + next RECORD; + } + } + } + } + if ( $authorities && $marcFlavour ) { #Skip if authority in database is the same as the on in database if ( $marcrecord->field('005') && $record->field('005') && @@ -843,6 +886,18 @@ modification template to apply as the MARC records are imported (these templates are created in the "MARC modification templates" tool in Koha). If not specified, no MARC modification templates are used (default). +=item B<-incomingfilter>='$=' + +This parameter allows you to prevent this script from importing a record when a +given value is present in the record to be imported. + +=item B<-localfilter>='$=' + +This parameter allows you to prevent this script from importing a record when a +given value is present in the koha record that would be replaced by the record +to be imported. + + =back =cut -- 2.7.4