Bug 25189: Don't create authority if results found
authorNick Clemens <nick@bywatersolutions.com>
Tue, 28 Apr 2020 15:34:20 +0000 (15:34 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 24 Jun 2020 12:31:37 +0000 (14:31 +0200)
Automatic authority creation assumes that if we don't match we need a new authority.

Using the Default linker, however, we don't match if there exists more than one match.
This leads to repeatedly generating authorities once there is a duplicate in the system

We shoudl instead only create a new authority if there are no results

To test:
1 - Set Linker Module to 'Default'
2 - Enable  AutoCreateAuthorities  and  BiblioAddsAuthorities and  CatalogModuleRelink and LinkerRelink
3 - Add two copies of a single authority via Z39
4 - Add a heading for that authority to a bib record
5 - Save the record and note a new authority is generated
6 - Repeat and see another is generated
7 - Apply patch
8 - Restart all the things
9 - Save the record again, no new authority created

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

C4/Biblio.pm
C4/Linker/Default.pm

index 9eb3696..9307a21 100644 (file)
@@ -506,7 +506,7 @@ sub LinkBibHeadingsToAuthorities {
             next;
         }
 
-        my ( $authid, $fuzzy ) = $linker->get_link($heading);
+        my ( $authid, $fuzzy, $match_count ) = $linker->get_link($heading);
         if ($authid) {
             $results{ $fuzzy ? 'fuzzy' : 'linked' }
               ->{ $heading->display_form() }++;
@@ -526,7 +526,7 @@ sub LinkBibHeadingsToAuthorities {
                 if ( _check_valid_auth_link( $current_link, $field ) ) {
                     $results{'linked'}->{ $heading->display_form() }++;
                 }
-                else {
+                elsif ( !$match_count ) {
                     my $authority_type = Koha::Authority::Types->find( $heading->auth_type() );
                     my $marcrecordauth = MARC::Record->new();
                     if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {
index bfe1d72..934e875 100644 (file)
@@ -33,6 +33,7 @@ sub get_link {
     my $auth_type = $heading->auth_type();
     my $authid;
     my $fuzzy = 0;
+    my $match_count;
 
     if ( $self->{'cache'}->{$search_form.$auth_type}->{'cached'} ) {
         $authid = $self->{'cache'}->{$search_form.$auth_type}->{'authid'};
@@ -42,6 +43,7 @@ sub get_link {
 
         # look for matching authorities
         my $authorities = $heading->authorities(1);    # $skipmetadata = true
+        $match_count = scalar @$authorities;
 
         if ( $behavior eq 'default' && $#{$authorities} == 0 ) {
             $authid = $authorities->[0]->{'authid'};
@@ -77,7 +79,7 @@ sub get_link {
         $self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
         $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'}  = $fuzzy;
     }
-    return $self->SUPER::_handle_auth_limit($authid), $fuzzy;
+    return $self->SUPER::_handle_auth_limit($authid), $fuzzy, $match_count;
 }
 
 sub update_cache {