Bug 18300: Delete missing upload records
[koha.git] / Koha / UploadedFiles.pm
1 package Koha::UploadedFiles;
2
3 # Copyright Rijksmuseum 2016
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use C4::Koha;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use Koha::UploadedFile;
26
27 use parent qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::UploadedFiles - Koha::Objects class for uploaded files
32
33 =head1 SYNOPSIS
34
35     use Koha::UploadedFiles;
36
37     # get one upload
38     my $upload01 = Koha::UploadedFiles->find( $id );
39
40     # get some uploads
41     my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
42
43     # delete all uploads
44     Koha::UploadedFiles->delete;
45
46 =head1 DESCRIPTION
47
48 Allows regular CRUD operations on uploaded_files via Koha::Objects / DBIx.
49
50 The delete method also takes care of deleting files. The search_term method
51 provides a wrapper around search to look for a term in multiple columns.
52
53 =head1 METHODS
54
55 =head2 INSTANCE METHODS
56
57 =head3 delete
58
59 Delete uploaded files.
60
61 Parameter keep_file may be used to delete records, but keep files.
62
63 Returns the number of deleted records, 0E0 or -1 (Unknown).
64 Please note that the number of deleted records is not automatically the same
65 as the number of files.
66
67 =cut
68
69 sub delete {
70     my ( $self, $params ) = @_;
71     $self = Koha::UploadedFiles->new if !ref($self); # handle class call
72     # We use the individual delete on each resultset record
73     my $rv = 0;
74     while( my $row = $self->next ) {
75         my $delete= $row->delete( $params ); # 1, 0E0 or -1
76         $rv = ( $delete < 0 || $rv < 0 ) ? -1 : ( $rv + $delete );
77     }
78     return $rv==0 ? "0E0" : $rv;
79 }
80
81 =head3 delete_temporary
82
83 Delete_temporary is called by cleanup_database and only removes temporary
84 uploads older than [pref UploadPurgeTemporaryFilesDays] days.
85 It is possible to override the pref with the override_pref parameter.
86
87 Return value: see delete.
88
89 =cut
90
91 sub delete_temporary {
92     my ( $self, $params ) = @_;
93     my $days = C4::Context->preference('UploadPurgeTemporaryFilesDays');
94     if( exists $params->{override_pref} ) {
95         $days = $params->{override_pref};
96     } elsif( !defined($days) || $days eq '' ) { # allow 0, not NULL or ""
97         return "0E0";
98     }
99     my $dt = dt_from_string();
100     $dt->subtract( days => $days );
101     my $parser = Koha::Database->new->schema->storage->datetime_parser;
102     return $self->search({
103         permanent => [ undef, 0 ],
104         dtcreated => { '<' => $parser->format_datetime($dt) },
105     })->delete;
106 }
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
138 =head3 search_term
139
140 Search_term allows you to pass a term to search in filename and hashvalue.
141 If you do not pass include_private, only public records are returned.
142
143 Is only a wrapper around Koha::Objects search. Has similar return value.
144
145 =cut
146
147 sub search_term {
148     my ( $self, $params ) = @_;
149     my $term = $params->{term} // '';
150     my %public = ();
151     if( !$params->{include_private} ) {
152         %public = ( public => 1 );
153     }
154     return $self->search(
155         [ { filename => { like => '%'.$term.'%' }, %public },
156           { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
157         { order_by => { -asc => 'id' }},
158     );
159 }
160
161 =head2 CLASS METHODS
162
163 =head3 getCategories
164
165 getCategories returns a list of upload category codes and names
166
167 =cut
168
169 sub getCategories {
170     my ( $class ) = @_;
171     my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
172     [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
173 }
174
175 =head3 _type
176
177 Returns name of corresponding DBIC resultset
178
179 =cut
180
181 sub _type {
182     return 'UploadedFile';
183 }
184
185 =head3 object_class
186
187 Returns name of corresponding Koha object class
188
189 =cut
190
191 sub object_class {
192     return 'Koha::UploadedFile';
193 }
194
195 =head1 AUTHOR
196
197 Marcel de Rooy (Rijksmuseum)
198
199 Koha Development Team
200
201 =cut
202
203 1;