ARK.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 253 CC 28 SCAR 43.6
CC
28
Cov
0%
BUILD
210.0
run
72.0
build_ark
20.0
CRAP 78.1
1 package Koha::Contrib::ARK;
2 # ABSTRACT: ARK Management
3 $Koha::Contrib::ARK::VERSION = '1.0.5';
4 use Moose;
5 use Modern::Perl;
6 use JSON;
7 use DateTime;
8 use Try::Tiny;
9 use Koha::Contrib::ARK::Reader;
10 use Koha::Contrib::ARK::Writer;
11 use Koha::Contrib::ARK::Update;
12 use Koha::Contrib::ARK::Clear;
13 use Koha::Contrib::ARK::Check;
14 use Term::ProgressBar;
15 use C4::Context;
16  
17  
18 # Action/error id/message
19 my $raw_actions = <<EOS;
20 found_right_field ARK found in the right field
21 found_wrong_field ARK found in the wrong field
22 not_found ARK not found
23 build ARK Build
24 clear Clear ARK field
25 add Add ARK field
26 remove_existing Remove existing field while adding ARK field
27 generated ARK generated
28 use_biblionumber No koha.id field, use biblionumber to generate ARK
29 err_pref_missing ARK_CONF preference is missing
30 err_pref_decoding Can't decode ARK_CONF
31 err_pref_ark_missing Invalid ARK_CONF preference: 'ark' variable is missing
32 err_pref_var_missing A variable is missing
33 err_pref_nothash Variable is not a HASH
34 err_pref_var_tag Tag invalid
35 err_pref_var_letter Letter missing
36 EOS
37  
38 my $what = { map {
39 /^(\w*) *(.*)$/;
40 { $1 => { id => $1, msg => $2 } }
41 } split /\n/, $raw_actions };
42  
43  
44 has c => ( is => 'rw', isa => 'HashRef' );
45  
46  
47 has cmd => (
48 is => 'rw',
49 isa => 'Str',
50 trigger => sub {
51 my ($self, $cmd) = @_;
52 $self->error("Invalid command: $cmd\n")
53 if $cmd !~ /check|clear|update/;
54 return $cmd;
55 },
56 default => 'check',
57 );
58  
59  
60 has doit => ( is => 'rw', isa => 'Bool', default => 0 );
61  
62  
63 has verbose => ( is => 'rw', isa => 'Bool', default => 0 );
64  
65  
66 has debug => ( is => 'rw', isa => 'Bool', default => 0 );
67  
68  
69 has field_query => ( is => 'rw', isa => 'Str' );
70  
71 has reader => (is => 'rw', isa => 'Koha::Contrib::ARK::Reader' );
72 has writer => (is => 'rw', isa => 'Koha::Contrib::ARK::Writer' );
73 has action => (is => 'rw', isa => 'Koha::Contrib::ARK::Action' );
74  
75  
76 has explain => (
77 is => 'rw',
78 isa => 'HashRef',
79 );
80  
81  
82 has current => (
83 is => 'rw',
84 isa => 'HashRef',
85 );
86  
87  
88 sub set_current {
89 my ($self, $biblionumber, $record) = @_;
90 my $current = { biblionumber => $biblionumber };
91 $current->{ record } = tojson($record) if $record && $self->debug;
92 $self->current($current);
93 }
94
95  
96 sub error {
97 my ($self, $id, $more) = @_;
98 my %r = %{$what->{$id}};
99 $r{more} = $more if $more;
100 $self->explain->{error}->{$id} = \%r;
101 }
102  
103  
104 sub what_append {
105 my ($self, $id, $more) = @_;
106 my %r = %{$what->{$id}};
107 $r{more} = $more if $more;
108 $self->current->{what}->{$id} = \%r;
109 }
110  
111  
112 sub dump_explain {
113 my $self = shift;
114  
115 open my $fh, '>:encoding(utf8)', 'koha-ark.json';
116 print $fh to_json($self->explain, { pretty => 1 });
117 }
118  
119  
120 sub BUILD {
121 my $self = shift;
122  
123 my $dt = DateTime->now();
124 my $explain = {
125 action => $self->cmd,
126 timestamp => '"' . $dt->ymd . " " . $dt->hms . '"',
127 testmode => $self->doit ? 0 : 1,
128 };
129 $self->explain($explain);
130  
131 my $c = C4::Context->preference("ARK_CONF");
132 unless ($c) {
133 $self->error('err_pref_missing');
134 return;
135 }
136  
137 try {
138 $c = decode_json($c);
139 } catch {
140 $self->error('err_pref_decoding', $_);
141 return;
142 };
143  
144 my $a = $c->{ark};
145 unless ($a) {
146 $self->error('err_pref_ark_missing');
147 return;
148 }
149  
150 # Check koha fields
151 for my $name ( qw/ id ark / ) {
152 my $field = $a->{koha}->{$name};
153 unless ($field) {
154 $self->error('err_pref_var_missing', "koha.$name");
155 next;
156 }
157 if ( ref $field ne "HASH" ) {
158 $self->error('err_pref_nothash', "koha.$name");
159 next;
160 }
161 if ( $field->{tag} ) {
162 $self->error('err_pref_var_tag', "koha.$name.tag") if $field->{tag} !~ /^[0-9]{3}$/;
163 }
164 else {
165 $self->error('err_pref_var_missing', "koha.$name.tag");
166 }
167 $self->error('err_pref_var_letter', "koha.$name.letter")
168 if $field->{tag} !~ /^00[0-9]$/ && ! $field->{letter};
169 }
170 $self->explain->{ark_conf} = $c;
171  
172 my $id = $a->{koha}->{ark};
173 my $field_query =
174 $id->{letter}
175 ? '//datafield[@tag="' . $id->{tag} . '"]/subfield[@code="' .
176 $id->{letter} . '"]'
177 : '//controlfield[@tag="' . $id->{tag} . '"]';
178 $field_query = "ExtractValue(metadata, '$field_query')";
179 $self->field_query( $field_query );
180  
181 $self->c($c);
182  
183 # Instanciation reader/writer/converter
184 $self->reader( Koha::Contrib::ARK::Reader->new(
185 ark => $self,
186 select => $self->cmd eq 'update' ? 'WithoutArk' :
187 $self->cmd eq 'clear' ? 'WithArk' : 'All',
188 ) );
189 $explain->{result} = {
190 count => $self->reader->total,
191 records => [],
192 };
193 $self->explain($explain);
194 $self->writer( Koha::Contrib::ARK::Writer->new( ark => $self ) );
195 $self->action(
196 $self->cmd eq 'check' ? Koha::Contrib::ARK::Check->new( ark => $self ) :
197 $self->cmd eq 'update' ? Koha::Contrib::ARK::Update->new( ark => $self ) :
198 Koha::Contrib::ARK::Clear->new( ark => $self )
199 );
200 }
201  
202  
203 sub build_ark {
204 my ($self, $biblionumber, $record) = @_;
205  
206 my $a = $self->c->{ark};
207 my $ark = $a->{ARK};
208 for my $var ( qw/ NMHA NAAN / ) {
209 my $value = $a->{$var};
210 $ark =~ s/{$var}/$value/;
211 }
212 my $kfield = $a->{koha}->{id};
213 my $id = $record->field($kfield->{tag});
214 if ( $id ) {
215 $id = $kfield->{letter}
216 ? $id->subfield($kfield->{letter})
217 : $id->value;
218 }
219 unless ($id) {
220 $self->what_append('use_biblionumber');
221 $id = $biblionumber;
222 }
223 $ark =~ s/{id}/$id/;
224 $self->what_append('generated', $ark);
225 return $ark;
226 }
227  
228  
229 sub tojson {
230 my $record = shift;
231 my $rec = {
232 leader => $record->leader,
233 fields => [ map {
234 my @values = ( $_->tag );
235 if ( ref($_) eq 'MARC::Moose::Field::Control' ) {
236 push @values, $_->value;
237 }
238 else {
239 push @values, $_->ind1 . $_->ind2;
240 for (@{$_->subf}) {
241 push @values, $_->[0], $_->[1];
242 }
243 }
244 \@values;
245 } @{ $record->fields } ],
246 };
247 return $rec;
248 }
249  
250  
251 sub run {
252 my $self = shift;
253  
254 unless ( $self->explain->{error} ) {
255 my $progress;
256 $progress = Term::ProgressBar->new({ count => $self->reader->total })
257 if $self->verbose;
258 my $next_update = 0;
259 while ( my ($biblionumber, $record) = $self->reader->read() ) {
260 if ( $record ) {
261 $self->action->action($biblionumber, $record);
262 if ( $self->cmd ne 'check' ) {
263 $self->writer->write($biblionumber, $record);
264 }
265 push @{$self->explain->{result}->{records}}, $self->current;
266 }
267 my $count = $self->reader->count;
268 next unless $progress;
269 $next_update = $progress->update($count) if $count >= $next_update;
270 last if $self->reader->count == 1000000;
271 }
272 }
273 $self->dump_explain();
274 }
275  
276 __PACKAGE__->meta->make_immutable;
277 1;
278  
279 __END__
280  
281 =pod
282  
283 =encoding UTF-8
284  
285 =head1 NAME
286  
287 Koha::Contrib::ARK - ARK Management
288  
289 =head1 VERSION
290  
291 version 1.0.5
292  
293 =head1 ATTRIBUTES
294  
295 =head2 cmd
296  
297 What processing? One of those values: check, clear, update. By default,
298 'check'.
299  
300 =head2 doit
301  
302 Is the process effective?
303  
304 =head2 verbose
305  
306 Operate in verbose mode
307  
308 =head2 debug
309  
310 In debug mode, there is more info produces. By default, false.
311  
312 =head2 explain
313  
314 A HASH containing the full explanation of the pending processing
315  
316 =head2 current
317  
318 What happens on the current biblio record?
319  
320 =head1 METHODS
321  
322 =head2 set_current($biblionumber, $record)
323  
324 Set the current biblio record. Called by the biblio records reader.
325  
326 =head2 error($id, $more)
327  
328 Set an error code $id to the L<explain> processing status. $more can contain
329 more information.
330  
331 =head2 build_ark($biblionumber, $record)
332  
333 Build ARK for biblio record $record (which has $biblionumber unique ID)
334  
335 =head1 AUTHOR
336  
337 Frédéric Demians <f.demians@tamil.fr>
338  
339 =head1 COPYRIGHT AND LICENSE
340  
341 This software is Copyright (c) 2020 by Fréderic Demians.
342  
343 This is free software, licensed under:
344  
345 The GNU General Public License, Version 3, June 2007
346  
347 =cut