View | Details | Raw Unified | Return to bug 25539
Collapse All | Expand All

(-)a/C4/Biblio.pm (-16 / +1 lines)
Lines 187-217 The first argument is a C<MARC::Record> object containing the Link Here
187
bib to add, while the second argument is the desired MARC
187
bib to add, while the second argument is the desired MARC
188
framework code.
188
framework code.
189
189
190
This function also accepts a third, optional argument: a hashref
191
to additional options.  The only defined option is C<defer_marc_save>,
192
which if present and mapped to a true value, causes C<AddBiblio>
193
to omit the call to save the MARC in C<biblio_metadata.metadata>
194
This option is provided B<only>
195
for the use of scripts such as C<bulkmarcimport.pl> that may need
196
to do some manipulation of the MARC record for item parsing before
197
saving it and which cannot afford the performance hit of saving
198
the MARC record twice.  Consequently, do not use that option
199
unless you can guarantee that C<ModBiblioMarc> will be called.
200
201
=cut
190
=cut
202
191
203
sub AddBiblio {
192
sub AddBiblio {
204
    my $record          = shift;
193
    my $record          = shift;
205
    my $frameworkcode   = shift;
194
    my $frameworkcode   = shift;
206
    my $options         = @_ ? shift : undef;
195
    my $options         = @_ ? shift : undef;
207
    my $defer_marc_save = 0;
208
    if (!$record) {
196
    if (!$record) {
209
        carp('AddBiblio called with undefined record');
197
        carp('AddBiblio called with undefined record');
210
        return;
198
        return;
211
    }
199
    }
212
    if ( defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'} ) {
213
        $defer_marc_save = 1;
214
    }
215
200
216
    my $schema = Koha::Database->schema;
201
    my $schema = Koha::Database->schema;
217
    my ( $biblionumber, $biblioitemnumber );
202
    my ( $biblionumber, $biblioitemnumber );
Lines 292-298 sub AddBiblio { Link Here
292
            }
277
            }
293
278
294
            # now add the record
279
            # now add the record
295
            ModBiblioMarc( $record, $biblionumber ) unless $defer_marc_save;
280
            ModBiblioMarc( $record, $biblionumber );
296
281
297
            # update OAI-PMH sets
282
            # update OAI-PMH sets
298
            if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
283
            if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
(-)a/misc/migration_tools/bulkmarcimport.pl (-30 / +18 lines)
Lines 499-505 RECORD: foreach my $record (@{$marc_records}) { Link Here
499
            }
499
            }
500
        }
500
        }
501
        else {
501
        else {
502
            my ($biblioitemnumber, $itemnumbers_ref, $errors_ref, $record_id);
502
            my ($biblioitemnumber, $itemnumbers, $errors, $record_id);
503
            # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
503
            # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
504
            if (!$matched_record_id && $isbn_check && $isbn) {
504
            if (!$matched_record_id && $isbn_check && $isbn) {
505
                $sth_isbn->execute($isbn);
505
                $sth_isbn->execute($isbn);
Lines 548-554 RECORD: foreach my $record (@{$marc_records}) { Link Here
548
                }
548
                }
549
            }
549
            }
550
            elsif ($insert) {
550
            elsif ($insert) {
551
                eval { ($record_id, $biblioitemnumber) = AddBiblio($record, $framework, { defer_marc_save => 1 }) };
551
                eval { ($record_id, $biblioitemnumber) = AddBiblio($record, $framework) };
552
                if ($@) {
552
                if ($@) {
553
                    warn "ERROR: Insert biblio $originalid failed: $@\n";
553
                    warn "ERROR: Insert biblio $originalid failed: $@\n";
554
                    printlog( { id => $originalid, op => "insert", status => "ERROR" } ) if ($logfile);
554
                    printlog( { id => $originalid, op => "insert", status => "ERROR" } ) if ($logfile);
Lines 566-582 RECORD: foreach my $record (@{$marc_records}) { Link Here
566
            my $record_has_added_items = 0;
566
            my $record_has_added_items = 0;
567
            if ($record_id) {
567
            if ($record_id) {
568
                $yamlhash->{$originalid} = $record_id if $yamlfile;
568
                $yamlhash->{$originalid} = $record_id if $yamlfile;
569
                eval { ($itemnumbers_ref, $errors_ref) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, $framework); };
569
                eval { ($itemnumbers, $errors) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, $framework); };
570
                $record_has_added_items = @{$itemnumbers_ref};
571
572
                my $error_adding = $@;
570
                my $error_adding = $@;
573
                # Work on a clone so that if there are real errors, we can maybe
571
                $record_has_added_items = @{$itemnumbers};
574
                # fix them up later.
575
                my $clone_record = $record->clone();
576
                C4::Biblio::_strip_item_fields($clone_record, $framework);
577
                # This sets the marc fields if there was an error, and also calls
578
                # defer_marc_save.
579
                ModBiblioMarc($clone_record, $record_id, $modify_biblio_marc_options);
580
                if ($error_adding) {
572
                if ($error_adding) {
581
                    warn "ERROR: Adding items to bib $record_id failed: $error_adding";
573
                    warn "ERROR: Adding items to bib $record_id failed: $error_adding";
582
                    printlog({ id => $record_id, op => "insert items", status => "ERROR"}) if ($logfile);
574
                    printlog({ id => $record_id, op => "insert items", status => "ERROR"}) if ($logfile);
Lines 587-605 RECORD: foreach my $record (@{$marc_records}) { Link Here
587
                else {
579
                else {
588
                    printlog({ id => $record_id, op => "insert items", status => "ok" }) if ($logfile);
580
                    printlog({ id => $record_id, op => "insert items", status => "ok" }) if ($logfile);
589
                }
581
                }
590
                if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @$errors_ref) {
582
                if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @{$errors}) {
591
                    # Find the record called 'barcode'
583
                    # Find the record called 'barcode'
592
                    my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode');
584
                    my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode');
593
                    # Now remove any items that didn't have a duplicate_barcode error,
585
                    # Now remove any items that didn't have a duplicate_barcode error,
594
                    # erase the barcodes on items that did, and re-add those items.
586
                    # erase the barcodes on items that did, and re-add those items.
595
                    my %dupes;
587
                    my %dupes;
596
                    foreach my $i (0 .. $#{$errors_ref}) {
588
                    foreach my $i (0 .. $#{$errors}) {
597
                        my $ref = $errors_ref->[$i];
589
                        my $error = $errors->[$i];
598
                        if ($ref && ($ref->{error_code} eq 'duplicate_barcode')) {
590
                        if ($error && ($error->{error_code} eq 'duplicate_barcode')) {
599
                            $dupes{$ref->{item_sequence}} = 1;
591
                            $dupes{$error->{item_sequence}} = 1;
600
                            # Delete the error message because we're going to
592
                            # Delete the error message because we're going to
601
                            # retry this one.
593
                            # retry this one.
602
                            delete $errors_ref->[$i];
594
                            delete $errors->[$i];
603
                        }
595
                        }
604
                    }
596
                    }
605
                    my $seq = 0;
597
                    my $seq = 0;
Lines 617-642 RECORD: foreach my $record (@{$marc_records}) { Link Here
617
                    }
609
                    }
618
                    # Now re-add the record as before, adding errors to the prev list
610
                    # Now re-add the record as before, adding errors to the prev list
619
                    my $more_errors;
611
                    my $more_errors;
620
                    eval { ($itemnumbers_ref, $more_errors) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, ''); };
612
                    eval { ($itemnumbers, $more_errors) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, ''); };
621
                    $record_has_added_items ||= @{$itemnumbers_ref};
613
                    $error_adding = $@;
622
                    if ($@) {
614
                    $record_has_added_items ||= @{$itemnumbers};
623
                        warn "ERROR: Adding items to bib $record_id failed: $@\n";
615
                    if ($error_adding) {
616
                        warn "ERROR: Adding items to bib $record_id failed: $error_adding\n";
624
                        printlog({ id => $record_id, op => "insert items", status => "ERROR" }) if ($logfile);
617
                        printlog({ id => $record_id, op => "insert items", status => "ERROR" }) if ($logfile);
625
                        # if we failed because of an exception, assume that
618
                        # if we failed because of an exception, assume that
626
                        # the MARC columns in biblioitems were not set.
619
                        # the MARC columns in biblioitems were not set.
627
628
                        # @FIXME: Why do we save here without stripping items? Besides,
629
                        # save with stripped items has already been performed
630
                        ModBiblioMarc($record, $record_id, $modify_biblio_marc_options);
631
                        next RECORD;
620
                        next RECORD;
632
                    }
621
                    }
633
                    else {
622
                    else {
634
                        printlog({ id => $record_id, op => "insert", status => "ok" }) if ($logfile);
623
                        printlog({ id => $record_id, op => "insert", status => "ok" }) if ($logfile);
635
                    }
624
                    }
636
                    push @$errors_ref, @{$more_errors};
625
                    push @{$errors}, @{$more_errors};
637
                }
626
                }
638
                if (@{$errors_ref}) {
627
                if (@{$errors}) {
639
                    report_item_errors($record_id, $errors_ref);
628
                    report_item_errors($record_id, $errors);
640
                }
629
                }
641
                C4::Biblio::_strip_item_fields($record, $framework);
630
                C4::Biblio::_strip_item_fields($record, $framework);
642
                if ($record_has_added_items || $matched_record_id) {
631
                if ($record_has_added_items || $matched_record_id) {
643
- 

Return to bug 25539