| 1 | package Koha::Contrib::Sudoc::Spool; | ||
| 2 | # ABSTRACT: Spool des fichiers de notices | ||
| 3 | $Koha::Contrib::Sudoc::Spool::VERSION = '2.50'; | ||
| 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__ |