More work on editions in the OPAC; Need both a staff client and OPAC system pref...
[koha_fer] / C4 / ImportBatch.pm
index 94c5f83..dfd0506 100644 (file)
@@ -21,7 +21,6 @@ use strict;
 use C4::Context;
 use C4::Koha;
 use C4::Biblio;
-use C4::Matcher;
 require Exporter;
 
 
@@ -60,6 +59,7 @@ use C4::ImportBatch;
     BatchCommitBibRecords
     BatchRevertBibRecords
 
+    GetAllImportBatches
     GetImportBatchRangeDesc
     GetNumberOfNonZ3950ImportBatches
     GetImportBibliosRange
@@ -68,6 +68,8 @@ use C4::ImportBatch;
     SetImportBatchStatus
     GetImportBatchOverlayAction
     SetImportBatchOverlayAction
+    GetImportBatchMatcher
+    SetImportBatchMatcher
     GetImportRecordOverlayStatus
     SetImportRecordOverlayStatus
     GetImportRecordStatus
@@ -230,15 +232,33 @@ sub ModBiblioInBatch {
 ($batch_id, $num_records, $num_items, @invalid_records) = 
     BatchStageMarcRecords($marc_flavor, $marc_records, $file_name, 
                           $comments, $branch_code, $parse_items,
-                          $leave_as_staging);
+                          $leave_as_staging, 
+                          $progress_interval, $progress_callback);
 
 =back
 
 =cut
 
 sub  BatchStageMarcRecords {
-    my ($marc_flavor, $marc_records, $file_name, $comments, $branch_code, $parse_items, $leave_as_staging) = @_;
-
+    my $marc_flavor = shift;
+    my $marc_records = shift;
+    my $file_name = shift;
+    my $comments = shift;
+    my $branch_code = shift;
+    my $parse_items = shift;
+    my $leave_as_staging = shift;
+   
+    # optional callback to monitor status 
+    # of job
+    my $progress_interval = 0;
+    my $progress_callback = undef;
+    if ($#_ == 1) {
+        $progress_interval = shift;
+        $progress_callback = shift;
+        $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
+        $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
+    } 
+    
     my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
     my @invalid_records = ();
     my $num_valid = 0;
@@ -247,6 +267,9 @@ sub  BatchStageMarcRecords {
     my $rec_num = 0;
     foreach my $marc_blob (split(/\x1D/, $marc_records)) {
         $rec_num++;
+        if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
+            &$progress_callback($rec_num);
+        }
         my $marc_record = FixEncoding($marc_blob, "\x1D");
         my $import_record_id;
         if (scalar($marc_record->fields()) == 0) {
@@ -313,7 +336,7 @@ sub AddItemsToImportBiblio {
 
 =over 4
 
-my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches);
+my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches, $progress_interval, $progress_callback);
 
 =back
 
@@ -325,6 +348,12 @@ of each record to "no_match" or "auto_match" as appropriate.
 The $max_matches parameter is optional; if it is not supplied,
 it defaults to 10.
 
+The $progress_interval and $progress_callback parameters are 
+optional; if both are supplied, the sub referred to by
+$progress_callback will be invoked every $progress_interval
+records using the number of records processed as the 
+singular argument.
+
 =cut
 
 sub BatchFindBibDuplicates {
@@ -332,6 +361,17 @@ sub BatchFindBibDuplicates {
     my $matcher = shift;
     my $max_matches = @_ ? shift : 10;
 
+    # optional callback to monitor status 
+    # of job
+    my $progress_interval = 0;
+    my $progress_callback = undef;
+    if ($#_ == 1) {
+        $progress_interval = shift;
+        $progress_callback = shift;
+        $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
+        $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
+    }
+
     my $dbh = C4::Context->dbh;
     my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
     if ($old_overlay_action eq "create_new") {
@@ -344,14 +384,23 @@ sub BatchFindBibDuplicates {
                              WHERE import_batch_id = ?");
     $sth->execute($batch_id);
     my $num_with_matches = 0;
+    my $rec_num = 0;
     while (my $rowref = $sth->fetchrow_hashref) {
+        $rec_num++;
+        if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
+            &$progress_callback($rec_num);
+        }
         my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
-        my @matches = $matcher->get_matches($marc_record, $max_matches);
+        my @matches = ();
+        if (defined $matcher) {
+            @matches = $matcher->get_matches($marc_record, $max_matches);
+        }
         if (scalar(@matches) > 0) {
             $num_with_matches++;
             SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
         } else {
+            SetImportRecordMatches($rowref->{'import_record_id'}, ());
             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
         }
     }
@@ -363,7 +412,8 @@ sub BatchFindBibDuplicates {
 
 =over 4
 
-my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($batch_id);
+my ($num_added, $num_updated, $num_items_added, $num_ignored) = 
+    BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback);
 
 =back
 
@@ -372,6 +422,17 @@ my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRe
 sub BatchCommitBibRecords {
     my $batch_id = shift;
 
+    # optional callback to monitor status 
+    # of job
+    my $progress_interval = 0;
+    my $progress_callback = undef;
+    if ($#_ == 1) {
+        $progress_interval = shift;
+        $progress_callback = shift;
+        $progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0;
+        $progress_interval = 0 unless 'CODE' eq ref $progress_callback;
+    }
+
     my $num_added = 0;
     my $num_updated = 0;
     my $num_items_added = 0;
@@ -386,7 +447,12 @@ sub BatchCommitBibRecords {
                              JOIN import_biblios USING (import_record_id)
                              WHERE import_batch_id = ?");
     $sth->execute($batch_id);
+    my $rec_num = 0;
     while (my $rowref = $sth->fetchrow_hashref) {
+        $rec_num++;
+        if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
+            &$progress_callback($rec_num);
+        }
         if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
             $num_ignored++;
         }
@@ -535,6 +601,7 @@ sub BatchRevertItems {
 
     my $sth = $dbh->prepare_cached("SELECT import_items_id, itemnumber
                                    FROM import_items
+                                   JOIN items USING (itemnumber)
                                    WHERE import_record_id = ?");
     $sth->bind_param(1, $import_record_id);
     $sth->execute();
@@ -551,6 +618,35 @@ sub BatchRevertItems {
     return $num_items_deleted;
 }
 
+=head2 GetAllImportBatches
+
+=over 4
+
+my $results = GetAllImportBatches();
+
+=back
+
+Returns a references to an array of hash references corresponding
+to all import_batches rows (of batch_type 'batch'), sorted in 
+ascending order by import_batch_id.
+
+=cut
+
+sub  GetAllImportBatches {
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
+                                    WHERE batch_type = 'batch'
+                                    ORDER BY import_batch_id ASC");
+
+    my $results = [];
+    $sth->execute();
+    while (my $row = $sth->fetchrow_hashref) {
+        push @$results, $row;
+    }
+    $sth->finish();
+    return $results;
+}
+
 =head2 GetImportBatchRangeDesc
 
 =over 4
@@ -751,6 +847,49 @@ sub SetImportBatchOverlayAction {
 
 }
 
+=head2 GetImportBatchMatcher
+
+=over 4
+
+my $matcher_id = GetImportBatchMatcher($batch_id);
+
+=back
+
+=cut
+
+sub GetImportBatchMatcher {
+    my ($batch_id) = @_;
+
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT matcher_id FROM import_batches WHERE import_batch_id = ?");
+    $sth->execute($batch_id);
+    my ($matcher_id) = $sth->fetchrow_array();
+    $sth->finish();
+    return $matcher_id;
+
+}
+
+
+=head2 SetImportBatchMatcher
+
+=over 4
+
+SetImportBatchMatcher($batch_id, $new_matcher_id);
+
+=back
+
+=cut
+
+sub SetImportBatchMatcher {
+    my ($batch_id, $new_matcher_id) = @_;
+
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("UPDATE import_batches SET matcher_id = ? WHERE import_batch_id = ?");
+    $sth->execute($new_matcher_id, $batch_id);
+    $sth->finish();
+
+}
+
 =head2 GetImportRecordOverlayStatus
 
 =over 4