Lines 6-16
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::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 $biblionumber = $params->{biblionumber}; |
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' => $biblionumber, |
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 (!$record) { |
135 |
Koha::Logger->get->warn( |
136 |
"Failed to load MARCXML for deleted biblio with biblionumber \"$biblionumber\": $@" |
137 |
); |
138 |
return; |
139 |
} |
140 |
# Set deleted flag (record status, position 05) |
141 |
my $leader = $record->leader; |
142 |
substr $leader, 5, 1, 'd'; |
143 |
$record->leader($leader); |
144 |
return $record; |
145 |
} |
146 |
|
108 |
sub _get_authority_for_export { |
147 |
sub _get_authority_for_export { |
109 |
my ($params) = @_; |
148 |
my ($params) = @_; |
110 |
my $authid = $params->{authid} || return; |
149 |
my $authid = $params->{authid} || return; |
Lines 122-128
sub _get_biblio_for_export {
Link Here
|
122 |
|
161 |
|
123 |
my $record = eval { C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber }); }; |
162 |
my $record = eval { C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber }); }; |
124 |
|
163 |
|
125 |
return if $@ or not defined $record; |
164 |
if (!$record) { |
|
|
165 |
Koha::Logger->get->warn( |
166 |
"Failed to load MARCXML for biblio with biblionumber \"$biblionumber\": $@" |
167 |
); |
168 |
return; |
169 |
} |
126 |
|
170 |
|
127 |
if ($export_items) { |
171 |
if ($export_items) { |
128 |
C4::Biblio::EmbedItemsInMarcBiblio({ |
172 |
C4::Biblio::EmbedItemsInMarcBiblio({ |
Lines 149-154
sub export {
Link Here
|
149 |
|
193 |
|
150 |
my $record_type = $params->{record_type}; |
194 |
my $record_type = $params->{record_type}; |
151 |
my $record_ids = $params->{record_ids} || []; |
195 |
my $record_ids = $params->{record_ids} || []; |
|
|
196 |
my $deleted_record_ids = $params->{deleted_record_ids} || []; |
152 |
my $format = $params->{format}; |
197 |
my $format = $params->{format}; |
153 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
198 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
154 |
my $export_items = $params->{export_items}; |
199 |
my $export_items = $params->{export_items}; |
Lines 160-166
sub export {
Link Here
|
160 |
Koha::Logger->get->warn( "No record_type given." ); |
205 |
Koha::Logger->get->warn( "No record_type given." ); |
161 |
return; |
206 |
return; |
162 |
} |
207 |
} |
163 |
return unless @$record_ids; |
208 |
return unless (@{$record_ids} || @{$deleted_record_ids} && $format ne 'csv'); |
164 |
|
209 |
|
165 |
my $fh; |
210 |
my $fh; |
166 |
if ( $output_filepath ) { |
211 |
if ( $output_filepath ) { |
Lines 171-210
sub export {
Link Here
|
171 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
216 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
172 |
} |
217 |
} |
173 |
|
218 |
|
174 |
if ( $format eq 'iso2709' ) { |
219 |
if ($format eq 'xml' || $format eq 'iso2709') { |
175 |
for my $record_id (@$record_ids) { |
220 |
my @records; |
176 |
my $record = _get_record_for_export( { %$params, record_id => $record_id } ); |
221 |
@records = map { |
177 |
next unless $record; |
222 |
my $record = _get_record_for_export({ %{$params}, record_id => $_ }); |
178 |
my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) }; |
223 |
$record ? $record : (); |
179 |
if ( $errorcount_on_decode or $@ ) { |
224 |
} @{$record_ids}; |
180 |
my $msg = "Record $record_id could not be exported. " . |
225 |
|
181 |
( $@ // '' ); |
226 |
my @deleted_records; |
182 |
chomp $msg; |
227 |
if (@{$deleted_record_ids}) { |
183 |
Koha::Logger->get->info( $msg ); |
228 |
my $resultset = Koha::Database |
184 |
next; |
229 |
->new() |
185 |
} |
230 |
->schema() |
186 |
print $record->as_usmarc(); |
231 |
->resultset('DeletedbiblioMetadata'); |
|
|
232 |
@deleted_records = map { |
233 |
my $record = _get_deleted_biblio_for_export({ |
234 |
biblionumber => $_, |
235 |
resultset => $resultset, |
236 |
}); |
237 |
$record ? $record : (); |
238 |
} @{$deleted_record_ids}; |
187 |
} |
239 |
} |
188 |
} elsif ( $format eq 'xml' ) { |
240 |
if ( $format eq 'iso2709' ) { |
189 |
my $marcflavour = C4::Context->preference("marcflavour"); |
241 |
my $encoding_validator = sub { |
190 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
242 |
my ($record_type) = @_; |
191 |
|
243 |
return sub { |
192 |
print MARC::File::XML::header(); |
244 |
my ($record) = @_; |
193 |
print "\n"; |
245 |
my $errorcount_on_decode = eval { scalar(MARC::File::USMARC->decode($record->as_usmarc)->warnings()) }; |
194 |
for my $record_id (@$record_ids) { |
246 |
if ($errorcount_on_decode || $@) { |
195 |
my $record = _get_record_for_export( { %$params, record_id => $record_id } ); |
247 |
my ($id_tag, $id_subfield) = GetMarcFromKohaField('biblio.biblionumber', ''); |
196 |
next unless $record; |
248 |
my $record_id = $record->subfield($id_tag, $id_subfield); |
197 |
print MARC::File::XML::record($record); |
249 |
my $msg = "$record_type $record_id could not be USMARC decoded/encoded. " . ($@ // ''); |
|
|
250 |
chomp $msg; |
251 |
Koha::Logger->get->warn($msg); |
252 |
return 0; |
253 |
} |
254 |
return 1; |
255 |
} |
256 |
}; |
257 |
my $validator = $encoding_validator->('Record'); |
258 |
for my $record (grep { $validator->($_) } @records) { |
259 |
print $record->as_usmarc(); |
260 |
} |
261 |
if (@deleted_records) { |
262 |
$validator = $encoding_validator->('Deleted record'); |
263 |
for my $deleted_record (grep { $validator->($_) } @deleted_records) { |
264 |
print $deleted_record->as_usmarc(); |
265 |
} |
266 |
} |
267 |
} elsif ( $format eq 'xml' ) { |
268 |
my $marcflavour = C4::Context->preference("marcflavour"); |
269 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
270 |
print MARC::File::XML::header(); |
271 |
print "\n"; |
272 |
for my $record (@records, @deleted_records) { |
273 |
print MARC::File::XML::record($record); |
274 |
print "\n"; |
275 |
} |
276 |
print MARC::File::XML::footer(); |
198 |
print "\n"; |
277 |
print "\n"; |
199 |
} |
278 |
} |
200 |
print MARC::File::XML::footer(); |
|
|
201 |
print "\n"; |
202 |
} elsif ( $format eq 'csv' ) { |
279 |
} elsif ( $format eq 'csv' ) { |
203 |
die 'There is no valid csv profile defined for this export' |
280 |
die 'There is no valid csv profile defined for this export' |
204 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
281 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
205 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
282 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
206 |
} |
283 |
} |
207 |
|
|
|
208 |
close $fh if $output_filepath; |
284 |
close $fh if $output_filepath; |
209 |
} |
285 |
} |
210 |
|
286 |
|