From a7d012bf9b817ba4f459724b3e8512fb94de1940 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Tue, 18 Feb 2020 12:41:55 +0100 Subject: [PATCH] Bug 24679: Allow to apply a MARC modification template in export tool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test plan: 1. Create a MARC modification template with a simple action (for instance, "Add new field 999$9 with value foobar") 2. Go to Tools ยป Export data 3. Select some records (for instance, from biblionumber 1 to 10) 4. In options, select the MARC modification template you just created 5. Select File format 'MARC' 6. Click on 'Export bibliographic records' 7. Verify that exported records contain the 999$9 field 8. Repeat steps 3-7 with other export formats, and with authority records Signed-off-by: Devinim --- C4/Record.pm | 19 +++-- Koha/Exporter/Record.pm | 18 +++- .../prog/en/modules/tools/export.tt | 22 +++++ t/db_dependent/Exporter/Record.t | 82 ++++++++++++++++++- tools/export.pl | 5 ++ 5 files changed, 139 insertions(+), 7 deletions(-) diff --git a/C4/Record.pm b/C4/Record.pm index 7a81bb86806..9aeba345654 100644 --- a/C4/Record.pm +++ b/C4/Record.pm @@ -29,6 +29,7 @@ use Unicode::Normalize qw( NFC ); # _entity_encode use C4::Biblio qw( GetFrameworkCode ); use C4::Koha; #marc2csv use C4::XSLT; +use C4::MarcModificationTemplates; use YAML::XS; #marcrecords2csv use Encode; use Template; @@ -420,10 +421,12 @@ C<$csvprofileid> - the id of the CSV profile to use for the export (see export_f C<$itemnumbers> - a list of itemnumbers to export +C<$marc_modification_template_id> - MARC modification template to apply + =cut sub marc2csv { - my ( $biblios, $id, $itemnumbers ) = @_; + my ( $biblios, $id, $itemnumbers, $marc_modification_template_id ) = @_; $itemnumbers ||= []; my $output; my $csv = Text::CSV::Encoded->new( { formula => "empty" } ); @@ -443,12 +446,12 @@ sub marc2csv { for my $itemnumber (@$itemnumbers) { my $item = Koha::Items->find($itemnumber); my $biblionumber = $item->biblio->biblionumber; - $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] ) // ''; + $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber], $marc_modification_template_id ) // ''; $firstpass = 0; } } else { foreach my $biblio (@$biblios) { - $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing ) // ''; + $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing, undef, $marc_modification_template_id ) // ''; $firstpass = 0; } } @@ -461,7 +464,7 @@ sub marc2csv { =head2 marcrecord2csv - Convert a single record from UNIMARC to CSV - my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header); + my ($csv) = marcrecord2csv($biblio, $csvprofileid, $header, $csv, $fieldprocessing, $itemnumbers, $marc_modification_template_id); Returns a CSV scalar @@ -477,10 +480,12 @@ C<$fieldprocessing> C<$itemnumbers> a list of itemnumbers to export +C<$marc_modification_template_id> - MARC modification template to apply + =cut sub marcrecord2csv { - my ( $biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers ) = @_; + my ( $biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers, $marc_modification_template_id ) = @_; my $output; # Getting the record @@ -489,6 +494,10 @@ sub marcrecord2csv { my $record = eval { $biblio->metadata->record( { embed_items => 1, itemnumbers => $itemnumbers } ); }; return unless $record; + if ($marc_modification_template_id) { + C4::MarcModificationTemplates::ModifyRecordWithTemplate($marc_modification_template_id, $record); + } + # Getting the framework my $frameworkcode = $biblio->frameworkcode; diff --git a/Koha/Exporter/Record.pm b/Koha/Exporter/Record.pm index 4d818e1614c..1031ec460d7 100644 --- a/Koha/Exporter/Record.pm +++ b/Koha/Exporter/Record.pm @@ -7,6 +7,7 @@ use MARC::File::USMARC; use C4::AuthoritiesMarc; use C4::Biblio qw( GetMarcFromKohaField ); use C4::Record; +use C4::MarcModificationTemplates; use Koha::Biblios; use Koha::CsvProfiles; use Koha::Logger; @@ -166,6 +167,7 @@ sub export { my $dont_export_fields = $params->{dont_export_fields}; my $csv_profile_id = $params->{csv_profile_id}; my $output_filepath = $params->{output_filepath}; + my $marc_modification_template_id = $params->{marc_modification_template_id}; if ( !$record_type ) { Koha::Logger->get->warn("No record_type given."); @@ -193,6 +195,11 @@ sub export { Koha::Logger->get->info($msg); next; } + + if ($marc_modification_template_id) { + C4::MarcModificationTemplates::ModifyRecordWithTemplate($marc_modification_template_id, $record); + } + print $record->as_usmarc(); } } elsif ( $format eq 'xml' ) { @@ -205,6 +212,11 @@ sub export { for my $record_id (@$record_ids) { my $record = _get_record_for_export( { %$params, record_id => $record_id } ); next unless $record; + + if ($marc_modification_template_id) { + C4::MarcModificationTemplates::ModifyRecordWithTemplate($marc_modification_template_id, $record); + } + print MARC::File::XML::record($record); print "\n"; } @@ -213,7 +225,7 @@ sub export { } 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 ); + print marc2csv( $record_ids, $csv_profile_id, $itemnumbers, $marc_modification_template_id ); } close $fh if $output_filepath; @@ -274,6 +286,10 @@ It will displays on STDOUT the generated file. If the format is csv, you have to define a csv_profile_id. +=item marc_modification_template_id + + Apply MARC modification template to records before export + =cut =back diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt index 10afc5c9be6..b59525db44c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt @@ -151,6 +151,17 @@ separate by a blank. (e.g., 100a 200 606) + [% IF marc_modification_templates.size > 0 %] +
  • + + +
  • + [% END %] @@ -238,6 +249,17 @@
    separate by a blank. (e.g., 100a 200 606)
    + [% IF marc_modification_templates.size > 0 %] +
  • + + +
  • + [% END %] diff --git a/t/db_dependent/Exporter/Record.t b/t/db_dependent/Exporter/Record.t index 0dbfb84016f..5d1cf83df97 100755 --- a/t/db_dependent/Exporter/Record.t +++ b/t/db_dependent/Exporter/Record.t @@ -18,7 +18,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 7; +use Test::More tests => 10; use Test::Warn; use t::lib::TestBuilder; @@ -436,4 +436,84 @@ subtest '_get_record_for_export MARC field conditions' => sub { is( $record, undef, "Record condition \"not_exists(035a)\" should not match" ); }; +subtest 'export csv with marc modification template' => sub { + plan tests => 1; + my $csv_content = qq{Title=$title_field\$$title_subfield|Test=999\$9}; + $dbh->do(q|INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)|, {}, "TEST_PROFILE_Records.t", "my useless desc", $csv_content, '|', ';', ',', 'utf8', 'marc'); + my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef ); + + my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); + C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); + + my $generated_csv_file = '/tmp/test_export_1.csv'; + Koha::Exporter::Record::export({ + record_type => 'bibs', + record_ids => [ $biblionumber_1 ], + format => 'csv', + csv_profile_id => $csv_profile_id, + marc_modification_template_id => $template_id, + output_filepath => $generated_csv_file, + }); + + my $expected_csv = < sub { + plan tests => 2; + + my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); + C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); + + my $generated_xml_file = '/tmp/test_export.xml'; + Koha::Exporter::Record::export({ + record_type => 'bibs', + record_ids => [ $biblionumber_1 ], + format => 'xml', + marc_modification_template_id => $template_id, + output_filepath => $generated_xml_file, + }); + + my $generated_xml_content = read_file( $generated_xml_file ); + $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8'; + open my $fh, '<', $generated_xml_file; + my $records = MARC::Batch->new( 'XML', $fh ); + my @records; + while ( my $record = $records->next ) { + push @records, $record; + } + is( scalar( @records ), 1, 'Export XML: 1 record should have been exported' ); + my $record = $records[0]; + is( $record->subfield('999', '9'), 'Foobar', 'Export XML: marc modification template should have added 999$9' ); +}; + +subtest 'export iso2709 with marc modification template' => sub { + plan tests => 2; + + my $template_id = C4::MarcModificationTemplates::AddModificationTemplate('test exporter'); + C4::MarcModificationTemplates::AddModificationTemplateAction($template_id, 'add_field', 0, '999', '9', 'Foobar'); + + my $generated_mrc_file = '/tmp/test_export.mrc'; + Koha::Exporter::Record::export({ + record_type => 'bibs', + record_ids => [ $biblionumber_1 ], + format => 'iso2709', + marc_modification_template_id => $template_id, + output_filepath => $generated_mrc_file, + }); + + my $records = MARC::File::USMARC->in( $generated_mrc_file ); + my @records; + while ( my $record = $records->next ) { + push @records, $record; + } + is( scalar( @records ), 1, 'Export ISO2709: 1 record should have been exported' ); + my $record = $records[0]; + is( $record->subfield('999', '9'), 'Foobar', 'Export ISO2709: marc modification template should have added 999$9' ); +}; + $schema->storage->txn_rollback; diff --git a/tools/export.pl b/tools/export.pl index 922e9b21854..77c856d8652 100755 --- a/tools/export.pl +++ b/tools/export.pl @@ -22,6 +22,7 @@ use MARC::File::XML; use List::MoreUtils qw( uniq ); use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); +use C4::MarcModificationTemplates; use Koha::Authority::Types; use Koha::Biblioitems; @@ -235,6 +236,7 @@ if ( $op eq "cud-export" ) { csv_profile_id => $csv_profile_id, export_items => ( not $dont_export_items ), only_export_items_for_branches => $only_export_items_for_branches, + marc_modification_template_id => scalar $query->param('marc_modification_template_id'), } ); } elsif ( $record_type eq 'db' or $record_type eq 'conf' ) { @@ -316,6 +318,8 @@ if ( $op eq "cud-export" ) { $template->{VARS}->{'conffiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'tar' } ); } + my @marc_modification_templates = C4::MarcModificationTemplates::GetModificationTemplates(); + $template->param( libraries => $libraries, itemtypes => $itemtypes, @@ -323,6 +327,7 @@ if ( $op eq "cud-export" ) { export_remove_fields => C4::Context->preference("ExportRemoveFields"), csv_profiles => [ Koha::CsvProfiles->search( { type => 'marc', used_for => 'export_records' } )->as_list ], messages => \@messages, + marc_modification_templates => \@marc_modification_templates, ); output_html_with_http_headers $query, $cookie, $template->output; -- 2.39.5