1 package Koha::Exporter::Record;
5 use MARC::File::USMARC;
7 use C4::AuthoritiesMarc;
10 use Koha::CsvProfiles;
13 sub _get_record_for_export {
15 my $record_type = $params->{record_type};
16 my $record_id = $params->{record_id};
17 my $dont_export_fields = $params->{dont_export_fields};
18 my $clean = $params->{clean};
21 if ( $record_type eq 'auths' ) {
22 $record = _get_authority_for_export( { %$params, authid => $record_id } );
23 } elsif ( $record_type eq 'bibs' ) {
24 $record = _get_biblio_for_export( { %$params, biblionumber => $record_id } );
26 Koha::Logger->get->warn( "Record_type $record_type not supported." );
28 return unless $record;
30 if ($dont_export_fields) {
31 for my $f ( split / /, $dont_export_fields ) {
32 if ( $f =~ m/^(\d{3})(.)?$/ ) {
33 my ( $field, $subfield ) = ( $1, $2 );
35 # skip if this record doesn't have this field
36 if ( defined $record->field($field) ) {
37 if ( defined $subfield ) {
38 my @tags = $record->field($field);
39 foreach my $t (@tags) {
40 $t->delete_subfields($subfield);
43 $record->delete_fields( $record->field($field) );
49 C4::Biblio::RemoveAllNsb($record) if $clean;
53 sub _get_authority_for_export {
55 my $authid = $params->{authid} || return;
56 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
57 return unless $authority;
58 return $authority->record;
61 sub _get_biblio_for_export {
63 my $biblionumber = $params->{biblionumber};
64 my $itemnumbers = $params->{itemnumbers};
65 my $export_items = $params->{export_items} // 1;
66 my $only_export_items_for_branches = $params->{only_export_items_for_branches};
68 my $record = eval { C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber }); };
70 return if $@ or not defined $record;
73 C4::Biblio::EmbedItemsInMarcBiblio({
74 marc_record => $record,
75 biblionumber => $biblionumber,
76 item_numbers => $itemnumbers });
77 if ($only_export_items_for_branches && @$only_export_items_for_branches) {
78 my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches;
79 my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch', '' ); # Should be GetFrameworkCode( $biblionumber )?
81 for my $itemfield ( $record->field($homebranchfield) ) {
82 my $homebranch = $itemfield->subfield($homebranchsubfield);
83 unless ( $export_items_for_branches{$homebranch} ) {
84 $record->delete_field($itemfield);
95 my $record_type = $params->{record_type};
96 my $record_ids = $params->{record_ids} || [];
97 my $format = $params->{format};
98 my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths
99 my $export_items = $params->{export_items};
100 my $dont_export_fields = $params->{dont_export_fields};
101 my $csv_profile_id = $params->{csv_profile_id};
102 my $output_filepath = $params->{output_filepath};
104 if( !$record_type ) {
105 Koha::Logger->get->warn( "No record_type given." );
108 return unless @$record_ids;
111 if ( $output_filepath ) {
112 open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
114 binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
116 binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv';
119 if ( $format eq 'iso2709' ) {
120 for my $record_id (@$record_ids) {
121 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
122 my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) };
123 if ( $errorcount_on_decode or $@ ) {
124 my $msg = "Record $record_id could not be exported. " .
127 Koha::Logger->get->info( $msg );
130 print $record->as_usmarc();
132 } elsif ( $format eq 'xml' ) {
133 my $marcflavour = C4::Context->preference("marcflavour");
134 MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour );
136 print MARC::File::XML::header();
138 for my $record_id (@$record_ids) {
139 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
141 Koha::Logger->get->info( "Record $record_id could not be exported." );
144 print MARC::File::XML::record($record);
147 print MARC::File::XML::footer();
149 } elsif ( $format eq 'csv' ) {
150 die 'There is no valid csv profile defined for this export'
151 unless Koha::CsvProfiles->find( $csv_profile_id );
152 print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
155 close $fh if $output_filepath;
164 Koha::Exporter::Records - module to export records (biblios and authorities)
168 This module provides a public subroutine to export records as xml, csv or iso2709.
174 Koha::Exporter::Record::export($params);
176 $params is a hashref with some keys:
178 It will displays on STDOUT the generated file.
184 Must be set to 'bibs' or 'auths'
188 The list of the records to export (a list of biblionumber or authid)
192 The format must be 'csv', 'xml' or 'iso2709'.
196 Generate the item infos only for these itemnumbers.
198 Must only be used with biblios.
202 If this flag is set, the items will be exported.
205 =item dont_export_fields
207 List of fields not to export.
211 If the format is csv, you have to define a csv_profile_id.
219 This file is part of Koha.
221 Copyright Koha Development Team
223 Koha is free software; you can redistribute it and/or modify it
224 under the terms of the GNU General Public License as published by
225 the Free Software Foundation; either version 3 of the License, or
226 (at your option) any later version.
228 Koha is distributed in the hope that it will be useful, but
229 WITHOUT ANY WARRANTY; without even the implied warranty of
230 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
231 GNU General Public License for more details.
233 You should have received a copy of the GNU General Public License
234 along with Koha; if not, see <http://www.gnu.org/licenses>.