| 1 | package Koha::Contrib::Sudoc::Localisation; | ||
| 2 | # ABSTRACT: Localisation auto de notices biblio | ||
| 3 | $Koha::Contrib::Sudoc::Localisation::VERSION = '2.20'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | extends 'AnyEvent::Processor'; | ||
| 7 | |||
| 8 | use Modern::Perl; | ||
| 9 | use utf8; | ||
| 10 | use Koha::Contrib::Sudoc; | ||
| 11 | use Koha::Contrib::Sudoc::BiblioReader; | ||
| 12 | use C4::Items; | ||
| 13 | use YAML; | ||
| 14 | use Encode; | ||
| 15 | use Business::ISBN; | ||
| 16 | use List::Util qw/first/; | ||
| 17 | |||
| 18 | |||
| 19 | # Moulinette SUDOC | ||
| 20 | has sudoc => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Koha::Contrib::Sudoc', | ||
| 23 | default => sub { Koha::Contrib::Sudoc->new; }, | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has select => ( is => 'rw'); | ||
| 28 | |||
| 29 | has reader => ( is => 'rw', isa => 'Koha::Contrib::Sudoc::BiblioReader' ); | ||
| 30 | |||
| 31 | # Type de sortie : isbn, ppn ou dat | ||
| 32 | has type => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Str', | ||
| 35 | default => 'isbn', | ||
| 36 | trigger => sub { | ||
| 37 | my ($self, $type) = @_; | ||
| 38 | if ( $type !~ /isbn|dat|ppn/i ) { | ||
| 39 | say "Type inconnu: $type"; | ||
| 40 | exit; | ||
| 41 | } | ||
| 42 | return $type; | ||
| 43 | }, | ||
| 44 | ); | ||
| 45 | |||
| 46 | # Champ contenant le PPN | ||
| 47 | has ppn => ( | ||
| 48 | is => 'rw', | ||
| 49 | isa => 'Str', | ||
| 50 | default => '001', | ||
| 51 | trigger => sub { | ||
| 52 | my ($self, $ppn) = @_; | ||
| 53 | if ( $ppn !~ /^[0-9]{3}$/ && $ppn !~ /^[0-9]{3}[a-z]$/ ) { | ||
| 54 | say "Champ PPN invalide: $ppn"; | ||
| 55 | exit; | ||
| 56 | } | ||
| 57 | return $ppn; | ||
| 58 | }, | ||
| 59 | ); | ||
| 60 | |||
| 61 | |||
| 62 | # Où placer la cote Koha dans la notice ABES, par défaut 930 $a | ||
| 63 | has coteabes => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Str', | ||
| 66 | default => '930 $a' | ||
| 67 | ); | ||
| 68 | |||
| 69 | # Test de recouvrement (on sort moins d'info) | ||
| 70 | has test => ( is => 'rw', isa => 'Bool', default => 1 ); | ||
| 71 | |||
| 72 | # Nombre max de lignes par fichier | ||
| 73 | has lines => ( is => 'rw', isa => 'Int', default => 1000 ); | ||
| 74 | |||
| 75 | # Disponibilité pour le PEB ? | ||
| 76 | has peb => ( is => 'rw', isa => 'Bool', default => 1 ); | ||
| 77 | |||
| 78 | |||
| 79 | # | ||
| 80 | # Les fichiers par RCR, avec branch Koha correspondante. Les info proviennent | ||
| 81 | # du fichier de conf sudoc.conf et sont construites à l'instantiation de | ||
| 82 | # l'objet. | ||
| 83 | # Par exemple : | ||
| 84 | # { | ||
| 85 | # BIB1 => { | ||
| 86 | # branch => 'BIB1', | ||
| 87 | # rcr => '1255872545', # RCR correspondant à la biblio Koha | ||
| 88 | # key => { | ||
| 89 | # cle1 => [ [biblionumber1, cote1], [biblionumber2, cote2], ... ], | ||
| 90 | # cle2 => [ [ | ||
| 91 | # }, | ||
| 92 | # line => 123, # N° de ligne dans le fichier courant | ||
| 93 | # index => 2, # Index du ficier (fichier.index) | ||
| 94 | # }, | ||
| 95 | # BIB2 => { | ||
| 96 | # ... | ||
| 97 | # } | ||
| 98 | # | ||
| 99 | has loc => ( | ||
| 100 | is => 'rw', | ||
| 101 | isa => 'HashRef', | ||
| 102 | default => sub { | ||
| 103 | my $self = shift; | ||
| 104 | my %loc; | ||
| 105 | my $hbranch = $self->sudoc->c->{branch}; | ||
| 106 | while ( my ($branch, $rcr) = each %$hbranch ) { | ||
| 107 | $loc{$branch} = { | ||
| 108 | branch => $branch, | ||
| 109 | rcr => $rcr, | ||
| 110 | key => {}, | ||
| 111 | }; | ||
| 112 | } | ||
| 113 | $self->loc( \%loc ); | ||
| 114 | }, | ||
| 115 | ); | ||
| 116 | |||
| 117 | |||
| 118 | # Listes de mots vides supprimés des titres/auteurs | ||
| 119 | # Cette liste est fournie par l'ABES | ||
| 120 | my @stopwords = qw( | ||
| 121 | per | ||
| 122 | org | ||
| 123 | mti | ||
| 124 | rec | ||
| 125 | isb | ||
| 126 | isn | ||
| 127 | ppn | ||
| 128 | dew | ||
| 129 | cla | ||
| 130 | msu | ||
| 131 | mee | ||
| 132 | cti | ||
| 133 | cot | ||
| 134 | lai | ||
| 135 | pai | ||
| 136 | rbc | ||
| 137 | res | ||
| 138 | the | ||
| 139 | prs | ||
| 140 | aut | ||
| 141 | num | ||
| 142 | tou | ||
| 143 | edi | ||
| 144 | sou | ||
| 145 | tir | ||
| 146 | bro | ||
| 147 | geo | ||
| 148 | mch | ||
| 149 | epn | ||
| 150 | tab | ||
| 151 | tco | ||
| 152 | dpn | ||
| 153 | sim | ||
| 154 | dup | ||
| 155 | vma | ||
| 156 | lva | ||
| 157 | pfm | ||
| 158 | mfm | ||
| 159 | pra | ||
| 160 | mra | ||
| 161 | kil | ||
| 162 | sel | ||
| 163 | col | ||
| 164 | nos | ||
| 165 | num | ||
| 166 | msa | ||
| 167 | cod | ||
| 168 | inl | ||
| 169 | cll | ||
| 170 | ati | ||
| 171 | nli | ||
| 172 | slo | ||
| 173 | rcr | ||
| 174 | typ | ||
| 175 | dep | ||
| 176 | spe | ||
| 177 | dom | ||
| 178 | reg | ||
| 179 | mno | ||
| 180 | mor | ||
| 181 | eta | ||
| 182 | nom | ||
| 183 | for | ||
| 184 | vil | ||
| 185 | dat | ||
| 186 | dac | ||
| 187 | dam | ||
| 188 | nrs | ||
| 189 | adr | ||
| 190 | apu | ||
| 191 | tdo | ||
| 192 | lan | ||
| 193 | pay | ||
| 194 | fct | ||
| 195 | a | ||
| 196 | ad | ||
| 197 | alla | ||
| 198 | am | ||
| 199 | at | ||
| 200 | aus | ||
| 201 | bei | ||
| 202 | cette | ||
| 203 | como | ||
| 204 | dalla | ||
| 205 | del | ||
| 206 | dem | ||
| 207 | des | ||
| 208 | dr | ||
| 209 | during | ||
| 210 | einem | ||
| 211 | es | ||
| 212 | fuer | ||
| 213 | i | ||
| 214 | impr | ||
| 215 | l | ||
| 216 | leur | ||
| 217 | mes | ||
| 218 | nel | ||
| 219 | o | ||
| 220 | over | ||
| 221 | por | ||
| 222 | r | ||
| 223 | ses | ||
| 224 | so | ||
| 225 | sur | ||
| 226 | this | ||
| 227 | under | ||
| 228 | vom | ||
| 229 | vous | ||
| 230 | with | ||
| 231 | ab | ||
| 232 | against | ||
| 233 | alle | ||
| 234 | among | ||
| 235 | atque | ||
| 236 | aussi | ||
| 237 | bis | ||
| 238 | ceux | ||
| 239 | cum | ||
| 240 | dans | ||
| 241 | dell | ||
| 242 | den | ||
| 243 | desde | ||
| 244 | du | ||
| 245 | e | ||
| 246 | einer | ||
| 247 | et | ||
| 248 | g | ||
| 249 | ihre | ||
| 250 | in | ||
| 251 | la | ||
| 252 | leurs | ||
| 253 | mit | ||
| 254 | no | ||
| 255 | oder | ||
| 256 | p | ||
| 257 | pour | ||
| 258 | s | ||
| 259 | sic | ||
| 260 | some | ||
| 261 | te | ||
| 262 | to | ||
| 263 | une | ||
| 264 | von | ||
| 265 | w | ||
| 266 | y | ||
| 267 | depuis | ||
| 268 | di | ||
| 269 | durant | ||
| 270 | ed | ||
| 271 | eines | ||
| 272 | f | ||
| 273 | gli | ||
| 274 | ihrer | ||
| 275 | into | ||
| 276 | las | ||
| 277 | lo | ||
| 278 | n | ||
| 279 | nos | ||
| 280 | of | ||
| 281 | par | ||
| 282 | qu | ||
| 283 | sans | ||
| 284 | since | ||
| 285 | sous | ||
| 286 | that | ||
| 287 | ueber | ||
| 288 | unless | ||
| 289 | vor | ||
| 290 | was | ||
| 291 | zu | ||
| 292 | der | ||
| 293 | die | ||
| 294 | durante | ||
| 295 | ein | ||
| 296 | el | ||
| 297 | for | ||
| 298 | h | ||
| 299 | il | ||
| 300 | its | ||
| 301 | le | ||
| 302 | los | ||
| 303 | nach | ||
| 304 | notre | ||
| 305 | on | ||
| 306 | per | ||
| 307 | quae | ||
| 308 | se | ||
| 309 | sive | ||
| 310 | st | ||
| 311 | the | ||
| 312 | um | ||
| 313 | unter | ||
| 314 | vos | ||
| 315 | we | ||
| 316 | zur | ||
| 317 | across | ||
| 318 | all | ||
| 319 | altre | ||
| 320 | asi | ||
| 321 | aupres | ||
| 322 | b | ||
| 323 | ce | ||
| 324 | comme | ||
| 325 | dall | ||
| 326 | degli | ||
| 327 | dello | ||
| 328 | deren | ||
| 329 | dont | ||
| 330 | durch | ||
| 331 | eine | ||
| 332 | en | ||
| 333 | from | ||
| 334 | his | ||
| 335 | im | ||
| 336 | j | ||
| 337 | les | ||
| 338 | m | ||
| 339 | ne | ||
| 340 | nous | ||
| 341 | ou | ||
| 342 | plus | ||
| 343 | que | ||
| 344 | selon | ||
| 345 | sn | ||
| 346 | sul | ||
| 347 | their | ||
| 348 | und | ||
| 349 | upon | ||
| 350 | votre | ||
| 351 | which | ||
| 352 | ); | ||
| 353 | |||
| 354 | |||
| 355 | sub BUILD { | ||
| 356 | my $self = shift; | ||
| 357 | |||
| 358 | my $reader = Koha::Contrib::Sudoc::BiblioReader->new( | ||
| 359 | koha => $self->sudoc->koha ); | ||
| 360 | $reader->select($self->select) if $self->select; | ||
| 361 | $self->reader($reader); | ||
| 362 | |||
| 363 | my $type = $self->type; | ||
| 364 | __PACKAGE__->meta-> add_method( 'write_keys' => | ||
| 365 | $type =~ /dat/i ? \&write_dat : | ||
| 366 | $type =~ /ppn/i ? \&write_ppn : \&write_isbn | ||
| 367 | ); | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | sub populate_key { | ||
| 373 | my ($self, $items, $key) = @_; | ||
| 374 | |||
| 375 | return unless @$items; | ||
| 376 | my $biblionumber = $items->[0]->{biblionumber}; | ||
| 377 | for my $ex ( @$items ) { | ||
| 378 | my $branch = $ex->{homebranch}; | ||
| 379 | my $loc = $self->loc->{$branch}; | ||
| 380 | next unless $loc; | ||
| 381 | my $keys = $loc->{key}; | ||
| 382 | my $cote = $ex->{itemcallnumber} || ''; | ||
| 383 | $cote =~ s/;//g; | ||
| 384 | my $bibcote = $keys->{$key} ||= []; | ||
| 385 | # On ne prend pas les doublons pour un même biblionumber | ||
| 386 | next if first { $_->[0] eq $biblionumber; } @$bibcote; | ||
| 387 | push @$bibcote, [$biblionumber, $cote]; | ||
| 388 | last; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 | |||
| 393 | sub write_ppn { | ||
| 394 | my ($self, $record) = @_; | ||
| 395 | |||
| 396 | my $tag = $self->ppn; | ||
| 397 | my $letter; | ||
| 398 | $letter = $1 if $tag =~ /([a-z])$/; | ||
| 399 | |||
| 400 | my $ppn = $record->field($tag); | ||
| 401 | return unless $ppn; | ||
| 402 | $ppn = $letter ? $ppn->subfield($letter) : $ppn->value; | ||
| 403 | next unless $ppn; | ||
| 404 | |||
| 405 | my $biblionumber = $self->sudoc->koha->get_biblionumber($record); | ||
| 406 | my $items = GetItemsByBiblioitemnumber($biblionumber); | ||
| 407 | $self->populate_key($items, $ppn); | ||
| 408 | } | ||
| 409 | |||
| 410 | |||
| 411 | sub write_isbn { | ||
| 412 | my ($self, $record) = @_; | ||
| 413 | |||
| 414 | my @isbns = $record->field('010'); | ||
| 415 | return unless @isbns; | ||
| 416 | |||
| 417 | my $biblionumber = $self->sudoc->koha->get_biblionumber($record); | ||
| 418 | my $items = GetItemsByBiblioitemnumber($biblionumber); | ||
| 419 | for my $isbn ( @isbns ) { | ||
| 420 | $isbn = $isbn->subfield('a'); | ||
| 421 | next unless $isbn; | ||
| 422 | # Si c'est un EAN, on convertit en ISBN... | ||
| 423 | if ( $isbn =~ /^978/ ) { | ||
| 424 | if ( my $i = Business::ISBN->new($isbn) ) { | ||
| 425 | if ( $i = $i->as_isbn10 ) { | ||
| 426 | $isbn = $i->as_string; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | } | ||
| 430 | $isbn =~ s/ //g; | ||
| 431 | $isbn =~ s/-//g; | ||
| 432 | # On nettoie les ISBN de la forme 122JX(vol1) | ||
| 433 | $isbn = $1 if $isbn =~ /(.*)\(/; | ||
| 434 | next unless $isbn; | ||
| 435 | $self->populate_key($items, $isbn); | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | sub _clean_string { | ||
| 441 | my $value = shift; | ||
| 442 | |||
| 443 | # Suppression des accents, passage en minuscule | ||
| 444 | $value = decode('UTF-8', $value) unless utf8::is_utf8($value); | ||
| 445 | $value = lc $value; | ||
| 446 | $value =~ y/âàáäçéèêëïîíôöóøùûüñčć°/aaaaceeeeiiioooouuuncco/; | ||
| 447 | |||
| 448 | $value =~ s/;/ /g; | ||
| 449 | $value =~ s/,/ /g; | ||
| 450 | $value =~ s/"/ /g; | ||
| 451 | $value =~ s/\?/ /g; | ||
| 452 | $value =~ s/!/ /g; | ||
| 453 | $value =~ s/'/ /g; | ||
| 454 | $value =~ s/\'/ /g; | ||
| 455 | $value =~ s/\)/ /g; | ||
| 456 | $value =~ s/\(/ /g; | ||
| 457 | $value =~ s/\]/ /g; | ||
| 458 | $value =~ s/\[/ /g; | ||
| 459 | $value =~ s/:/ /g; | ||
| 460 | $value =~ s/=/ /g; | ||
| 461 | $value =~ s/-/ /g; | ||
| 462 | $value =~ s/\x{0088}/ /g; | ||
| 463 | $value =~ s/\x{0089}/ /g; | ||
| 464 | $value =~ s/\x{0098}/ /g; | ||
| 465 | $value =~ s/\x{0099}/ /g; | ||
| 466 | $value =~ s/\x9c/ /g; | ||
| 467 | $value =~ s/\./ /g; | ||
| 468 | |||
| 469 | while ( $value =~ s/ / / ) { ; } | ||
| 470 | |||
| 471 | return $value; | ||
| 472 | } | ||
| 473 | |||
| 474 | |||
| 475 | sub write_dat { | ||
| 476 | my ($self, $record) = @_; | ||
| 477 | |||
| 478 | my $date = $record->field('210'); | ||
| 479 | return unless $date; | ||
| 480 | $date = $date->subfield('d') || ''; | ||
| 481 | return unless $date =~ /(\d{4})/; | ||
| 482 | $date = $1; | ||
| 483 | |||
| 484 | my $auteur; | ||
| 485 | for my $tag ( qw( 700 701 702 710 711 712 ) ) { | ||
| 486 | $auteur = $record->field($tag); | ||
| 487 | next unless $auteur; | ||
| 488 | $auteur = $auteur->subfield('a') || ''; | ||
| 489 | $auteur = _clean_string($auteur); | ||
| 490 | last if $auteur; | ||
| 491 | } | ||
| 492 | $auteur ||= ''; | ||
| 493 | |||
| 494 | # Traitement du titre | ||
| 495 | my $titre = $record->field('200') || ''; | ||
| 496 | $titre = $titre->subfield('a') || '' if $titre; | ||
| 497 | |||
| 498 | # Suppression des accents, passage en minuscule | ||
| 499 | $titre = _clean_string($titre); | ||
| 500 | |||
| 501 | # Les mots vides | ||
| 502 | for my $word ( @stopwords ) { | ||
| 503 | $titre =~ s/\b$word\b/ /gi; | ||
| 504 | } | ||
| 505 | |||
| 506 | while ( $titre =~ s/ / / ) { ; } | ||
| 507 | $titre =~ s/^ *//; | ||
| 508 | $titre =~ s/ *$//; | ||
| 509 | |||
| 510 | my $dat = "$date;$auteur;$titre"; | ||
| 511 | my $biblionumber = $self->sudoc->koha->get_biblionumber($record); | ||
| 512 | my $items = GetItemsByBiblioitemnumber($biblionumber); | ||
| 513 | $self->populate_key($items, $dat); | ||
| 514 | } | ||
| 515 | |||
| 516 | |||
| 517 | |||
| 518 | |||
| 519 | sub process { | ||
| 520 | my $self = shift; | ||
| 521 | |||
| 522 | my $record = $self->reader->read(); | ||
| 523 | return 0 unless $record; | ||
| 524 | |||
| 525 | # Si la notice contient déjà un PPN, inutile de la traiter | ||
| 526 | my $tag = $self->sudoc->c->{biblio}->{ppn_move}; | ||
| 527 | my $letter; | ||
| 528 | if ( $tag =~ /(\d{3})([0-9a-z])/ ) { $tag = $1, $letter = $2; } | ||
| 529 | elsif ( $tag =~ /(\d{3})/ ) { $tag = $1 }; | ||
| 530 | my $field = $record->field($tag); | ||
| 531 | |||
| 532 | return 1 if $field && ( ( $letter && $field->subfield($letter) ) || | ||
| 533 | $field->value ); | ||
| 534 | |||
| 535 | $self->SUPER::process(); | ||
| 536 | $self->write_keys($record); | ||
| 537 | return 1; | ||
| 538 | } | ||
| 539 | |||
| 540 | |||
| 541 | sub start_message { | ||
| 542 | say "Lecture des notices biblio du Catalogue Koha"; | ||
| 543 | } | ||
| 544 | |||
| 545 | |||
| 546 | sub end_message { | ||
| 547 | my $self = shift; | ||
| 548 | |||
| 549 | say "Notices lues : ", $self->reader->count, "\n", | ||
| 550 | "Notices traitées : ", $self->count; | ||
| 551 | } | ||
| 552 | |||
| 553 | |||
| 554 | sub end_process { | ||
| 555 | my $self = shift; | ||
| 556 | say "Génération des fichiers ABES de localisation automatique"; | ||
| 557 | |||
| 558 | my $max_lines = $self->lines; | ||
| 559 | my $type = $self->type; | ||
| 560 | |||
| 561 | my ($prefix, $header) = | ||
| 562 | $type =~ /dat/i ? ('r', 'date;auteur;titre') : | ||
| 563 | $type =~ /ppn/i ? ('p', 'PPN') : ('i', 'ISBN'); | ||
| 564 | $header = "$header;" . $self->coteabes . ';L035 $a'; | ||
| 565 | for my $loc ( values %{$self->loc} ) { | ||
| 566 | my $fh; | ||
| 567 | open my $fh_mult, ">:encoding(utf8)", | ||
| 568 | $prefix . $loc->{rcr} . ( $self->peb ? 'u' : 'g' ) . "_clemult.txt"; | ||
| 569 | $loc->{index} = 0; | ||
| 570 | $loc->{line} = 99999999; | ||
| 571 | for my $key ( sort keys %{$loc->{key}} ) { | ||
| 572 | my @bncote = @{$loc->{key}->{$key}}; | ||
| 573 | if ( @bncote == 1 ) { | ||
| 574 | if ( $loc->{line} >= $self->lines ) { | ||
| 575 | $loc->{index}++; | ||
| 576 | my $name = $prefix . $loc->{rcr} . | ||
| 577 | ( $self->peb ? 'u' : 'g' ) . | ||
| 578 | '_' . | ||
| 579 | sprintf("%04d", $loc->{index}) . '.txt'; | ||
| 580 | close($fh) if $fh; | ||
| 581 | open $fh, ">:encoding(utf8)", $name; | ||
| 582 | say $fh $header; | ||
| 583 | $loc->{line} = 1; | ||
| 584 | } | ||
| 585 | if ( $self->test ) { | ||
| 586 | say $fh $key; | ||
| 587 | } | ||
| 588 | else { | ||
| 589 | my ($biblionumber, $cote) = @{$bncote[0]}; | ||
| 590 | say $fh "$key;$cote;$biblionumber" | ||
| 591 | } | ||
| 592 | $loc->{line}++; | ||
| 593 | } | ||
| 594 | else { | ||
| 595 | say $fh_mult | ||
| 596 | "$key\n ", | ||
| 597 | join("\n ", map { $_->[0] . " " . $_->[1] } @bncote); | ||
| 598 | } | ||
| 599 | } | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | |||
| 604 | 1; | ||
| 605 | |||
| 606 | __END__ | ||
| 607 | |||
| 608 | =pod | ||
| 609 | |||
| 610 | =encoding UTF-8 | ||
| 611 | |||
| 612 | =head1 NAME | ||
| 613 | |||
| 614 | Koha::Contrib::Sudoc::Localisation - Localisation auto de notices biblio | ||
| 615 | |||
| 616 | =head1 VERSION | ||
| 617 | |||
| 618 | version 2.20 | ||
| 619 | |||
| 620 | =head1 AUTHOR | ||
| 621 | |||
| 622 | Frédéric Demians <f.demians@tamil.fr> | ||
| 623 | |||
| 624 | =head1 COPYRIGHT AND LICENSE | ||
| 625 | |||
| 626 | This software is Copyright (c) 2016 by Fréderic Demians. | ||
| 627 | |||
| 628 | This is free software, licensed under: | ||
| 629 | |||
| 630 | The GNU General Public License, Version 3, June 2007 | ||
| 631 | |||
| 632 | =cut |