Bug 22770: DBRev 19.05.00.003
[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 Return value: If you pass keep_record, it returns the number of records where
119 the file is not found, or 0E0. Otherwise it returns a number, 0E0 or -1 just
120 as delete does.
121
122 =cut
123
124 sub delete_missing {
125     my ( $self, $params ) = @_;
126     $self = Koha::UploadedFiles->new if !ref($self); # handle class call
127     my $rv = 0;
128     while( my $row = $self->next ) {
129         my $file = $row->full_path;
130         next if -e $file;
131         if( $params->{keep_record} ) {
132             $rv++;
133             next;
134         }
135         # We are passing keep_file since we already know that the file
136         # is missing and we do not want to see the warning
137         # Apply the same logic as in delete for the return value
138         my $delete = $row->delete({ keep_file => 1 }); # 1, 0E0 or -1
139         $rv = ( $delete < 0 || $rv < 0 ) ? -1 : ( $rv + $delete );
140     }
141     return $rv==0 ? "0E0" : $rv;
142 }
143
144 =head3 search_term
145
146 Search_term allows you to pass a term to search in filename and hashvalue.
147 If you do not pass include_private, only public records are returned.
148
149 Is only a wrapper around Koha::Objects search. Has similar return value.
150
151 =cut
152
153 sub search_term {
154     my ( $self, $params ) = @_;
155     my $term = $params->{term} // '';
156     my %public = ();
157     if( !$params->{include_private} ) {
158         %public = ( public => 1 );
159     }
160     return $self->search(
161         [ { filename => { like => '%'.$term.'%' }, %public },
162           { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
163         { order_by => { -asc => 'id' }},
164     );
165 }
166
167 =head2 CLASS METHODS
168
169 =head3 getCategories
170
171 getCategories returns a list of upload category codes and names
172
173 =cut
174
175 sub getCategories {
176     my ( $class ) = @_;
177     my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
178     [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
179 }
180
181 =head3 _type
182
183 Returns name of corresponding DBIC resultset
184
185 =cut
186
187 sub _type {
188     return 'UploadedFile';
189 }
190
191 =head3 object_class
192
193 Returns name of corresponding Koha object class
194
195 =cut
196
197 sub object_class {
198     return 'Koha::UploadedFile';
199 }
200
201 =head1 AUTHOR
202
203 Marcel de Rooy (Rijksmuseum)
204
205 Koha Development Team
206
207 =cut
208
209 1;