Bug 18398: (follow-up) Update POD & Unit tests
[koha.git] / C4 / Linker / Default.pm
1 package C4::Linker::Default;
2
3 # Copyright 2011 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use MARC::Field;
23 use C4::Heading;
24
25 use base qw(C4::Linker);
26
27 sub get_link {
28     my $self        = shift;
29     my $heading     = shift;
30     my $behavior    = shift || 'default';
31     my $search_form = $heading->search_form();
32     my $auth_type = $heading->auth_type();
33     my $thesaurus   = $heading->{thesaurus} || 'notdefined';
34     my $authid;
35     my $fuzzy = 0;
36     my $match_count;
37
38     if ( $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'cached'} ) {
39         $authid = $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'authid'};
40         $fuzzy  = $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'fuzzy'};
41         $match_count = $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'match_count'};
42     }
43     else {
44
45         # look for matching authorities
46         my $authorities = $heading->authorities(1);    # $skipmetadata = true
47         $match_count = scalar @$authorities;
48
49         if ( $behavior eq 'default' && $#{$authorities} == 0 ) {
50             $authid = $authorities->[0]->{'authid'};
51         }
52         elsif ( $behavior eq 'first' && $#{$authorities} >= 0 ) {
53             $authid = $authorities->[0]->{'authid'};
54             $fuzzy  = $#{$authorities} > 0;
55         }
56         elsif ( $behavior eq 'last' && $#{$authorities} >= 0 ) {
57             $authid = $authorities->[ $#{$authorities} ]->{'authid'};
58             $fuzzy  = $#{$authorities} > 0;
59         }
60
61         if ( !defined $authid && $self->{'broader_headings'} ) {
62             my $field     = $heading->field();
63             my @subfields = grep { $_->[0] ne '9' } $field->subfields();
64             if ( scalar @subfields > 1 ) {
65                 pop @subfields;
66                 $field =
67                     MARC::Field->new(
68                         $field->tag,
69                         $field->indicator(1),
70                         $field->indicator(2),
71                         map { $_->[0] => $_->[1] } @subfields
72                     );
73                 ( $authid, $fuzzy ) =
74                   $self->get_link( C4::Heading->new_from_field($field),
75                     $behavior );
76             }
77         }
78
79         $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'cached'} = 1;
80         $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'authid'} = $authid;
81         $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'fuzzy'}  = $fuzzy;
82         $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'match_count'} = $match_count;
83     }
84     return $self->SUPER::_handle_auth_limit($authid), $fuzzy, $match_count;
85 }
86
87 sub update_cache {
88     my $self        = shift;
89     my $heading     = shift;
90     my $authid      = shift;
91     my $search_form = $heading->search_form();
92     my $auth_type = $heading->auth_type();
93     my $thesaurus   = $heading->{thesaurus} || 'notdefined';
94     my $fuzzy = 0;
95
96     $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'cached'} = 1;
97     $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'authid'} = $authid;
98     $self->{'cache'}->{$search_form.$auth_type.$thesaurus}->{'fuzzy'}  = $fuzzy;
99 }
100
101 sub flip_heading {
102     my $self    = shift;
103     my $heading = shift;
104
105     # TODO: implement
106 }
107
108 1;
109 __END__
110
111 =head1 NAME
112
113 C4::Linker::Default - match only if there is a single matching auth
114
115 =cut