| 1 | package Koha::Contrib::Sudoc; | ||
| 2 | # ABSTRACT: Chargeur Koha par Tamil | ||
| 3 | $Koha::Contrib::Sudoc::VERSION = '2.20'; | ||
| 4 | use Moose; | ||
| 5 | use Modern::Perl; | ||
| 6 | use YAML qw( LoadFile Dump ); | ||
| 7 | use Koha::Contrib::Sudoc::Koha; | ||
| 8 | use Koha::Contrib::Sudoc::Spool; | ||
| 9 | use MARC::Moose::Field::Std; | ||
| 10 | use MARC::Moose::Field::Control; | ||
| 11 | use File::Copy; | ||
| 12 | use Path::Tiny; | ||
| 13 | use Mail::Box::Manager; | ||
| 14 | use DateTime; | ||
| 15 | use File::ShareDir ':ALL'; | ||
| 16 | |||
| 17 | |||
| 18 | # L'instance de Koha de l'ILN courant | ||
| 19 | has koha => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Koha::Contrib::Sudoc::Koha', | ||
| 22 | default => sub { Koha::Contrib::Sudoc::Koha->new() } | ||
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | # La racine de l'environnement d'exécution du chargeur | ||
| 27 | has root => ( | ||
| 28 | is => 'rw', | ||
| 29 | isa => 'Str', | ||
| 30 | default => sub { | ||
| 31 | my $self = shift; | ||
| 32 | my $root = $ENV{SUDOC}; | ||
| 33 | unless ($root) { | ||
| 34 | say "Il manque la variable d'environnement SUDOC."; | ||
| 35 | exit; | ||
| 36 | } | ||
| 37 | unless ( -d $root ) { | ||
| 38 | say "variable d'environnement SUDOC=$root"; | ||
| 39 | say "Ce répertoire n'existe pas. Il faut le créer, puis initialiser le chargeur si nécessaire."; | ||
| 40 | exit; | ||
| 41 | } | ||
| 42 | unshift @INC, "$root/lib"; | ||
| 43 | $self->root( $root ); | ||
| 44 | }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | # Le contenu du fichier de config | ||
| 48 | has c => ( is => 'rw', ); | ||
| 49 | |||
| 50 | # Le Spool | ||
| 51 | has spool => ( is => 'rw', isa => 'Koha::Contrib::Sudoc::Spool' ); | ||
| 52 | |||
| 53 | |||
| 54 | sub BUILD { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | # Lecture du fichier de config et création du hash branchcode => RCR par ILN | ||
| 58 | my $file = $self->root . "/etc/sudoc.conf"; | ||
| 59 | return unless -e $file; | ||
| 60 | my $c = LoadFile($file); | ||
| 61 | my %branchcode; | ||
| 62 | while ( my ($rcr, $branch) = each %{$c->{rcr}} ) { | ||
| 63 | $branchcode{$branch} = $rcr; | ||
| 64 | } | ||
| 65 | $c->{branch} = \%branchcode; | ||
| 66 | $self->c($c); | ||
| 67 | |||
| 68 | # Contrôle de quelques paramètres | ||
| 69 | my $loading = $c->{loading}; | ||
| 70 | unless ($loading) { | ||
| 71 | say "Erreur sudoc.conf: Il manque la section 'loading'"; | ||
| 72 | exit; | ||
| 73 | } | ||
| 74 | my $log = $loading->{log}; | ||
| 75 | unless ($log) { | ||
| 76 | say "Erreur sudoc.conf: Il manque la section 'loading::log'"; | ||
| 77 | exit; | ||
| 78 | } | ||
| 79 | if ( $log->{level} !~ /debug|notice/ ) { | ||
| 80 | say "Erreur sudoc.conf: 'loading::log::level n'est pas égal à debug ou notice."; | ||
| 81 | exit; | ||
| 82 | } | ||
| 83 | my $timeout = $loading->{timeout}; | ||
| 84 | unless ($timeout) { | ||
| 85 | say "Erreur sudoc.conf: Il manque la section 'loading::timeout'"; | ||
| 86 | exit; | ||
| 87 | } | ||
| 88 | for ( qw/ transfer indexing / ) { | ||
| 89 | my $value = $timeout->{$_}; | ||
| 90 | unless ( $value && $value =~ /^[0-9]+$/ && $value >= 1 && $value <= 60) { | ||
| 91 | say "Erreur sudoc.conf: Le paramètre 'loading::timeout::$_' doit être compris entre 1 et 60."; | ||
| 92 | exit; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | # L'object Sudoc::Spool | ||
| 97 | $self->spool( Koha::Contrib::Sudoc::Spool->new( sudoc => $self ) ); | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | # Déplace le PPN (001) d'une notice SUDOC dans une zone Koha | ||
| 102 | sub ppn_move { | ||
| 103 | my ($self, $record, $tag) = @_; | ||
| 104 | |||
| 105 | return unless $tag && length($tag >= 3); | ||
| 106 | |||
| 107 | my $letter; | ||
| 108 | if ( $tag =~ /(\d{3})([0-9a-z])/ ) { $tag = $1, $letter = $2; } | ||
| 109 | elsif ( $tag =~ /(\d{3})/ ) { $tag = $1 }; | ||
| 110 | |||
| 111 | return if $tag eq '001'; | ||
| 112 | |||
| 113 | my $ppn = $record->field('001')->value; | ||
| 114 | $record->append( | ||
| 115 | $letter | ||
| 116 | ? MARC::Moose::Field::Std->new( tag => $tag, subf => [ [ $letter => $ppn ] ] ) | ||
| 117 | : MARC::Moose::Field::Control->new( tag => $tag, value => $ppn ) | ||
| 118 | ); | ||
| 119 | |||
| 120 | $record->fields( [ grep { $_->tag ne '001' } @{$record->fields} ] ); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | # Crée les sous-répertoires d'un ILN, s'ils n'existent pas déjà | ||
| 125 | sub init { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | say "La variable d'environnement SUDOC définit le répertoire racine : ", $self->root; | ||
| 129 | say "Initialisation du répertoire des données du Chargeur SUDOC d'un ILN."; | ||
| 130 | chdir($self->root); | ||
| 131 | |||
| 132 | say "Création des répertoire 'etc', 'lib' et 'var'."; | ||
| 133 | mkdir $_ for qw/ etc lib var /; | ||
| 134 | |||
| 135 | if ( -f 'etc/sudoc.conf') { | ||
| 136 | say "Le fichier 'etc/sudoc.conf' existe déjà. On ne le remplace pas par le fichier modèle."; | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | say "Création d'un fichier modèle 'etc/sudoc.conf'."; | ||
| 140 | my $conf_path = dist_file('Koha-Contrib-Sudoc', 'etc/sudoc.conf'); | ||
| 141 | copy($conf_path, "etc/sudoc.conf"); | ||
| 142 | } | ||
| 143 | |||
| 144 | say "Création des répertoires 'var/log' et 'var/spool'."; | ||
| 145 | chdir('var'); | ||
| 146 | mkdir $_ for qw/ log spool /; | ||
| 147 | |||
| 148 | chdir('spool'); | ||
| 149 | mkdir $_ for qw/ staged waiting done /; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub reset_email_log { | ||
| 154 | my $root = shift->root; | ||
| 155 | unlink "$root/var/log/email.txt"; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | # Envoi courriel GTD | ||
| 160 | sub send_gtd_email { | ||
| 161 | my ($self, $jobid, $dir) = @_; | ||
| 162 | $dir ||= 'staged'; | ||
| 163 | |||
| 164 | # La date | ||
| 165 | my $year = DateTime->now->year; | ||
| 166 | |||
| 167 | my $c = $self->c->{trans}; | ||
| 168 | |||
| 169 | my $head = Mail::Message::Head->new; | ||
| 170 | $head->add( From => $c->{email}->{koha} ); | ||
| 171 | $head->add( To => $c->{email}->{abes} ); | ||
| 172 | $head->add( Subject => 'GET TITLE DATA' ); | ||
| 173 | my @data = ( | ||
| 174 | "GTD_ILN = " . $self->c->{iln}, | ||
| 175 | "GTD_YEAR = $year", | ||
| 176 | "GTD_FILE_TO = " . $c->{ftp_host}, | ||
| 177 | "GTD_REMOTE_DIR = $dir", | ||
| 178 | ); | ||
| 179 | push @data, "GTD_ORDER = TR$jobid*" if $jobid; | ||
| 180 | my $body = Mail::Message::Body::Lines->new(data => join("\n", @data)); | ||
| 181 | my $message = Mail::Message->new(head => $head, body => $body); | ||
| 182 | $message->send; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | # Chargement de tous les fichiers qui se trouvent dans 'waiting' | ||
| 187 | sub load_waiting { | ||
| 188 | my $self = shift; | ||
| 189 | |||
| 190 | my $c = $self->c; | ||
| 191 | my $loading = $c->{loading}; | ||
| 192 | |||
| 193 | # Temps nécessaire entre deux chargements de fichier pour indexation | ||
| 194 | my $timeout = $loading->{timeout}->{indexing} * 60; | ||
| 195 | my $doit = $loading->{doit}; | ||
| 196 | $self->reset_email_log(); | ||
| 197 | |||
| 198 | # Etape 1 - Chargement de tous les fichiers autorités (type c) | ||
| 199 | for my $file ( @{ $self->spool->files('waiting', 'c') } ) { | ||
| 200 | my $loader = Koha::Contrib::Sudoc::Loader::Authorities->new( | ||
| 201 | sudoc => $self, file => $file, doit => $doit ); | ||
| 202 | $loader->run(); | ||
| 203 | sleep($timeout); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Etape 2 - Chargement de tous les fichiers biblio (type a et b) | ||
| 207 | for my $file ( @{ $self->spool->files('waiting', '[a|b]') } ) { | ||
| 208 | my $loader = Koha::Contrib::Sudoc::Loader::Biblios->new( | ||
| 209 | sudoc => $self, file => $file, doit => $doit ); | ||
| 210 | $loader->run(); | ||
| 211 | sleep($timeout); | ||
| 212 | } | ||
| 213 | |||
| 214 | # Envoi des log s'il y en a | ||
| 215 | my $logfile = $self->root . "/var/log/email.txt"; | ||
| 216 | return unless -e $logfile; | ||
| 217 | my $head = Mail::Message::Head->new; | ||
| 218 | $head->add( From => $loading->{log}->{from} ); | ||
| 219 | $head->add( To => $loading->{log}->{to} ); | ||
| 220 | $head->add( Subject => 'Chargeur Sudoc Koha Tamil' ); | ||
| 221 | my $body = Mail::Message::Body::Lines->new( data => path($logfile)->slurp ); | ||
| 222 | my $message = Mail::Message->new(head => $head, body => $body); | ||
| 223 | $message->send; | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | 1; | ||
| 228 | |||
| 229 | __END__ | ||
| 230 | |||
| 231 | =pod | ||
| 232 | |||
| 233 | =encoding UTF-8 | ||
| 234 | |||
| 235 | =head1 NAME | ||
| 236 | |||
| 237 | Koha::Contrib::Sudoc - Chargeur Koha par Tamil | ||
| 238 | |||
| 239 | =head1 VERSION | ||
| 240 | |||
| 241 | version 2.20 | ||
| 242 | |||
| 243 | =head1 DESCRIPTION | ||
| 244 | |||
| 245 | Koha::Contrib::Sudoc est le Chargeur Sudoc pour Koha développé par Tamil. Le | ||
| 246 | fonctionnement de cet outil est décrit ici : | ||
| 247 | L<http://www.tamil.fr/sudoc/sudoc.html>. | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | Frédéric Demians <f.demians@tamil.fr> | ||
| 252 | |||
| 253 | =head1 COPYRIGHT AND LICENSE | ||
| 254 | |||
| 255 | This software is Copyright (c) 2016 by Fréderic Demians. | ||
| 256 | |||
| 257 | This is free software, licensed under: | ||
| 258 | |||
| 259 | The GNU General Public License, Version 3, June 2007 | ||
| 260 | |||
| 261 | =cut |