1 package Koha::Exporter::Record;
5 use MARC::File::USMARC;
7 use C4::AuthoritiesMarc;
10 use Koha::CsvProfiles;
12 use List::Util qw(all any);
14 sub _get_record_for_export {
16 my $record_type = $params->{record_type};
17 my $record_id = $params->{record_id};
18 my $conditions = $params->{record_conditions};
19 my $dont_export_fields = $params->{dont_export_fields};
20 my $clean = $params->{clean};
23 if ( $record_type eq 'auths' ) {
24 $record = _get_authority_for_export( { %$params, authid => $record_id } );
25 } elsif ( $record_type eq 'bibs' ) {
26 $record = _get_biblio_for_export( { %$params, biblionumber => $record_id } );
28 Koha::Logger->get->warn( "Record_type $record_type not supported." );
31 Koha::Logger->get->warn( "Record $record_id could not be exported." );
35 # If multiple conditions all are required to match (and)
36 # For matching against multiple marc targets all are also required to match
39 return $_[0] eq $_[1];
42 return $_[0] ne $_[1];
45 return $_[0] gt $_[1];
48 return $_[0] lt $_[1];
52 foreach my $condition (@{$conditions}) {
53 my ($field_tag, $subfield, $operator, $match_value) = @{$condition};
54 my @fields = $record->field($field_tag);
61 if ($operator eq '?') {
62 return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields;
63 } elsif ($operator eq '!?') {
64 return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields;
67 if (exists $operators{$operator}) {
68 $op = $operators{$operator};
70 die("Invalid operator: $op");
72 my @target_values = map { $subfield ? $_->subfield($subfield) : ($_->data()) } @fields;
73 if (!@target_values) {
77 return unless all { $op->($_, $match_value) } @target_values;
81 return if $no_target && $operator ne '!=';
85 if ($dont_export_fields) {
86 for my $f ( split / /, $dont_export_fields ) {
87 if ( $f =~ m/^(\d{3})(.)?$/ ) {
88 my ( $field, $subfield ) = ( $1, $2 );
90 # skip if this record doesn't have this field
91 if ( defined $record->field($field) ) {
92 if ( defined $subfield ) {
93 my @tags = $record->field($field);
94 foreach my $t (@tags) {
95 $t->delete_subfields($subfield);
98 $record->delete_fields( $record->field($field) );
104 C4::Biblio::RemoveAllNsb($record) if $clean;
108 sub _get_authority_for_export {
110 my $authid = $params->{authid} || return;
111 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
112 return unless $authority;
113 return $authority->record;
116 sub _get_biblio_for_export {
118 my $biblionumber = $params->{biblionumber};
119 my $itemnumbers = $params->{itemnumbers};
120 my $export_items = $params->{export_items} // 1;
121 my $only_export_items_for_branches = $params->{only_export_items_for_branches};
123 my $record = eval { C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber }); };
125 return if $@ or not defined $record;
128 C4::Biblio::EmbedItemsInMarcBiblio({
129 marc_record => $record,
130 biblionumber => $biblionumber,
131 item_numbers => $itemnumbers });
132 if ($only_export_items_for_branches && @$only_export_items_for_branches) {
133 my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches;
134 my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch', '' ); # Should be GetFrameworkCode( $biblionumber )?
136 for my $itemfield ( $record->field($homebranchfield) ) {
137 my $homebranch = $itemfield->subfield($homebranchsubfield);
138 unless ( $export_items_for_branches{$homebranch} ) {
139 $record->delete_field($itemfield);
150 my $record_type = $params->{record_type};
151 my $record_ids = $params->{record_ids} || [];
152 my $format = $params->{format};
153 my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths
154 my $export_items = $params->{export_items};
155 my $dont_export_fields = $params->{dont_export_fields};
156 my $csv_profile_id = $params->{csv_profile_id};
157 my $output_filepath = $params->{output_filepath};
159 if( !$record_type ) {
160 Koha::Logger->get->warn( "No record_type given." );
163 return unless @$record_ids;
166 if ( $output_filepath ) {
167 open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
169 binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
171 binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv';
174 if ( $format eq 'iso2709' ) {
175 for my $record_id (@$record_ids) {
176 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
178 my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) };
179 if ( $errorcount_on_decode or $@ ) {
180 my $msg = "Record $record_id could not be exported. " .
183 Koha::Logger->get->info( $msg );
186 print $record->as_usmarc();
188 } elsif ( $format eq 'xml' ) {
189 my $marcflavour = C4::Context->preference("marcflavour");
190 MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour );
192 print MARC::File::XML::header();
194 for my $record_id (@$record_ids) {
195 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
197 print MARC::File::XML::record($record);
200 print MARC::File::XML::footer();
202 } elsif ( $format eq 'csv' ) {
203 die 'There is no valid csv profile defined for this export'
204 unless Koha::CsvProfiles->find( $csv_profile_id );
205 print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
208 close $fh if $output_filepath;
217 Koha::Exporter::Records - module to export records (biblios and authorities)
221 This module provides a public subroutine to export records as xml, csv or iso2709.
227 Koha::Exporter::Record::export($params);
229 $params is a hashref with some keys:
231 It will displays on STDOUT the generated file.
237 Must be set to 'bibs' or 'auths'
241 The list of the records to export (a list of biblionumber or authid)
245 The format must be 'csv', 'xml' or 'iso2709'.
249 Generate the item infos only for these itemnumbers.
251 Must only be used with biblios.
255 If this flag is set, the items will be exported.
258 =item dont_export_fields
260 List of fields not to export.
264 If the format is csv, you have to define a csv_profile_id.
272 This file is part of Koha.
274 Copyright Koha Development Team
276 Koha is free software; you can redistribute it and/or modify it
277 under the terms of the GNU General Public License as published by
278 the Free Software Foundation; either version 3 of the License, or
279 (at your option) any later version.
281 Koha is distributed in the hope that it will be useful, but
282 WITHOUT ANY WARRANTY; without even the implied warranty of
283 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
284 GNU General Public License for more details.
286 You should have received a copy of the GNU General Public License
287 along with Koha; if not, see <http://www.gnu.org/licenses>.