matching enhancements -- allow matching rule to be changed on the fly
authorGalen Charlton <galen.charlton@liblime.com>
Wed, 14 Nov 2007 23:11:34 +0000 (17:11 -0600)
committerJoshua Ferraro <jmf@liblime.com>
Fri, 16 Nov 2007 13:37:24 +0000 (07:37 -0600)
Enhancement to store the matching rule associated with an
import batch and to allow the current matching rule in
effect to be changed and the duplicate detection redone.

Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/ImportBatch.pm
C4/Matcher.pm
installer/data/mysql/kohastructure.sql
installer/data/mysql/updatedatabase.pl
koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl
kohaversion.pl
tools/manage-marc-import.pl
tools/stage-marc-import.pl

index 05dae67..dfd0506 100644 (file)
@@ -21,7 +21,6 @@ use strict;
 use C4::Context;
 use C4::Koha;
 use C4::Biblio;
-use C4::Matcher;
 require Exporter;
 
 
@@ -69,6 +68,8 @@ use C4::ImportBatch;
     SetImportBatchStatus
     GetImportBatchOverlayAction
     SetImportBatchOverlayAction
+    GetImportBatchMatcher
+    SetImportBatchMatcher
     GetImportRecordOverlayStatus
     SetImportRecordOverlayStatus
     GetImportRecordStatus
@@ -390,12 +391,16 @@ sub BatchFindBibDuplicates {
             &$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');
         }
     }
@@ -842,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
index 16f42c2..505ff27 100644 (file)
@@ -166,6 +166,7 @@ sub fetch {
     my $sth = $dbh->prepare_cached("SELECT * FROM marc_matchers WHERE matcher_id = ?");
     $sth->execute($id);
     my $row = $sth->fetchrow_hashref;
+    $sth->finish();
     return undef unless defined $row;
 
     my $self = {};
index 739e08d..3d3b6a2 100644 (file)
@@ -846,6 +846,7 @@ CREATE TABLE `ethnicity` (
 DROP TABLE IF EXISTS `import_batches`;
 CREATE TABLE `import_batches` (
   `import_batch_id` int(11) NOT NULL auto_increment,
+  `matcher_id` int(11) default NULL,
   `template_id` int(11) default NULL,
   `branchcode` varchar(10) default NULL,
   `num_biblios` int(11) NOT NULL default 0,
index 6242034..688c262 100755 (executable)
@@ -673,6 +673,13 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
 }
 
 
+$DBversion = "3.00.00.029";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+       $dbh->do("ALTER TABLE `import_batches` ADD `matcher_id` int(11) NULL AFTER `import_batch_id`");
+    print "Upgrade to $DBversion done (adding matcher_id to import_batches)\n";
+    SetVersion ($DBversion);
+}
+
 =item DropAllForeignKeys($table)
 
   Drop all foreign keys of the table $table
index 4d05c06..0153499 100644 (file)
 <p><label>Comments</label> <!-- TMPL_VAR name="comments" --></p>
 <p><label>Staged</label> <!-- TMPL_VAR name="upload_timestamp" --></p>
 <p><label>Status</label> <!-- TMPL_VAR name="import_status" --></p>
+<p><label>Matching rule applied</label> 
+<!-- TMPL_IF name="current_matcher_id" -->
+  <!-- TMPL_VAR name="current_matcher_code" --> (<!-- TMPL_VAR name="current_matcher_description" -->)
+<!-- TMPL_ELSE -->
+  No matching rule in effect
+<!-- /TMPL_IF -->
+<!-- TMPL_IF name="can_commit" -->
+<p>
+  <form action="<!-- TMPL_VAR name="script_name" -->" method="post">
+    <input type="hidden" name="op" value="redo-matching" />
+    <input type="hidden" name="import_batch_id" value="<!-- TMPL_VAR name="import_batch_id" -->" />
+    <input type="hidden" name="current_matcher_id" value="<!-- TMPL_VAR name="current_matcher_id" -->" />
+    <select name="new_matcher_id" id="new_matcher_id">
+       <option value="">Do not look for matching records</option> 
+       <!-- TMPL_LOOP name="available_matchers" -->
+          <!-- TMPL_IF name="selected" -->
+          <option value="<!-- TMPL_VAR name="matcher_id" -->" selected="selected">
+             <!-- TMPL_VAR name="code" --> (<!-- TMPL_VAR name="description" -->)
+          </option>
+          <!-- TMPL_ELSE -->
+          <option value="<!-- TMPL_VAR name="matcher_id" -->">
+            <!-- TMPL_VAR name="code" --> (<!-- TMPL_VAR name="description" -->)
+          </option>
+          <!-- /TMPL_IF -->
+       <!-- /TMPL_LOOP -->
+    </select>
+    <input type="submit" class="button" value="Apply different matching rule" />
+  </form>
+</p>
+<!-- /TMPL_IF -->
 <p>
   <!-- TMPL_IF name="can_commit" -->
   <form action="<!-- TMPL_VAR name="script_name" -->" method="post">
   <tr><td>Number of records ignored</td><td><!-- TMPL_VAR name="num_ignored" --></td></tr>
   </table>
   <!-- /TMPL_IF -->
+  <!-- TMPL_IF name="rematch_attempted" -->
+    <!-- TMPL_IF name="rematch_failed" -->
+      <p><span class="problem">Failed to apply different matching rule</span></p>
+    <!-- TMPL_ELSE -->
+      <p><span class="problem">Applied different matching rule.  Number of records matched now
+       <!-- TMPL_VAR name="num_with_matches" -->
+      </span></p>
+    <!-- /TMPL_IF -->
+  <!-- /TMPL_IF -->
 <!-- /TMPL_IF -->
 <br />
 
index 6aa0995..c18be35 100644 (file)
@@ -8,7 +8,7 @@
 #        and is automatically called by Auth.pm when needed.
 
 sub kohaversion {
-    return "3.00.00.028";
+    return "3.00.00.029";
 }
 
 1;
index 6832eb7..1eb41dd 100755 (executable)
@@ -72,12 +72,40 @@ if ($op eq "") {
     import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
 } elsif ($op eq "clean-batch") {
     ;
+} elsif ($op eq "redo-matching") {
+    my $new_matcher_id = $input->param('new_matcher_id');
+    my $current_matcher_id = $input->param('current_matcher_id');
+    redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id);
+    import_biblios_list($template, $import_batch_id, $offset, $results_per_page);
 }
 
 output_html_with_http_headers $input, $cookie, $template->output;
 
 exit 0;
 
+sub redo_matching {
+    my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id) = @_;
+    my $rematch_failed = 0;
+    return if not defined $new_matcher_id and not defined $current_matcher_id;
+    return if $new_matcher_id == $current_matcher_id;
+    my $num_with_matches = 0;
+    if (defined $new_matcher_id and $new_matcher_id ne "") {
+        my $matcher = C4::Matcher->fetch($new_matcher_id);
+        if (defined $matcher) {
+            $num_with_matches = BatchFindBibDuplicates($import_batch_id, $matcher);
+            SetImportBatchMatcher($import_batch_id, $new_matcher_id);
+        } else {
+            $rematch_failed = 1;
+        }
+    } else {
+        $num_with_matches = BatchFindBibDuplicates($import_batch_id, undef);
+         SetImportBatchMatcher($import_batch_id, undef);
+    }
+    $template->param(rematch_failed => $rematch_failed);
+    $template->param(rematch_attempted => 1);
+    $template->param(num_with_matches => $num_with_matches);
+}
+
 sub import_batches_list {
     my ($template, $offset, $results_per_page) = @_;
     my $batches = GetImportBatchRangeDesc($offset, $results_per_page);
@@ -178,6 +206,28 @@ sub batch_info {
     if ($batch->{'import_status'} eq 'imported') {
         $template->param(can_revert => 1);
     }
+    if (defined $batch->{'matcher_id'}) {
+        my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
+        if (defined $matcher) {
+            $template->param('current_matcher_id' => $batch->{'matcher_id'});
+            $template->param('current_matcher_code' => $matcher->code());
+            $template->param('current_matcher_description' => $matcher->description());
+        }
+    }
+    add_matcher_list($batch->{'matcher_id'});
+}
+
+sub add_matcher_list {
+    my $current_matcher_id = shift;
+    my @matchers = C4::Matcher::GetMatcherList();
+    if (defined $current_matcher_id) {
+        for (my $i = 0; $i <= $#matchers; $i++) {
+            if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
+                $matchers[$i]->{'selected'} = 1;
+            }
+        }
+    }
+    $template->param(available_matchers => \@matchers);
 }
 
 sub add_page_numbers {
index 7f8063d..22dff90 100755 (executable)
@@ -94,6 +94,7 @@ if ($uploadmarc && length($uploadmarc)>0) {
             $checked_matches = 1;
             $matcher_code = $matcher->code();
             $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher);
+            SetImportBatchMatcher($batch_id, $matcher_id);
         } else {
             $matcher_failed = 1;
         }