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