| 1 | package Koha::Contrib::Tamil::Authority::FromFile; | ||
| 2 | # ABSTRACT: Find Koha authorities from an ISO2709 file | ||
| 3 | $Koha::Contrib::Tamil::Authority::FromFile::VERSION = '0.074'; | ||
| 4 | |||
| 5 | use Moose; | ||
| 6 | |||
| 7 | extends qw/ AnyEvent::Processor | ||
| 8 | Koha::Contrib::Tamil::Logger /; | ||
| 9 | |||
| 10 | use Modern::Perl; | ||
| 11 | use utf8; | ||
| 12 | use FindBin qw( $Bin ); | ||
| 13 | use Carp; | ||
| 14 | use YAML; | ||
| 15 | use Koha::Contrib::Tamil::Koha; | ||
| 16 | use MARC::Moose::Reader::File::Marcxml; | ||
| 17 | use MARC::Moose::Formater::Marcxml; | ||
| 18 | use MARC::Moose::Formater::Text; | ||
| 19 | use MARC::Moose::Parser::Iso2709; | ||
| 20 | use YAML qw/Dump LoadFile/ ; | ||
| 21 | use Try::Tiny; | ||
| 22 | |||
| 23 | |||
| 24 | has koha => ( is => 'rw', isa => 'Koha::Contrib::Tamil::Koha' ); | ||
| 25 | |||
| 26 | has reader => ( is => 'rw', isa => 'MARC::Moose::Reader' ); | ||
| 27 | |||
| 28 | |||
| 29 | has writer => ( is => 'rw', isa => 'MARC::Moose::Writer' ); | ||
| 30 | |||
| 31 | |||
| 32 | has authority => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Str', | ||
| 35 | required => 1, | ||
| 36 | trigger => sub { | ||
| 37 | my ($self, $name) = @_; | ||
| 38 | #FIXME: Le fichier des autorités est écrasé | ||
| 39 | # On pourrait le signaler. | ||
| 40 | #croak "Le fichier des autorités existe déjà : ", $name if $name; | ||
| 41 | open my $fh, ">", $name or croak "Impossible de créer le fichier $name"; | ||
| 42 | binmode($fh, ':utf8'); | ||
| 43 | $self->authority_writer( $fh ); | ||
| 44 | return $name; | ||
| 45 | }, | ||
| 46 | ); | ||
| 47 | |||
| 48 | |||
| 49 | has authority_writer => ( is => 'rw' ); | ||
| 50 | |||
| 51 | # Le cache des autorités déjà trouvées | ||
| 52 | # Un ref à un tableau à deux dimensions : | ||
| 53 | # 0: l'autorité principale | ||
| 54 | # 1: l'autorité non vedette | ||
| 55 | has cache_auth => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'ArrayRef', | ||
| 58 | default => sub { | ||
| 59 | [ {}, {} ] | ||
| 60 | } | ||
| 61 | ); | ||
| 62 | |||
| 63 | has use_cache_auth => (is => 'rw', isa => 'Bool', default => '1'); | ||
| 64 | |||
| 65 | |||
| 66 | has equivalence => ( | ||
| 67 | is => 'rw', | ||
| 68 | isa => 'Str', | ||
| 69 | trigger => sub { | ||
| 70 | my ($self, $name) = @_; | ||
| 71 | open my $fh, "<:utf8", $name or croak "Impossible d'ouvrir le fichier $name"; | ||
| 72 | my %equival; | ||
| 73 | while (<$fh>) { | ||
| 74 | chop; | ||
| 75 | while (/\t$/) { s/\t$//; } | ||
| 76 | my ($key, $id) = /(.*)\t(\d*)$/; | ||
| 77 | next unless $key; | ||
| 78 | $equival{lc $key} = $id; | ||
| 79 | } | ||
| 80 | $self->equival(\%equival); | ||
| 81 | }, | ||
| 82 | ); | ||
| 83 | |||
| 84 | # Les equivalences mots clés fichiers source => autorité Koha | ||
| 85 | has equival => ( is => 'rw', isa => 'HashRef', default => sub { {} }, ); | ||
| 86 | |||
| 87 | # Compte des autorités remplacées | ||
| 88 | has replaced => ( | ||
| 89 | is => 'rw', | ||
| 90 | isa => 'HashRef', | ||
| 91 | default => sub { | ||
| 92 | { autorite => 0, vedette => 0, equival => 0, non => 0, rejete => 0, } }, ); | ||
| 93 | |||
| 94 | |||
| 95 | # Définition des autorités : etc/config.yaml | ||
| 96 | |||
| 97 | my $c; | ||
| 98 | my %authdef; | ||
| 99 | my $authdef_perid = {}; | ||
| 100 | |||
| 101 | sub BUILD { | ||
| 102 | my $file = 'config.yaml'; | ||
| 103 | unless ( -e $file) { | ||
| 104 | say "Configuration file doesn't exist: $file"; | ||
| 105 | exit; | ||
| 106 | } | ||
| 107 | $c = LoadFile($file); | ||
| 108 | my @authorities = @{$c->{authorities}}; | ||
| 109 | for my $authority (@authorities) { | ||
| 110 | my $def = {}; | ||
| 111 | $def->{$_} = $authority->{$_} for qw/ name id heading idx /; | ||
| 112 | if ( my $cd = $authority->{biblio}->{cd} ) { | ||
| 113 | $def->{rejected} = $cd->{tag}; | ||
| 114 | } | ||
| 115 | if ( my $de = $authority->{biblio}->{de} ) { | ||
| 116 | $def->{tag} = $de->{tag}; | ||
| 117 | } | ||
| 118 | my $tag = $authority->{biblio}->{de}->{tag}; | ||
| 119 | $tag = [ $tag ] if ref $tag ne 'ARRAY'; | ||
| 120 | $authdef{$_} = $def for @$tag; | ||
| 121 | } | ||
| 122 | for (values %authdef) { | ||
| 123 | $authdef_perid->{$_->{id}} = $_; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub get_field_term { | ||
| 129 | my ($field, $auth) = @_; | ||
| 130 | |||
| 131 | my @search; | ||
| 132 | my @view = ( $auth->{id} ); | ||
| 133 | for my $subf ( @{$field->subf} ) { | ||
| 134 | my ($letter, $value) = @$subf; | ||
| 135 | if ( $letter ne '9' && $letter ne '4' ) { | ||
| 136 | push @search, $value; | ||
| 137 | push @view, "$letter|$value"; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | return { search => join(' ', @search), view => join("\t", @view) }; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | sub search_authority { | ||
| 145 | my ($self, $auth, $term) = @_; | ||
| 146 | |||
| 147 | my $search = $term->{search}; | ||
| 148 | $search =~ s/["\-]/ /g; | ||
| 149 | $search =~ s/ {2,}/ /g; | ||
| 150 | |||
| 151 | my $type = $auth->{id}; | ||
| 152 | my $indexes = $auth->{idx}; | ||
| 153 | my ($id, $replace) = (0, 0); | ||
| 154 | my $record; | ||
| 155 | |||
| 156 | # say "auth:", Dump($auth); | ||
| 157 | #say "term:", Dump($term); | ||
| 158 | if ($self->use_cache_auth) { | ||
| 159 | my $i = 0; | ||
| 160 | for my $index (@$indexes) { | ||
| 161 | $record = $self->cache_auth->[$i]->{"$index$search"}; | ||
| 162 | if ($record) { | ||
| 163 | $id = $record->field('001')->value; | ||
| 164 | $id = $id + 0; # Indispensable, sinon pas tjrs numérique | ||
| 165 | $replace = $i > 0; | ||
| 166 | return $id, $record, $replace; | ||
| 167 | } | ||
| 168 | $i++; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | my $zconn = $self->koha->zconn( 'authorityserver' ); | ||
| 173 | |||
| 174 | # Recherche de l'index le plus précis à l'index le moins précis | ||
| 175 | my $rs; | ||
| 176 | my $i_index = 0; | ||
| 177 | for my $index (@$indexes) { | ||
| 178 | my $query = '@and @attr 1=authtype ' . $type . | ||
| 179 | ' @attr 4=1 @attr 6=3 @attr 1=' . $index . ' "' . $search . '"'; | ||
| 180 | try { | ||
| 181 | #say "Recherche: $query"; | ||
| 182 | $rs = $zconn->search_pqf( $query ); | ||
| 183 | #say " OK" if $rs && $rs->size() > 0; | ||
| 184 | } catch { | ||
| 185 | $self->log->info("ERROR ZOOM $_ -- query: $query\n"); | ||
| 186 | }; | ||
| 187 | last if $rs && $rs->size() > 0; | ||
| 188 | $replace = 1; | ||
| 189 | $i_index++; | ||
| 190 | } | ||
| 191 | |||
| 192 | if ( $rs && $rs->size() >= 1 ) { | ||
| 193 | ($id, $record) = _get_marc_record($rs); | ||
| 194 | my $index = $indexes->[$i_index]; | ||
| 195 | $self->cache_auth->[$replace]->{"$index$search"} = $record | ||
| 196 | if $self->use_cache_auth && $record; | ||
| 197 | } | ||
| 198 | $rs->destroy() if $rs; | ||
| 199 | $rs = undef; | ||
| 200 | return $id, $record, $replace; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | sub _get_marc_record { | ||
| 205 | my $rs = shift; | ||
| 206 | my $record = $rs->record(0); | ||
| 207 | $record = MARC::Moose::Record::new_from($record->raw(), 'iso2709'); | ||
| 208 | my $id = $record->field('001')->value; | ||
| 209 | $id = $id + 0; # Indispensable, sinon pas tjrs numérique | ||
| 210 | return ($id, $record); | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub get_authority_by_id { | ||
| 215 | my ($self, $id) = @_; | ||
| 216 | my $query = '@attr 1=localnumber '. $id; | ||
| 217 | my $zconn = $self->koha->zconn( 'authorityserver' ); | ||
| 218 | my $rs = $zconn->search_pqf($query); | ||
| 219 | my $record; | ||
| 220 | ($id, $record) = _get_marc_record($rs) if $rs->size() == 1; | ||
| 221 | return $record; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | sub process_field { | ||
| 226 | my ($self, $field) = @_; | ||
| 227 | |||
| 228 | my $auth = $authdef{$field->tag}; | ||
| 229 | return $field unless $auth; | ||
| 230 | return if ref $field ne 'MARC::Moose::Field::Std'; | ||
| 231 | return $field if $field->subfield('9'); # Déjà le numéro d'autorité | ||
| 232 | |||
| 233 | my $term = get_field_term($field, $auth); | ||
| 234 | my ($id, $marc_auth, $replace_equival, $replace_vedette); | ||
| 235 | # Le terme a-t-il une équivalence ? | ||
| 236 | $id = $self->equival->{lc $term->{view}}; | ||
| 237 | if ($id) { | ||
| 238 | if ( $marc_auth = $self->get_authority_by_id($id) ) { | ||
| 239 | $replace_equival = 1; | ||
| 240 | my $cat = $c->{authtype}; | ||
| 241 | my $code = $marc_auth->field($cat->{tag})->subfield($cat->{letter}); | ||
| 242 | $auth = $authdef_perid->{$code}; | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | $id = 0; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | else { | ||
| 249 | # Sinon on cherche dans les autorités | ||
| 250 | ($id, $marc_auth, $replace_vedette) = | ||
| 251 | $self->search_authority($auth, $term); | ||
| 252 | } | ||
| 253 | $self->replaced->{ | ||
| 254 | !$id ? 'non' : | ||
| 255 | $replace_equival ? 'equival' : | ||
| 256 | $replace_vedette ? 'vedette' : 'autorite' }++; | ||
| 257 | # print $field->tag, " : $term : $id\n"; | ||
| 258 | if ( $id ) { | ||
| 259 | my @subfields = (); | ||
| 260 | my $from = $marc_auth->field( $auth->{heading} ); | ||
| 261 | if ( $from ) { | ||
| 262 | push @subfields, [ 9 => $id ]; | ||
| 263 | if ( my @values = $field->subfield('4') ) { | ||
| 264 | push @subfields, [ 4 => $_ ] for @values; | ||
| 265 | } | ||
| 266 | foreach my $subf ( @{$from->subf} ) { | ||
| 267 | my ($letter, $value) = @$subf; | ||
| 268 | #print "letter:value = $letter:$value\n"; | ||
| 269 | next if $letter =~ /[0-9]/; | ||
| 270 | #print "après: letter:value = $letter:$value\n"; | ||
| 271 | utf8::decode($value); #FIXME aille. | ||
| 272 | push @subfields, [ $letter => $value]; | ||
| 273 | } | ||
| 274 | $field->subf( \@subfields ); | ||
| 275 | |||
| 276 | # Faut-il changer le tag | ||
| 277 | my $auth_code = $marc_auth->field($c->{authtype}->{tag})->subfield($c->{authtype}->{letter}); | ||
| 278 | my $target_auth = $authdef_perid->{$auth_code}; | ||
| 279 | my $tag_move_text = ''; | ||
| 280 | if ( $target_auth->{id} ne $auth->{id} ) { | ||
| 281 | $field->tag( $target_auth->{tag} ); | ||
| 282 | $tag_move_text = | ||
| 283 | " +tag " . $auth->{tag} . " => " . $target_auth->{tag} . | ||
| 284 | " [" . $target_auth->{name} . "]"; | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | my $original_text = $term->{search}; | ||
| 289 | #utf8::decode($original_text); | ||
| 290 | my $replaced_text = join(' ', map { '$' . $_->[0] . ' ' . $_->[1] } @subfields); | ||
| 291 | #utf8::decode($replaced_text); | ||
| 292 | $self->log->info( | ||
| 293 | "[$auth->{name}] " . | ||
| 294 | ( $replace_equival ? "Remplacement par équivalence" : | ||
| 295 | $replace_vedette ? "Remplacement par vedette" : | ||
| 296 | "Remplacement par autorité" ) . | ||
| 297 | ": \"$original_text\" => \"$replaced_text\"$tag_move_text\n" | ||
| 298 | ); | ||
| 299 | return $field; | ||
| 300 | } | ||
| 301 | $self->log->warning( | ||
| 302 | "Récupéré une autorité sans vedette en " . $auth->{headind} . ":\n" . | ||
| 303 | $marc_auth->as('Text') | ||
| 304 | ); | ||
| 305 | return $field; | ||
| 306 | } | ||
| 307 | # On rejette certains champs non trouvés dans un autre tag | ||
| 308 | if ( $auth->{rejected} ) { | ||
| 309 | $field->tag( $auth->{rejected} ); | ||
| 310 | $self->replaced->{rejete}++; | ||
| 311 | } | ||
| 312 | # Terme non trouvé => on l'écrit | ||
| 313 | my $fh = $self->authority_writer; | ||
| 314 | print $fh $term->{view}, "\n"; | ||
| 315 | return $field; | ||
| 316 | } | ||
| 317 | |||
| 318 | |||
| 319 | # Lie une notice biblio aux autorités Frantiq, soit au moyen de la liste | ||
| 320 | # d'équivalence soit en effectuant une recherche sur les autorités Koha. | ||
| 321 | # Pour certains termes (Pactol), les termes qui n'ont pas été trouvés sont | ||
| 322 | # déplacés en zone non-descripteur | ||
| 323 | |||
| 324 | sub process { | ||
| 325 | my $self = shift; | ||
| 326 | |||
| 327 | my $record = $self->reader->read(); | ||
| 328 | |||
| 329 | # Fin du traitement | ||
| 330 | unless ( $record ) { | ||
| 331 | close $self->authority_writer; | ||
| 332 | |||
| 333 | # Le fichier des termes non trouvés dans le thesau Frantiq | ||
| 334 | # On tri et on ne garde qu'un exemplaire de chaque descripteur | ||
| 335 | # FIXME: à faire en Perl... | ||
| 336 | my $name = $self->authority; | ||
| 337 | my $cmd = "sort -f " . $name . " | uniq -i >$name~; " . | ||
| 338 | "mv $name~ $name"; | ||
| 339 | system( $cmd ); | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | |||
| 343 | $self->SUPER::process(); | ||
| 344 | $self->log->info( | ||
| 345 | ('-' x 80) . " #" . $self->count . "\n" . | ||
| 346 | $record->as('Text')); | ||
| 347 | |||
| 348 | $record->fields( [ | ||
| 349 | map { $self->process_field($_) } @{$record->fields} | ||
| 350 | ] ); | ||
| 351 | |||
| 352 | $self->log->info( "\n" . $record->as('Text')); | ||
| 353 | $self->writer->write( $record ); | ||
| 354 | $self->koha->zconn_reset() if $self->count % 10; | ||
| 355 | |||
| 356 | return 1; | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | override 'start_message' => sub { | ||
| 361 | my $self = shift; | ||
| 362 | say "Notices lues : autorités / vedettes / équivalences / non / rejetées"; | ||
| 363 | }; | ||
| 364 | |||
| 365 | |||
| 366 | override 'process_message' => sub { | ||
| 367 | my $self = shift; | ||
| 368 | say sprintf("%#6d", $self->reader->count), ' (', | ||
| 369 | sprintf("%d", $self->reader->percentage), '%) : ', | ||
| 370 | $self->replaced->{autorite}, ' / ', | ||
| 371 | $self->replaced->{vedette}, ' / ', | ||
| 372 | $self->replaced->{equival}, ' / ', | ||
| 373 | $self->replaced->{non}, ' / ', | ||
| 374 | $self->replaced->{rejete}; | ||
| 375 | }; | ||
| 376 | |||
| 377 | |||
| 378 | override 'end_message' => sub { | ||
| 379 | my $self = shift; | ||
| 380 | $self->log->warning( | ||
| 381 | "Notices autoritisées : " . $self->count . "\n" . | ||
| 382 | "Autorités trouvées : " . $self->replaced->{autorite} . "\n" . | ||
| 383 | "Vedettes trouvées : " . $self->replaced->{vedette} . "\n" . | ||
| 384 | "Équivalences trouvées : " . $self->replaced->{equival} . "\n" . | ||
| 385 | "Autorités non trouvées : " . $self->replaced->{non} . "\n" . | ||
| 386 | "Autorités déplacées : " . $self->replaced->{rejete} . "\n" | ||
| 387 | ); | ||
| 388 | }; | ||
| 389 | |||
| 390 | |||
| 391 | override 'run' => sub { | ||
| 392 | my $self = shift; | ||
| 393 | $self->writer->begin; | ||
| 394 | $self->SUPER::run(); | ||
| 395 | $self->writer->end; | ||
| 396 | }; | ||
| 397 | |||
| 398 | |||
| 399 | no Moose; | ||
| 400 | __PACKAGE__->meta->make_immutable; | ||
| 401 | |||
| 402 | 1; | ||
| 403 | |||
| 404 | __END__ |