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