Bug 15380: Move Koha::Authority to Koha::MetadataRecord::Authority
[koha.git] / Koha / Exporter / Record.pm
1 package Koha::Exporter::Record;
2
3 use Modern::Perl;
4 use MARC::File::XML;
5 use MARC::File::USMARC;
6
7 use C4::AuthoritiesMarc;
8 use C4::Biblio;
9 use C4::Record;
10
11 sub _get_record_for_export {
12     my ($params)           = @_;
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};
17
18     my $record;
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 } );
23     } else {
24
25         # TODO log "record_type not supported"
26         return;
27     }
28
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 );
33
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);
40                         }
41                     } else {
42                         $record->delete_fields( $record->field($field) );
43                     }
44                 }
45             }
46         }
47     }
48     C4::Biblio::RemoveAllNsb($record) if $clean;
49     return $record;
50 }
51
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;
58 }
59
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};
66
67     my $record = eval { C4::Biblio::GetMarcBiblio($biblionumber); };
68
69     return if $@ or not defined $record;
70
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 )?
75
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);
80                 }
81             }
82         }
83     }
84     return $record;
85 }
86
87 sub export {
88     my ($params) = @_;
89
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};
98
99     return unless $record_type;
100     return unless @$record_ids;
101
102     my $fh;
103     if ( $output_filepath ) {
104         open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
105         select $fh;
106         binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
107     } else {
108         binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv';
109     }
110
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 $@ ) {
116                 warn $@ if $@;
117                 warn "record (number $record_id) is invalid and therefore not exported because its reopening generates warnings above";
118                 next;
119             }
120             print $record->as_usmarc();
121         }
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 );
125
126         print MARC::File::XML::header();
127         print "\n";
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);
131             print "\n";
132         }
133         print MARC::File::XML::footer();
134         print "\n";
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 );
138     }
139
140     close $fh if $output_filepath;
141 }
142
143 1;
144
145 __END__
146
147 =head1 NAME
148
149 Koha::Exporter::Records - module to export records (biblios and authorities)
150
151 =head1 SYNOPSIS
152
153 This module provides a public subroutine to export records as xml, csv or iso2709.
154
155 =head2 FUNCTIONS
156
157 =head3 export
158
159     Koha::Exporter::Record::export($params);
160
161 $params is a hashref with some keys:
162
163 It will displays on STDOUT the generated file.
164
165 =over 4
166
167 =item record_type
168
169   Must be set to 'bibs' or 'auths'
170
171 =item record_ids
172
173   The list of the records to export (a list of biblionumber or authid)
174
175 =item format
176
177   The format must be 'csv', 'xml' or 'iso2709'.
178
179 =item itemnumbers
180
181   Generate the item infos only for these itemnumbers.
182
183   Must only be used with biblios.
184
185 =item export_items
186
187   If this flag is set, the items will be exported.
188   Default is ON.
189
190 =item dont_export_fields
191
192   List of fields not to export.
193
194 =item csv_profile_id
195
196   If the format is csv, a csv_profile_id can be provide to overwrite the default value (syspref ExportWithCsvProfile).
197
198 =cut
199
200 =back
201
202 =head1 LICENSE
203
204 This file is part of Koha.
205
206 Copyright Koha Development Team
207
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.
212
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.
217
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>.