Bug 9988: Refactor the cron script
[koha.git] / Koha / Authority / MergeRequests.pm
1 package Koha::Authority::MergeRequests;
2
3 # Copyright Rijksmuseum 2017
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 use MARC::File::XML;
22 use MARC::Record;
23
24 use C4::Context;
25 use Koha::Authority::MergeRequest;
26 use Koha::Database;
27 use Koha::DateUtils;
28
29 use parent qw(Koha::Objects);
30
31 =head1 NAME
32
33 Koha::Authority::MergeRequests - Koha::Objects class for need_merge_authorities
34
35 =head1 SYNOPSIS
36
37 use Koha::Authority::MergeRequests;
38
39 =head1 DESCRIPTION
40
41 Description
42
43 =head1 METHODS
44
45 =head2 INSTANCE METHODS
46
47 =head2 CLASS METHODS
48
49 =head3 reporting_tag_xml
50
51     my $xml = Koha::Authority::MergeRequests->reporting_tag_xml({
52         record => $record, tag => $tag,
53     });
54
55 =cut
56
57 sub reporting_tag_xml {
58     my ( $class, $params ) = @_;
59     return if !$params->{record} || !$params->{tag};
60
61     my $newrecord = MARC::Record->new;
62     $newrecord->encoding( 'UTF-8' );
63     my $reportfield = $params->{record}->field( $params->{tag} );
64     return if !$reportfield;
65
66     $newrecord->append_fields( $reportfield );
67     return $newrecord->as_xml(
68         C4::Context->preference('marcflavour') eq 'UNIMARC' ?
69         'UNIMARCAUTH' :
70         'MARC21'
71     );
72 }
73
74 =head3 cron_cleanup
75
76     Koha::Authority::MergeRequests->cron_cleanup({
77         reset_hours => 24, remove_days => 90,
78     });
79
80     Removes all entries with status "done" older than remove_days.
81     Set all entries with status "in progress" back to 0 when the timestamp
82     is older than reset_hours.
83     Defaults: reset_hours = 1, remove_days = 30.
84
85 =cut
86
87 sub cron_cleanup {
88     my ( $class_or_self, $params ) = @_;
89     my $reset_hours = $params->{reset_hours} || 1;
90     my $remove_days = $params->{remove_days} || 30;
91     my $parser = Koha::Database->new->schema->storage->datetime_parser;
92
93     my $dt = dt_from_string;
94     $dt->subtract( hours => $reset_hours );
95     $class_or_self->search({
96         done => 2,
97         timestamp => { '<' => $parser->format_datetime($dt) },
98     })->update({ done => 0 });
99
100     $dt = dt_from_string;
101     $dt->subtract( days => $remove_days );
102     $class_or_self->search({
103         done => 1,
104         timestamp => { '<' => $parser->format_datetime($dt) },
105     })->delete;
106 }
107
108 =head3 _type
109
110 Returns name of corresponding DBIC resultset
111
112 =cut
113
114 sub _type {
115     return 'NeedMergeAuthority';
116 }
117
118 =head3 object_class
119
120 Returns name of corresponding Koha object class
121
122 =cut
123
124 sub object_class {
125     return 'Koha::Authority::MergeRequest';
126 }
127
128 =head1 AUTHOR
129
130 Marcel de Rooy (Rijksmuseum)
131
132 Koha Development Team
133
134 =cut
135
136 1;