Add release notes for the 16.05.09 security release
[koha.git] / Koha / Exporter / Record.pm
blob0508660f86fceef340de55372e2c719fd86b90ab
1 package Koha::Exporter::Record;
3 use Modern::Perl;
4 use MARC::File::XML;
5 use MARC::File::USMARC;
7 use C4::AuthoritiesMarc;
8 use C4::Biblio;
9 use C4::Record;
10 use Koha::Logger;
12 sub _get_record_for_export {
13 my ($params) = @_;
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};
19 my $record;
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 } );
24 } else {
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);
41 } else {
42 $record->delete_fields( $record->field($field) );
48 C4::Biblio::RemoveAllNsb($record) if $clean;
49 return $record;
52 sub _get_authority_for_export {
53 my ($params) = @_;
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 {
61 my ($params) = @_;
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;
71 if ($export_items) {
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);
84 return $record;
87 sub export {
88 my ($params) = @_;
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 if( !$record_type ) {
100 Koha::Logger->get->warn( "No record_type given." );
101 return;
103 return unless @$record_ids;
105 my $fh;
106 if ( $output_filepath ) {
107 open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
108 select $fh;
109 binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
110 } else {
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. " .
120 ( $@ // '' );
121 chomp $msg;
122 Koha::Logger->get->info( $msg );
123 next;
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();
132 print "\n";
133 for my $record_id (@$record_ids) {
134 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
135 if( !$record ) {
136 Koha::Logger->get->info( "Record $record_id could not be exported." );
137 next;
139 print MARC::File::XML::record($record);
140 print "\n";
142 print MARC::File::XML::footer();
143 print "\n";
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;
154 __END__
156 =head1 NAME
158 Koha::Exporter::Records - module to export records (biblios and authorities)
160 =head1 SYNOPSIS
162 This module provides a public subroutine to export records as xml, csv or iso2709.
164 =head2 FUNCTIONS
166 =head3 export
168 Koha::Exporter::Record::export($params);
170 $params is a hashref with some keys:
172 It will displays on STDOUT the generated file.
174 =over 4
176 =item record_type
178 Must be set to 'bibs' or 'auths'
180 =item record_ids
182 The list of the records to export (a list of biblionumber or authid)
184 =item format
186 The format must be 'csv', 'xml' or 'iso2709'.
188 =item itemnumbers
190 Generate the item infos only for these itemnumbers.
192 Must only be used with biblios.
194 =item export_items
196 If this flag is set, the items will be exported.
197 Default is ON.
199 =item dont_export_fields
201 List of fields not to export.
203 =item csv_profile_id
205 If the format is csv, a csv_profile_id can be provide to overwrite the default value (syspref ExportWithCsvProfile).
207 =cut
209 =back
211 =head1 LICENSE
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>.