Lines 6-15
use MARC::File::USMARC;
Link Here
|
6 |
|
6 |
|
7 |
use C4::AuthoritiesMarc; |
7 |
use C4::AuthoritiesMarc; |
8 |
use C4::Biblio; |
8 |
use C4::Biblio; |
|
|
9 |
use C4::Charset; |
9 |
use C4::Record; |
10 |
use C4::Record; |
10 |
use Koha::CsvProfiles; |
11 |
use Koha::CsvProfiles; |
|
|
12 |
use Koha::Database; |
11 |
use Koha::Logger; |
13 |
use Koha::Logger; |
12 |
|
14 |
|
|
|
15 |
use MARC::Record; |
16 |
use MARC::File::XML; |
17 |
|
13 |
sub _get_record_for_export { |
18 |
sub _get_record_for_export { |
14 |
my ($params) = @_; |
19 |
my ($params) = @_; |
15 |
my $record_type = $params->{record_type}; |
20 |
my $record_type = $params->{record_type}; |
Lines 50-55
sub _get_record_for_export {
Link Here
|
50 |
return $record; |
55 |
return $record; |
51 |
} |
56 |
} |
52 |
|
57 |
|
|
|
58 |
sub _get_deleted_biblio_for_export { |
59 |
my ($params) = @_; |
60 |
my $record_id = $params->{record_id}; |
61 |
# Creating schema is expensive, allow caller to |
62 |
# pass it so don't have to recreate for each call |
63 |
my $resultset = $params->{resultset} || Koha::Database |
64 |
->new() |
65 |
->schema() |
66 |
->resultset('DeletedbiblioMetadata'); |
67 |
my $marc_flavour = C4::Context->preference('marcflavour'); |
68 |
my $biblio_metadata = $resultset->find({ |
69 |
'biblionumber' => $record_id, |
70 |
'format' => 'marcxml', |
71 |
'marcflavour' => $marc_flavour |
72 |
}); |
73 |
my $marc_xml = $biblio_metadata->metadata; |
74 |
$marc_xml = StripNonXmlChars($marc_xml); |
75 |
|
76 |
my $record = eval { |
77 |
MARC::Record::new_from_xml($marc_xml, 'UTF-8', $marc_flavour) |
78 |
}; |
79 |
if ($@) { |
80 |
warn "Failed to load MARCXML for biblio with biblionumber \"$record_id\": $@"; |
81 |
return undef; |
82 |
} |
83 |
# Set deleted flag (record status, position 05) |
84 |
my $leader = $record->leader; |
85 |
substr $leader, 5, 1, 'd'; |
86 |
$record->leader($leader); |
87 |
return $record; |
88 |
} |
89 |
|
53 |
sub _get_authority_for_export { |
90 |
sub _get_authority_for_export { |
54 |
my ($params) = @_; |
91 |
my ($params) = @_; |
55 |
my $authid = $params->{authid} || return; |
92 |
my $authid = $params->{authid} || return; |
Lines 91-96
sub export {
Link Here
|
91 |
|
128 |
|
92 |
my $record_type = $params->{record_type}; |
129 |
my $record_type = $params->{record_type}; |
93 |
my $record_ids = $params->{record_ids} || []; |
130 |
my $record_ids = $params->{record_ids} || []; |
|
|
131 |
my $deleted_record_ids = $params->{deleted_record_ids} || []; |
94 |
my $format = $params->{format}; |
132 |
my $format = $params->{format}; |
95 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
133 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
96 |
my $export_items = $params->{export_items}; |
134 |
my $export_items = $params->{export_items}; |
Lines 113-154
sub export {
Link Here
|
113 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
151 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
114 |
} |
152 |
} |
115 |
|
153 |
|
116 |
if ( $format eq 'iso2709' ) { |
154 |
if ($format eq 'xml' || $format eq 'iso2709') { |
117 |
for my $record_id (@$record_ids) { |
155 |
my @records; |
118 |
my $record = _get_record_for_export( { %$params, record_id => $record_id } ); |
156 |
@records = map { |
119 |
my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) }; |
157 |
my $record = _get_record_for_export({ %{$params}, record_id => $_ }); |
120 |
if ( $errorcount_on_decode or $@ ) { |
158 |
Koha::Logger->get->warn( "Record $_ could not be loaded." ) unless $record; |
121 |
my $msg = "Record $record_id could not be exported. " . |
159 |
$record ? $record : (); |
122 |
( $@ // '' ); |
160 |
} @{$record_ids}; |
123 |
chomp $msg; |
161 |
|
124 |
Koha::Logger->get->info( $msg ); |
162 |
my @deleted_records; |
125 |
next; |
163 |
if (@{$deleted_record_ids}) { |
126 |
} |
164 |
my $resultset = Koha::Database |
127 |
print $record->as_usmarc(); |
165 |
->new() |
|
|
166 |
->schema() |
167 |
->resultset('DeletedbiblioMetadata'); |
168 |
@deleted_records = map { |
169 |
my $record = _get_deleted_biblio_for_export({ |
170 |
record_id => $_, |
171 |
resultset => $resultset, |
172 |
}); |
173 |
Koha::Logger->get->warn( "Deleted record $_ could not be loaded." ) unless $record; |
174 |
$record ? $record : (); |
175 |
} @{$deleted_record_ids}; |
128 |
} |
176 |
} |
129 |
} elsif ( $format eq 'xml' ) { |
177 |
if ( $format eq 'iso2709' ) { |
130 |
my $marcflavour = C4::Context->preference("marcflavour"); |
178 |
my $encoding_validator = sub { |
131 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
179 |
my ($record_type) = @_; |
132 |
|
180 |
return sub { |
133 |
print MARC::File::XML::header(); |
181 |
my ($record) = @_; |
134 |
print "\n"; |
182 |
my $errorcount_on_decode = eval { scalar(MARC::File::USMARC->decode($record->as_usmarc)->warnings()) }; |
135 |
for my $record_id (@$record_ids) { |
183 |
if ($errorcount_on_decode || $@) { |
136 |
my $record = _get_record_for_export( { %$params, record_id => $record_id } ); |
184 |
my ($id_tag, $id_subfield) = GetMarcFromKohaField('biblio.biblionumber', ''); |
137 |
if( !$record ) { |
185 |
my $record_id = $record->subfield($id_tag, $id_subfield); |
138 |
Koha::Logger->get->info( "Record $record_id could not be exported." ); |
186 |
my $msg = "$record_type $record_id could not be USMARC decoded/encoded. " . ($@ // ''); |
139 |
next; |
187 |
chomp $msg; |
|
|
188 |
Koha::Logger->get->warn($msg); |
189 |
return 0; |
190 |
} |
191 |
return 1; |
192 |
} |
193 |
}; |
194 |
my $validator = $encoding_validator->('Record'); |
195 |
for my $record (grep { $validator->($_) } @records) { |
196 |
print $record->as_usmarc(); |
197 |
} |
198 |
if (@deleted_records) { |
199 |
$validator = $encoding_validator->('Deleted record'); |
200 |
for my $deleted_record (grep { $validator->($_) } @deleted_records) { |
201 |
print $deleted_record->as_usmarc(); |
202 |
} |
140 |
} |
203 |
} |
141 |
print MARC::File::XML::record($record); |
204 |
} elsif ( $format eq 'xml' ) { |
|
|
205 |
my $marcflavour = C4::Context->preference("marcflavour"); |
206 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
207 |
print MARC::File::XML::header(); |
208 |
print "\n"; |
209 |
for my $record (@records, @deleted_records) { |
210 |
print MARC::File::XML::record($record); |
211 |
print "\n"; |
212 |
} |
213 |
print MARC::File::XML::footer(); |
142 |
print "\n"; |
214 |
print "\n"; |
143 |
} |
215 |
} |
144 |
print MARC::File::XML::footer(); |
|
|
145 |
print "\n"; |
146 |
} elsif ( $format eq 'csv' ) { |
216 |
} elsif ( $format eq 'csv' ) { |
147 |
die 'There is no valid csv profile defined for this export' |
217 |
die 'There is no valid csv profile defined for this export' |
148 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
218 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
149 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
219 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
150 |
} |
220 |
} |
151 |
|
|
|
152 |
close $fh if $output_filepath; |
221 |
close $fh if $output_filepath; |
153 |
} |
222 |
} |
154 |
|
223 |
|