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