Bug 31177: Fix misplaced import in C4::ILSDI::Services
[srvgit] / C4 / ImportBatch.pm
index d4e1a41..ee76020 100644 (file)
@@ -35,6 +35,8 @@ use C4::Charset qw( MarcToUTF8Record SetUTF8Flag StripNonXmlChars );
 use C4::AuthoritiesMarc qw( AddAuthority GuessAuthTypeCode GetAuthorityXML ModAuthority DelAuthority );
 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
 use Koha::Items;
+use Koha::SearchEngine;
+use Koha::SearchEngine::Indexer;
 use Koha::Plugins::Handler;
 use Koha::Logger;
 
@@ -46,8 +48,6 @@ BEGIN {
       GetZ3950BatchId
       GetWebserviceBatchId
       GetImportRecordMarc
-      GetImportRecordMarcXML
-      GetRecordFromImportBiblio
       AddImportBatch
       GetImportBatch
       AddAuthToBatch
@@ -191,17 +191,6 @@ sub GetImportRecordMarc {
     return $marc, $encoding;
 }
 
-sub GetRecordFromImportBiblio {
-    my ( $import_record_id, $embed_items ) = @_;
-
-    my ($marc) = GetImportRecordMarc($import_record_id);
-    my $record = MARC::Record->new_from_usmarc($marc);
-
-    EmbedItemsInImportBiblio( $record, $import_record_id ) if $embed_items;
-
-    return $record;
-}
-
 sub EmbedItemsInImportBiblio {
     my ( $record, $import_record_id ) = @_;
     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
@@ -220,24 +209,6 @@ sub EmbedItemsInImportBiblio {
     return $record;
 }
 
-=head2 GetImportRecordMarcXML
-
-  my $marcxml = GetImportRecordMarcXML($import_record_id);
-
-=cut
-
-sub GetImportRecordMarcXML {
-    my ($import_record_id) = @_;
-
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT marcxml FROM import_records WHERE import_record_id = ?");
-    $sth->execute($import_record_id);
-    my ($marcxml) = $sth->fetchrow();
-    $sth->finish();
-    return $marcxml;
-
-}
-
 =head2 AddImportBatch
 
   my $batch_id = AddImportBatch($params_hash);
@@ -404,13 +375,13 @@ sub BatchStageMarcRecords {
 
             $num_valid++;
             if ($record_type eq 'biblio') {
-                $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0);
+                $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, 0);
                 if ($parse_items) {
                     my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0);
                     $num_items += scalar(@import_items_ids);
                 }
             } elsif ($record_type eq 'auth') {
-                $import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type);
+                $import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, 0, $marc_type);
             }
         }
     }
@@ -456,7 +427,6 @@ sub AddItemsToImportBiblio {
 
     if ($#import_items_ids > -1) {
         _update_batch_record_counts($batch_id) if $update_counts;
-        _update_import_record_marc($import_record_id, $marc_record, C4::Context->preference('marcflavour'));
     }
     return @import_items_ids;
 }
@@ -540,6 +510,8 @@ sub BatchCommitRecords {
     my $batch_id = shift;
     my $framework = shift;
 
+    my $schema = Koha::Database->schema;
+
     # optional callback to monitor status 
     # of job
     my $progress_interval = 0;
@@ -578,11 +550,18 @@ sub BatchCommitRecords {
     my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
 
     my $rec_num = 0;
+    my @biblio_ids;
+    $schema->txn_begin; # We commit in a transaction
     while (my $rowref = $sth->fetchrow_hashref) {
         $record_type = $rowref->{'record_type'};
+
         $rec_num++;
+
         if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
-            &$progress_callback($rec_num);
+            # report progress and commit
+            $schema->txn_commit;
+            &$progress_callback( $rec_num );
+            $schema->txn_begin;
         }
         if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
             $num_ignored++;
@@ -600,7 +579,7 @@ sub BatchCommitRecords {
         my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
 
         if ($record_type eq 'biblio') {
-            # remove any item tags - rely on BatchCommitItems
+            # remove any item tags - rely on _batchCommitItems
             ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" );
             foreach my $item_field ($marc_record->field($item_tag)) {
                 $marc_record->delete_field($item_field);
@@ -617,10 +596,11 @@ sub BatchCommitRecords {
             $num_added++;
             if ($record_type eq 'biblio') {
                 my $biblioitemnumber;
-                ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework);
+                ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework, { skip_record_index => 1 });
+                push @biblio_ids, $recordid;
                 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
                 if ($item_result eq 'create_new' || $item_result eq 'replace') {
-                    my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
+                    my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = _batchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result, $biblioitemnumber);
                     $num_items_added += $bib_items_added;
                     $num_items_replaced += $bib_items_replaced;
                     $num_items_errored += $bib_items_errored;
@@ -656,13 +636,20 @@ sub BatchCommitRecords {
                     $context->{userid} = $logged_in_patron->userid;
                 }
 
-                ModBiblio($marc_record, $recordid, $oldbiblio->frameworkcode, {
-                    overlay_context => $context
-                });
+                ModBiblio(
+                    $marc_record,
+                    $recordid,
+                    $oldbiblio->frameworkcode,
+                    {
+                        overlay_context   => $context,
+                        skip_record_index => 1
+                    }
+                );
+                push @biblio_ids, $recordid;
                 $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead
 
                 if ($item_result eq 'create_new' || $item_result eq 'replace') {
-                    my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
+                    my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = _batchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
                     $num_items_added += $bib_items_added;
                     $num_items_replaced += $bib_items_replaced;
                     $num_items_errored += $bib_items_errored;
@@ -685,7 +672,8 @@ sub BatchCommitRecords {
             $recordid = $record_match;
             $num_ignored++;
             if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
-                my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
+                my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = _batchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
+                push @biblio_ids, $recordid if $bib_items_added || $bib_items_replaced;
                 $num_items_added += $bib_items_added;
          $num_items_replaced += $bib_items_replaced;
                 $num_items_errored += $bib_items_errored;
@@ -698,20 +686,29 @@ sub BatchCommitRecords {
             SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
         }
     }
+    $schema->txn_commit; # Commit final records that may not have hit callback threshold
     $sth->finish();
+
+    if ( @biblio_ids ) {
+        my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
+        $indexer->index_records( \@biblio_ids, "specialUpdate", "biblioserver" );
+    }
+
     SetImportBatchStatus($batch_id, 'imported');
     return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
 }
 
-=head2 BatchCommitItems
+=head2 _batchCommitItems
 
   ($num_items_added, $num_items_errored) = 
-         BatchCommitItems($import_record_id, $biblionumber);
+         _batchCommitItems($import_record_id, $biblionumber, [$action, $biblioitemnumber]);
+
+Private function for batch committing item changes. We do not trigger a re-index here, that is left to the caller.
 
 =cut
 
-sub BatchCommitItems {
-    my ( $import_record_id, $biblionumber, $action ) = @_;
+sub _batchCommitItems {
+    my ( $import_record_id, $biblionumber, $action, $biblioitemnumber ) = @_;
 
     my $dbh = C4::Context->dbh;
 
@@ -736,7 +733,7 @@ sub BatchCommitItems {
         my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan' );
         $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
 
-        my $item = TransformMarcToKoha({ record => $item_marc });
+        my $item = TransformMarcToKoha({ record => $item_marc, kohafields => ['items.barcode','items.itemnumber'] });
 
         my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
         my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
@@ -744,7 +741,7 @@ sub BatchCommitItems {
         my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ?, import_error = ? WHERE import_items_id = ?");
         if ( $action eq "replace" && $duplicate_itemnumber ) {
             # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
-            ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
+            ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber}, { skip_record_index => 1 } );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
             $updsth->bind_param( 3, undef );
@@ -754,7 +751,7 @@ sub BatchCommitItems {
             $num_items_replaced++;
         } elsif ( $action eq "replace" && $duplicate_barcode ) {
             my $itemnumber = $duplicate_barcode->itemnumber;
-            ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
+            ModItemFromMarc( $item_marc, $biblionumber, $itemnumber, { skip_record_index => 1 } );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
             $updsth->bind_param( 3, undef );
@@ -774,7 +771,7 @@ sub BatchCommitItems {
             my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" );
             $item_marc->field($itemtag)->delete_subfield( code => $itemsubfield );
 
-            my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
+            my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber, { biblioitemnumber => $biblioitemnumber, skip_record_index => 1 } );
             if( $itemnumber ) {
                 $updsth->bind_param( 1, 'imported' );
                 $updsth->bind_param( 2, $itemnumber );
@@ -1628,16 +1625,6 @@ sub _create_import_record {
     return $import_record_id;
 }
 
-sub _update_import_record_marc {
-    my ($import_record_id, $marc_record, $marc_type) = @_;
-
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
-                             WHERE  import_record_id = ?");
-    $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml($marc_type), $import_record_id);
-    $sth->finish();
-}
-
 sub _add_auth_fields {
     my ($import_record_id, $marc_record) = @_;