|
Lines 23-29
marc_ordering_process.pl - cron script to retrieve MARC files and create order l
Link Here
|
| 23 |
|
23 |
|
| 24 |
=head1 SYNOPSIS |
24 |
=head1 SYNOPSIS |
| 25 |
|
25 |
|
| 26 |
./marc_ordering_process.pl [-c|--confirm] [-v|--verbose] |
26 |
./marc_ordering_process.pl [-c|--confirm] [-v|--verbose] [--dr|--delete-remote] [--rr|--rename-remote] ext] |
| 27 |
|
27 |
|
| 28 |
or, in crontab: |
28 |
or, in crontab: |
| 29 |
# Once every day |
29 |
# Once every day |
|
Lines 50-55
Without this parameter no changes will be made
Link Here
|
| 50 |
|
50 |
|
| 51 |
Delete the file once it has been processed |
51 |
Delete the file once it has been processed |
| 52 |
|
52 |
|
|
|
53 |
=item B<--dr|--delete-remote> |
| 54 |
|
| 55 |
Delete the remote file once it has been downloaded |
| 56 |
|
| 57 |
=item B<-rr|--rename-remove ext> |
| 58 |
|
| 59 |
Rename the remote file once it has been downloaded, adding the given file extension |
| 60 |
|
| 53 |
=back |
61 |
=back |
| 54 |
|
62 |
|
| 55 |
=cut |
63 |
=cut |
|
Lines 62-79
use File::Copy qw( copy move );
Link Here
|
| 62 |
use Koha::Script -cron; |
70 |
use Koha::Script -cron; |
| 63 |
use Koha::MarcOrder; |
71 |
use Koha::MarcOrder; |
| 64 |
use Koha::MarcOrderAccounts; |
72 |
use Koha::MarcOrderAccounts; |
|
|
73 |
use Koha::File::Transports; |
| 65 |
|
74 |
|
| 66 |
use C4::Log qw( cronlogaction ); |
75 |
use C4::Log qw( cronlogaction ); |
| 67 |
|
76 |
|
| 68 |
my $command_line_options = join( " ", @ARGV ); |
77 |
my $command_line_options = join( " ", @ARGV ); |
| 69 |
cronlogaction( { info => $command_line_options } ); |
78 |
cronlogaction( { info => $command_line_options } ); |
| 70 |
|
79 |
|
| 71 |
my ( $help, $verbose, $confirm, $delete ); |
80 |
my ( $help, $verbose, $confirm, $delete, $rename_ext, $delete_remote ); |
| 72 |
GetOptions( |
81 |
GetOptions( |
| 73 |
'h|help' => \$help, |
82 |
'h|help' => \$help, |
| 74 |
'v|verbose' => \$verbose, |
83 |
'v|verbose' => \$verbose, |
| 75 |
'c|confirm' => \$confirm, |
84 |
'c|confirm' => \$confirm, |
| 76 |
'd|delete' => \$delete, |
85 |
'd|delete' => \$delete, |
|
|
86 |
'dr|delete-remote' => \$delete_remote, |
| 87 |
'rr|rename-remote=s' => \$rename_ext, |
| 77 |
) || pod2usage(1); |
88 |
) || pod2usage(1); |
| 78 |
|
89 |
|
| 79 |
pod2usage(0) if $help; |
90 |
pod2usage(0) if $help; |
|
Lines 91-96
if ( scalar(@accounts) == 0 ) {
Link Here
|
| 91 |
print "No accounts found - you must create a MARC order account for this cronjob to run\n" if $verbose; |
102 |
print "No accounts found - you must create a MARC order account for this cronjob to run\n" if $verbose; |
| 92 |
} |
103 |
} |
| 93 |
|
104 |
|
|
|
105 |
my $valid_file_extensions = qr/\.(mrc|marcxml|mrk)$/i; |
| 106 |
|
| 94 |
foreach my $acct (@accounts) { |
107 |
foreach my $acct (@accounts) { |
| 95 |
if ($verbose) { |
108 |
if ($verbose) { |
| 96 |
say sprintf "Starting MARC ordering process for %s", $acct->vendor->name; |
109 |
say sprintf "Starting MARC ordering process for %s", $acct->vendor->name; |
|
Lines 98-105
foreach my $acct (@accounts) {
Link Here
|
| 98 |
} |
111 |
} |
| 99 |
|
112 |
|
| 100 |
my $working_dir = $acct->download_directory; |
113 |
my $working_dir = $acct->download_directory; |
|
|
114 |
|
| 115 |
my $file_transport = $acct->file_transport_id ? Koha::File::Transports->find( $acct->file_transport_id ) : undef; |
| 116 |
if ($file_transport) { |
| 117 |
if ( $file_transport->connect() ) { |
| 118 |
my $download_dir = $file_transport->download_directory; |
| 119 |
|
| 120 |
my $success = $download_dir ? $file_transport->change_directory($download_dir) : 1; |
| 121 |
if ( $download_dir && !$success ) { |
| 122 |
say "Failed to change to download directory: $download_dir"; |
| 123 |
} else { |
| 124 |
|
| 125 |
# Get file list |
| 126 |
my $file_list = $file_transport->list_files(); |
| 127 |
if ($file_list) { |
| 128 |
|
| 129 |
# Process files matching our criteria |
| 130 |
foreach my $file ( @{$file_list} ) { |
| 131 |
my $filename = $file->{filename}; |
| 132 |
|
| 133 |
if ( $filename =~ $valid_file_extensions ) { |
| 134 |
|
| 135 |
my $local_file = "$working_dir/$filename"; |
| 136 |
|
| 137 |
# Download the file |
| 138 |
if ( $file_transport->download_file( $filename, $local_file ) ) { |
| 139 |
if ($rename_ext) { |
| 140 |
$file_transport->rename_file( $filename, "$filename.$rename_ext" ); |
| 141 |
} elsif ($delete_remote) { |
| 142 |
$file_transport->delete_file($filename); |
| 143 |
} |
| 144 |
} else { |
| 145 |
say "Failed to download file: $filename"; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
} else { |
| 151 |
say "Failed to get file list from transport"; |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
} else { |
| 156 |
say "Failed to connect to file transport: " . $file_transport->id; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 101 |
opendir my $dir, $working_dir or die "Can't open filepath"; |
160 |
opendir my $dir, $working_dir or die "Can't open filepath"; |
| 102 |
my @files = grep { /\.(mrc|marcxml|mrk)/i } readdir $dir; |
161 |
my @files = grep { /$valid_file_extensions/ } readdir $dir; |
| 103 |
closedir $dir; |
162 |
closedir $dir; |
| 104 |
print "No new files found\n" if scalar(@files) == 0; |
163 |
print "No new files found\n" if scalar(@files) == 0; |
| 105 |
|
164 |
|
| 106 |
- |
|
|