Lines 29-34
use C4::AuthoritiesMarc;
Link Here
|
29 |
use C4::MarcModificationTemplates; |
29 |
use C4::MarcModificationTemplates; |
30 |
use DateTime; |
30 |
use DateTime; |
31 |
use DateTime::Format::Strptime; |
31 |
use DateTime::Format::Strptime; |
|
|
32 |
use Koha::Database; |
32 |
use Koha::Plugins::Handler; |
33 |
use Koha::Plugins::Handler; |
33 |
use Koha::Logger; |
34 |
use Koha::Logger; |
34 |
|
35 |
|
Lines 49-54
BEGIN {
Link Here
|
49 |
AddItemsToImportBiblio |
50 |
AddItemsToImportBiblio |
50 |
ModAuthorityInBatch |
51 |
ModAuthorityInBatch |
51 |
ModBiblioInBatch |
52 |
ModBiblioInBatch |
|
|
53 |
CheckRecordControlNumberInBatch |
52 |
|
54 |
|
53 |
BatchStageMarcRecords |
55 |
BatchStageMarcRecords |
54 |
BatchFindDuplicates |
56 |
BatchFindDuplicates |
Lines 312-317
sub ModBiblioInBatch {
Link Here
|
312 |
|
314 |
|
313 |
} |
315 |
} |
314 |
|
316 |
|
|
|
317 |
=head2 CheckRecordControlNumberInBatch |
318 |
|
319 |
CheckRecordControlNumberInBatch( $marc_record, $batch_id[, $import_record_id] ); |
320 |
|
321 |
Check to see if the given record's control number is already in use in the given batch. If provided, |
322 |
will check only for conflicts in records besides the given C<$import_batch_id>. |
323 |
|
324 |
If a record with the given control number in the given batch exists, will return its C<import_record_id>. |
325 |
|
326 |
=cut |
327 |
|
328 |
sub CheckRecordControlNumberInBatch { |
329 |
my ( $marc_record, $batch_id, $import_record_id ) = @_; |
330 |
|
331 |
return unless ( $marc_record->field('001') ); |
332 |
|
333 |
my %extra_conditions; |
334 |
|
335 |
$extra_conditions{'import_record.import_record_id'} = { '!=', $import_record_id } if ( $import_record_id ); |
336 |
|
337 |
my $control_number = $marc_record->field('001')->data; |
338 |
|
339 |
my $schema = Koha::Database->new()->schema(); |
340 |
return $schema->resultset('ImportBiblio')->search( |
341 |
{ |
342 |
control_number => $control_number, |
343 |
'import_record.import_batch_id' => $batch_id, |
344 |
%extra_conditions |
345 |
}, |
346 |
{ |
347 |
join => 'import_record', |
348 |
} |
349 |
)->get_column('import_record.import_batch_id')->first(); |
350 |
} |
351 |
|
315 |
=head2 AddAuthToBatch |
352 |
=head2 AddAuthToBatch |
316 |
|
353 |
|
317 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
354 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
Lines 352-428
sub ModAuthInBatch {
Link Here
|
352 |
|
389 |
|
353 |
=head2 BatchStageMarcRecords |
390 |
=head2 BatchStageMarcRecords |
354 |
|
391 |
|
355 |
( $batch_id, $num_records, $num_items, @invalid_records ) = |
392 |
{ |
356 |
BatchStageMarcRecords( |
393 |
batch_id => $batch_id, |
357 |
$record_type, $encoding, |
394 |
num_records => $num_records, |
358 |
$marc_records, $file_name, |
395 |
num_items => $num_items, |
359 |
$marc_modification_template, $comments, |
396 |
invalid_records => \@invalid_records |
360 |
$branch_code, $parse_items, |
397 |
} = BatchStageMarcRecords( { |
361 |
$leave_as_staging, $progress_interval, |
398 |
record_type => $record_type, |
362 |
$progress_callback |
399 |
encoding => $encoding, |
363 |
); |
400 |
marc_records => $marc_records, |
|
|
401 |
file_name => $file_name, |
402 |
comments => $comments, |
403 |
[ control_number_handling => 'preserve' | 'overwrite' ], |
404 |
[ existing_batch_id => $existing_batch_id, ] |
405 |
[ leave_as_staging => 1 ] |
406 |
[ marc_modification_template => $marc_modification_template, ] |
407 |
[ parse_items => 1 ] |
408 |
[ progress_interval => $progress_interval, |
409 |
progress_callback => \&progress_callback, ] |
410 |
[ timestamp_update => 'now' ], |
411 |
[ to_marc_plugin => $to_marc_plugin, ] |
412 |
} ); |
413 |
|
414 |
Any optional parameters are assumed to be no / off if omitted. If C<progress_interval> is specified, C<progress_callback> must be as well. |
364 |
|
415 |
|
365 |
=cut |
416 |
=cut |
366 |
|
417 |
|
367 |
sub BatchStageMarcRecords { |
418 |
sub BatchStageMarcRecords { |
368 |
my $record_type = shift; |
419 |
my $params = shift; |
369 |
my $encoding = shift; |
|
|
370 |
my $marc_records = shift; |
371 |
my $file_name = shift; |
372 |
my $marc_modification_template = shift; |
373 |
my $comments = shift; |
374 |
my $branch_code = shift; |
375 |
my $parse_items = shift; |
376 |
my $leave_as_staging = shift; |
377 |
|
420 |
|
378 |
# optional callback to monitor status |
421 |
# optional callback to monitor status |
379 |
# of job |
422 |
# of job |
380 |
my $progress_interval = 0; |
423 |
my $progress_interval = 0; |
381 |
my $progress_callback = undef; |
424 |
my $progress_callback = undef; |
382 |
if ($#_ >= 1) { |
425 |
if ( $params->{progress_interval} ) { |
383 |
$progress_interval = shift; |
426 |
$progress_interval = $params->{progress_interval}; |
384 |
$progress_callback = shift; |
427 |
$progress_callback = $params->{progress_callback}; |
385 |
$progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0; |
428 |
$progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0; |
386 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
429 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
387 |
} |
430 |
} |
388 |
my $existing_batch_id = shift; |
|
|
389 |
|
431 |
|
390 |
my $batch_id; |
432 |
my $batch_id; |
391 |
|
433 |
|
392 |
if ( $existing_batch_id ) { |
434 |
if ( $params->{existing_batch_id} ) { |
393 |
$batch_id = $existing_batch_id; |
435 |
$batch_id = $params->{existing_batch_id}; |
394 |
} else { |
436 |
} else { |
395 |
$batch_id = AddImportBatch( { |
437 |
$batch_id = AddImportBatch( { |
396 |
overlay_action => 'create_new', |
438 |
overlay_action => 'create_new', |
397 |
import_status => 'staging', |
439 |
import_status => 'staging', |
398 |
batch_type => 'batch', |
440 |
batch_type => 'batch', |
399 |
file_name => $file_name, |
441 |
file_name => $params->{file_name}, |
400 |
comments => $comments, |
442 |
comments => $params->{comments}, |
401 |
record_type => $record_type, |
443 |
record_type => $params->{record_type}, |
402 |
} ); |
444 |
} ); |
403 |
} |
445 |
} |
404 |
|
446 |
|
405 |
if ($parse_items) { |
447 |
if ($params->{parse_items}) { |
406 |
SetImportBatchItemAction($batch_id, 'always_add'); |
448 |
SetImportBatchItemAction($batch_id, 'always_add'); |
407 |
} else { |
449 |
} else { |
408 |
SetImportBatchItemAction($batch_id, 'ignore'); |
450 |
SetImportBatchItemAction($batch_id, 'ignore'); |
409 |
} |
451 |
} |
410 |
|
452 |
|
|
|
453 |
$params->{marc_records} = Koha::Plugins::Handler->run( |
454 |
{ |
455 |
class => $params->{to_marc_plugin}, |
456 |
method => 'to_marc', |
457 |
params => { data => $params->{marc_records} } |
458 |
} |
459 |
) if $params->{to_marc_plugin} && $params->{marc_records}; |
411 |
|
460 |
|
412 |
my $marc_type = C4::Context->preference('marcflavour'); |
461 |
my $marc_type = C4::Context->preference('marcflavour'); |
413 |
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth'); |
462 |
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $params->{record_type} eq 'auth'); |
414 |
my @invalid_records = (); |
463 |
my @invalid_records = (); |
415 |
my $num_valid = 0; |
464 |
my $num_valid = 0; |
416 |
my $num_items = 0; |
465 |
my $num_items = 0; |
|
|
466 |
my $num_matched_control_number = 0; |
417 |
# FIXME - for now, we're dealing only with bibs |
467 |
# FIXME - for now, we're dealing only with bibs |
418 |
my $rec_num = 0; |
468 |
my $rec_num = 0; |
419 |
foreach my $marc_record (@$marc_records) { |
469 |
foreach my $marc_blob ($params->{marc_records}) { |
|
|
470 |
$marc_blob =~ s/^\s+//g; |
471 |
$marc_blob =~ s/\s+$//g; |
472 |
next unless $marc_blob; |
420 |
$rec_num++; |
473 |
$rec_num++; |
421 |
if ($progress_interval and (0 == ($rec_num % $progress_interval))) { |
474 |
if ($progress_interval and (0 == ($rec_num % $progress_interval))) { |
422 |
&$progress_callback($rec_num); |
475 |
&$progress_callback($rec_num); |
423 |
} |
476 |
} |
|
|
477 |
my ($marc_record, $charset_guessed, $char_errors) = |
478 |
MarcToUTF8Record($marc_blob, $marc_type, $params->{encoding}); |
479 |
|
480 |
$params->{encoding} = $charset_guessed unless $params->{encoding}; |
424 |
|
481 |
|
425 |
ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template ); |
482 |
ModifyRecordWithTemplate( $params->{marc_modification_template}, $marc_record ) if ( $params->{marc_modification_template} ); |
426 |
|
483 |
|
427 |
my $import_record_id; |
484 |
my $import_record_id; |
428 |
if (scalar($marc_record->fields()) == 0) { |
485 |
if (scalar($marc_record->fields()) == 0) { |
Lines 431-455
sub BatchStageMarcRecords {
Link Here
|
431 |
|
488 |
|
432 |
# Normalize the record so it doesn't have separated diacritics |
489 |
# Normalize the record so it doesn't have separated diacritics |
433 |
SetUTF8Flag($marc_record); |
490 |
SetUTF8Flag($marc_record); |
434 |
|
|
|
435 |
$num_valid++; |
491 |
$num_valid++; |
436 |
if ($record_type eq 'biblio') { |
492 |
|
437 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0); |
493 |
C4::Biblio::UpdateMarcTimestamp( $marc_record ) if ( $params->{timestamp_update} eq 'now' ); |
438 |
if ($parse_items) { |
494 |
my $existing_import_record_id; |
|
|
495 |
|
496 |
if ( $params->{control_number_handling} ) { |
497 |
$existing_import_record_id = CheckRecordControlNumberInBatch( $marc_record, $batch_id ); |
498 |
|
499 |
if ( $existing_import_record_id ) { |
500 |
$num_matched_control_number++; |
501 |
|
502 |
next if ( $params->{control_number_handling} eq 'preserve' ); |
503 |
} |
504 |
} |
505 |
|
506 |
if ($params->{record_type} eq 'biblio') { |
507 |
if ( $existing_import_record_id ) { |
508 |
$import_record_id = $existing_import_record_id; |
509 |
ModBiblioInBatch( $import_record_id, $marc_record ); |
510 |
} else { |
511 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $params->{encoding}, int(rand(99999)), 0); |
512 |
} |
513 |
|
514 |
if ($params->{parse_items}) { |
439 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
515 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
440 |
$num_items += scalar(@import_items_ids); |
516 |
$num_items += scalar(@import_items_ids); |
441 |
} |
517 |
} |
442 |
} elsif ($record_type eq 'auth') { |
518 |
} elsif ($params->{record_type} eq 'auth') { |
443 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type); |
519 |
if ( $existing_import_record_id ) { |
|
|
520 |
$import_record_id = $existing_import_record_id; |
521 |
ModAuthInBatch( $import_record_id, $marc_record ); |
522 |
} else { |
523 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $params->{encoding}, int(rand(99999)), 0, $marc_type); |
524 |
} |
444 |
} |
525 |
} |
445 |
} |
526 |
} |
446 |
} |
527 |
} |
447 |
unless ($leave_as_staging) { |
528 |
unless ($params->{leave_as_staging}) { |
448 |
SetImportBatchStatus($batch_id, 'staged'); |
529 |
SetImportBatchStatus($batch_id, 'staged'); |
449 |
} |
530 |
} |
450 |
# FIXME branch_code, number of bibs, number of items |
531 |
# FIXME branch_code, number of bibs, number of items |
451 |
_update_batch_record_counts($batch_id); |
532 |
_update_batch_record_counts($batch_id); |
452 |
return ($batch_id, $num_valid, $num_items, @invalid_records); |
533 |
return { |
|
|
534 |
batch_id => $batch_id, |
535 |
num_items => $num_items, |
536 |
num_valid => $num_valid, |
537 |
num_matched_control_number => $num_matched_control_number, |
538 |
invalid_records => \@invalid_records |
539 |
}; |
453 |
} |
540 |
} |
454 |
|
541 |
|
455 |
=head2 AddItemsToImportBiblio |
542 |
=head2 AddItemsToImportBiblio |
Lines 468-473
sub AddItemsToImportBiblio {
Link Here
|
468 |
my @import_items_ids = (); |
555 |
my @import_items_ids = (); |
469 |
|
556 |
|
470 |
my $dbh = C4::Context->dbh; |
557 |
my $dbh = C4::Context->dbh; |
|
|
558 |
$dbh->do( "DELETE FROM import_items WHERE import_record_id = ?", undef, $import_record_id ); |
471 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); |
559 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); |
472 |
foreach my $item_field ($marc_record->field($item_tag)) { |
560 |
foreach my $item_field ($marc_record->field($item_tag)) { |
473 |
my $item_marc = MARC::Record->new(); |
561 |
my $item_marc = MARC::Record->new(); |