Tamil/Authority/LoadFileTask.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 122 CC 9 SCAR 30.8
CC
9
Cov
0%
process
56.0
run
6.0
start_message
6.0
CRAP 21.7
1 package Koha::Contrib::Tamil::Authority::LoadFileTask;
2 # ABSTRACT: Task loading authorities into a Koha instance
3 $Koha::Contrib::Tamil::Authority::LoadFileTask::VERSION = '0.074';
4 use Moose;
5  
6 extends 'Koha::Contrib::Tamil::Authority::Task';
7  
8 with 'MooseX::LogDispatch';
9  
10  
11 use 5.010;
12 use utf8;
13 use Locale::TextDomain 'Koha-Contrib-Tamil';
14  
15  
16  
17 has file => ( is => 'rw', isa => 'Str' );
18  
19 has fh => ( is => 'rw' );
20  
21 has truncate => ( is => 'rw', isa => 'Bool' );
22  
23 has dbh => ( is => 'rw' );
24  
25 # Le chargement est-il effectivement fait ?
26 has doit => (is=> 'rw', isa => 'Bool', default => 0);
27  
28 use Carp;
29 use C4::Context;
30 use C4::AuthoritiesMarc qw(AddAuthority);
31 use List::Util qw( first );
32  
33  
34 has log_dispatch_conf => (
35 is => 'ro',
36 isa => 'HashRef',
37 lazy => 1,
38 required => 1,
39 default => sub {
40 {
41 class => 'Log::Dispatch::Screen',
42 min_level => 'notice',
43 stderr => 1,
44 },
45 {
46 class => 'Log::Dispatch::File',
47 min_level => 'debug',
48 filename => 'koha_auth_load.log',
49 binmode => ':utf8',
50 #format => '[%p] %m at %F line %L%n',
51 },
52 },
53 );
54  
55  
56 sub run {
57 my $self = shift;
58  
59 my $file = $self->file;
60 open my $fh, "<:utf8", $file
61 or croak "Can't open authorities file: $file";
62 $self->fh( $fh );
63  
64 my $dbh = C4::Context->dbh;
65 $self->dbh( $dbh );
66  
67 if ( $self->truncate ) {
68 $self->logger->info( __"Truncate table: auth_header\n" );
69 $dbh->do( "truncate auth_header" );
70 }
71  
72 $self->SUPER::run();
73 }
74  
75  
76 sub start_message {
77 my $self = shift;
78 my $test = $self->doit ? "" : __"** TEST **";
79 my $file = $self->file;
80 $self->logger->notice(
81 __x("Load authorities into Koha from a file {test_flag}\n" .
82 " source: {source_file}\n" .
83 " target: Koha DB\n",
84 test_flag => $test,
85 source_file => $file) );
86 }
87  
88  
89 sub process {
90 my $self = shift;
91 my $fh = $self->fh;
92  
93 my $line = <$fh>;
94 if ( defined($line) ) {
95 $self->SUPER::process();
96 chop $line;
97 my ($authcode, $sub) = $line =~ /(\w+)\t(.*)/;
98 my (@subfields) = split /\t|\|/, $sub;
99 #print "auth_code => $sub\n";
100 #print "tbl : ", @subfields, "\n";
101 #print "size: ", $#subfields, "\n";
102 my $authority =
103 first { $_->{authcode} eq $authcode } @{ $self->conf_authorities };
104 return 1 if !$authority;
105 #print "<$authcode>:", "0:",$subfields[0], " - 1:",$subfields[1], " => $tag\n";
106 if ( $#subfields > 0 ) {
107 my $record = MARC::Record->new();
108 my $leader = $record->leader();
109 substr($leader, 5, 3) = 'naa';
110 substr($leader, 9, 1) = 'a'; # encodage utf8
111 $record->encoding( 'UTF-8' );
112 $record->leader($leader);
113 my $field = MARC::Field->new(
114 $authority->{authtag}, '', '', @subfields);
115 for my $i (1..2) {
116 my $value = $authority->{"ind$i"};
117 next unless $value;
118 next if length($value) == 0 || $value =~ /null/i;
119 $field->set_indicator($i, substr($value,0,1));
120 }
121 $record->append_fields($field);
122 $self->logger->info( "$authcode: " . $field->as_formatted() . "\n" );
123 AddAuthority($record, 0, $authcode) if $self->doit;
124 }
125 return 1;
126 }
127 return 0;
128 }
129  
130  
131 no Moose;
132 __PACKAGE__->meta->make_immutable;
133  
134 1;
135  
136 __END__