1 package Koha::Exporter::Record;
5 use MARC::File::USMARC;
7 use C4::AuthoritiesMarc;
8 use C4::Biblio qw( GetMarcFromKohaField );
11 use Koha::CsvProfiles;
13 use List::Util qw( all any );
15 sub _get_record_for_export {
17 my $record_type = $params->{record_type};
18 my $record_id = $params->{record_id};
19 my $conditions = $params->{record_conditions};
20 my $dont_export_fields = $params->{dont_export_fields};
21 my $clean = $params->{clean};
24 if ( $record_type eq 'auths' ) {
25 $record = _get_authority_for_export( { %$params, authid => $record_id } );
26 } elsif ( $record_type eq 'bibs' ) {
27 $record = _get_biblio_for_export( { %$params, biblionumber => $record_id } );
29 Koha::Logger->get->warn( "Record_type $record_type not supported." );
32 Koha::Logger->get->warn( "Record $record_id could not be exported." );
36 # If multiple conditions all are required to match (and)
37 # For matching against multiple marc targets all are also required to match
40 return $_[0] eq $_[1];
43 return $_[0] ne $_[1];
46 return $_[0] gt $_[1];
49 return $_[0] lt $_[1];
53 foreach my $condition (@{$conditions}) {
54 my ($field_tag, $subfield, $operator, $match_value) = @{$condition};
55 my @fields = $record->field($field_tag);
62 if ($operator eq '?') {
63 return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields;
64 } elsif ($operator eq '!?') {
65 return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields;
68 if (exists $operators{$operator}) {
69 $op = $operators{$operator};
71 die("Invalid operator: $op");
73 my @target_values = map { $subfield ? $_->subfield($subfield) : ($_->data()) } @fields;
74 if (!@target_values) {
78 return unless all { $op->($_, $match_value) } @target_values;
82 return if $no_target && $operator ne '!=';
86 if ($dont_export_fields) {
87 for my $f ( split / /, $dont_export_fields ) {
88 if ( $f =~ m/^(\d{3})(.)?$/ ) {
89 my ( $field, $subfield ) = ( $1, $2 );
91 # skip if this record doesn't have this field
92 if ( defined $record->field($field) ) {
93 if ( defined $subfield ) {
94 my @tags = $record->field($field);
95 foreach my $t (@tags) {
96 $t->delete_subfields($subfield);
99 $record->delete_fields( $record->field($field) );
105 C4::Biblio::RemoveAllNsb($record) if $clean;
109 sub _get_authority_for_export {
111 my $authid = $params->{authid} || return;
112 my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
113 return unless $authority;
114 return $authority->record;
117 sub _get_biblio_for_export {
119 my $biblionumber = $params->{biblionumber};
120 my $itemnumbers = $params->{itemnumbers};
121 my $export_items = $params->{export_items} // 1;
122 my $only_export_items_for_branches = $params->{only_export_items_for_branches};
124 my $biblio = Koha::Biblios->find($biblionumber);
125 my $record = eval { $biblio->metadata->record };
127 return if $@ or not defined $record;
130 Koha::Biblio::Metadata->record(
134 biblionumber => $biblionumber,
135 item_numbers => $itemnumbers,
138 if ($only_export_items_for_branches && @$only_export_items_for_branches) {
139 my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches;
140 my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch' );
142 for my $itemfield ( $record->field($homebranchfield) ) {
143 my $homebranch = $itemfield->subfield($homebranchsubfield);
144 unless ( $export_items_for_branches{$homebranch} ) {
145 $record->delete_field($itemfield);
156 my $record_type = $params->{record_type};
157 my $record_ids = $params->{record_ids} || [];
158 my $format = $params->{format};
159 my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths
160 my $export_items = $params->{export_items};
161 my $dont_export_fields = $params->{dont_export_fields};
162 my $csv_profile_id = $params->{csv_profile_id};
163 my $output_filepath = $params->{output_filepath};
165 if( !$record_type ) {
166 Koha::Logger->get->warn( "No record_type given." );
169 return unless @$record_ids;
172 if ( $output_filepath ) {
173 open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)";
175 binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv';
177 binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv';
180 if ( $format eq 'iso2709' ) {
181 for my $record_id (@$record_ids) {
182 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
184 my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) };
185 if ( $errorcount_on_decode or $@ ) {
186 my $msg = "Record $record_id could not be exported. " .
189 Koha::Logger->get->info( $msg );
192 print $record->as_usmarc();
194 } elsif ( $format eq 'xml' ) {
195 my $marcflavour = C4::Context->preference("marcflavour");
196 MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour );
198 print MARC::File::XML::header();
200 for my $record_id (@$record_ids) {
201 my $record = _get_record_for_export( { %$params, record_id => $record_id } );
203 print MARC::File::XML::record($record);
206 print MARC::File::XML::footer();
208 } elsif ( $format eq 'csv' ) {
209 die 'There is no valid csv profile defined for this export'
210 unless Koha::CsvProfiles->find( $csv_profile_id );
211 print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
214 close $fh if $output_filepath;
223 Koha::Exporter::Records - module to export records (biblios and authorities)
227 This module provides a public subroutine to export records as xml, csv or iso2709.
233 Koha::Exporter::Record::export($params);
235 $params is a hashref with some keys:
237 It will displays on STDOUT the generated file.
243 Must be set to 'bibs' or 'auths'
247 The list of the records to export (a list of biblionumber or authid)
251 The format must be 'csv', 'xml' or 'iso2709'.
255 Generate the item infos only for these itemnumbers.
257 Must only be used with biblios.
261 If this flag is set, the items will be exported.
264 =item dont_export_fields
266 List of fields not to export.
270 If the format is csv, you have to define a csv_profile_id.
278 This file is part of Koha.
280 Copyright Koha Development Team
282 Koha is free software; you can redistribute it and/or modify it
283 under the terms of the GNU General Public License as published by
284 the Free Software Foundation; either version 3 of the License, or
285 (at your option) any later version.
287 Koha is distributed in the hope that it will be useful, but
288 WITHOUT ANY WARRANTY; without even the implied warranty of
289 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
290 GNU General Public License for more details.
292 You should have received a copy of the GNU General Public License
293 along with Koha; if not, see <http://www.gnu.org/licenses>.