From 1b471778fbe60aabca7e1c8bf8040032f4e8d4c2 Mon Sep 17 00:00:00 2001 From: Jake Deery Date: Tue, 18 Feb 2025 14:08:09 +0000 Subject: [PATCH] Bug 38115: Add destination_server_id to export_records This patch adds two new flags to the export_records.pl script. * --destination_server_id=ID This flag will, when passed, upload the file generated by this script to the destination server, as configured in the FTP/SFTP Servers settings. * --delete_local_after_run This flag will, when passed, cause the script to unlink (delete) the generated marc or CSV file found on the local disk. This is useful for folk who want to upload the file, but not necessarily keep a local copy, filling up the server's storage medium TO TEST: a) APPLY PATCH, reset_all b) Create valid FTP/SFTP Server in Koha settings, ensuring that the server you create is valid and passes testing c) Run ./misc/export_records.pl with no agruments 1) Notice how koha.mrc file is generated d) Run ./misc/export_records.pl --destination_server_id=1 1) Notice how koha.mrc file is generated 2) On your FTP/SFTP Server, notice how koha.mrc is now present in the upload directory e) Run ./misc/export_records.pl --delete_local_after_run 1) Notice how koha.mrc file is NOT generated locally f) Run ./misc/export_records.pl --destination_server_id=1 --delete_local_after_run 1) Notice how koha.mrc file is NOT generated locally 2) On your FTP/SFTP Server, notice how koha.mrc is still present in the upload directory AND the file age is fresh --- misc/export_records.pl | 52 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/misc/export_records.pl b/misc/export_records.pl index 683cb1971be..9bea531172a 100755 --- a/misc/export_records.pl +++ b/misc/export_records.pl @@ -34,6 +34,7 @@ use Koha::CsvProfiles; use Koha::Exporter::Record; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Reports; +use Koha::File::Transports; my ( $output_format, @@ -62,7 +63,9 @@ my ( $report, $sql, $params_needed, - $help + $destination_server_id, + $delete_local_after_run, + $help, ); GetOptions( @@ -89,7 +92,9 @@ GetOptions( 'embed_see_from_headings' => \$embed_see_from_headings, 'report_id=s' => \$report_id, 'report_param=s' => \@report_params, - 'h|help|?' => \$help + 'destination_server_id=s' => \$destination_server_id, + 'delete_local_after_run' => \$delete_local_after_run, + 'h|help|?' => \$help, ) || pod2usage(1); if ($help) { @@ -119,12 +124,20 @@ if ( $deleted_barcodes and $record_type ne 'bibs' ) { pod2usage(q|--deleted_barcodes can only be used with biblios|); } +my $sftp_server; +if ($destination_server_id) { + $sftp_server = Koha::File::Transports->find($destination_server_id); + + pod2usage( sprintf( "No FTP/SFTP Server (%s) found", $destination_server_id ) ) + unless $sftp_server; +} + if ($report_id) { # Check report exists $report = Koha::Reports->find($report_id); unless ($report) { - pod2usage( sprintf( "No saved report (%s) found", $report_id ) ); + pod2usage( sprintf( "No FTP/SFTP Server (%s) found", $report_id ) ); } $sql = $report->savedsql; @@ -340,6 +353,26 @@ if ($deleted_barcodes) { } ); } + +if ($sftp_server) { + $sftp_server->connect + or die pod2usage( sprintf( "Unable to connect server (%s)", $destination_server_id ) ); + + my $upload_dir = $sftp_server->upload_directory; + if ($upload_dir) { + $sftp_server->change_directory( $upload_dir ) + or die pod2usage( sprintf( "Unable to change directory on server (%s) to path (%s)", $destination_server_id, $upload_dir ) ); + } + + $sftp_server->upload_file ( $filename, $filename ) + or die pod2usage( sprintf( "Unable to upload file (%s) to server (%s)", $filename, $destination_server_id ) ); +} + +if ($delete_local_after_run) { + unlink $filename + or die pod2usage( sprintf( "Unable to delete local file (%s)", $filename ) ); +} + exit; =head1 NAME @@ -483,6 +516,19 @@ Print a brief help message. Report params are not combined as on the staff side, so you may need to repeat params. +=item B<--destination_server_id> + +--destination_server_id=ID Provide this option, along with the destination server ID, to + upload the resultant mrc file to the selected FTP/SFTP Server. + You can create FTP/SFTP Servers via the Koha Staff client, under + Koha Administration. + +=item B<--delete_local_after_run> + +--delete_local_after_run Deletes the local file at the end of the script run. Can be + useful if, for example, you are uploading the file to an + FTP/SFTP server. + =back =head1 AUTHOR -- 2.43.0