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