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

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