Koha/Contrib/Sudoc/Localisation.pm untested

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