From 34672d67ed0fce0d0211656be8aeca53e1931d0a Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Thu, 2 Dec 2021 19:16:53 +0100 Subject: [PATCH] Bug 29597: Add tomarcplugin option to bulkmarcimport.pl Add tomarcplugin option to bulkmarcimport script. To test: 1) Place ToMarcExample.pm in a Koha plugin directory at Koha/Plugin/ToMarcExample.pm and make sure its enabled and installed in Koha 2) Import some biblio with ./bulkmarcimport.pl -v -b -file biblios.mrc -insert -update -c=MARC21 -n=1 -tomarcplugin="Koha::Plugin::ToMarcExample" 3) For the imported biblio, check that field 590a has been populated with the value 'ToMarcExample added field' Sponsored-by: Gothenburg University Library --- misc/migration_tools/bulkmarcimport.pl | 66 ++++++++++++++++++-------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/misc/migration_tools/bulkmarcimport.pl b/misc/migration_tools/bulkmarcimport.pl index 6e04841071..f094cf3ae5 100755 --- a/misc/migration_tools/bulkmarcimport.pl +++ b/misc/migration_tools/bulkmarcimport.pl @@ -45,6 +45,7 @@ use Koha::Logger; use Koha::Biblios; use Koha::SearchEngine; use Koha::SearchEngine::Search; +use Koha::Plugins::Handler; use open qw( :std :encoding(UTF-8) ); binmode(STDOUT, ":encoding(UTF-8)"); @@ -79,6 +80,7 @@ my $framework = ''; my $localcust; my $marc_mod_template = ''; my $marc_mod_template_id = -1; +my $to_marc_plugin; $| = 1; GetOptions( @@ -115,6 +117,7 @@ GetOptions( 'framework=s' => \$framework, 'custom:s' => \$localcust, 'marcmodtemplate:s' => \$marc_mod_template, + 'tomarcplugin:s' => \$to_marc_plugin, ); $biblios ||= !$authorities; @@ -254,26 +257,47 @@ my $searcher = Koha::SearchEngine::Search->new( print "Characteristic MARC flavour: $marc_flavour\n" if $verbose; my $starttime = gettimeofday; -my $fh = IO::File->new($input_marc_file); # don't let MARC::Batch open the file, as it applies the ':utf8' IO layer -if (defined $format && $format =~ /XML/i) { - # ugly hack follows -- MARC::File::XML, when used by MARC::Batch, - # appears to try to convert incoming XML records from MARC-8 - # to UTF-8. Setting the BinaryEncoding key turns that off - # TODO: see what happens to ISO-8859-1 XML files. - # TODO: determine if MARC::Batch can be fixed to handle - # XML records properly -- it probably should be - # be using a proper push or pull XML parser to - # extract the records, not using regexes to look - # for .*. - $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8'; - my $recordformat = ($marc_flavour eq "MARC21" ? "USMARC" : uc($marc_flavour)); - #UNIMARC Authorities have a different way to manage encoding than UNIMARC biblios. - $recordformat = $recordformat . "AUTH" if ($authorities and $marc_flavour ne "MARC21"); - $MARC::File::XML::_load_args{RecordFormat} = $recordformat; - $batch = MARC::Batch->new('XML', $fh); -} -else { +if ($to_marc_plugin) { + my $marc_records_blob = undef; + { + local $/ = undef; + open FILE, $input_marc_file; + binmode FILE, ':raw'; + $marc_records_blob = ; + close FILE; + } + $marc_records_blob = Koha::Plugins::Handler->run( + { + class => $to_marc_plugin, + method => 'to_marc', + params => { data => $marc_records_blob } + } + ); + open(my $fh, '<', \$marc_records_blob); + binmode $fh, ':raw'; $batch = MARC::Batch->new('USMARC', $fh); +} else { + my $fh = IO::File->new($input_marc_file); # don't let MARC::Batch open the file, as it applies the ':utf8' IO layer + if (defined $format && $format =~ /XML/i) { + # ugly hack follows -- MARC::File::XML, when used by MARC::Batch, + # appears to try to convert incoming XML records from MARC-8 + # to UTF-8. Setting the BinaryEncoding key turns that off + # TODO: see what happens to ISO-8859-1 XML files. + # TODO: determine if MARC::Batch can be fixed to handle + # XML records properly -- it probably should be + # be using a proper push or pull XML parser to + # extract the records, not using regexes to look + # for .*. + $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8'; + my $recordformat = ($marc_flavour eq "MARC21" ? "USMARC" : uc($marc_flavour)); + #UNIMARC Authorities have a different way to manage encoding than UNIMARC biblios. + $recordformat = $recordformat . "AUTH" if ($authorities and $marc_flavour ne "MARC21"); + $MARC::File::XML::_load_args{RecordFormat} = $recordformat; + $batch = MARC::Batch->new('XML', $fh); + } + else { + $batch = MARC::Batch->new('USMARC', $fh); + } } $batch->warnings_off(); @@ -962,6 +986,10 @@ 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<-tomarcplugin>=I + +Fully qualified class name of plugin with to_marc method for processing raw MARC data. + =back =cut -- 2.20.1