Bug 9988: Merge should have a parameter hash
[koha.git] / C4 / ImportBatch.pm
index 1d387e1..a4f4c55 100644 (file)
@@ -53,6 +53,7 @@ BEGIN {
     BatchCommitRecords
     BatchRevertRecords
     CleanBatch
+    DeleteBatch
 
     GetAllImportBatches
     GetStagedWebserviceBatches
@@ -404,7 +405,7 @@ sub BatchStageMarcRecords {
             method => 'to_marc',
             params => { data => $marc_records }
         }
-    ) if $to_marc_plugin;
+    ) if $to_marc_plugin && @$marc_records;
 
     my $marc_type = C4::Context->preference('marcflavour');
     $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
@@ -413,24 +414,17 @@ sub BatchStageMarcRecords {
     my $num_items = 0;
     # FIXME - for now, we're dealing only with bibs
     my $rec_num = 0;
-    foreach my $marc_blob (split(/\x1D/, $marc_records)) {
-        $marc_blob =~ s/^\s+//g;
-        $marc_blob =~ s/\s+$//g;
-        next unless $marc_blob;
+    foreach my $marc_record (@$marc_records) {
         $rec_num++;
         if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
             &$progress_callback($rec_num);
         }
-        my ($marc_record, $charset_guessed, $char_errors) =
-            MarcToUTF8Record($marc_blob, $marc_type, $encoding);
-
-        $encoding = $charset_guessed unless $encoding;
 
         ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template );
 
         my $import_record_id;
         if (scalar($marc_record->fields()) == 0) {
-            push @invalid_records, $marc_blob;
+            push @invalid_records, $marc_record;
         } else {
 
             # Normalize the record so it doesn't have separated diacritics
@@ -791,12 +785,14 @@ sub BatchCommitItems {
             $num_items_errored++;
         } else {
             my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber );
-            $updsth->bind_param( 1, 'imported' );
-            $updsth->bind_param( 2, $itemnumber );
-            $updsth->bind_param( 3, $row->{'import_items_id'} );
-            $updsth->execute();
-            $updsth->finish();
-            $num_items_added++;
+            if( $itemnumber ) {
+                $updsth->bind_param( 1, 'imported' );
+                $updsth->bind_param( 2, $itemnumber );
+                $updsth->bind_param( 3, $row->{'import_items_id'} );
+                $updsth->execute();
+                $updsth->finish();
+                $num_items_added++;
+            }
         }
     }
 
@@ -856,7 +852,7 @@ sub BatchRevertRecords {
                 $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'});
                 $error = DelBiblio($rowref->{'matched_biblionumber'});
             } else {
-                my $deletedauthid = DelAuthority($rowref->{'matched_authid'});
+                DelAuthority({ authid => $rowref->{'matched_authid'} });
             }
             if (defined $error) {
                 $num_errors++;
@@ -923,7 +919,7 @@ sub BatchRevertItems {
     $sth->bind_param(1, $import_record_id);
     $sth->execute();
     while (my $row = $sth->fetchrow_hashref()) {
-        my $error = DelItemCheck($dbh, $biblionumber, $row->{'itemnumber'});
+        my $error = DelItemCheck( $biblionumber, $row->{'itemnumber'});
         if ($error == 1){
             my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?");
             $updsth->bind_param(1, 'reverted');
@@ -959,6 +955,24 @@ sub CleanBatch {
     SetImportBatchStatus($batch_id, 'cleaned');
 }
 
+=head2 DeleteBatch
+
+  DeleteBatch($batch_id)
+
+Deletes the record from the database. This can only be done
+once the batch has been cleaned.
+
+=cut
+
+sub DeleteBatch {
+    my $batch_id = shift;
+    return unless defined $batch_id;
+
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare('DELETE FROM import_batches WHERE import_batch_id = ?');
+    $sth->execute( $batch_id );
+}
+
 =head2 GetAllImportBatches
 
   my $results = GetAllImportBatches();
@@ -1448,7 +1462,6 @@ sub GetImportRecordMatches {
     
 }
 
-
 =head2 SetImportRecordMatches
 
   SetImportRecordMatches($import_record_id, @matches);
@@ -1471,6 +1484,72 @@ sub SetImportRecordMatches {
     }
 }
 
+=head2 RecordsFromISO2709File
+
+    my ($errors, $records) = C4::ImportBatch::RecordsFromISO2709File($input_file, $record_type, $encoding);
+
+Reads ISO2709 binary porridge from the given file and creates MARC::Record-objects out of it.
+
+@PARAM1, String, absolute path to the ISO2709 file.
+@PARAM2, String, see stage_file.pl
+@PARAM3, String, should be utf8
+
+Returns two array refs.
+
+=cut
+
+sub RecordsFromISO2709File {
+    my ($input_file, $record_type, $encoding) = @_;
+    my @errors;
+
+    my $marc_type = C4::Context->preference('marcflavour');
+    $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
+
+    open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+    my @marc_records;
+    $/ = "\035";
+    while (<IN>) {
+        s/^\s+//;
+        s/\s+$//;
+        next unless $_; # skip if record has only whitespace, as might occur
+                        # if file includes newlines between each MARC record
+        my ($marc_record, $charset_guessed, $char_errors) = MarcToUTF8Record($_, $marc_type, $encoding);
+        push @marc_records, $marc_record;
+        if ($charset_guessed ne $encoding) {
+            push @errors,
+                "Unexpected charset $charset_guessed, expecting $encoding";
+        }
+    }
+    close IN;
+    return ( \@errors, \@marc_records );
+}
+
+=head2 RecordsFromMARCXMLFile
+
+    my ($errors, $records) = C4::ImportBatch::RecordsFromMARCXMLFile($input_file, $encoding);
+
+Creates MARC::Record-objects out of the given MARCXML-file.
+
+@PARAM1, String, absolute path to the ISO2709 file.
+@PARAM2, String, should be utf8
+
+Returns two array refs.
+
+=cut
+
+sub RecordsFromMARCXMLFile {
+    my ( $filename, $encoding ) = @_;
+    my $batch = MARC::File::XML->in( $filename );
+    my ( @marcRecords, @errors, $record );
+    do {
+        eval { $record = $batch->next( $encoding ); };
+        if ($@) {
+            push @errors, $@;
+        }
+        push @marcRecords, $record if $record;
+    } while( $record );
+    return (\@errors, \@marcRecords);
+}
 
 # internal functions