1 package Koha::Exporter::Record;
5 use MARC::File::USMARC;
7 use C4::AuthoritiesMarc;
12 sub _get_record_for_export {
14 my $record_type = $params->{record_type};
15 my $record_id = $params->{record_id};
16 my $dont_export_fields = $params->{dont_export_fields};
17 my $clean = $params->{clean};
20 if ( $record_type eq 'auths' ) {
21 $record = _get_authority_for_export( { %$params, authid => $record_id } );
22 } elsif ( $record_type eq 'bibs' ) {
23 $record = _get_biblio_for_export( { %$params, biblionumber => $record_id } );
25 Koha::Logger->get->warn( "Record_type $record_type not supported." );
27 return unless $record;
29 if ($dont_export_fields) {
30 for my $f ( split / /, $dont_export_fields ) {
31 if ( $f =~ m/^(\d{3})(.)?$/ ) {
32 my ( $field, $subfield ) = ( $1, $2 );
34 # skip if this record doesn't have this field
35 if ( defined $record->field($field) ) {
36 if ( defined $subfield ) {
37 my @tags = $record->field($field);
38 foreach my $t (@tags) {
39 $t->delete_subfields($subfield);
42 $record->delete_fields( $record->field($field) );
48 C4::Biblio::RemoveAllNsb($record) if $clean;
52 sub _get_authority_for_export {
54 my $authid = $params->{authid} || return;
55 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
56 return unless $authority;
57 return $authority->record;
60 sub _get_biblio_for_export {
62 my $biblionumber = $params->{biblionumber};
63 my $itemnumbers = $params->{itemnumbers};
64 my $export_items = $params->{export_items} // 1;
65 my $only_export_items_for_branch = $params->{only_export_items_for_branch};
67 my $record = eval { C4::Biblio::GetMarcBiblio($biblionumber); };
69 return if $@ or not defined $record;
72 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, $itemnumbers );
73 if ($only_export_items_for_branch) {
74 my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch', '' ); # Should be GetFrameworkCode( $biblionumber )?
76 for my $itemfield ( $record->field($homebranchfield) ) {
77 my $homebranch = $itemfield->subfield($homebranchsubfield);
78 if ( $only_export_items_for_branch ne $homebranch ) {
79 $record->delete_field($itemfield);
90 my $record_type = $params->{record_type};
91 my $record_ids = $params->{record_ids} || [];
92 my $format = $params->{format};
93 my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths
94 my $export_items = $params->{export_items};
95 my $dont_export_fields = $params->{dont_export_fields};
96 my $csv_profile_id = $params->{csv_profile_id};
97 my $output_filepath = $params->{output_filepath};
100 Koha::Logger->get->warn( "No record_type given." );
103 return unless @$record_ids;
106 if ( $output_filepath ) {
107 open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
109 binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
111 binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv';
114 if ( $format eq 'iso2709' ) {
115 for my $record_id (@$record_ids) {
116 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
117 my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) };
118 if ( $errorcount_on_decode or $@ ) {
119 my $msg = "Record $record_id could not be exported. " .
122 Koha::Logger->get->info( $msg );
125 print $record->as_usmarc();
127 } elsif ( $format eq 'xml' ) {
128 my $marcflavour = C4::Context->preference("marcflavour");
129 MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour );
131 print MARC::File::XML::header();
133 for my $record_id (@$record_ids) {
134 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
136 Koha::Logger->get->info( "Record $record_id could not be exported." );
139 print MARC::File::XML::record($record);
142 print MARC::File::XML::footer();
144 } elsif ( $format eq 'csv' ) {
145 $csv_profile_id ||= C4::Csv::GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') );
146 print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
149 close $fh if $output_filepath;
158 Koha::Exporter::Records - module to export records (biblios and authorities)
162 This module provides a public subroutine to export records as xml, csv or iso2709.
168 Koha::Exporter::Record::export($params);
170 $params is a hashref with some keys:
172 It will displays on STDOUT the generated file.
178 Must be set to 'bibs' or 'auths'
182 The list of the records to export (a list of biblionumber or authid)
186 The format must be 'csv', 'xml' or 'iso2709'.
190 Generate the item infos only for these itemnumbers.
192 Must only be used with biblios.
196 If this flag is set, the items will be exported.
199 =item dont_export_fields
201 List of fields not to export.
205 If the format is csv, a csv_profile_id can be provide to overwrite the default value (syspref ExportWithCsvProfile).
213 This file is part of Koha.
215 Copyright Koha Development Team
217 Koha is free software; you can redistribute it and/or modify it
218 under the terms of the GNU General Public License as published by
219 the Free Software Foundation; either version 3 of the License, or
220 (at your option) any later version.
222 Koha is distributed in the hope that it will be useful, but
223 WITHOUT ANY WARRANTY; without even the implied warranty of
224 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
225 GNU General Public License for more details.
227 You should have received a copy of the GNU General Public License
228 along with Koha; if not, see <http://www.gnu.org/licenses>.