Koha/Contrib/Sudoc/Converter.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 214 CC 23 SCAR 31.6
CC
23
Cov
0%
merge
56.0
build
30.0
itemize
30.0
CRAP 23.5
1 package Koha::Contrib::Sudoc::Converter;
2 # ABSTRACT: Classe de base pour convertir les notices
3 $Koha::Contrib::Sudoc::Converter::VERSION = '2.50';
4 use Moose;
5 use utf8;
6 use Modern::Perl;
7  
8 # Moulinette SUDOC
9 has sudoc => ( is => 'rw', isa => 'Koha::Contrib::Sudoc', required => 1 );
10  
11 has log => ( is => 'rw', isa => 'Log::Dispatch' );
12  
13 has item => ( is => 'rw', isa => 'HashRef' );
14  
15  
16  
17 sub build {
18 my ($self, $record) = @_;
19  
20 # On crée la structure de données items
21 my $myrcr = $self->sudoc->c->{rcr};
22 my $item = {};
23 for my $field ( @{$record->fields} ) {
24 next if ref $field eq 'MARC::Moose::Field::Control';
25 my $value = $field->subfield('5');
26 next unless $value;
27 my ($rcr, $id) = $value =~ /(.*):(.*)/;
28 next unless $rcr; # Probablement un champ 035
29 unless ( $myrcr->{$rcr} ) {
30 # Cas, improbable, d'un RCR qui ne serait pas dans la liste des RCR
31 # FIXME On pourrait le logguer quelque part.
32 next;
33 }
34 $item->{$rcr} ||= {};
35 $item->{$rcr}->{$id} ||= {};
36 $item->{$rcr}->{$id}->{$field->tag} = $field;
37 }
38 $self->item($item);
39 }
40  
41  
42 sub skip {
43 return 0;
44 }
45  
46  
47 sub init {
48 my ($self, $record) = @_;
49  
50 # On supprime de la notice SUDOC les champs à exclure
51 my $exclure = $self->sudoc->c->{biblio}->{exclure};
52 if ( $exclure && ref($exclure) eq 'ARRAY' ) {
53 my %hexclure;
54 $hexclure{$_} = 1 for @$exclure;
55 $record->fields( [ grep { not $hexclure{$_->tag} } @{$record->fields} ] );
56 }
57 }
58  
59  
60 sub authoritize {
61 my ($self, $record) = @_;
62  
63 # Ne rien faire si c'est demandé pour l'ILN
64 return unless $self->sudoc->c->{biblio}->{authoritize};
65  
66 my $koha = $self->sudoc->koha;
67 for my $field ( $record->field('5..|6..|7..') ) {
68 my @subf;
69 for my $sf ( @{$field->subf} ) {
70 my ($letter, $value) = @$sf;
71 push @subf, [ $letter => $value ];
72 next if $letter ne '3';
73 my ($authid, undef) = $koha->get_auth_by_ppn($value);
74 push @subf, [ 9 => $authid ] if $authid;
75 }
76 $field->subf(\@subf);
77 }
78 }
79  
80  
81 sub linking {
82 my ($self, $record) = @_;
83  
84 # Ne rien faire si c'est demandé pour l'ILN
85 return unless $self->sudoc->c->{biblio}->{linking};
86  
87 my $koha = $self->sudoc->koha;
88 for my $field ( $record->field('4..|5..') ) {
89 my @subf;
90 for my $sf ( @{$field->subf} ) {
91 my ($letter, $value) = @$sf;
92 push @subf, [ $letter => $value ];
93 next if $letter ne '0';
94 my ($biblionumber) = $koha->get_biblio_by_ppn($value);
95 push @subf, [ '9' => $biblionumber ] if $biblionumber;
96 }
97 $field->subf(\@subf);
98 }
99 }
100  
101  
102 sub itemize {
103 my ($self, $record, $koha_record) = @_;
104  
105 # Ne rien faire si c'est demandé pour l'ILN
106 return unless $self->sudoc->c->{biblio}->{itemize};
107  
108 # Pas d'exemplarisation si on modifie une notice Koha
109 return if $koha_record;
110  
111 my $myrcr = $self->sudoc->c->{rcr};
112 my $item = $self->{item};
113  
114 # On crée les exemplaires à partir de 930 et 915
115 while ( my ($rcr, $item_rcr) = each %$item ) {
116 my $branch = $myrcr->{$rcr};
117 while ( my ($id, $ex) = each %$item_rcr ) { # Les exemplaires d'un RCR
118 # On prend le code à barres en 915$b, et s'il n'y en a pas on prend
119 # l'EPN SUDOC ($id)
120 my $barcode = $ex->{915};
121 $barcode = $barcode->subfield('b') if $barcode;
122 $barcode = $id unless $barcode;
123 my $cote = $ex->{930}->subfield('a');
124 $record->append( MARC::Moose::Field::Std->new(
125 tag => '995',
126 subf => [
127 [ b => $branch ],
128 [ c => $branch ],
129 [ f => $barcode ],
130 [ k => $cote ],
131 ]
132 ) );
133 }
134 }
135 }
136  
137  
138 sub _key_dedup {
139 join('', map { lc $_->[1] } grep { $_->[0] =~ /[a-z]/; } @{shift->subf});
140 }
141  
142  
143 sub merge {
144 my ($self, $record, $krecord) = @_;
145  
146 # On garde les champs "protégés" de la notice Koha
147 # On évite les doublons
148 my $conf = $self->sudoc->c->{biblio};
149 if ( my $proteger = $conf->{proteger} ) {
150 my $pt = {}; # Hash de hash de tag - clé de dédoublonnage
151 for my $tag ( @$proteger ) {
152 $pt->{$tag} ||= {};
153 for my $field ( $record->field($tag) ) {
154 my $key = _key_dedup($field);
155 next unless $key;
156 $pt->{$tag}->{$key} = undef;
157 }
158 }
159 for my $tag ( @$proteger ) {
160 my @fields = $krecord->field($tag);
161 next unless @fields;
162 if ( exists $pt->{$tag} ) {
163 my @keeps;
164 for my $field (@fields) {
165 my $key = _key_dedup($field);
166 next unless $key;
167 push @keeps, $field unless exists $pt->{$tag}->{$key};
168 }
169 $record->append(@keeps);
170 }
171 else {
172 $record->append(@fields);
173 }
174 }
175 }
176 }
177  
178  
179 sub clean {
180 my ($self, $record) = @_;
181 }
182  
183  
184 sub framework {
185 my ($self, $record) = @_;
186 $self->sudoc->c->{biblio}->{framework} || '';
187 }
188  
189  
190 sub biblio_modify {
191 my ($self, $record, $biblionumber, $framework) = @_;
192 $self->log->debug(
193 " Notice après traitement :\n" . $self->sudoc->record_as_text($record) );
194 $self->log->notice(" * Remplace $biblionumber\n" );
195 }
196  
197  
198 sub biblio_add {
199 my ($self, $record, $biblionumber, $framework) = @_;
200  
201 $self->log->debug(
202 " Notice après traitement :\n" . $self->sudoc->record_as_text($record) );
203 my $plus = '';
204 $plus = " $biblionumber $framework" if $biblionumber;
205 $self->log->notice( " * Ajout$plus\n" );
206 }
207  
208  
209 sub end { }
210  
211 1;
212  
213 __END__