Koha/C4/Linker/Default.pm
Nick Clemens ea88cfb3ab Bug 28676: Cache and retrieve match_count when searching a cached heading
We use match_count to determine if a new authority record should be created, however,
we were not adding this count to the cache, so if a record returned too many matches on first
lookup, we would create a new record on the second lookup

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 two headings for that authority to a bib record (e.g. a 610 and 710)
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: Phil Ringnalda <phil@chetcolibrary.org>

Signed-off-by: Joonas Kylmälä <joonas.kylmala@iki.fi>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-09-20 12:06:56 +02:00

113 lines
3.7 KiB
Perl

package C4::Linker::Default;
# Copyright 2011 C & P Bibliography Services
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use strict;
use warnings;
use MARC::Field;
use C4::Heading;
use base qw(C4::Linker);
sub get_link {
my $self = shift;
my $heading = shift;
my $behavior = shift || 'default';
my $search_form = $heading->search_form();
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'};
$fuzzy = $self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'};
$match_count = $self->{'cache'}->{$search_form.$auth_type}->{'match_count'};
}
else {
# 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'};
}
elsif ( $behavior eq 'first' && $#{$authorities} >= 0 ) {
$authid = $authorities->[0]->{'authid'};
$fuzzy = $#{$authorities} > 0;
}
elsif ( $behavior eq 'last' && $#{$authorities} >= 0 ) {
$authid = $authorities->[ $#{$authorities} ]->{'authid'};
$fuzzy = $#{$authorities} > 0;
}
if ( !defined $authid && $self->{'broader_headings'} ) {
my $field = $heading->field();
my @subfields = grep { $_->[0] ne '9' } $field->subfields();
if ( scalar @subfields > 1 ) {
pop @subfields;
$field =
MARC::Field->new(
$field->tag,
$field->indicator(1),
$field->indicator(2),
map { $_->[0] => $_->[1] } @subfields
);
( $authid, $fuzzy ) =
$self->get_link( C4::Heading->new_from_field($field),
$behavior );
}
}
$self->{'cache'}->{$search_form.$auth_type}->{'cached'} = 1;
$self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
$self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'} = $fuzzy;
$self->{'cache'}->{$search_form.$auth_type}->{'match_count'} = $match_count;
}
return $self->SUPER::_handle_auth_limit($authid), $fuzzy, $match_count;
}
sub update_cache {
my $self = shift;
my $heading = shift;
my $authid = shift;
my $search_form = $heading->search_form();
my $auth_type = $heading->auth_type();
my $fuzzy = 0;
$self->{'cache'}->{$search_form.$auth_type}->{'cached'} = 1;
$self->{'cache'}->{$search_form.$auth_type}->{'authid'} = $authid;
$self->{'cache'}->{$search_form.$auth_type}->{'fuzzy'} = $fuzzy;
}
sub flip_heading {
my $self = shift;
my $heading = shift;
# TODO: implement
}
1;
__END__
=head1 NAME
C4::Linker::Default - match only if there is a single matching auth
=cut