})->delete;
}
+=head3 delete_missing
+
+ $cnt = Koha::UploadedFiles->delete_missing();
+
+ $cnt = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
+
+Deletes all records where the actual file is not found.
+
+Supports a keep_record hash parameter to do a check only.
+
+Returns the number of missing files (and/or deleted records).
+
+=cut
+
+sub delete_missing {
+ my ( $self, $params ) = @_;
+ $self = Koha::UploadedFiles->new if !ref($self); # handle class call
+ my $cnt = 0;
+ while( my $row = $self->next ) {
+ if( my $file = $row->full_path ) {
+ next if -e $file;
+ # We are passing keep_file since we already know that the file
+ # is missing and we do not want to see the warning
+ $row->delete({ keep_file => 1 }) if !$params->{keep_record};
+ $cnt++;
+ }
+ }
+ return $cnt;
+}
+
=head3 search_term
Search_term allows you to pass a term to search in filename and hashvalue.
sub usage {
print STDERR <<USAGE;
-Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS] [--temp-uploads] [--temp-uploads-days DAYS]
+Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS] [--temp-uploads] [--temp-uploads-days DAYS] [--uploads-missing 0|1 ]
-h --help prints this help message, and exits, ignoring all
other options
--unique-holidays DAYS Delete all unique holidays older than DAYS
--temp-uploads Delete temporary uploads.
--temp-uploads-days DAYS Override the corresponding preference value.
+ --uploads-missing FLAG Delete upload records for missing files when FLAG is true, count them otherwise
USAGE
exit $_[0];
}
my $special_holidays_days;
my $temp_uploads;
my $temp_uploads_days;
+my $uploads_missing;
GetOptions(
'h|help' => \$help,
'unique-holidays:i' => \$special_holidays_days,
'temp-uploads' => \$temp_uploads,
'temp-uploads-days:i' => \$temp_uploads_days,
+ 'uploads-missing:i' => \$uploads_missing,
) || usage(1);
# Use default values
|| $pUnvSelfReg
|| $special_holidays_days
|| $temp_uploads
+ || defined $uploads_missing
) {
print "You did not specify any cleanup work for the script to do.\n\n";
usage(1);
print "Done purging temporary uploads.\n" if $verbose;
}
+if( defined $uploads_missing ) {
+ print "Looking for missing uploads\n" if $verbose;
+ my $keep = $uploads_missing == 1 ? 0 : 1;
+ my $count = Koha::UploadedFiles->delete_missing({ keep_record => $keep });
+ if( $keep ) {
+ print "Counted $count missing uploaded files\n";
+ } else {
+ print "Removed $count records for missing uploads\n";
+ }
+}
+
exit(0);
sub RemoveOldSessions {
use Modern::Perl;
use File::Temp qw/ tempdir /;
-use Test::More tests => 11;
+use Test::More tests => 12;
use Test::Warn;
use Test::MockModule;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
-my $builder = t::lib::TestBuilder->new;
+our $builder = t::lib::TestBuilder->new;
our $current_upload = 0;
our $uploads = [
# NOTE: Koha::Object->delete does not return 0E0 (yet?)
};
+subtest 'Test delete_missing' => sub {
+ plan tests => 4;
+
+ # If we add files via TestBuilder, they do not exist
+ my $upload01 = $builder->build({ source => 'UploadedFile' });
+ my $upload02 = $builder->build({ source => 'UploadedFile' });
+ # dry run first
+ my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
+ is( $deleted, 2, 'Expect two missing files' );
+ isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
+ $deleted = Koha::UploadedFiles->delete_missing;
+ is( $deleted, 2, 'Deleted two missing files' );
+ is( Koha::UploadedFiles->search({
+ id => [ $upload01->{id}, $upload02->{id} ],
+ })->count, 0, 'Records are gone' );
+};
+
subtest 'Call search_term with[out] private flag' => sub {
plan tests => 3;