Bug 13287: Add a system preference to define the number of days used in purge_suggest...
[koha.git] / misc / cronjobs / purge_suggestions.pl
1 #!/usr/bin/perl -w
2
3 # Copyright 2010 Biblibre SARL
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22 use utf8;
23
24 BEGIN {
25
26     # find Koha's Perl modules
27     # test carefully before changing this
28     use FindBin;
29     eval { require "$FindBin::Bin/../kohalib.pl" };
30 }
31
32 use Getopt::Long;
33 use Pod::Usage;
34 use C4::Suggestions;
35 use C4::Log;
36 use C4::Context;
37
38 my ($help, $days);
39
40 GetOptions(
41     'help|?'         => \$help,
42     'days=s'         => \$days,
43 );
44
45 my $usage = << 'ENDUSAGE';
46 This script delete old suggestions
47 Parameters:
48 -help|? This message
49 -days TTT to define the age of suggestions to delete
50
51 Example:
52 $PERL5LIB/misc/cronjobs/purge_suggestions.pl -days 30
53 ENDUSAGE
54
55 # If this script is called without the 'days' parameter, we use the system preferences value instead.
56 if ( ! defined($days) and not $help) {
57     my $purge_sugg_days = C4::Context->preference('PurgeSuggestionsOlderThan') || '';
58     if($purge_sugg_days ne '' and $purge_sugg_days >= 0) {
59         $days = $purge_sugg_days;
60     }
61 }
62 # If this script is called with the 'help' parameter, we show up the help message and we leave the script without doing anything.
63 if ($help) {
64     print $usage;
65     exit;
66 }
67
68 if(defined($days) && $days > 0 && $days ne ''){
69     cronlogaction();
70     DelSuggestionsOlderThan($days);
71 }
72
73 elsif(defined($days) && $days == 0) {
74     print << 'ERROR';
75     This script is not executed with 0 days. Aborted.
76 ERROR
77 }
78 else {
79     print << 'ERROR';
80     This script requires a positive number of days. Aborted.
81 ERROR
82 }