| 1 | package Koha::Contrib::Sudoc; | ||
| 2 | # ABSTRACT: Chargeur Koha par Tamil | ||
| 3 | $Koha::Contrib::Sudoc::VERSION = '2.50'; | ||
| 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 | use Net::FTP; | ||
| 17 | use Net::SFTP::Foreign; | ||
| 18 | |||
| 19 | |||
| 20 | # L'instance de Koha de l'ILN courant | ||
| 21 | has koha => ( | ||
| 22 | is => 'rw', | ||
| 23 | isa => 'Koha::Contrib::Sudoc::Koha', | ||
| 24 | default => sub { Koha::Contrib::Sudoc::Koha->new() } | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | # La racine de l'environnement d'exécution du chargeur | ||
| 29 | has root => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'Str', | ||
| 32 | default => sub { | ||
| 33 | my $self = shift; | ||
| 34 | my $root = $ENV{SUDOC}; | ||
| 35 | unless ($root) { | ||
| 36 | say "Il manque la variable d'environnement SUDOC."; | ||
| 37 | exit; | ||
| 38 | } | ||
| 39 | unless ( -d $root ) { | ||
| 40 | say "variable d'environnement SUDOC=$root"; | ||
| 41 | say "Ce répertoire n'existe pas. Il faut le créer, puis initialiser le chargeur si nécessaire."; | ||
| 42 | exit; | ||
| 43 | } | ||
| 44 | unshift @INC, "$root/lib"; | ||
| 45 | $self->root( $root ); | ||
| 46 | }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | # Le contenu du fichier de config | ||
| 50 | has c => ( is => 'rw', ); | ||
| 51 | |||
| 52 | # Le Spool | ||
| 53 | has spool => ( is => 'rw', isa => 'Koha::Contrib::Sudoc::Spool' ); | ||
| 54 | |||
| 55 | |||
| 56 | sub BUILD { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | # Lecture du fichier de config et création du hash branchcode => RCR par ILN | ||
| 60 | my $file = $self->root . "/etc/sudoc.conf"; | ||
| 61 | return unless -e $file; | ||
| 62 | my $c = LoadFile($file); | ||
| 63 | my %branchcode; | ||
| 64 | while ( my ($rcr, $branch) = each %{$c->{rcr}} ) { | ||
| 65 | $branchcode{$branch} = $rcr; | ||
| 66 | } | ||
| 67 | $c->{branch} = \%branchcode; | ||
| 68 | $self->c($c); | ||
| 69 | |||
| 70 | # Contrôle de quelques paramètres | ||
| 71 | my $loading = $c->{loading}; | ||
| 72 | unless ($loading) { | ||
| 73 | say "Erreur sudoc.conf: Il manque la section 'loading'"; | ||
| 74 | exit; | ||
| 75 | } | ||
| 76 | my $log = $loading->{log}; | ||
| 77 | unless ($log) { | ||
| 78 | say "Erreur sudoc.conf: Il manque la section 'loading::log'"; | ||
| 79 | exit; | ||
| 80 | } | ||
| 81 | if ( $log->{level} !~ /debug|notice/ ) { | ||
| 82 | say "Erreur sudoc.conf: 'loading::log::level n'est pas égal à debug ou notice."; | ||
| 83 | exit; | ||
| 84 | } | ||
| 85 | my $timeout = $loading->{timeout}; | ||
| 86 | unless ($timeout) { | ||
| 87 | say "Erreur sudoc.conf: Il manque la section 'loading::timeout'"; | ||
| 88 | exit; | ||
| 89 | } | ||
| 90 | for ( qw/ transfer indexing / ) { | ||
| 91 | my $value = $timeout->{$_}; | ||
| 92 | unless ( $value && $value =~ /^[0-9]+$/ && $value >= 1 && $value <= 60) { | ||
| 93 | say "Erreur sudoc.conf: Le paramètre 'loading::timeout::$_' doit être compris entre 1 et 60."; | ||
| 94 | exit; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | # L'object Sudoc::Spool | ||
| 99 | $self->spool( Koha::Contrib::Sudoc::Spool->new( sudoc => $self ) ); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | # Déplace le PPN (001) d'une notice SUDOC dans une zone Koha | ||
| 104 | sub ppn_move { | ||
| 105 | my ($self, $record, $tag) = @_; | ||
| 106 | |||
| 107 | return unless $tag && length($tag >= 3); | ||
| 108 | |||
| 109 | my $letter; | ||
| 110 | if ( $tag =~ /(\d{3})([0-9a-z])/ ) { $tag = $1, $letter = $2; } | ||
| 111 | elsif ( $tag =~ /(\d{3})/ ) { $tag = $1 }; | ||
| 112 | |||
| 113 | return if $tag eq '001'; | ||
| 114 | |||
| 115 | my $ppn = $record->field('001')->value; | ||
| 116 | $record->append( | ||
| 117 | $letter | ||
| 118 | ? MARC::Moose::Field::Std->new( tag => $tag, subf => [ [ $letter => $ppn ] ] ) | ||
| 119 | : MARC::Moose::Field::Control->new( tag => $tag, value => $ppn ) | ||
| 120 | ); | ||
| 121 | |||
| 122 | $record->fields( [ grep { $_->tag ne '001' } @{$record->fields} ] ); | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | # Crée les sous-répertoires d'un ILN, s'ils n'existent pas déjà | ||
| 127 | sub init { | ||
| 128 | my $self = shift; | ||
| 129 | |||
| 130 | say "La variable d'environnement SUDOC définit le répertoire racine : ", $self->root; | ||
| 131 | say "Initialisation du répertoire des données du Chargeur SUDOC d'un ILN."; | ||
| 132 | chdir($self->root); | ||
| 133 | |||
| 134 | say "Création des répertoire 'etc', 'lib' et 'var'."; | ||
| 135 | mkdir $_ for qw/ etc lib var /; | ||
| 136 | |||
| 137 | if ( -f 'etc/sudoc.conf') { | ||
| 138 | say "Le fichier 'etc/sudoc.conf' existe déjà. On ne le remplace pas par le fichier modèle."; | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | say "Création d'un fichier modèle 'etc/sudoc.conf'."; | ||
| 142 | my $conf_path = dist_file('Koha-Contrib-Sudoc', 'etc/sudoc.conf'); | ||
| 143 | copy($conf_path, "etc/sudoc.conf"); | ||
| 144 | } | ||
| 145 | |||
| 146 | say "Création des répertoires 'var/log' et 'var/spool'."; | ||
| 147 | chdir('var'); | ||
| 148 | mkdir $_ for qw/ log spool /; | ||
| 149 | |||
| 150 | chdir('spool'); | ||
| 151 | mkdir $_ for qw/ staged waiting done /; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | sub reset_email_log { | ||
| 156 | my $root = shift->root; | ||
| 157 | unlink "$root/var/log/email.txt"; | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | # Envoi courriel GTD | ||
| 162 | sub send_gtd_email { | ||
| 163 | my ($self, $jobid, $dir) = @_; | ||
| 164 | |||
| 165 | my $c = $self->c->{trans}; | ||
| 166 | my $ftp_host = $c->{ftp_host}; | ||
| 167 | |||
| 168 | $dir ||= 'staged'; | ||
| 169 | $dir = '' if $ftp_host =~ /abes/; | ||
| 170 | |||
| 171 | # La date | ||
| 172 | my $year = DateTime->now->year; | ||
| 173 | |||
| 174 | my $head = Mail::Message::Head->new; | ||
| 175 | $head->add( From => $c->{email}->{koha} ); | ||
| 176 | $head->add( To => $c->{email}->{abes} ); | ||
| 177 | $head->add( Subject => 'GET TITLE DATA' ); | ||
| 178 | my @data = ( | ||
| 179 | "GTD_ILN = " . $self->c->{iln}, | ||
| 180 | "GTD_YEAR = $year", | ||
| 181 | "GTD_FILE_TO = $ftp_host", | ||
| 182 | "GTD_REMOTE_DIR = $dir", | ||
| 183 | ); | ||
| 184 | push @data, "GTD_ORDER = TR$jobid*" if $jobid; | ||
| 185 | my $body = Mail::Message::Body::Lines->new(data => join("\n", @data)); | ||
| 186 | my $message = Mail::Message->new(head => $head, body => $body); | ||
| 187 | $message->send; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | # Méthode GET | ||
| 192 | sub get { | ||
| 193 | my $self = shift; | ||
| 194 | |||
| 195 | my $c = $self->c->{trans}; | ||
| 196 | my $ftp_host = $c->{ftp_host}; | ||
| 197 | |||
| 198 | # Si le serveur FTP n'est pas celui de l'ABES, on n'est pas en mode GET | ||
| 199 | return if $ftp_host !~ /abes/; | ||
| 200 | |||
| 201 | chdir($self->root . "/var/spool/waiting"); | ||
| 202 | |||
| 203 | my $is_sftp = $c->{protocol} || ''; | ||
| 204 | $is_sftp = $is_sftp =~ /sftp/; | ||
| 205 | if ($is_sftp) { | ||
| 206 | my %arg = ( | ||
| 207 | user => $c->{login}, | ||
| 208 | password => $c->{password}, | ||
| 209 | ); | ||
| 210 | my $ftp = Net::SFTP::Foreign->new($ftp_host, user => $c->{login}, password => $c->{password}); | ||
| 211 | die "Login au serveur SFTP impossible : " . $ftp->error if $ftp->error; | ||
| 212 | my $files = $ftp->ls('.', names_only => 1); | ||
| 213 | for my $file (@$files) { | ||
| 214 | next if $file =~ /^\./; | ||
| 215 | $ftp->get($file); | ||
| 216 | $ftp->remove($file); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | else { | ||
| 220 | my $ftp = Net::FTP->new($ftp_host, Debug => 0); | ||
| 221 | $ftp->login($c->{login}, $c->{password}) | ||
| 222 | or die "Login au serveur FTP impossible : ", $ftp->message; | ||
| 223 | $ftp->binary(); | ||
| 224 | my @files = $ftp->ls(); | ||
| 225 | for my $file (@files) { | ||
| 226 | $ftp->get($file); | ||
| 227 | $ftp->delete($file); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | # Chargement de tous les fichiers qui se trouvent dans 'waiting' | ||
| 234 | sub load_waiting { | ||
| 235 | my $self = shift; | ||
| 236 | |||
| 237 | my $c = $self->c; | ||
| 238 | my $loading = $c->{loading}; | ||
| 239 | |||
| 240 | # Temps nécessaire entre deux chargements de fichier pour indexation | ||
| 241 | my $timeout = $loading->{timeout}->{indexing} * 60; | ||
| 242 | my $doit = $loading->{doit}; | ||
| 243 | $self->reset_email_log(); | ||
| 244 | |||
| 245 | # Etape 1 - Chargement de tous les fichiers autorités (type c) | ||
| 246 | for my $file ( @{ $self->spool->files('waiting', 'c') } ) { | ||
| 247 | my $loader = Koha::Contrib::Sudoc::Loader::Authorities->new( | ||
| 248 | sudoc => $self, file => $file, doit => $doit ); | ||
| 249 | $loader->run(); | ||
| 250 | sleep($timeout); | ||
| 251 | } | ||
| 252 | |||
| 253 | # Etape 2 - Chargement de tous les fichiers biblio (type a et b) | ||
| 254 | for my $file ( @{ $self->spool->files('waiting', '[a|b]') } ) { | ||
| 255 | my $loader = Koha::Contrib::Sudoc::Loader::Biblios->new( | ||
| 256 | sudoc => $self, file => $file, doit => $doit ); | ||
| 257 | $loader->run(); | ||
| 258 | sleep($timeout); | ||
| 259 | } | ||
| 260 | |||
| 261 | # Envoi des log s'il y en a | ||
| 262 | my $logfile = $self->root . "/var/log/email.txt"; | ||
| 263 | return unless -e $logfile; | ||
| 264 | my $head = Mail::Message::Head->new; | ||
| 265 | $head->add( From => $loading->{log}->{from} ); | ||
| 266 | $head->add( To => $loading->{log}->{to} ); | ||
| 267 | $head->add( Subject => 'Chargeur Sudoc Koha Tamil' ); | ||
| 268 | my $body = Mail::Message::Body::Lines->new( data => path($logfile)->slurp ); | ||
| 269 | my $message = Mail::Message->new(head => $head, body => $body); | ||
| 270 | $message->send; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub record_as_text { | ||
| 275 | my ($self, $record) = @_; | ||
| 276 | |||
| 277 | my $text = $record->as('Text'); | ||
| 278 | my @lines; | ||
| 279 | for my $line (split /\n/, $text) { | ||
| 280 | if (length($line) > 100) { | ||
| 281 | my @words = split / /, $line; | ||
| 282 | $line = ''; | ||
| 283 | while (@words) { | ||
| 284 | my $word = shift @words; | ||
| 285 | if (length($word) + length($line) > 100) { | ||
| 286 | push @lines, $line; | ||
| 287 | $line = " $word"; | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | $line = $line ? "$line $word" : $word; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | push @lines, $line; | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | push @lines, $line; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | return join("\n", @lines) . "\n\n"; | ||
| 300 | } | ||
| 301 | |||
| 302 | 1; | ||
| 303 | |||
| 304 | __END__ |