Lines 5-11
use MARC::File::XML;
Link Here
|
5 |
use MARC::File::USMARC; |
5 |
use MARC::File::USMARC; |
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 qw( StripNonXmlChars ); |
9 |
use C4::Charset qw( StripNonXmlChars ); |
10 |
use C4::Record; |
10 |
use C4::Record; |
11 |
use Koha::Biblios; |
11 |
use Koha::Biblios; |
Lines 34-43
sub _get_record_for_export {
Link Here
|
34 |
} elsif ( $record_type eq 'deleted_bibs' ) { |
34 |
} elsif ( $record_type eq 'deleted_bibs' ) { |
35 |
$record = _get_deleted_biblio_for_export( { %$params, biblionumber => $record_id } ); |
35 |
$record = _get_deleted_biblio_for_export( { %$params, biblionumber => $record_id } ); |
36 |
} else { |
36 |
} else { |
37 |
Koha::Logger->get->warn( "Record type $record_type not supported." ); |
37 |
Koha::Logger->get->warn("Record type $record_type not supported."); |
38 |
} |
38 |
} |
39 |
if ( !$record ) { |
39 |
if ( !$record ) { |
40 |
Koha::Logger->get->warn( "Record $record_id with record type $record_type could not be exported." ); |
40 |
Koha::Logger->get->warn("Record $record_id with record type $record_type could not be exported."); |
41 |
return; |
41 |
return; |
42 |
} |
42 |
} |
43 |
|
43 |
|
Lines 58-89
sub _get_record_for_export {
Link Here
|
58 |
}, |
58 |
}, |
59 |
); |
59 |
); |
60 |
if ($conditions) { |
60 |
if ($conditions) { |
61 |
foreach my $condition (@{$conditions}) { |
61 |
foreach my $condition ( @{$conditions} ) { |
62 |
my ($field_tag, $subfield, $operator, $match_value) = @{$condition}; |
62 |
my ( $field_tag, $subfield, $operator, $match_value ) = @{$condition}; |
63 |
my @fields = $record->field($field_tag); |
63 |
my @fields = $record->field($field_tag); |
64 |
my $no_target = 0; |
64 |
my $no_target = 0; |
65 |
|
65 |
|
66 |
if (!@fields) { |
66 |
if ( !@fields ) { |
67 |
$no_target = 1; |
67 |
$no_target = 1; |
68 |
} |
68 |
} else { |
69 |
else { |
69 |
if ( $operator eq '?' ) { |
70 |
if ($operator eq '?') { |
|
|
71 |
return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; |
70 |
return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; |
72 |
} elsif ($operator eq '!?') { |
71 |
} elsif ( $operator eq '!?' ) { |
73 |
return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; |
72 |
return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields; |
74 |
} else { |
73 |
} else { |
75 |
my $op; |
74 |
my $op; |
76 |
if (exists $operators{$operator}) { |
75 |
if ( exists $operators{$operator} ) { |
77 |
$op = $operators{$operator}; |
76 |
$op = $operators{$operator}; |
78 |
} else { |
77 |
} else { |
79 |
die("Invalid operator: $op"); |
78 |
die("Invalid operator: $op"); |
80 |
} |
79 |
} |
81 |
my @target_values = map { $subfield ? $_->subfield($subfield) : ($_->data()) } @fields; |
80 |
my @target_values = map { $subfield ? $_->subfield($subfield) : ( $_->data() ) } @fields; |
82 |
if (!@target_values) { |
81 |
if ( !@target_values ) { |
83 |
$no_target = 1; |
82 |
$no_target = 1; |
84 |
} |
83 |
} else { |
85 |
else { |
84 |
return unless all { $op->( $_, $match_value ) } @target_values; |
86 |
return unless all { $op->($_, $match_value) } @target_values; |
|
|
87 |
} |
85 |
} |
88 |
} |
86 |
} |
89 |
} |
87 |
} |
Lines 115-145
sub _get_record_for_export {
Link Here
|
115 |
} |
113 |
} |
116 |
|
114 |
|
117 |
sub _get_deleted_biblio_for_export { |
115 |
sub _get_deleted_biblio_for_export { |
118 |
my ($params) = @_; |
116 |
my ($params) = @_; |
119 |
my $biblionumber = $params->{biblionumber}; |
117 |
my $biblionumber = $params->{biblionumber}; |
|
|
118 |
|
120 |
# Creating schema is expensive, allow caller to |
119 |
# Creating schema is expensive, allow caller to |
121 |
# pass it so don't have to recreate for each call |
120 |
# pass it so don't have to recreate for each call |
122 |
my $resultset = $params->{resultset} || Koha::Database->new() |
121 |
my $resultset = $params->{resultset} || Koha::Database->new()->schema()->resultset('DeletedbiblioMetadata'); |
123 |
->schema() |
122 |
my $marc_flavour = C4::Context->preference('marcflavour'); |
124 |
->resultset('DeletedbiblioMetadata'); |
123 |
my $biblio_metadata = $resultset->find( |
125 |
my $marc_flavour = C4::Context->preference('marcflavour'); |
124 |
{ |
126 |
my $biblio_metadata = $resultset->find({ |
125 |
'biblionumber' => $biblionumber, |
127 |
'biblionumber' => $biblionumber, |
126 |
'format' => 'marcxml', |
128 |
'format' => 'marcxml', |
127 |
'schema' => $marc_flavour |
129 |
'schema' => $marc_flavour |
128 |
} |
130 |
}); |
129 |
); |
131 |
my $marc_xml = $biblio_metadata->metadata; |
130 |
my $marc_xml = $biblio_metadata->metadata; |
132 |
$marc_xml = StripNonXmlChars($marc_xml); |
131 |
$marc_xml = StripNonXmlChars($marc_xml); |
133 |
|
132 |
|
134 |
my $record = eval { |
133 |
my $record = eval { MARC::Record::new_from_xml( $marc_xml, 'UTF-8', $marc_flavour ) }; |
135 |
MARC::Record::new_from_xml($marc_xml, 'UTF-8', $marc_flavour) |
134 |
if ( !$record ) { |
136 |
}; |
135 |
Koha::Logger->get->warn("Failed to load MARCXML for deleted biblio with biblionumber \"$biblionumber\": $@"); |
137 |
if (!$record) { |
|
|
138 |
Koha::Logger->get->warn( |
139 |
"Failed to load MARCXML for deleted biblio with biblionumber \"$biblionumber\": $@" |
140 |
); |
141 |
return; |
136 |
return; |
142 |
} |
137 |
} |
|
|
138 |
|
143 |
# Set deleted flag (record status, position 05) |
139 |
# Set deleted flag (record status, position 05) |
144 |
my $leader = $record->leader; |
140 |
my $leader = $record->leader; |
145 |
substr $leader, 5, 1, 'd'; |
141 |
substr $leader, 5, 1, 'd'; |
Lines 148-155
sub _get_deleted_biblio_for_export {
Link Here
|
148 |
} |
144 |
} |
149 |
|
145 |
|
150 |
sub _get_authority_for_export { |
146 |
sub _get_authority_for_export { |
151 |
my ($params) = @_; |
147 |
my ($params) = @_; |
152 |
my $authid = $params->{authid} || return; |
148 |
my $authid = $params->{authid} || return; |
153 |
my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid); |
149 |
my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid); |
154 |
return unless $authority; |
150 |
return unless $authority; |
155 |
return $authority->record; |
151 |
return $authority->record; |
Lines 166-175
sub _get_biblio_for_export {
Link Here
|
166 |
my $biblio = Koha::Biblios->find($biblionumber); |
162 |
my $biblio = Koha::Biblios->find($biblionumber); |
167 |
my $record = eval { $biblio->metadata->record }; |
163 |
my $record = eval { $biblio->metadata->record }; |
168 |
|
164 |
|
169 |
if (!$record) { |
165 |
if ( !$record ) { |
170 |
Koha::Logger->get->warn( |
166 |
Koha::Logger->get->warn("Failed to load MARCXML for biblio with biblionumber \"$biblionumber\": $@"); |
171 |
"Failed to load MARCXML for biblio with biblionumber \"$biblionumber\": $@" |
|
|
172 |
); |
173 |
return; |
167 |
return; |
174 |
} |
168 |
} |
175 |
|
169 |
|
Lines 184-195
sub _get_biblio_for_export {
Link Here
|
184 |
record => $record, |
178 |
record => $record, |
185 |
embed_items => 1, |
179 |
embed_items => 1, |
186 |
biblionumber => $biblionumber, |
180 |
biblionumber => $biblionumber, |
187 |
itemnumbers => $itemnumbers, |
181 |
itemnumbers => $itemnumbers, |
188 |
} |
182 |
} |
189 |
); |
183 |
); |
190 |
if ($only_export_items_for_branches && @$only_export_items_for_branches) { |
184 |
if ( $only_export_items_for_branches && @$only_export_items_for_branches ) { |
191 |
my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches; |
185 |
my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches; |
192 |
my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch' ); |
186 |
my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField('items.homebranch'); |
193 |
|
187 |
|
194 |
for my $itemfield ( $record->field($homebranchfield) ) { |
188 |
for my $itemfield ( $record->field($homebranchfield) ) { |
195 |
my $homebranch = $itemfield->subfield($homebranchsubfield); |
189 |
my $homebranch = $itemfield->subfield($homebranchsubfield); |
Lines 206-212
sub export {
Link Here
|
206 |
my ($params) = @_; |
200 |
my ($params) = @_; |
207 |
|
201 |
|
208 |
my $record_type = $params->{record_type}; |
202 |
my $record_type = $params->{record_type}; |
209 |
my $record_ids = $params->{record_ids} || []; |
203 |
my $record_ids = $params->{record_ids} || []; |
210 |
my $deleted_record_ids = $params->{deleted_record_ids} || []; |
204 |
my $deleted_record_ids = $params->{deleted_record_ids} || []; |
211 |
my $format = $params->{format}; |
205 |
my $format = $params->{format}; |
212 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
206 |
my $itemnumbers = $params->{itemnumbers} || []; # Does not make sense with record_type eq auths |
Lines 215-228
sub export {
Link Here
|
215 |
my $csv_profile_id = $params->{csv_profile_id}; |
209 |
my $csv_profile_id = $params->{csv_profile_id}; |
216 |
my $output_filepath = $params->{output_filepath}; |
210 |
my $output_filepath = $params->{output_filepath}; |
217 |
|
211 |
|
218 |
if( !$record_type ) { |
212 |
if ( !$record_type ) { |
219 |
Koha::Logger->get->warn( "No record_type given." ); |
213 |
Koha::Logger->get->warn("No record_type given."); |
220 |
return; |
214 |
return; |
221 |
} |
215 |
} |
222 |
return unless @{$record_ids} || @{$deleted_record_ids} && $format ne 'csv'; |
216 |
return unless @{$record_ids} || @{$deleted_record_ids} && $format ne 'csv'; |
223 |
|
217 |
|
224 |
my $fh; |
218 |
my $fh; |
225 |
if ( $output_filepath ) { |
219 |
if ($output_filepath) { |
226 |
open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)"; |
220 |
open $fh, '>', $output_filepath or die "Cannot open file $output_filepath ($!)"; |
227 |
select $fh; |
221 |
select $fh; |
228 |
binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv'; |
222 |
binmode $fh, ':encoding(UTF-8)' unless $format eq 'csv'; |
Lines 230-286
sub export {
Link Here
|
230 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
224 |
binmode STDOUT, ':encoding(UTF-8)' unless $format eq 'csv'; |
231 |
} |
225 |
} |
232 |
|
226 |
|
233 |
if ($format eq 'xml' || $format eq 'iso2709') { |
227 |
if ( $format eq 'xml' || $format eq 'iso2709' ) { |
234 |
my @records; |
228 |
my @records; |
235 |
@records = map { |
229 |
@records = map { |
236 |
my $record = _get_record_for_export({ %{$params}, record_id => $_ }); |
230 |
my $record = _get_record_for_export( { %{$params}, record_id => $_ } ); |
237 |
$record ? $record : (); |
231 |
$record ? $record : (); |
238 |
} @{$record_ids}; |
232 |
} @{$record_ids}; |
239 |
|
233 |
|
240 |
my @deleted_records; |
234 |
my @deleted_records; |
241 |
if (@{$deleted_record_ids}) { |
235 |
if ( @{$deleted_record_ids} ) { |
242 |
my $resultset = Koha::Database->new() |
236 |
my $resultset = Koha::Database->new()->schema()->resultset('DeletedbiblioMetadata'); |
243 |
->schema() |
|
|
244 |
->resultset('DeletedbiblioMetadata'); |
245 |
|
237 |
|
246 |
@deleted_records = map { |
238 |
@deleted_records = map { |
247 |
my $record = _get_record_for_export({ |
239 |
my $record = _get_record_for_export( |
248 |
%{$params}, |
240 |
{ |
249 |
record_type => 'deleted_bibs', |
241 |
%{$params}, |
250 |
record_id => $_, |
242 |
record_type => 'deleted_bibs', |
251 |
resultset => $resultset |
243 |
record_id => $_, |
252 |
}); |
244 |
resultset => $resultset |
|
|
245 |
} |
246 |
); |
253 |
$record ? $record : (); |
247 |
$record ? $record : (); |
254 |
} @{$deleted_record_ids}; |
248 |
} @{$deleted_record_ids}; |
255 |
} |
249 |
} |
256 |
if ( $format eq 'iso2709' ) { |
250 |
if ( $format eq 'iso2709' ) { |
257 |
my $encoding_validator = sub { |
251 |
my $encoding_validator = sub { |
258 |
my ($record, $record_type) = @_; |
252 |
my ( $record, $record_type ) = @_; |
259 |
my $errorcount_on_decode = eval { scalar(MARC::File::USMARC->decode($record->as_usmarc)->warnings()) }; |
253 |
my $errorcount_on_decode = |
260 |
if ($errorcount_on_decode || $@) { |
254 |
eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) }; |
261 |
my ($id_tag, $id_subfield) = GetMarcFromKohaField('biblio.biblionumber', ''); |
255 |
if ( $errorcount_on_decode || $@ ) { |
262 |
my $record_id = $record->subfield($id_tag, $id_subfield); |
256 |
my ( $id_tag, $id_subfield ) = GetMarcFromKohaField( 'biblio.biblionumber', '' ); |
263 |
my $msg = "$record_type $record_id could not be USMARC decoded/encoded. " . ($@ // ''); |
257 |
my $record_id = $record->subfield( $id_tag, $id_subfield ); |
|
|
258 |
my $msg = "$record_type $record_id could not be USMARC decoded/encoded. " . ( $@ // '' ); |
264 |
chomp $msg; |
259 |
chomp $msg; |
265 |
Koha::Logger->get->warn($msg); |
260 |
Koha::Logger->get->warn($msg); |
266 |
return 0; |
261 |
return 0; |
267 |
} |
262 |
} |
268 |
return 1; |
263 |
return 1; |
269 |
}; |
264 |
}; |
270 |
for my $record (grep { $encoding_validator->($_, 'Record') } @records) { |
265 |
for my $record ( grep { $encoding_validator->( $_, 'Record' ) } @records ) { |
271 |
print $record->as_usmarc(); |
266 |
print $record->as_usmarc(); |
272 |
} |
267 |
} |
273 |
if (@deleted_records) { |
268 |
if (@deleted_records) { |
274 |
for my $deleted_record (grep { $encoding_validator->($_, 'Deleted record') } @deleted_records) { |
269 |
for my $deleted_record ( grep { $encoding_validator->( $_, 'Deleted record' ) } @deleted_records ) { |
275 |
print $deleted_record->as_usmarc(); |
270 |
print $deleted_record->as_usmarc(); |
276 |
} |
271 |
} |
277 |
} |
272 |
} |
278 |
} elsif ( $format eq 'xml' ) { |
273 |
} elsif ( $format eq 'xml' ) { |
279 |
my $marcflavour = C4::Context->preference("marcflavour"); |
274 |
my $marcflavour = C4::Context->preference("marcflavour"); |
280 |
MARC::File::XML->default_record_format( ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
275 |
MARC::File::XML->default_record_format( |
|
|
276 |
( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) ? 'UNIMARCAUTH' : $marcflavour ); |
281 |
print MARC::File::XML::header(); |
277 |
print MARC::File::XML::header(); |
282 |
print "\n"; |
278 |
print "\n"; |
283 |
for my $record (@records, @deleted_records) { |
279 |
for my $record ( @records, @deleted_records ) { |
284 |
print MARC::File::XML::record($record); |
280 |
print MARC::File::XML::record($record); |
285 |
print "\n"; |
281 |
print "\n"; |
286 |
} |
282 |
} |
Lines 289-295
sub export {
Link Here
|
289 |
} |
285 |
} |
290 |
} elsif ( $format eq 'csv' ) { |
286 |
} elsif ( $format eq 'csv' ) { |
291 |
die 'There is no valid csv profile defined for this export' |
287 |
die 'There is no valid csv profile defined for this export' |
292 |
unless Koha::CsvProfiles->find( $csv_profile_id ); |
288 |
unless Koha::CsvProfiles->find($csv_profile_id); |
293 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
289 |
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers ); |
294 |
} |
290 |
} |
295 |
close $fh if $output_filepath; |
291 |
close $fh if $output_filepath; |