Tamil/Authority/LinkBiblioTask.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 159 CC 17 SCAR 50.9
CC
17
Cov
0%
process
272.0
run
6.0
BEGIN
2.0
CRAP 162.3
1 package Koha::Contrib::Tamil::Authority::LinkBiblioTask;
2 $Koha::Contrib::Tamil::Authority::LinkBiblioTask::VERSION = '0.074';
3 # ABSTRACT: Task linking biblio records to authorities
4 use Moose;
5  
6 extends 'Koha::Contrib::Tamil::Authority::Task';
7  
8 use 5.010;
9 use utf8;
10 use Carp;
11 use Koha::Contrib::Tamil::Koha;
12 use Koha::Contrib::Tamil::RecordReader;
13 use C4::Context;
14 use C4::Biblio qw(ModBiblio);
15  
16 has reader => ( is => 'rw', isa => 'Koha::Contrib::Tamil::RecordReader' );
17  
18 has koha => (
19 is => 'rw',
20 default => sub { Koha::Contrib::Tamil::Koha->new() }
21 );
22  
23 has cached => ( is => 'rw', isa => 'Bool', default => 1 );
24  
25 has doit => ( is => 'rw', isa => 'Bool', default => 0 );
26  
27 has _heading => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
28  
29  
30 sub run {
31 my $self = shift;
32 $self->reader(
33 Koha::Contrib::Tamil::RecordReader->new( koha => $self->koha ) )
34 unless $self->reader;
35 $self->koha->dbh->{AutoCommit} = 0;
36 $self->SUPER::run();
37 }
38  
39  
40 sub process {
41 my $self = shift;
42  
43 my $record = $self->reader->read();
44  
45 return 0 unless $record;
46  
47 $self->SUPER::process();
48  
49 # FIXME Reset de la connexion tous les 100 enregistrements
50 unless ( $self->reader->count % 100 ) {
51 $self->koha->zconn_reset();
52 $self->koha->dbh->commit();
53 }
54 my $zconn = $self->koha->zauth();
55 my $modified = 0;
56 my $biblionumber = $self->reader->id;
57 my $cached = $self->cached;
58 my $_heading = $self->_heading;
59 foreach my $authority ( @{ $self->conf_authorities } ) { # loop on all authority types
60 foreach my $tag ( @{ $authority->{bibliotags} } ) {
61 # loop on all biblio tags related to the current authority
62 FIELD:
63 foreach my $field ( $record->field( $tag ) ) {
64 # All field repetitions
65 my @values;
66 SUBFIELD:
67 foreach my $subfield ( $field->subfields() ) {
68 my ($letter, $value) = @$subfield;
69 $value =~ s/^\s+//;
70 $value =~ s/\s+$//;
71 $value =~ /([\w ,.'-_]+)/;
72 $value = $1;
73 $value =~ s/^\s+//;
74 $value =~ s/\s+$//;
75 $value = ucfirst $value;
76 next SUBFIELD if !$value;
77 push @values, $value
78 if $authority->{authletters} =~ /$letter/;
79 }
80 next FIELD unless @values;
81 my $heading = join(' ', @values);
82 my $id = $_heading->{$heading};
83 if ( defined($id) && $id == -1 ) {
84 #print "FOUND IN CACHE AND NOT IN ZEBRA: $heading\n";
85 next;
86 }
87 unless ( $id ) {
88 # 6=3 Exact matching
89 my $query = '@and @attr 1=authtype ' . $authority->{authcode} .
90 ' @attr 1=Heading @attr 4=1 @attr 6=3 "' . $heading . '"';
91 #print "$query\n";
92 eval {
93 my $rs = $zconn->search_pqf( $query );
94 # FIXME: If there are more than two authorities, the biblio
95 # record is linked to the first one
96 if ( (my $size = $rs->size()) >= 1 ) {
97 print STDERR "[$biblionumber] WARNING: " . $rs->size() . " matching authorities for $heading\n"
98 if $size > 1;
99 my $auth = $rs->record(0);
100 my $m = new_from_usmarc MARC::Record( $auth->raw() );
101 $id = $m->field('001')->data();
102 }
103 else {
104 print STDERR "[$biblionumber] WARNING: authority not found -- $heading\n";
105 }
106 $rs->destroy();
107 };
108 print STDERR "ERROR: ZOOM ", $@, "\n" if $@;
109 }
110 else {
111 #print "FOUND IN CACHE: $heading\n";
112 }
113 #print "ID: $id\n";
114 if ( defined $id ) {
115 $_heading->{$heading} = $id if $cached;
116 my @ns = ();
117 push @ns, '9', $id;
118 for ( $field->subfields() ) {
119 my ($letter, $value) = @$_;
120 push @ns, $letter, $value if $letter ne '9';
121 }
122 $field->replace_with( new MARC::Field(
123 $field->tag, $field->indicator(1), $field->indicator(2),
124 @ns ) );
125 $modified = 1;
126 }
127 else {
128 $_heading->{$heading} = -1 if $cached;
129 }
130 }
131 }
132 }
133 return 1 if !$self->doit || !$modified;
134  
135 my $framework_code = C4::Biblio::GetFrameworkCode( $self->reader->id );
136 ModBiblio( $record, $self->reader->id, $framework_code );
137  
138 return 1;
139 }
140  
141  
142 no Moose;
143 __PACKAGE__->meta->make_immutable;
144  
145 1;
146  
147 __END__