Koha/Contrib/Sudoc/Converter.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 181 CC 20 SCAR 33.1
CC
20
Cov
0%
merge
56.0
authoritize
30.0
linking
30.0
CRAP 27.3
1 package Koha::Contrib::Sudoc::Converter;
2 # ABSTRACT: Classe de base pour convertir les notices
3 $Koha::Contrib::Sudoc::Converter::VERSION = '2.20';
4 use Moose;
5 use Modern::Perl;
6  
7 # Moulinette SUDOC
8 has sudoc => ( is => 'rw', isa => 'Koha::Contrib::Sudoc', required => 1 );
9  
10 has log => ( is => 'rw', isa => 'Log::Dispatch' );
11  
12 has item => ( is => 'rw', isa => 'HashRef' );
13  
14  
15  
16 sub skip {
17 my ($self, $record) = @_;
18 return 0;
19 }
20  
21  
22 sub init {
23 my ($self, $record) = @_;
24  
25 my $myrcr = $self->sudoc->c->{rcr};
26 # On crée la structure de données items
27 my $item = {};
28 for my $field ( $record->field('9..') ) {
29 my $value = $field->subfield('5');
30 next unless $value;
31 my ($rcr, $id) = $value =~ /(.*):(.*)/;
32 unless ( $myrcr->{$rcr} ) {
33 # Cas, improbable, d'un RCR qui ne serait pas dans la liste des RCR
34 # FIXME On pourrait le logguer quelque part.
35 next;
36 }
37 $item->{$rcr} ||= {};
38 $item->{$rcr}->{$id} ||= {};
39 $item->{$rcr}->{$id}->{$field->tag} = $field;
40 }
41 $self->item($item);
42  
43 # On supprime de la notice SUDOC les champs à exclure
44 my $exclure = $self->sudoc->c->{biblio}->{exclure};
45 if ( $exclure && ref($exclure) eq 'ARRAY' ) {
46 my %hexclure;
47 $hexclure{$_} = 1 for @$exclure;
48 $record->fields( [ grep { not $hexclure{$_->tag} } @{$record->fields} ] );
49 }
50 }
51  
52  
53 sub authoritize {
54 my ($self, $record) = @_;
55  
56 # Ne rien faire si c'est demandé pour l'ILN
57 return unless $self->sudoc->c->{biblio}->{authoritize};
58  
59 my $zconn = $self->sudoc->koha->zauth();
60 for my $field ( $record->field('5..|6..|7..') ) {
61 my @subf;
62 for my $sf ( @{$field->subf} ) {
63 my ($letter, $value) = @$sf;
64 push @subf, [ $letter => $value ];
65 if ( $letter eq '3' ) {
66 my $rs = $zconn->search_pqf( "\@attr 1=PPN $value" );
67 if ($rs->size() >= 1 ) {
68 my $auth = MARC::Moose::Record::new_from(
69 $rs->record(0)->raw(), 'Iso2709' );
70 push @subf, [ '9' => $auth->field('001')->value ]
71 if $auth;
72 }
73 }
74 }
75 $field->subf(\@subf);
76 }
77 }
78  
79  
80 sub linking {
81 my ($self, $record) = @_;
82  
83 # Ne rien faire si c'est demandé pour l'ILN
84 return unless $self->sudoc->c->{biblio}->{linking};
85  
86 my $zconn = $self->sudoc->koha->zbiblio();
87 for my $field ( $record->field('4..|5..') ) {
88 my @subf;
89 for my $sf ( @{$field->subf} ) {
90 my ($letter, $value) = @$sf;
91 push @subf, [ $letter => $value ];
92 if ( $letter eq '0' ) {
93 my $rs = $zconn->search_pqf( "\@attr 1=PPN $value" );
94 if ($rs->size() >= 1 ) {
95 my $biblio = MARC::Moose::Record::new_from(
96 $rs->record(0)->raw(), 'Iso2709' );
97 push @subf, [ '9' => $self->sudoc->koha->get_biblionumber($biblio) ],
98 if $biblio;
99 }
100 }
101 }
102 $field->subf(\@subf);
103 }
104 }
105  
106  
107 sub itemize {
108 my ($self, $record) = @_;
109  
110 my $myrcr = $self->sudoc->c->{rcr};
111 my $item = $self->{item};
112  
113 # On crée les exemplaires à partir de 930 et 915
114 while ( my ($rcr, $item_rcr) = each %$item ) {
115 my $branch = $myrcr->{$rcr};
116 while ( my ($id, $ex) = each %$item_rcr ) { # Les exemplaires d'un RCR
117 # On prend le code à barres en 915$b, et s'il n'y en a pas on prend
118 # l'EPN SUDOC ($id)
119 my $barcode = $ex->{915};
120 $barcode = $barcode->subfield('b') if $barcode;
121 $barcode = $id unless $barcode;
122 my $cote = $ex->{930}->subfield('a');
123 $record->append( MARC::Moose::Field::Std->new(
124 tag => '995',
125 subf => [
126 [ b => $branch ],
127 [ c => $branch ],
128 [ f => $barcode ],
129 [ k => $cote ],
130 ]
131 ) );
132 }
133 }
134 }
135  
136  
137 sub _key_dedup {
138 join('', map { lc $_->[1] } grep { $_->[0] =~ /[a-z]/; } @{shift->subf});
139 }
140  
141  
142 sub merge {
143 my ($self, $record, $krecord) = @_;
144  
145 # On garde les champs "protégés" de la notice Koha
146 # On évite les doublons
147 my $conf = $self->sudoc->c->{biblio};
148 if ( my $proteger = $conf->{proteger} ) {
149 my $pt = {}; # Hash de hash de tag - clé de dédoublonnage
150 for my $tag ( @$proteger ) {
151 $pt->{$tag} ||= {};
152 for my $field ( $record->field($tag) ) {
153 my $key = _key_dedup($field);
154 next unless $key;
155 $pt->{$tag}->{$key} = undef;
156 }
157 }
158 for my $tag ( @$proteger ) {
159 my @fields = $krecord->field($tag);
160 next unless @fields;
161 if ( exists $pt->{$tag} ) {
162 my @keeps;
163 for my $field (@fields) {
164 my $key = _key_dedup($field);
165 next unless $key;
166 push @keeps, $field unless exists $pt->{$tag}->{$key};
167 }
168 $record->append(@keeps);
169 }
170 else {
171 $record->append(@fields);
172 }
173 }
174 }
175 }
176  
177  
178 sub clean {
179 my ($self, $record) = @_;
180 }
181  
182  
183 sub framework {
184 my ($self, $record) = @_;
185 $self->sudoc->c->{biblio}->{framework} || '';
186 }
187  
188 1;
189  
190 __END__
191  
192 =pod
193  
194 =encoding UTF-8
195  
196 =head1 NAME
197  
198 Koha::Contrib::Sudoc::Converter - Classe de base pour convertir les notices
199  
200 =head1 VERSION
201  
202 version 2.20
203  
204 =head1 DESCRIPTION
205  
206 Les méthodes de cette classe sont appelées dans un certain ordre par
207 le chargeur des notices biblios, selon qu'il s'agisse d'une nouvelle
208 notice ou d'une notice qui existe déjà dans Koha:
209  
210 Méthode ajout modif
211 --------------------------
212 skip O O
213 init O O
214 authoritize O O
215 linking O O
216 itemize N O
217 merge O N
218 clean O O
219 framework O N
220  
221 =head1 ATTRIBUTES
222  
223 =head2 log
224  
225 Logger L<Log::Dispatch> hérité de la classe parente L<Koha::Contrib::Sudoc::Loader>.
226  
227 =head2 item
228  
229 Les exemplaires courants.
230  
231 ->{rcr}->{id}->{915}
232 ->{930}
233 ->{999}
234 076797597:
235 915:
236 917:
237 930:
238 999:
239 243615450:
240 915:
241 930:
242 991:
243  
244 =head1 METHODS
245  
246 =head2 skip
247  
248 La notice doit-elle être passée ? Par défaut, on garde toute notice.
249  
250 =head2 init
251  
252 Méthode appelée après C<skip> pour un enregistrement SUDOC entrant, que ce
253 soit un doublon ou une nouvelle notice. Initialisation du hash item.
254 Suppression de la notice entrante des champs définis dans C<sudoc.conf> :
255 C<biblio-exclure>
256  
257 =head2 authoritize
258  
259 On remplit le $9 Koha des champs liés à des autorités
260  
261 =head2 linking
262  
263 Lien des notices biblio entre elles. Les liens entre notices se trouvent dans
264 les zones 4xx et 5xx, sous-champ $0 qui contient un PPN. A partir du PPN, la
265 notice liée est retrouvée dans Koha et son biblionumber est placée en $9, le
266 $0 étant conservé.
267  
268 =head2 itemize
269  
270 Création des exemplaires Koha en 995 en fonction des données locales SUDOC, au
271 moyen de la structure de données $self->item. Les champs bib propriétaire
272 ($b), bib détentrice ($c), code à barres ($f) et cote ($k) sont remplis.
273  
274 =head2 merge
275  
276 Fusion d'une notice entrante Sudoc avec une notice Koha. Les champs "protégés"
277 sont conservés dans la notices Koha. Tout le reste de la notice est remplacé
278 par la notice SUDOC. Les champs prorégés sont dédoublonnés entre la notices
279 Koha et la notice SUDOC.
280  
281 =head2 clean
282  
283 On nettoie la notice : suppression de champs, ajout auto de champs, etc. Cette
284 opération est faite après la fusion (éventuelle) de notices.
285  
286 =head2 framework
287  
288 Le framework auquel affecter la notice biblio. Valeur par défaut prise dans
289 C<sudoc.conf>. Peut-être surchargée pour attribuer un framework différent en
290 fonction du type de doc ou de tout autre critère.
291  
292 =head1 AUTHOR
293  
294 Frédéric Demians <f.demians@tamil.fr>
295  
296 =head1 COPYRIGHT AND LICENSE
297  
298 This software is Copyright (c) 2016 by Fréderic Demians.
299  
300 This is free software, licensed under:
301  
302 The GNU General Public License, Version 3, June 2007
303  
304 =cut