Bug 24813: Prevent api/v1/holds.t to fail randomly
[koha.git] / Koha / UploadedFiles.pm
blob2f44e974c84714674de5c6d959908def20ee21c3
1 package Koha::UploadedFiles;
3 # Copyright Rijksmuseum 2016
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use C4::Koha;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use Koha::UploadedFile;
27 use parent qw(Koha::Objects);
29 =head1 NAME
31 Koha::UploadedFiles - Koha::Objects class for uploaded files
33 =head1 SYNOPSIS
35 use Koha::UploadedFiles;
37 # get one upload
38 my $upload01 = Koha::UploadedFiles->find( $id );
40 # get some uploads
41 my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
43 # delete all uploads
44 Koha::UploadedFiles->delete;
46 =head1 DESCRIPTION
48 Allows regular CRUD operations on uploaded_files via Koha::Objects / DBIx.
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.
53 =head1 METHODS
55 =head2 INSTANCE METHODS
57 =head3 delete
59 Delete uploaded files.
61 Parameter keep_file may be used to delete records, but keep files.
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.
67 =cut
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 $deleted = eval { $row->delete( $params ) };
76 $rv++ if $deleted && !$@;
78 return $rv==0 ? "0E0" : $rv;
81 =head3 delete_temporary
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.
87 Return value: see delete.
89 =cut
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";
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;
108 =head3 delete_missing
110 $cnt = Koha::UploadedFiles->delete_missing();
112 $cnt = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
114 Deletes all records where the actual file is not found.
116 Supports a keep_record hash parameter to do a check only.
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.
122 =cut
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;
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 $deleted = eval { $row->delete({ keep_file => 1 }) };
139 $rv++ if $deleted && !$@;
141 return $rv==0 ? "0E0" : $rv;
144 =head3 search_term
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.
149 Is only a wrapper around Koha::Objects search. Has similar return value.
151 =cut
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 );
160 return $self->search(
161 [ { filename => { like => '%'.$term.'%' }, %public },
162 { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
163 { order_by => { -asc => 'id' }},
167 =head2 CLASS METHODS
169 =head3 getCategories
171 getCategories returns a list of upload category codes and names
173 =cut
175 sub getCategories {
176 my ( $class ) = @_;
177 my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
178 [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
181 =head3 _type
183 Returns name of corresponding DBIC resultset
185 =cut
187 sub _type {
188 return 'UploadedFile';
191 =head3 object_class
193 Returns name of corresponding Koha object class
195 =cut
197 sub object_class {
198 return 'Koha::UploadedFile';
201 =head1 AUTHOR
203 Marcel de Rooy (Rijksmuseum)
205 Koha Development Team
207 =cut