View | Details | Raw Unified | Return to bug 18300
Collapse All | Expand All

(-)a/Koha/UploadedFiles.pm (+30 lines)
Lines 105-110 sub delete_temporary { Link Here
105
    })->delete;
105
    })->delete;
106
}
106
}
107
107
108
=head3 delete_missing
109
110
    $cnt = Koha::UploadedFiles->delete_missing();
111
112
    $cnt = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
113
114
Deletes all records where the actual file is not found.
115
116
Supports a keep_record hash parameter to do a check only.
117
118
Returns the number of missing files (and/or deleted records).
119
120
=cut
121
122
sub delete_missing {
123
    my ( $self, $params ) = @_;
124
    $self = Koha::UploadedFiles->new if !ref($self); # handle class call
125
    my $cnt = 0;
126
    while( my $row = $self->next ) {
127
        if( my $file = $row->full_path ) {
128
            next if -e $file;
129
            # We are passing keep_file since we already know that the file
130
            # is missing and we do not want to see the warning
131
            $row->delete({ keep_file => 1 }) if !$params->{keep_record};
132
            $cnt++;
133
        }
134
    }
135
    return $cnt;
136
}
137
108
=head3 search_term
138
=head3 search_term
109
139
110
Search_term allows you to pass a term to search in filename and hashvalue.
140
Search_term allows you to pass a term to search in filename and hashvalue.
(-)a/misc/cronjobs/cleanup_database.pl (-1 / +16 lines)
Lines 44-50 use Koha::UploadedFiles; Link Here
44
44
45
sub usage {
45
sub usage {
46
    print STDERR <<USAGE;
46
    print STDERR <<USAGE;
47
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]
47
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 ]
48
48
49
   -h --help          prints this help message, and exits, ignoring all
49
   -h --help          prints this help message, and exits, ignoring all
50
                      other options
50
                      other options
Lines 83-88 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu Link Here
83
   --unique-holidays DAYS  Delete all unique holidays older than DAYS
83
   --unique-holidays DAYS  Delete all unique holidays older than DAYS
84
   --temp-uploads     Delete temporary uploads.
84
   --temp-uploads     Delete temporary uploads.
85
   --temp-uploads-days DAYS Override the corresponding preference value.
85
   --temp-uploads-days DAYS Override the corresponding preference value.
86
   --uploads-missing FLAG Delete upload records for missing files when FLAG is true, count them otherwise
86
USAGE
87
USAGE
87
    exit $_[0];
88
    exit $_[0];
88
}
89
}
Lines 107-112 my $fees_days; Link Here
107
my $special_holidays_days;
108
my $special_holidays_days;
108
my $temp_uploads;
109
my $temp_uploads;
109
my $temp_uploads_days;
110
my $temp_uploads_days;
111
my $uploads_missing;
110
112
111
GetOptions(
113
GetOptions(
112
    'h|help'            => \$help,
114
    'h|help'            => \$help,
Lines 129-134 GetOptions( Link Here
129
    'unique-holidays:i' => \$special_holidays_days,
131
    'unique-holidays:i' => \$special_holidays_days,
130
    'temp-uploads'      => \$temp_uploads,
132
    'temp-uploads'      => \$temp_uploads,
131
    'temp-uploads-days:i' => \$temp_uploads_days,
133
    'temp-uploads-days:i' => \$temp_uploads_days,
134
    'uploads-missing:i' => \$uploads_missing,
132
) || usage(1);
135
) || usage(1);
133
136
134
# Use default values
137
# Use default values
Lines 161-166 unless ( $sessions Link Here
161
    || $pUnvSelfReg
164
    || $pUnvSelfReg
162
    || $special_holidays_days
165
    || $special_holidays_days
163
    || $temp_uploads
166
    || $temp_uploads
167
    || defined $uploads_missing
164
) {
168
) {
165
    print "You did not specify any cleanup work for the script to do.\n\n";
169
    print "You did not specify any cleanup work for the script to do.\n\n";
166
    usage(1);
170
    usage(1);
Lines 321-326 if( $temp_uploads ) { Link Here
321
    print "Done purging temporary uploads.\n" if $verbose;
325
    print "Done purging temporary uploads.\n" if $verbose;
322
}
326
}
323
327
328
if( defined $uploads_missing ) {
329
    print "Looking for missing uploads\n" if $verbose;
330
    my $keep = $uploads_missing == 1 ? 0 : 1;
331
    my $count = Koha::UploadedFiles->delete_missing({ keep_record => $keep });
332
    if( $keep ) {
333
        print "Counted $count missing uploaded files\n";
334
    } else {
335
        print "Removed $count records for missing uploads\n";
336
    }
337
}
338
324
exit(0);
339
exit(0);
325
340
326
sub RemoveOldSessions {
341
sub RemoveOldSessions {
(-)a/t/db_dependent/Upload.t (-3 / +19 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use File::Temp qw/ tempdir /;
4
use File::Temp qw/ tempdir /;
5
use Test::More tests => 11;
5
use Test::More tests => 12;
6
use Test::Warn;
6
use Test::Warn;
7
7
8
use Test::MockModule;
8
use Test::MockModule;
Lines 18-24 use Koha::Uploader; Link Here
18
18
19
my $schema  = Koha::Database->new->schema;
19
my $schema  = Koha::Database->new->schema;
20
$schema->storage->txn_begin;
20
$schema->storage->txn_begin;
21
my $builder = t::lib::TestBuilder->new;
21
our $builder = t::lib::TestBuilder->new;
22
22
23
our $current_upload = 0;
23
our $current_upload = 0;
24
our $uploads = [
24
our $uploads = [
Lines 192-197 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { Link Here
192
    # NOTE: Koha::Object->delete does not return 0E0 (yet?)
192
    # NOTE: Koha::Object->delete does not return 0E0 (yet?)
193
};
193
};
194
194
195
subtest 'Test delete_missing' => sub {
196
    plan tests => 4;
197
198
    # If we add files via TestBuilder, they do not exist
199
    my $upload01 = $builder->build({ source => 'UploadedFile' });
200
    my $upload02 = $builder->build({ source => 'UploadedFile' });
201
    # dry run first
202
    my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
203
    is( $deleted, 2, 'Expect two missing files' );
204
    isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
205
    $deleted = Koha::UploadedFiles->delete_missing;
206
    is( $deleted, 2, 'Deleted two missing files' );
207
    is( Koha::UploadedFiles->search({
208
        id => [ $upload01->{id}, $upload02->{id} ],
209
    })->count, 0, 'Records are gone' );
210
};
211
195
subtest 'Call search_term with[out] private flag' => sub {
212
subtest 'Call search_term with[out] private flag' => sub {
196
    plan tests => 3;
213
    plan tests => 3;
197
214
198
- 

Return to bug 18300