| 1 | package Koha::Contrib::Sudoc::Spool; | ||
| 2 | # ABSTRACT: Spool des fichiers de notices | ||
| 3 | $Koha::Contrib::Sudoc::Spool::VERSION = '2.49'; | ||
| 4 | use Moose; | ||
| 5 | use Modern::Perl; | ||
| 6 | use File::Copy; | ||
| 7 | use DateTime; | ||
| 8 | use Format::Human::Bytes; | ||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | my $dirstatus = [ | ||
| 13 | [ | ||
| 14 | "Fichiers contenant les autorités qui ont été chargées :", | ||
| 15 | 'done', | ||
| 16 | 'c', | ||
| 17 | ], | ||
| 18 | [ | ||
| 19 | "Fichiers contenant les notices biblio qui ont été chargées :", | ||
| 20 | 'done', | ||
| 21 | '[a|b]', | ||
| 22 | ], | ||
| 23 | [ | ||
| 24 | "Fichiers contenant les autorités en attente de chargement :", | ||
| 25 | 'waiting', | ||
| 26 | 'c', | ||
| 27 | ], | ||
| 28 | [ | ||
| 29 | "Fichiers contenant les notices biblio en attente de chargement :", | ||
| 30 | 'waiting', | ||
| 31 | '[a|b]', | ||
| 32 | ], | ||
| 33 | [ | ||
| 34 | "Fichiers des autorités en cours de transfert :", | ||
| 35 | 'staged', | ||
| 36 | 'c', | ||
| 37 | ], | ||
| 38 | [ | ||
| 39 | "Fichiers de notices biblio en cours de transfert :", | ||
| 40 | 'staged', | ||
| 41 | '[a|b]', | ||
| 42 | ], | ||
| 43 | ]; | ||
| 44 | |||
| 45 | |||
| 46 | # Moulinette SUDOC | ||
| 47 | has sudoc => ( | ||
| 48 | is => 'rw', | ||
| 49 | isa => 'Koha::Contrib::Sudoc', | ||
| 50 | required => 1, | ||
| 51 | ); | ||
| 52 | |||
| 53 | has root => ( | ||
| 54 | is => 'rw', | ||
| 55 | isa => 'Str', | ||
| 56 | lazy => 1, | ||
| 57 | builder => '_build_root', | ||
| 58 | ); | ||
| 59 | |||
| 60 | sub _build_root { shift->sudoc->root . '/var/spool'; } | ||
| 61 | |||
| 62 | |||
| 63 | sub _sortable_name { | ||
| 64 | my $name = shift; | ||
| 65 | if ( $name =~ /^TR(\d*)R(\d*)([A-C])(.*)$/ ) { | ||
| 66 | my $letter = $3 eq 'A' ? 'B' : | ||
| 67 | $3 eq 'B' ? 'A' : $3; | ||
| 68 | $name = sprintf("TR%05dR%05d", $1, $2) . $letter . $4; | ||
| 69 | } | ||
| 70 | elsif ( $name =~ /^(.*)R(\d*)([A-C])(.*)\.RAW$/ ) { | ||
| 71 | my $letter = $3 eq 'A' ? 'B' : | ||
| 72 | $3 eq 'B' ? 'A' : $3; | ||
| 73 | $name = "$1R$2$letter$4.RAW"; | ||
| 74 | } | ||
| 75 | return $name; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub files { | ||
| 80 | my ($self, $where, $type) = @_; | ||
| 81 | my $dir = $self->root . "/$where"; | ||
| 82 | opendir(my $hdir, $dir) || die "Impossible d'ouvrir $dir: $!"; | ||
| 83 | [ sort { _sortable_name($a) cmp _sortable_name($b) } | ||
| 84 | grep { /$type\d{3}.raw$/i } readdir($hdir) ]; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub first_batch_files { | ||
| 89 | my ($self, $where, $type) = @_; | ||
| 90 | |||
| 91 | my $files = $self->files($where, $type); | ||
| 92 | return $files unless @$files; | ||
| 93 | |||
| 94 | my ($prefix_first) = $files->[0] =~ /^(.*)\d{3}.RAW/; | ||
| 95 | my @first_files; | ||
| 96 | for my $file (@$files) { | ||
| 97 | my ($prefix) = $file =~ /^(.*)\d{3}.RAW/; | ||
| 98 | last if $prefix ne $prefix_first; | ||
| 99 | push @first_files, $file; | ||
| 100 | } | ||
| 101 | return \@first_files; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub file_path { | ||
| 106 | my ($self, $name) = @_; | ||
| 107 | |||
| 108 | for my $where (qw /waiting done/) { | ||
| 109 | my $path = $self->root . "/$where/$name"; | ||
| 110 | return $path if -f $path; | ||
| 111 | } | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub move_done { | ||
| 117 | my ($self, $name) = @_; | ||
| 118 | my $path = $self->root . "/waiting/$name"; | ||
| 119 | return unless -f $path; | ||
| 120 | my $target = $self->root . "/done/$name"; | ||
| 121 | move($path, $target); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | sub staged_to_waiting { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | my $staged = $self->root . "/staged"; | ||
| 129 | my $target = $self->root . "/waiting"; | ||
| 130 | opendir(my $hdir, $staged) || die "Impossible d'ouvrir $staged: $!"; | ||
| 131 | my @files = sort grep { not /^\./ } readdir($hdir); | ||
| 132 | for my $file (@files) { | ||
| 133 | move("$staged/$file", $target); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | sub list { | ||
| 139 | my $self = shift; | ||
| 140 | for ( @$dirstatus ) { | ||
| 141 | my ($msg, $where, $type) = @$_; | ||
| 142 | my $files = $self->sudoc->spool->files($where, $type); | ||
| 143 | next unless @$files; | ||
| 144 | say $msg; | ||
| 145 | chdir $self->sudoc->root . "/var/spool/$where"; | ||
| 146 | my $count = 0; | ||
| 147 | for my $file (@$files) { | ||
| 148 | $count++; | ||
| 149 | my (undef, undef, undef, undef, undef, undef, undef, $size, $atime, | ||
| 150 | $mtime, $ctime) = stat($file); | ||
| 151 | my $dt = DateTime->from_epoch( epoch => $mtime ); | ||
| 152 | say sprintf (" %3d. ", $count), $file, | ||
| 153 | " - " . $dt->dmy('.') . ', ' . Format::Human::Bytes::base10($size); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | sub vide { | ||
| 160 | my $self = shift; | ||
| 161 | for ( @$dirstatus ) { | ||
| 162 | my ($msg, $where, $type) = @$_; | ||
| 163 | my @files = @{ $self->sudoc->spool->files($where, $type) }; | ||
| 164 | next unless @files; | ||
| 165 | chdir $self->sudoc->root . "/var/spool/$where"; | ||
| 166 | @files = grep { | ||
| 167 | my (undef, undef, undef, undef, undef, undef, undef, $size, , ,) = | ||
| 168 | stat($_); | ||
| 169 | $size == 0; | ||
| 170 | } @files; | ||
| 171 | next unless @files; | ||
| 172 | for my $file (@files) { | ||
| 173 | unlink $file; | ||
| 174 | say "$file: supprimé"; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | sub command { | ||
| 181 | my $self = shift; | ||
| 182 | if ( @_ ) { | ||
| 183 | if ($_[0] =~ /vide/i ) { | ||
| 184 | $self->vide(); | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | for my $file (@_) { | ||
| 188 | my $path = $self->file_path($file); | ||
| 189 | unless ( $path ) { | ||
| 190 | say "Le fichier '$file' n'existe pas"; | ||
| 191 | next; | ||
| 192 | } | ||
| 193 | say "Fichier $path"; | ||
| 194 | system( "yaz-marcdump $path | less" ); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | else { | ||
| 198 | $self->list(); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | __END__ | ||
| 206 | |||
| 207 | =pod | ||
| 208 | |||
| 209 | =encoding UTF-8 | ||
| 210 | |||
| 211 | =head1 NAME | ||
| 212 | |||
| 213 | Koha::Contrib::Sudoc::Spool - Spool des fichiers de notices | ||
| 214 | |||
| 215 | =head1 VERSION | ||
| 216 | |||
| 217 | version 2.49 | ||
| 218 | |||
| 219 | =head1 DESCRIPTION | ||
| 220 | |||
| 221 | Le spool se trouve dans le sous-répertore C<var/spool> du répertoire racine | ||
| 222 | pointé par la variable d'environnement C<SUDOC>. Les fichiers arrivent de | ||
| 223 | l'ABES dans le répertoire C<staged>. Puis quand ils sont entièrement | ||
| 224 | téléchargés, ils sont déplacés en C<waiting>. De là, ils sont chargés un à un | ||
| 225 | dans Koha. Après chargement, ils sont déplacés en C<done>. | ||
| 226 | |||
| 227 | =head1 METHODS | ||
| 228 | |||
| 229 | =head2 files | ||
| 230 | |||
| 231 | Retourne les fichiers d'une categorie (staged/waiting/done) et d'un | ||
| 232 | type donnée. Par ex: | ||
| 233 | |||
| 234 | $files = $spool->file('waiting', 'c'); | ||
| 235 | $files = $spool->file('done', '[a|b]'); | ||
| 236 | |||
| 237 | =head2 first_batch_files($where, $type) | ||
| 238 | |||
| 239 | Retourne dans un tableau le premier lot de fichiers d'une catégorie et d'un | ||
| 240 | type donnée. For example: | ||
| 241 | |||
| 242 | my @files = $spool->first_batch_files('waiting', '[a|b]'); | ||
| 243 | |||
| 244 | =head2 file_path($name) | ||
| 245 | |||
| 246 | Retourne la pathname d'un fichier qu'on retrouve, dans l'ordre, soit | ||
| 247 | dans le spool C<waiting> soit dans le spool C<done>. Si le fichier | ||
| 248 | n'existe pas, retourne undef. | ||
| 249 | |||
| 250 | =head2 move_done($name) | ||
| 251 | |||
| 252 | Déplace un fichier dans le spool 'done' | ||
| 253 | |||
| 254 | =head2 staged_to_waiting | ||
| 255 | |||
| 256 | Déplace tous les fichiers de l'ILN courant de staged dans waiting | ||
| 257 | |||
| 258 | =head2 list | ||
| 259 | |||
| 260 | Liste le contenu des répertoires du spool | ||
| 261 | |||
| 262 | =head2 vide | ||
| 263 | |||
| 264 | Supprime du spool tous les fichiers vides. | ||
| 265 | |||
| 266 | =head2 command | ||
| 267 | |||
| 268 | Sans paramètre, liste le contenu des répertoires du spool, en appelant | ||
| 269 | L<list>. Les paramètres sont des noms de fichiers du spool. Leur contenu est | ||
| 270 | affiché. | ||
| 271 | |||
| 272 | =head1 AUTHOR | ||
| 273 | |||
| 274 | Frédéric Demians <f.demians@tamil.fr> | ||
| 275 | |||
| 276 | =head1 COPYRIGHT AND LICENSE | ||
| 277 | |||
| 278 | This software is Copyright (c) 2026 by Fréderic Demians. | ||
| 279 | |||
| 280 | This is free software, licensed under: | ||
| 281 | |||
| 282 | The GNU General Public License, Version 3, June 2007 | ||
| 283 | |||
| 284 | =cut |