(bug #3726) fix ISBD url translation
[koha.git] / C4 / Matcher.pm
index 505ff27..512562c 100644 (file)
@@ -25,8 +25,10 @@ use C4::Biblio;
 
 use vars qw($VERSION);
 
-# set the version for version checking
-$VERSION = 3.00;
+BEGIN {
+       # set the version for version checking
+       $VERSION = 3.01;
+}
 
 =head1 NAME
 
@@ -62,6 +64,8 @@ foreach $match (@matches) {
 
 }
 
+my $matcher_description = $matcher->dump();
+
 =back
 
 =head1 FUNCTIONS
@@ -365,6 +369,29 @@ sub _store_matchpoint {
     return $matchpoint_id;
 }
 
+
+=head2 delete
+
+=over 4
+
+C4::Matcher->delete($id);
+
+=back
+
+Deletes the matcher of the specified ID
+from the database.
+
+=cut
+
+sub delete {
+    my $class = shift;
+    my $matcher_id = shift;
+
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("DELETE FROM marc_matchers WHERE matcher_id = ?");
+    $sth->execute($matcher_id); # relying on cascading deletes to clean up everything
+}
+
 =head2 threshold
 
 =over 4
@@ -383,6 +410,26 @@ sub threshold {
     @_ ? $self->{'threshold'} = shift : $self->{'threshold'};
 }
 
+=head2 _id
+
+=over 4
+
+$matcher->_id(123);
+my $id = $matcher->_id();
+
+=back
+
+Accessor method.  Note that using this method
+to set the DB ID of the matcher should not be
+done outside of the editing CGI.
+
+=cut
+
+sub _id {
+    my $self = shift;
+    @_ ? $self->{'id'} = shift : $self->{'id'};
+}
+
 =head2 code
 
 =over 4
@@ -483,7 +530,7 @@ sub add_simple_matchpoint {
 
     $self->add_matchpoint($index, $score, [
                           { tag => $source_tag, subfields => $source_subfields,
-                            offset => $source_offset, length => $source_length,
+                            offset => $source_offset, 'length' => $source_length,
                             norms => [ $source_normalizer ]
                           }
                          ]);
@@ -565,9 +612,9 @@ sub add_simple_required_check {
         $target_tag, $target_subfields, $target_offset, $target_length, $target_normalizer) = @_;
 
     $self->add_required_check(
-      [ { tag => $source_tag, subfields => $source_subfields, offset => $source_offset, length => $source_length,
+      [ { tag => $source_tag, subfields => $source_subfields, offset => $source_offset, 'length' => $source_length,
           norms => [ $source_normalizer ] } ],
-      [ { tag => $target_tag, subfields => $target_subfields, offset => $target_offset, length => $target_length,
+      [ { tag => $target_tag, subfields => $target_subfields, offset => $target_offset, 'length' => $target_length,
           norms => [ $target_normalizer ] } ]
     );
 }
@@ -614,7 +661,7 @@ sub get_matches {
         # build query
         my $query = join(" or ", map { "$matchpoint->{'index'}=$_" } @source_keys);
         # FIXME only searching biblio index at the moment
-        my ($error, $searchresults) = SimpleSearch($query);
+        my ($error, $searchresults, $total_hits) = SimpleSearch($query, 0, $max_matches);
 
         warn "search failed ($query) $error" if $error;
         foreach my $matched (@$searchresults) {
@@ -646,6 +693,41 @@ sub get_matches {
 
 }
 
+=head2 dump
+
+=over 4
+
+$description = $matcher->dump();
+
+=back
+
+Returns a reference to a structure containing all of the information
+in the matcher object.  This is mainly a convenience method to
+aid setting up a HTML editing form.
+
+=cut
+
+sub dump {
+    my $self = shift;
+   
+    my $result = {};
+
+    $result->{'matcher_id'} = $self->{'id'};
+    $result->{'code'} = $self->{'code'};
+    $result->{'description'} = $self->{'description'};
+
+    $result->{'matchpoints'} = [];
+    foreach my $matchpoint (@{ $self->{'matchpoints'} }) {
+        push @{  $result->{'matchpoints'} }, $matchpoint;
+    }
+    $result->{'matchchecks'} = [];
+    foreach my $matchcheck (@{ $self->{'required_checks'} }) {
+        push @{  $result->{'matchchecks'} }, $matchcheck;
+    }
+
+    return $result;
+}
+
 sub _passes_required_checks {
     my ($source_record, $target_blob, $matchchecks) = @_;
     my $target_record = MARC::Record->new_from_usmarc($target_blob); # FIXME -- need to avoid parsing record twice
@@ -704,6 +786,11 @@ sub _get_match_keys {
                     }
                 }
                 $key = _normalize($key);
+                if ($component->{'length'}){
+                   if (length($key) > $component->{'length'}){
+                     $key = _normalize(substr($key,$component->{'offset'},$component->{'length'}));
+                   }
+                }
             }
             if ($i == 0) {
                 push @keys, $key if $key;
@@ -733,14 +820,17 @@ sub _parse_match_component {
 # FIXME - default normalizer
 sub _normalize {
     my $value = uc shift;
+    $value =~ s/.;:,\]\[\)\(\/'"//g;
     $value =~ s/^\s+//;
-    $value =~ s/^\s+$//;
+    #$value =~ s/^\s+$//;
+    $value =~ s/\s+$//;
     $value =~ s/\s+/ /g;
-    $value =~ s/[.;,\]\[\)\(\/"']//g;
+    #$value =~ s/[.;,\]\[\)\(\/"']//g;
     return $value;
 }
 
 1;
+__END__
 
 =head1 AUTHOR