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 (-29 / +18 lines)
Lines 502-508 RECORD: foreach my $record (@{$marc_records}) { Link Here
502
            }
502
            }
503
        }
503
        }
504
        else {
504
        else {
505
            my ($biblioitemnumber, $itemnumbers_ref, $errors_ref, $record_id);
505
            my ($biblioitemnumber, $itemnumbers, $errors, $record_id);
506
            # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
506
            # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
507
            if (!$matched_record_id && $isbn_check && $isbn) {
507
            if (!$matched_record_id && $isbn_check && $isbn) {
508
                $sth_isbn->execute($isbn);
508
                $sth_isbn->execute($isbn);
Lines 551-557 RECORD: foreach my $record (@{$marc_records}) { Link Here
551
                }
551
                }
552
            }
552
            }
553
            elsif ($insert) {
553
            elsif ($insert) {
554
                eval { ($record_id, $biblioitemnumber) = AddBiblio($record, $framework, { defer_marc_save => 1 }) };
554
                eval { ($record_id, $biblioitemnumber) = AddBiblio($record, $framework) };
555
                if ($@) {
555
                if ($@) {
556
                    warn "ERROR: Insert biblio $originalid failed: $@\n";
556
                    warn "ERROR: Insert biblio $originalid failed: $@\n";
557
                    printlog( { id => $originalid, op => "insert", status => "ERROR" } ) if ($logfile);
557
                    printlog( { id => $originalid, op => "insert", status => "ERROR" } ) if ($logfile);
Lines 570-585 RECORD: foreach my $record (@{$marc_records}) { Link Here
570
            if ($record_id) {
570
            if ($record_id) {
571
                $yamlhash->{$originalid} = $record_id if $yamlfile;
571
                $yamlhash->{$originalid} = $record_id if $yamlfile;
572
                # TODO: Add option for skipping items?
572
                # TODO: Add option for skipping items?
573
                eval { ($itemnumbers_ref, $errors_ref) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, $framework); };
573
                eval { ($itemnumbers, $errors) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, $framework); };
574
                $record_has_added_items = @{$itemnumbers_ref};
575
                my $error_adding = $@;
574
                my $error_adding = $@;
576
                # Work on a clone so that if there are real errors, we can maybe
575
                $record_has_added_items = @{$itemnumbers};
577
                # fix them up later.
578
                my $clone_record = $record->clone();
579
                C4::Biblio::_strip_item_fields($clone_record, $framework);
580
                # This sets the marc fields if there was an error, and also calls
581
                # defer_marc_save.
582
                ModBiblioMarc($clone_record, $record_id, $modify_biblio_marc_options);
583
                if ($error_adding) {
576
                if ($error_adding) {
584
                    warn "ERROR: Adding items to bib $record_id failed: $error_adding";
577
                    warn "ERROR: Adding items to bib $record_id failed: $error_adding";
585
                    printlog({ id => $record_id, op => "insert items", status => "ERROR"}) if ($logfile);
578
                    printlog({ id => $record_id, op => "insert items", status => "ERROR"}) if ($logfile);
Lines 590-609 RECORD: foreach my $record (@{$marc_records}) { Link Here
590
                else {
583
                else {
591
                    printlog({ id => $record_id, op => "insert items", status => "ok" }) if ($logfile);
584
                    printlog({ id => $record_id, op => "insert items", status => "ok" }) if ($logfile);
592
                }
585
                }
593
                if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @$errors_ref) {
586
                if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @{$errors}) {
594
                    # Find the record called 'barcode'
587
                    # Find the record called 'barcode'
595
                    my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode');
588
                    my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode');
596
                    # Now remove any items that didn't have a duplicate_barcode error,
589
                    # Now remove any items that didn't have a duplicate_barcode error,
597
                    # erase the barcodes on items that did, and re-add those items.
590
                    # erase the barcodes on items that did, and re-add those items.
598
                    my %dupes;
591
                    my %dupes;
599
                    # FIXME: This could cause array out of bounds because shifting down rest of items on array item delete?
592
                    # FIXME: This could cause array out of bounds because shifting down rest of items on array item delete?
600
                    foreach my $i (0 .. $#{$errors_ref}) {
593
                    foreach my $i (0 .. $#{$errors}) {
601
                        my $ref = $errors_ref->[$i];
594
                        my $error = $errors->[$i];
602
                        if ($ref && ($ref->{error_code} eq 'duplicate_barcode')) {
595
                        if ($error && ($error->{error_code} eq 'duplicate_barcode')) {
603
                            $dupes{$ref->{item_sequence}} = 1;
596
                            $dupes{$error->{item_sequence}} = 1;
604
                            # Delete the error message because we're going to
597
                            # Delete the error message because we're going to
605
                            # retry this one.
598
                            # retry this one.
606
                            delete $errors_ref->[$i];
599
                            delete $errors->[$i];
607
                        }
600
                        }
608
                    }
601
                    }
609
                    my $seq = 0;
602
                    my $seq = 0;
Lines 621-646 RECORD: foreach my $record (@{$marc_records}) { Link Here
621
                    }
614
                    }
622
                    # Now re-add the record as before, adding errors to the prev list
615
                    # Now re-add the record as before, adding errors to the prev list
623
                    my $more_errors;
616
                    my $more_errors;
624
                    eval { ($itemnumbers_ref, $more_errors) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, ''); };
617
                    eval { ($itemnumbers, $more_errors) = AddItemBatchFromMarc($record, $record_id, $biblioitemnumber, ''); };
625
                    $record_has_added_items ||= @{$itemnumbers_ref};
618
                    $error_adding = $@;
626
                    if ($@) {
619
                    $record_has_added_items ||= @{$itemnumbers};
627
                        warn "ERROR: Adding items to bib $record_id failed: $@\n";
620
                    if ($error_adding) {
621
                        warn "ERROR: Adding items to bib $record_id failed: $error_adding\n";
628
                        printlog({ id => $record_id, op => "insert items", status => "ERROR" }) if ($logfile);
622
                        printlog({ id => $record_id, op => "insert items", status => "ERROR" }) if ($logfile);
629
                        # if we failed because of an exception, assume that
623
                        # if we failed because of an exception, assume that
630
                        # the MARC columns in biblioitems were not set.
624
                        # the MARC columns in biblioitems were not set.
631
632
                        # @FIXME: Why do we save here without stripping items? Besides,
633
                        # save with stripped items has already been performed
634
                        ModBiblioMarc($record, $record_id, $modify_biblio_marc_options);
635
                        next RECORD;
625
                        next RECORD;
636
                    }
626
                    }
637
                    else {
627
                    else {
638
                        printlog({ id => $record_id, op => "insert", status => "ok" }) if ($logfile);
628
                        printlog({ id => $record_id, op => "insert", status => "ok" }) if ($logfile);
639
                    }
629
                    }
640
                    push @$errors_ref, @{$more_errors};
630
                    push @{$errors}, @{$more_errors};
641
                }
631
                }
642
                if (@{$errors_ref}) {
632
                if (@{$errors}) {
643
                    report_item_errors($record_id, $errors_ref);
633
                    report_item_errors($record_id, $errors);
644
                }
634
                }
645
                C4::Biblio::_strip_item_fields($record, $framework);
635
                C4::Biblio::_strip_item_fields($record, $framework);
646
                if ($record_has_added_items || $matched_record_id) {
636
                if ($record_has_added_items || $matched_record_id) {
647
- 

Return to bug 25539