Bug 10403: (follow-up) fix test to use vendor created earlier during test
[koha.git] / C4 / Matcher.pm
index 3f44783..4e064be 100644 (file)
@@ -1,6 +1,6 @@
 package C4::Matcher;
 
-# Copyright (C) 2007 LibLime
+# Copyright (C) 2007 LibLime, 2012 C & P Bibliography Services
 #
 # This file is part of Koha.
 #
@@ -22,14 +22,12 @@ use warnings;
 
 use C4::Context;
 use MARC::Record;
-use C4::Search;
-use C4::Biblio;
 
 use vars qw($VERSION);
 
 BEGIN {
        # set the version for version checking
-       $VERSION = 3.01;
+    $VERSION = 3.07.00.049;
 }
 
 =head1 NAME
@@ -95,6 +93,22 @@ sub GetMatcherList {
     return @results;
 }
 
+=head2 GetMatcherId
+
+  my $matcher_id = C4::Matcher::GetMatcherId($code);
+
+Returns the matcher_id of a code.
+
+=cut
+
+sub GetMatcherId {
+    my ($code) = @_;
+    my $dbh = C4::Context->dbh;
+
+    my $matcher_id = $dbh->selectrow_array("SELECT matcher_id FROM marc_matchers WHERE code = ?", undef, $code);
+    return $matcher_id;
+}
+
 =head1 METHODS
 
 =head2 new
@@ -368,6 +382,20 @@ sub delete {
     $sth->execute($matcher_id); # relying on cascading deletes to clean up everything
 }
 
+=head2 record_type
+
+  $matcher->record_type('biblio');
+  my $record_type = $matcher->record_type();
+
+Accessor method.
+
+=cut
+
+sub record_type {
+    my $self = shift;
+    @_ ? $self->{'record_type'} = shift : $self->{'record_type'};
+}
+
 =head2 threshold
 
   $matcher->threshold(1000);
@@ -566,7 +594,7 @@ sub add_simple_required_check {
     );
 }
 
-=head2 find_matches
+=head2 get_matches
 
   my @matches = $matcher->get_matches($marc_record, $max_matches);
   foreach $match (@matches) {
@@ -598,17 +626,62 @@ sub get_matches {
 
     my %matches = ();
 
-    foreach my $matchpoint (@{ $self->{'matchpoints'} }) {
-        my @source_keys = _get_match_keys($source_record, $matchpoint);
+    my $QParser;
+    $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
+    foreach my $matchpoint ( @{ $self->{'matchpoints'} } ) {
+        my @source_keys = _get_match_keys( $source_record, $matchpoint );
         next if scalar(@source_keys) == 0;
+
         # build query
-        my $query = join(" or ", map { "$matchpoint->{'index'}=$_" } @source_keys);
-        # FIXME only searching biblio index at the moment
-        my ($error, $searchresults, $total_hits) = SimpleSearch($query, 0, $max_matches);
+        my $query;
+        my $error;
+        my $searchresults;
+        my $total_hits;
+        if ( $self->{'record_type'} eq 'biblio' ) {
+            if ($QParser) {
+                $query = join( " || ",
+                    map { "$matchpoint->{'index'}:$_" } @source_keys );
+            }
+            else {
+                $query = join( " or ",
+                    map { "$matchpoint->{'index'}=$_" } @source_keys );
+            }
+            require C4::Search;
+            ( $error, $searchresults, $total_hits ) =
+              C4::Search::SimpleSearch( $query, 0, $max_matches );
+        }
+        elsif ( $self->{'record_type'} eq 'authority' ) {
+            my $authresults;
+            my @marclist;
+            my @and_or;
+            my @excluding = [];
+            my @operator;
+            my @value;
+            foreach my $key (@source_keys) {
+                push @marclist, $matchpoint->{'index'};
+                push @and_or,   'or';
+                push @operator, 'exact';
+                push @value,    $key;
+            }
+            require C4::AuthoritiesMarc;
+            ( $authresults, $total_hits ) =
+              C4::AuthoritiesMarc::SearchAuthorities(
+                \@marclist,  \@and_or, \@excluding, \@operator,
+                \@value,     0,        20,          undef,
+                'AuthidAsc', 1
+              );
+            foreach my $result (@$authresults) {
+                push @$searchresults, $result->{'authid'};
+            }
+        }
 
-        warn "search failed ($query) $error" if $error;
-        foreach my $matched (@$searchresults) {
-            $matches{$matched} += $matchpoint->{'score'};
+        if ( defined $error ) {
+            warn "search failed ($query) $error";
+        }
+        else {
+            foreach my $matched ( @{$searchresults} ) {
+                $matches{$matched} += $matchpoint->{'score'};
+            }
         }
     }
 
@@ -617,16 +690,23 @@ sub get_matches {
 
     # get rid of any that don't meet the required checks
     %matches = map { _passes_required_checks($source_record, $_, $self->{'required_checks'}) ?  ($_ => $matches{$_}) : () } 
-                keys %matches;
+                keys %matches unless ($self->{'record_type'} eq 'auth');
 
     my @results = ();
-    foreach my $marcblob (keys %matches) {
-        my $target_record = MARC::Record->new_from_usmarc($marcblob);
-        my $result = TransformMarcToKoha(C4::Context->dbh, $target_record, '');
-        # FIXME - again, bibliospecific
-        # also, can search engine be induced to give just the number in the first place?
-        my $record_number = $result->{'biblionumber'};
-        push @results, { 'record_id' => $record_number, 'score' => $matches{$marcblob} };
+    if ($self->{'record_type'} eq 'biblio') {
+        require C4::Biblio;
+        foreach my $marcblob (keys %matches) {
+            my $target_record = MARC::Record->new_from_usmarc($marcblob);
+            my $record_number;
+            my $result = C4::Biblio::TransformMarcToKoha(C4::Context->dbh, $target_record, '');
+            $record_number = $result->{'biblionumber'};
+            push @results, { 'record_id' => $record_number, 'score' => $matches{$marcblob} };
+        }
+    } elsif ($self->{'record_type'} eq 'authority') {
+        require C4::AuthoritiesMarc;
+        foreach my $authid (keys %matches) {
+            push @results, { 'record_id' => $authid, 'score' => $matches{$authid} };
+        }
     }
     @results = sort { $b->{'score'} cmp $a->{'score'} } @results;
     if (scalar(@results) > $max_matches) {
@@ -654,6 +734,7 @@ sub dump {
     $result->{'matcher_id'} = $self->{'id'};
     $result->{'code'} = $self->{'code'};
     $result->{'description'} = $self->{'description'};
+    $result->{'record_type'} = $self->{'record_type'};
 
     $result->{'matchpoints'} = [];
     foreach my $matchpoint (@{ $self->{'matchpoints'} }) {
@@ -769,7 +850,7 @@ __END__
 
 =head1 AUTHOR
 
-Koha Development Team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 Galen Charlton <galen.charlton@liblime.com>