From b69facedc4f0c1c2194244df9d97460851b273a5 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Wed, 14 Nov 2007 17:11:34 -0600 Subject: [PATCH] matching enhancements -- allow matching rule to be changed on the fly 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 Signed-off-by: Joshua Ferraro --- C4/ImportBatch.pm | 52 ++++++++++++++++++- C4/Matcher.pm | 1 + installer/data/mysql/kohastructure.sql | 1 + installer/data/mysql/updatedatabase.pl | 7 +++ .../en/modules/tools/manage-marc-import.tmpl | 39 ++++++++++++++ kohaversion.pl | 2 +- tools/manage-marc-import.pl | 50 ++++++++++++++++++ tools/stage-marc-import.pl | 1 + 8 files changed, 150 insertions(+), 3 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 05dae6719d..dfd0506a8c 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -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 diff --git a/C4/Matcher.pm b/C4/Matcher.pm index 16f42c268f..505ff27f36 100644 --- a/C4/Matcher.pm +++ b/C4/Matcher.pm @@ -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 = {}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 739e08d402..3d3b6a2125 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -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, diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 6242034cba..688c26214a 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -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 diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl index 4d05c0621c..0153499f7b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl @@ -37,6 +37,36 @@

+

+ + () + + No matching rule in effect + + +

+

" method="post"> + + " /> + " /> + + +
+

+

" method="post"> @@ -72,6 +102,15 @@ Number of records ignored + + +

Failed to apply different matching rule

+ +

Applied different matching rule. Number of records matched now + +

+ +
diff --git a/kohaversion.pl b/kohaversion.pl index 6aa0995a4b..c18be35ee2 100644 --- a/kohaversion.pl +++ b/kohaversion.pl @@ -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; diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl index 6832eb7c4d..1eb41dd055 100755 --- a/tools/manage-marc-import.pl +++ b/tools/manage-marc-import.pl @@ -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 { diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index 7f8063d3d6..22dff90062 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -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; } -- 2.39.5