blib/lib/Koha/Contrib/Mirabel.pm untested

filter: Statementstmt 0.0% 0 / 168 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 9 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 269 CC 35 SCAR 46.2
CC
35
Cov
0%
update
210.0
clean
156.0
full_clean
56.0
CRAP 102.0
1 package Koha::Contrib::Mirabel;
2 $Koha::Contrib::Mirabel::VERSION = '0.0.5';
3 # ABSTRACT: Synchronise un catalogue Koha avec Mir@bel
4 use Moose;
5  
6 use Modern::Perl;
7 use utf8;
8 use FindBin qw($Bin);
9 use List::Util qw/first/;
10 use YAML qw/LoadFile Dump/;
11 use XML::Simple;
12 use LWP::Simple;
13 use DateTime;
14 use C4::Biblio;
15 use MARC::Moose::Record;
16 use MARC::Moose::Field::Std;
17  
18  
19 has url => (
20 is => 'rw',
21 isa => 'Str',
22 default => 'http://www.reseau-mirabel.info/site/service?',
23 );
24  
25  
26  
27 has partenaire => ( is => 'rw', isa => 'Int' );
28  
29  
30  
31 has tag => ( is => 'rw', isa => 'Str' );
32  
33  
34  
35 has verbose => ( is => 'rw', isa => 'Bool', default => 1 );
36  
37  
38  
39 has doit => ( is => 'rw', isa => 'Bool', default => 0 );
40  
41  
42 sub BUILD {
43 my $self = shift;
44  
45 my $partenaire = C4::Context->preference('MirabelPartenaire');
46 die "Il manque la preference MirabelPartenaire" unless $partenaire;
47 $self->partenaire($partenaire);
48  
49 my $tag = C4::Context->preference('MirabelTag');
50 die "Il manque la preference MirabelTag" unless $tag;
51 $self->tag($tag);
52 }
53  
54  
55 sub update {
56 my ($self, $biblionumber, $services) = @_;
57  
58 say '_' x 40, " #$biblionumber" if $self->verbose;
59  
60 my $record = GetMarcBiblio($biblionumber);
61 unless ($record) {
62 say 'ERREUR: Notice présente dans Mir@bel mais supprimée du Catalogue Koha'
63 if $self->verbose;
64 return;
65 }
66 $record = MARC::Moose::Record::new_from($record, 'Legacy');
67 print $record->as('Text') if $self->verbose;
68  
69 # On supprime de la notice biblio les champs cibles existants
70 $record->delete( $self->tag );
71  
72 for my $service (@$services) {
73 say "Mirabel #", $service->{id}, "\n",
74 join("\n",
75 map { " $_: " . $service->{$_} } grep { $_ ne 'id' }
76 keys %$service )
77 if $self->verbose;
78 my @sf = (
79 [ 3 => $service->{id} ],
80 [ 4 => $service->{type} ],
81 [ a => $service->{urldirecte} || $service->{urlservice} ],
82 [ b => $service->{nom} ],
83 );
84 if (my $value = $service->{acces}) { push @sf, [ c => $value ]; }
85  
86 my @coll;
87 push @coll, $service->{debut} if $service->{debut};
88 push @coll, '-';
89 push @coll, $service->{fin} if $service->{fin};
90 push @sf, [ d => join('', @coll) ] if @coll > 1;
91  
92 if (my $value = $service->{couverture}) { push @sf, [ e => $value ]; }
93 if (my $value = $service->{lacunaire}) { push @sf, [ f => $value ]; }
94 $record->append( MARC::Moose::Field::Std->new(
95 tag => $self->tag, subf => \@sf ) );
96 }
97 print "\nAPRÈS:\n", $record->as('Text') if $self->verbose;
98  
99 # On réécrit la notice
100 if ( $self->doit ) {
101 $record = $record->as('Legacy');
102 ModBiblioMarc( $record, $biblionumber, GetFrameworkCode($biblionumber) );
103 }
104 }
105  
106  
107  
108 sub sync {
109 my $self = shift;
110  
111 if ($self->verbose) {
112 say "Synchro";
113 say "** TEST **" unless $self->doit;
114 }
115  
116 my $doc = get($self->url . 'partenaire=' . $self->partenaire);
117 my $xml = XML::Simple->new(
118 keyattr => [], SuppressEmpty => 1,
119 ForceArray => [ 'revue', 'service' ], );
120 my $result = $xml->XMLin($doc);
121  
122 for my $revue (@{$result->{revue}}) {
123 $self->update($revue->{idpartenairerevue}, $revue->{services}->{service});
124 }
125 }
126  
127  
128  
129 sub all_bibs {
130 my $tag = shift->tag;
131 my $query =
132 "SELECT biblionumber " .
133 "FROM biblioitems " .
134 "WHERE ExtractValue(marcxml,'//datafield[\@tag=$tag]/subfield[\@code]')";
135 my $st = C4::Context->dbh->prepare($query);
136 $st->execute;
137 my @bibs;
138 while (my ($biblionumber) = $st->fetchrow ) {
139 push @bibs, $biblionumber;
140 }
141 return \@bibs;
142 }
143
144  
145  
146 sub clean {
147 my $self = shift;
148  
149 if ($self->verbose) {
150 say "Suppression dans Koha des services retirés de Mir\@bel depuis un an";
151 say "** TEST **" unless $self->doit;
152 }
153  
154 # Récupération des identifiants des services supprimés depuis un an
155 my $dt = DateTime->now->subtract( years => 1 );
156 my $doc = get($self->url . 'suppr=' . $dt->ymd);
157 my $xml = XML::Simple->new( ForceArray => [ 'service' ], );
158 my $result = $xml->XMLin($doc);
159 my @ids = sort { $a <=> $b } @{$result->{service}};
160 my %is_removed_id = map { $_ => 1 } @ids;
161 say "Services supprimés : ", join(', ', @ids), "\n" if $self->verbose;
162  
163 # Modification des notices contenant un service supprimé
164 my $found = 0;
165 my $tag = $self->tag;
166 for my $biblionumber (@{$self->all_bibs}) {
167 my $record = GetMarcBiblio($biblionumber);
168 $record = MARC::Moose::Record::new_from($record, 'Legacy');
169 my $has_id = 0;
170 for my $field ( $record->field($tag) ) {
171 my $id = first { $_->[0] eq '3' } @{$field->subf};
172 next unless $id;
173 $has_id = $is_removed_id{$id->[1]};
174 last if $has_id;
175 }
176 next unless $has_id;
177 $found = 1;
178  
179 say '_' x 40, " #$biblionumber";
180 print $record->as('Text') if $self->verbose;
181  
182 # On supprime les zones dont l'id ($3) appartient à la liste des
183 # services Mir@bel supprimés
184 $record->fields( [ grep {
185 $_->tag eq $tag
186 ? ! first { $_->[0] eq '3' && $is_removed_id{$_->[1]} } @{$_->subf}
187 : 1
188 } @{$record->fields} ] );
189  
190 print "APRÈS\n", $record->as('Text') if $self->verbose;
191 if ( $self->doit ) {
192 $record = $record->as('Legacy');
193 ModBiblioMarc( $record, $biblionumber, GetFrameworkCode($biblionumber) );
194 }
195 }
196  
197 say "Aucune notice ne contient de service Mir\@bel supprimé"
198 if !$found && $self->verbose;
199 }
200  
201  
202 sub full_clean {
203 my $self = shift;
204  
205 if ($self->verbose) {
206 say "Suppression dans Koha de tous les champs Mir\@bel";
207 say "** TEST **" unless $self->doit;
208 }
209  
210 for my $biblionumber (@{$self->all_bibs}) {
211 my $record = GetMarcBiblio($biblionumber);
212 $record = MARC::Moose::Record::new_from($record, 'Legacy');
213 next unless $record->field($self->tag); # Impossible normalement...
214 say '_' x 40, " #$biblionumber";
215 print $record->as('Text') if $self->verbose;
216 $record->delete( $self->tag );
217 print "APRÈS\n", $record->as('Text') if $self->verbose;
218 if ( $self->doit ) {
219 $record = $record->as('Legacy');
220 ModBiblioMarc( $record, $biblionumber, GetFrameworkCode($biblionumber) );
221 }
222 }
223 }
224  
225  
226 __PACKAGE__->meta->make_immutable;
227 1;
228  
229 __END__
230  
231 =pod
232  
233 =encoding UTF-8
234  
235 =head1 NAME
236  
237 Koha::Contrib::Mirabel - Synchronise un catalogue Koha avec Mir@bel
238  
239 =head1 VERSION
240  
241 version 0.0.5
242  
243 =head1 ATTRIBUTES
244  
245 =head2 partenaire
246  
247 Le numéro de partenaire Mir@bel. Récupéré dans la préférence MirabelPartenaire
248 de l'instance courtante de Koha.
249  
250 =head2 tag
251  
252 Le tag du champ des notices biblio où se trouvent les infor Mir@bel. Récupéré
253 dans la préférence MirabelTag de l'instance courtante de Koha.
254  
255 =head2 verbose
256  
257 Mode verbeux pas défaut. Ecrit sur la sortie standard des info sur les
258 traitements réalisés.
259  
260 =head2 doit
261  
262 Effecture réellement les traitements de mise à jour du Catalogue Koha. Par défaut NON.
263  
264 =head1 METHODS
265  
266 =head2 sync
267  
268 Synchronise le Catalogue Koha avec les info de Mir@bel.
269  
270 =head2 all_bibs
271  
272 Retourne un ArrayRef des biblionumbers des notices ayant au moins un service
273 Mir@bel dans MirabelTag
274  
275 =head2 clean
276  
277 Nettoie les notices du Catalogue Koha des info Mir@bel qui ont été supprimées
278 de Mir@bel depuis 1 an.
279  
280 =head2 full_clean
281  
282 Supprime tous les champs MirabelTag des notices du Catalogue Koha.
283  
284 =head1 AUTHOR
285  
286 Frédéric Demians <f.demians@tamil.fr>
287  
288 =head1 COPYRIGHT AND LICENSE
289  
290 This software is copyright (c) 2015 by Frédéric Demians.
291  
292 This is free software; you can redistribute it and/or modify it under
293 the same terms as the Perl 5 programming language system itself.
294  
295 =cut