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

Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Jake Deery 2025-02-18 14:08:09 +00:00 committed by Lucas Gass
parent b6233aeb84
commit 1ee76c6a9f
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
1 changed files with 49 additions and 3 deletions

View File

@ -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