1 package Koha::Exporter::Record;
5 use MARC::File::USMARC;
7 use C4::AuthoritiesMarc;
11 sub _get_record_for_export {
13 my $record_type = $params->{record_type};
14 my $record_id = $params->{record_id};
15 my $dont_export_fields = $params->{dont_export_fields};
16 my $clean = $params->{clean};
19 if ( $record_type eq 'auths' ) {
20 $record = _get_authority_for_export( { %$params, authid => $record_id } );
21 } elsif ( $record_type eq 'bibs' ) {
22 $record = _get_biblio_for_export( { %$params, biblionumber => $record_id } );
25 # TODO log "record_type not supported"
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::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};
99 return unless $record_type;
100 return unless @$record_ids;
103 if ( $output_filepath ) {
104 open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
106 binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
108 binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv';
111 if ( $format eq 'iso2709' ) {
112 for my $record_id (@$record_ids) {
113 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
114 my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) };
115 if ( $errorcount_on_decode or $@ ) {
117 warn "record (number $record_id) is invalid and therefore not exported because its reopening generates warnings above";
120 print $record->as_usmarc();
122 } elsif ( $format eq 'xml' ) {
123 my $marcflavour = C4::Context->preference("marcflavour");
124 MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour );
126 print MARC::File::XML::header();
128 for my $record_id (@$record_ids) {
129 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
130 print MARC::File::XML::record($record);
133 print MARC::File::XML::footer();
135 } elsif ( $format eq 'csv' ) {
136 $csv_profile_id ||= C4::Csv::GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') );
137 print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
140 close $fh if $output_filepath;
149 Koha::Exporter::Records - module to export records (biblios and authorities)
153 This module provides a public subroutine to export records as xml, csv or iso2709.
159 Koha::Exporter::Record::export($params);
161 $params is a hashref with some keys:
163 It will displays on STDOUT the generated file.
169 Must be set to 'bibs' or 'auths'
173 The list of the records to export (a list of biblionumber or authid)
177 The format must be 'csv', 'xml' or 'iso2709'.
181 Generate the item infos only for these itemnumbers.
183 Must only be used with biblios.
187 If this flag is set, the items will be exported.
190 =item dont_export_fields
192 List of fields not to export.
196 If the format is csv, a csv_profile_id can be provide to overwrite the default value (syspref ExportWithCsvProfile).
204 This file is part of Koha.
206 Copyright Koha Development Team
208 Koha is free software; you can redistribute it and/or modify it
209 under the terms of the GNU General Public License as published by
210 the Free Software Foundation; either version 3 of the License, or
211 (at your option) any later version.
213 Koha is distributed in the hope that it will be useful, but
214 WITHOUT ANY WARRANTY; without even the implied warranty of
215 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
216 GNU General Public License for more details.
218 You should have received a copy of the GNU General Public License
219 along with Koha; if not, see <http://www.gnu.org/licenses>.