| 1 | package Koha::Contrib::Mirabel; | ||
| 2 | $Koha::Contrib::Mirabel::VERSION = '1.0.2'; | ||
| 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 | |||
| 20 | has url => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | default => 'https://reseau-mirabel.info/site/service?', | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | has partenaire => ( is => 'rw', isa => 'Int' ); | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | has tag => ( is => 'rw', isa => 'Str' ); | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | has verbose => ( is => 'rw', isa => 'Bool', default => 1 ); | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | has doit => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 41 | |||
| 42 | |||
| 43 | sub BUILD { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my $partenaire = C4::Context->preference('MirabelPartenaire'); | ||
| 47 | die "Il manque la préférence MirabelPartenaire" unless $partenaire; | ||
| 48 | $self->partenaire($partenaire); | ||
| 49 | |||
| 50 | my $tag = C4::Context->preference('MirabelTag'); | ||
| 51 | die "Il manque la préférence MirabelTag" unless $tag; | ||
| 52 | $self->tag($tag); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub get_biblio { | ||
| 57 | my ($self, $biblionumber) = @_; | ||
| 58 | |||
| 59 | my $record = GetMarcBiblio( { biblionumber => $biblionumber } ); | ||
| 60 | return unless $record; | ||
| 61 | return MARC::Moose::Record::new_from($record, 'Legacy'); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub update { | ||
| 66 | my ($self, $biblionumber, $services) = @_; | ||
| 67 | |||
| 68 | say '_' x 40, " #$biblionumber" if $self->verbose; | ||
| 69 | |||
| 70 | my $record = $self->get_biblio($biblionumber); | ||
| 71 | unless ($record) { | ||
| 72 | say 'ERREUR: Notice présente dans Mir@bel mais supprimée du Catalogue Koha' | ||
| 73 | if $self->verbose; | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | print $record->as('Text') if $self->verbose; | ||
| 77 | |||
| 78 | # On supprime de la notice biblio les champs cibles existants | ||
| 79 | $record->delete( $self->tag ); | ||
| 80 | |||
| 81 | for my $service (@$services) { | ||
| 82 | say "Mir\@bel #", $service->{id}, "\n", | ||
| 83 | join("\n", | ||
| 84 | map { " $_: " . $service->{$_} } grep { $_ ne 'id' } | ||
| 85 | keys %$service ) | ||
| 86 | if $self->verbose; | ||
| 87 | my @sf = ( | ||
| 88 | [ 3 => $service->{id} ], | ||
| 89 | [ 4 => $service->{type} ], | ||
| 90 | [ a => $service->{urldirecte} || $service->{urlservice} ], | ||
| 91 | [ b => $service->{nom} ], | ||
| 92 | ); | ||
| 93 | if (my $value = $service->{acces}) { push @sf, [ c => $value ]; } | ||
| 94 | |||
| 95 | my @coll; | ||
| 96 | push @coll, $service->{debut} if $service->{debut}; | ||
| 97 | push @coll, '-'; | ||
| 98 | push @coll, $service->{fin} if $service->{fin}; | ||
| 99 | push @sf, [ d => join('', @coll) ] if @coll > 1; | ||
| 100 | |||
| 101 | if (my $value = $service->{couverture}) { push @sf, [ e => $value ]; } | ||
| 102 | if (my $value = $service->{lacunaire}) { push @sf, [ f => $value ]; } | ||
| 103 | $record->append( MARC::Moose::Field::Std->new( | ||
| 104 | tag => $self->tag, subf => \@sf ) ); | ||
| 105 | } | ||
| 106 | print "\nAPRÈS:\n", $record->as('Text') if $self->verbose; | ||
| 107 | |||
| 108 | # On réécrit la notice | ||
| 109 | if ( $self->doit ) { | ||
| 110 | $record = $record->as('Legacy'); | ||
| 111 | ModBiblioMarc( $record, $biblionumber, GetFrameworkCode($biblionumber) ); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | sub sync { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | if ($self->verbose) { | ||
| 121 | say "Synchro"; | ||
| 122 | say "** TEST **" unless $self->doit; | ||
| 123 | } | ||
| 124 | |||
| 125 | my $url = $self->url . 'partenaire=' . $self->partenaire; | ||
| 126 | my $doc = get($url); | ||
| 127 | unless ($doc) { | ||
| 128 | say "Le service web ne répond pas : $url"; | ||
| 129 | exit; | ||
| 130 | } | ||
| 131 | my $xml = XML::Simple->new( | ||
| 132 | keyattr => [], SuppressEmpty => 1, | ||
| 133 | ForceArray => [ 'revue', 'service' ], ); | ||
| 134 | my $result = $xml->XMLin($doc); | ||
| 135 | |||
| 136 | for my $revue (@{$result->{revue}}) { | ||
| 137 | $self->update($revue->{idpartenairerevue}, $revue->{services}->{service}); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | sub all_bibs { | ||
| 144 | my $tag = shift->tag; | ||
| 145 | my $query = | ||
| 146 | "SELECT biblionumber " . | ||
| 147 | "FROM biblio_metadata " . | ||
| 148 | "WHERE ExtractValue(metadata,'//datafield[\@tag=$tag]/subfield[\@code]')"; | ||
| 149 | my $st = C4::Context->dbh->prepare($query); | ||
| 150 | $st->execute; | ||
| 151 | my @bibs; | ||
| 152 | while (my ($biblionumber) = $st->fetchrow ) { | ||
| 153 | push @bibs, $biblionumber; | ||
| 154 | } | ||
| 155 | return \@bibs; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | |||
| 160 | sub clean { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | if ($self->verbose) { | ||
| 164 | say "Suppression dans Koha des services retirés de Mir\@bel depuis un an"; | ||
| 165 | say "** TEST **" unless $self->doit; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Récupération des identifiants des services supprimés depuis un an | ||
| 169 | my $dt = DateTime->now->subtract( years => 1 ); | ||
| 170 | my $doc = get($self->url . 'suppr=' . $dt->ymd); | ||
| 171 | my $xml = XML::Simple->new( ForceArray => [ 'service' ], ); | ||
| 172 | my $result = $xml->XMLin($doc); | ||
| 173 | my @ids = sort { $a <=> $b } @{$result->{service}}; | ||
| 174 | my %is_removed_id = map { $_ => 1 } @ids; | ||
| 175 | say "Services supprimés : ", join(', ', @ids), "\n" if $self->verbose; | ||
| 176 | |||
| 177 | # Modification des notices contenant un service supprimé | ||
| 178 | my $found = 0; | ||
| 179 | my $tag = $self->tag; | ||
| 180 | for my $biblionumber (@{$self->all_bibs}) { | ||
| 181 | my $record = GetMarcBiblio( { biblionumber => $biblionumber } ); | ||
| 182 | $record = MARC::Moose::Record::new_from($record, 'Legacy'); | ||
| 183 | my $has_id = 0; | ||
| 184 | for my $field ( $record->field($tag) ) { | ||
| 185 | my $id = first { $_->[0] eq '3' } @{$field->subf}; | ||
| 186 | next unless $id; | ||
| 187 | $has_id = $is_removed_id{$id->[1]}; | ||
| 188 | last if $has_id; | ||
| 189 | } | ||
| 190 | next unless $has_id; | ||
| 191 | $found = 1; | ||
| 192 | |||
| 193 | say '_' x 40, " #$biblionumber"; | ||
| 194 | print $record->as('Text') if $self->verbose; | ||
| 195 | |||
| 196 | # On supprime les zones dont l'id ($3) appartient à la liste des | ||
| 197 | # services Mir@bel supprimés | ||
| 198 | $record->fields( [ grep { | ||
| 199 | $_->tag eq $tag | ||
| 200 | ? ! first { $_->[0] eq '3' && $is_removed_id{$_->[1]} } @{$_->subf} | ||
| 201 | : 1 | ||
| 202 | } @{$record->fields} ] ); | ||
| 203 | |||
| 204 | print "APRÈS\n", $record->as('Text') if $self->verbose; | ||
| 205 | if ( $self->doit ) { | ||
| 206 | $record = $record->as('Legacy'); | ||
| 207 | ModBiblioMarc( $record, $biblionumber, GetFrameworkCode($biblionumber) ); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | say "Aucune notice ne contient de service Mir\@bel supprimé" | ||
| 212 | if !$found && $self->verbose; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | sub full_clean { | ||
| 217 | my $self = shift; | ||
| 218 | |||
| 219 | if ($self->verbose) { | ||
| 220 | say "Suppression dans Koha de tous les champs Mir\@bel"; | ||
| 221 | say "** TEST **" unless $self->doit; | ||
| 222 | } | ||
| 223 | |||
| 224 | for my $biblionumber (@{$self->all_bibs}) { | ||
| 225 | my $record = $self->get_biblio($biblionumber); | ||
| 226 | next unless $record->field($self->tag); # Impossible normalement... | ||
| 227 | say '_' x 40, " #$biblionumber"; | ||
| 228 | print $record->as('Text') if $self->verbose; | ||
| 229 | $record->delete( $self->tag ); | ||
| 230 | print "APRÈS\n", $record->as('Text') if $self->verbose; | ||
| 231 | if ( $self->doit ) { | ||
| 232 | $record = $record->as('Legacy'); | ||
| 233 | ModBiblioMarc( $record, $biblionumber, GetFrameworkCode($biblionumber) ); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | __PACKAGE__->meta->make_immutable; | ||
| 240 | 1; | ||
| 241 | |||
| 242 | __END__ |