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

(-)a/Koha/UploadedFiles.pm (+30 lines)
Lines 98-103 sub delete_temporary { Link Here
98
    })->delete;
98
    })->delete;
99
}
99
}
100
100
101
=head3 delete_missing
102
103
    $cnt = Koha::UploadedFiles->delete_missing();
104
105
    $cnt = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
106
107
Deletes all records where the actual file is not found.
108
109
Supports a keep_record hash parameter to do a check only.
110
111
Returns the number of missing files (and/or deleted records).
112
113
=cut
114
115
sub delete_missing {
116
    my ( $self, $params ) = @_;
117
    $self = Koha::UploadedFiles->new if !ref($self); # handle class call
118
    my $cnt = 0;
119
    while( my $row = $self->next ) {
120
        if( my $file = $row->full_path ) {
121
            next if -e $file;
122
            # We are passing keep_file since we already know that the file
123
            # is missing and we do not want to see the warning
124
            $row->delete({ keep_file => 1 }) if !$params->{keep_record};
125
            $cnt++;
126
        }
127
    }
128
    return $cnt;
129
}
130
101
=head3 search_term
131
=head3 search_term
102
132
103
Search_term allows you to pass a term to search in filename and hashvalue.
133
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-override 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-override 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-override DAYS Override the corresponding preference value.
85
   --temp-uploads-override 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 $override_temp_uploads;
110
my $override_temp_uploads;
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-override:i' => \$override_temp_uploads,
133
    'temp-uploads-override:i' => \$override_temp_uploads,
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 320-325 if( $temp_uploads ) { Link Here
320
    print "Done purging temporary uploads.\n" if $verbose;
324
    print "Done purging temporary uploads.\n" if $verbose;
321
}
325
}
322
326
327
if( defined $uploads_missing ) {
328
    print "Looking for missing uploads\n" if $verbose;
329
    my $keep = $uploads_missing == 1 ? 0 : 1;
330
    my $count = Koha::UploadedFiles->delete_missing({ keep_record => $keep });
331
    if( $keep ) {
332
        print "Counted $count missing uploaded files\n";
333
    } else {
334
        print "Removed $count records for missing uploads\n";
335
    }
336
}
337
323
exit(0);
338
exit(0);
324
339
325
sub RemoveOldSessions {
340
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 183-188 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { Link Here
183
    is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
183
    is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
184
};
184
};
185
185
186
subtest 'Test delete_missing' => sub {
187
    plan tests => 4;
188
189
    # If we add files via TestBuilder, they do not exist
190
    my $upload01 = $builder->build({ source => 'UploadedFile' });
191
    my $upload02 = $builder->build({ source => 'UploadedFile' });
192
    # dry run first
193
    my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
194
    is( $deleted, 2, 'Expect two missing files' );
195
    isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
196
    $deleted = Koha::UploadedFiles->delete_missing;
197
    is( $deleted, 2, 'Deleted two missing files' );
198
    is( Koha::UploadedFiles->search({
199
        id => [ $upload01->{id}, $upload02->{id} ],
200
    })->count, 0, 'Records are gone' );
201
};
202
186
subtest 'Call search_term with[out] private flag' => sub {
203
subtest 'Call search_term with[out] private flag' => sub {
187
    plan tests => 3;
204
    plan tests => 3;
188
205
189
- 

Return to bug 18300