|
Lines 6-16
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 |
use List::Util qw(all any); |
14 |
use List::Util qw(all any); |
| 13 |
|
15 |
|
|
|
16 |
use MARC::Record; |
| 17 |
use MARC::File::XML; |
| 18 |
|
| 14 |
sub _get_record_for_export { |
19 |
sub _get_record_for_export { |
| 15 |
my ($params) = @_; |
20 |
my ($params) = @_; |
| 16 |
my $record_type = $params->{record_type}; |
21 |
my $record_type = $params->{record_type}; |
|
Lines 105-110
sub _get_record_for_export {
Link Here
|
| 105 |
return $record; |
110 |
return $record; |
| 106 |
} |
111 |
} |
| 107 |
|
112 |
|
|
|
113 |
sub _get_deleted_biblio_for_export { |
| 114 |
my ($params) = @_; |
| 115 |
my $record_id = $params->{record_id}; |
| 116 |
# Creating schema is expensive, allow caller to |
| 117 |
# pass it so don't have to recreate for each call |
| 118 |
my $resultset = $params->{resultset} || Koha::Database |
| 119 |
->new() |
| 120 |
->schema() |
| 121 |
->resultset('DeletedbiblioMetadata'); |
| 122 |
my $marc_flavour = C4::Context->preference('marcflavour'); |
| 123 |
my $biblio_metadata = $resultset->find({ |
| 124 |
'biblionumber' => $record_id, |
| 125 |
'format' => 'marcxml', |
| 126 |
'marcflavour' => $marc_flavour |
| 127 |
}); |
| 128 |
my $marc_xml = $biblio_metadata->metadata; |
| 129 |
$marc_xml = StripNonXmlChars($marc_xml); |
| 130 |
|
| 131 |
my $record = eval { |
| 132 |
MARC::Record::new_from_xml($marc_xml, 'UTF-8', $marc_flavour) |
| 133 |
}; |
| 134 |
if ($@) { |
| 135 |
warn "Failed to load MARCXML for biblio with biblionumber \"$record_id\": $@"; |
| 136 |
return undef; |
| 137 |
} |
| 138 |
# Set deleted flag (record status, position 05) |
| 139 |
my $leader = $record->leader; |
| 140 |
substr $leader, 5, 1, 'd'; |
| 141 |
$record->leader($leader); |
| 142 |
return $record; |
| 143 |
} |
| 144 |
|
| 108 |
sub _get_authority_for_export { |
145 |
sub _get_authority_for_export { |
| 109 |
my ($params) = @_; |
146 |
my ($params) = @_; |
| 110 |
my $authid = $params->{authid} || return; |
147 |
my $authid = $params->{authid} || return; |
|
Lines 149-154
sub export {
Link Here
|
| 149 |
|
186 |
|
| 150 |
my $record_type = $params->{record_type}; |
187 |
my $record_type = $params->{record_type}; |
| 151 |
my $record_ids = $params->{record_ids} || []; |
188 |
my $record_ids = $params->{record_ids} || []; |
|
|
189 |
my $deleted_record_ids = $params->{deleted_record_ids} || []; |
| 152 |
my $format = $params->{format}; |
190 |
my $format = $params->{format}; |
| 153 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
191 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
| 154 |
my $export_items = $params->{export_items}; |
192 |
my $export_items = $params->{export_items}; |
|
Lines 160-166
sub export {
Link Here
|
| 160 |
Koha::Logger->get->warn( "No record_type given." ); |
198 |
Koha::Logger->get->warn( "No record_type given." ); |
| 161 |
return; |
199 |
return; |
| 162 |
} |
200 |
} |
| 163 |
return unless @$record_ids; |
201 |
return unless (@{$record_ids} || @{$deleted_record_ids} && $format ne 'csv'); |
| 164 |
|
202 |
|
| 165 |
my $fh; |
203 |
my $fh; |
| 166 |
if ( $output_filepath ) { |
204 |
if ( $output_filepath ) { |
|
Lines 171-210
sub export {
Link Here
|
| 171 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
209 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
| 172 |
} |
210 |
} |
| 173 |
|
211 |
|
| 174 |
if ( $format eq 'iso2709' ) { |
212 |
if ($format eq 'xml' || $format eq 'iso2709') { |
| 175 |
for my $record_id (@$record_ids) { |
213 |
my @records; |
| 176 |
my $record = _get_record_for_export( { %$params, record_id => $record_id } ); |
214 |
@records = map { |
| 177 |
next unless $record; |
215 |
my $record = _get_record_for_export({ %{$params}, record_id => $_ }); |
| 178 |
my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) }; |
216 |
$record ? $record : (); |
| 179 |
if ( $errorcount_on_decode or $@ ) { |
217 |
} @{$record_ids}; |
| 180 |
my $msg = "Record $record_id could not be exported. " . |
218 |
|
| 181 |
( $@ // '' ); |
219 |
my @deleted_records; |
| 182 |
chomp $msg; |
220 |
if (@{$deleted_record_ids}) { |
| 183 |
Koha::Logger->get->info( $msg ); |
221 |
my $resultset = Koha::Database |
| 184 |
next; |
222 |
->new() |
| 185 |
} |
223 |
->schema() |
| 186 |
print $record->as_usmarc(); |
224 |
->resultset('DeletedbiblioMetadata'); |
|
|
225 |
@deleted_records = map { |
| 226 |
my $record = _get_deleted_biblio_for_export({ |
| 227 |
record_id => $_, |
| 228 |
resultset => $resultset, |
| 229 |
}); |
| 230 |
$record ? $record : (); |
| 231 |
} @{$deleted_record_ids}; |
| 187 |
} |
232 |
} |
| 188 |
} elsif ( $format eq 'xml' ) { |
233 |
if ( $format eq 'iso2709' ) { |
| 189 |
my $marcflavour = C4::Context->preference("marcflavour"); |
234 |
my $encoding_validator = sub { |
| 190 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
235 |
my ($record_type) = @_; |
| 191 |
|
236 |
return sub { |
| 192 |
print MARC::File::XML::header(); |
237 |
my ($record) = @_; |
| 193 |
print "\n"; |
238 |
my $errorcount_on_decode = eval { scalar(MARC::File::USMARC->decode($record->as_usmarc)->warnings()) }; |
| 194 |
for my $record_id (@$record_ids) { |
239 |
if ($errorcount_on_decode || $@) { |
| 195 |
my $record = _get_record_for_export( { %$params, record_id => $record_id } ); |
240 |
my ($id_tag, $id_subfield) = GetMarcFromKohaField('biblio.biblionumber', ''); |
| 196 |
next unless $record; |
241 |
my $record_id = $record->subfield($id_tag, $id_subfield); |
| 197 |
print MARC::File::XML::record($record); |
242 |
my $msg = "$record_type $record_id could not be USMARC decoded/encoded. " . ($@ // ''); |
|
|
243 |
chomp $msg; |
| 244 |
Koha::Logger->get->warn($msg); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
return 1; |
| 248 |
} |
| 249 |
}; |
| 250 |
my $validator = $encoding_validator->('Record'); |
| 251 |
for my $record (grep { $validator->($_) } @records) { |
| 252 |
print $record->as_usmarc(); |
| 253 |
} |
| 254 |
if (@deleted_records) { |
| 255 |
$validator = $encoding_validator->('Deleted record'); |
| 256 |
for my $deleted_record (grep { $validator->($_) } @deleted_records) { |
| 257 |
print $deleted_record->as_usmarc(); |
| 258 |
} |
| 259 |
} |
| 260 |
} elsif ( $format eq 'xml' ) { |
| 261 |
my $marcflavour = C4::Context->preference("marcflavour"); |
| 262 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
| 263 |
print MARC::File::XML::header(); |
| 264 |
print "\n"; |
| 265 |
for my $record (@records, @deleted_records) { |
| 266 |
print MARC::File::XML::record($record); |
| 267 |
print "\n"; |
| 268 |
} |
| 269 |
print MARC::File::XML::footer(); |
| 198 |
print "\n"; |
270 |
print "\n"; |
| 199 |
} |
271 |
} |
| 200 |
print MARC::File::XML::footer(); |
|
|
| 201 |
print "\n"; |
| 202 |
} elsif ( $format eq 'csv' ) { |
272 |
} elsif ( $format eq 'csv' ) { |
| 203 |
die 'There is no valid csv profile defined for this export' |
273 |
die 'There is no valid csv profile defined for this export' |
| 204 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
274 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
| 205 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
275 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
| 206 |
} |
276 |
} |
| 207 |
|
|
|
| 208 |
close $fh if $output_filepath; |
277 |
close $fh if $output_filepath; |
| 209 |
} |
278 |
} |
| 210 |
|
279 |
|