Bug 28740: Add option for cleaning up ILL requests

This adds two new parameters:
--illrequests-status
--illrequests-days (optional, defaults to 30 days)

These can be repeated to include specific statuses for cleanup. At least one status must be specified.

To test:
0 - Apply patch
1 - Create several ILL requests with different statuses (e.g., CANCELLED, REJECTED, COMPLETED, PENDING)
2 - Set some requests to be older than 30 days (update their updated timestamp)
3 - Run cleanup_database.pl with --illrequests-status CANCELLED
4 - CANCELLED requests older than 30 days are removed, others remain
5 - Run cleanup_database.pl with --illrequests-status CANCELLED --illrequests-status REJECTED
6 - Both CANCELLED and REJECTED requests older than 30 days are removed, other statuses remain
7 - Run cleanup_database.pl with --illrequests-days 5 --illrequests-status PENDING
8 - PENDING requests older than 5 days are removed
9 - Verify dry-run mode (no --confirm) correctly counts records that would be deleted without deleting them
10 - Run cleanup_database.pl with --illrequests-days without any --illrequests-status
11 - Error message displayed requiring at least one status to be specified

Sponsored-by: Koha-Suomi Oy
Signed-off-by: John Doe <tomascohen@gmail.com>
edit: tidied the file (tcohen)
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Lari Strand 2026-05-12 09:48:04 +03:00 committed by Pedro Amorim
parent f2003e3e52
commit 5ebc4d79a1
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
1 changed files with 72 additions and 2 deletions

View File

@ -32,6 +32,7 @@ use constant DEFAULT_JOBS_PURGETYPES => qw{ update_elastic_index };
use constant DEFAULT_JOBS_UNFINISHED_PURGEDAYS => 365;
use constant DEFAULT_JOBS_UNFINISHED_PURGETYPES => qw{ update_elastic_index };
use constant DEFAULT_EDIFACT_MSG_PURGEDAYS => 365;
use constant DEFAULT_ILLREQUESTS_PURGEDAYS => 30;
use Getopt::Long qw( GetOptions );
use Try::Tiny;
@ -65,7 +66,7 @@ Usage: $0
-h --help prints this help message, and exits, ignoring all
other options
--confirm Confirmation flag, the script will be running in dry-run mode is not set.
--sessions purge the sessions table. If you use this while users
--sessions purge the sessions table. If you use this while users
are logged into Koha, they will have to reconnect.
--sessdays DAYS purge only sessions older than DAYS days.
-v --verbose will cause the script to give you a bit more information
@ -134,6 +135,9 @@ Usage: $0
--reports DAYS Purge reports data saved more than DAYS days ago. The data is created by running runreport.pl with the --store-results option.
--edifact-messages DAYS Purge process and failed EDIFACT messages handled more than DAYS days.
Defaults to 365 days if no days specified.
--illrequests-status STATUS Specify which ILL request statuses to remove. Repeatable.
--illrequests-days DAYS Purge ILL requests older than DAYS days.
Defaults to 30 days if omitted. Requires at least one --illrequests-status.
USAGE
exit $_[0];
}
@ -184,6 +188,12 @@ my $jobs_days;
my @jobs_types;
my $reports;
my $edifact_msg_days;
my $illrequests_days;
my @illrequests_statuses;
my $command_line_options = join( " ", @ARGV );
cronlogaction( { info => $command_line_options } );
GetOptions(
'h|help' => \$help,
'confirm' => \$confirm,
@ -232,6 +242,8 @@ GetOptions(
'jobs-days:i' => \$jobs_days,
'reports:i' => \$reports,
'edifact-messages:i' => \$edifact_msg_days,
'illrequests-days:i' => \$illrequests_days,
'illrequests-status:s' => \@illrequests_statuses,
) || usage(1);
# Use default values
@ -249,6 +261,7 @@ $jobs_unfinished_days = DEFAULT_JOBS_UNFINISHED_PURGEDAYS
$jobs_days = DEFAULT_JOBS_PURGEDAYS if defined($jobs_days) && $jobs_days == 0;
@jobs_types = (DEFAULT_JOBS_PURGETYPES) if $jobs_days && @jobs_types == 0;
$edifact_msg_days = DEFAULT_EDIFACT_MSG_PURGEDAYS if defined($edifact_msg_days) && $edifact_msg_days == 0;
$illrequests_days = DEFAULT_ILLREQUESTS_PURGEDAYS if defined($illrequests_days) && $illrequests_days == 0;
@statistics_types = ( @statistics_types, @Koha::Statistic::pseudonymization_types ) if defined($statistics_type_pseudo);
@statistics_types = keys %{ { map { $_, 1 } @statistics_types } }; # remove duplicate types
@ -304,7 +317,9 @@ unless ( $sessions
|| $jobs_days
|| $jobs_unfinished_days
|| $reports
|| $edifact_msg_days )
|| $edifact_msg_days
|| $illrequests_days
|| @illrequests_statuses )
{
print "You did not specify any cleanup work for the script to do.\n\n";
usage(1);
@ -315,6 +330,15 @@ if ( $pDebarments && $allDebarments ) {
usage(1);
}
if ( @illrequests_statuses && !$illrequests_days ) {
$illrequests_days = DEFAULT_ILLREQUESTS_PURGEDAYS;
}
if ( $illrequests_days && !@illrequests_statuses ) {
print "You must specify at least one --illrequests-status when using --illrequests-days.\n\n";
usage(1);
}
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
my $dbh = C4::Context->dbh();
@ -858,6 +882,21 @@ if ($edifact_msg_days) {
}
}
if ($illrequests_days) {
print "Purging ILL requests with statuses "
. join( ', ', @illrequests_statuses )
. " older than $illrequests_days days.\n"
if $verbose;
my $count = PurgeIllRequests( $illrequests_days, \@illrequests_statuses, $confirm );
if ($verbose) {
say $confirm
? sprintf( "Done with purging %d ILL requests.", $count )
: sprintf( "%d ILL requests would have been removed", $count );
}
}
cronlogaction( { action => 'End', info => "COMPLETED" } );
exit(0);
sub RemoveOldSessions {
@ -1014,3 +1053,34 @@ sub PurgeEdifactMessages {
return $count;
}
sub PurgeIllRequests {
my ( $days, $statuses, $doit ) = @_;
my $count = 0;
$sth = $dbh->prepare(
q{
DELETE FROM illrequests
WHERE updated < date_sub(curdate(), INTERVAL ? DAY)
AND status IN (}
. join( ',', ('?') x @$statuses ) . q{)
}
);
if ($doit) {
$sth->execute( $days, @$statuses ) or die $dbh->errstr;
$count = $sth->rows;
} else {
my $count_sth = $dbh->prepare(
q{
SELECT COUNT(*) FROM illrequests
WHERE updated < date_sub(curdate(), INTERVAL ? DAY)
AND status IN (}
. join( ',', ('?') x @$statuses ) . q{)
}
);
$count_sth->execute( $days, @$statuses ) or die $dbh->errstr;
($count) = $count_sth->fetchrow_array;
}
return $count;
}