fix malformed call of XSLTParse4Display
[koha.git] / misc / cronjobs / cleanup_database.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 PTFS, Inc.
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 2 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 strict;
21 use warnings;
22
23 BEGIN {
24
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { require "$FindBin::Bin/../kohalib.pl" };
29 }
30
31 use C4::Context;
32 use C4::Dates;
33 #use C4::Debug;
34 #use C4::Letters;
35 #use File::Spec;
36 use Getopt::Long;
37
38 sub usage {
39     print STDERR <<USAGE;
40 Usage: $0 [-h|--help] [--sessions] [-v|--verbose] [--zebraqueue DAYS]
41    -h --help         prints this help message, and exits, ignoring all other options
42    --sessions        purge the sessions table.  If you use this while users are logged
43                      into Koha, they will have to reconnect.
44    -v --verbose      will cause the script to give you a bit more information about the run.
45    --zebraqueue DAYS purge completed entries from the zebraqueue from more than DAYS days ago.
46 USAGE
47     exit $_[0];
48 }
49
50 my ($help, $sessions, $verbose, $zebraqueue_days);
51
52 GetOptions(
53     'h|help' => \$help,
54     'sessions' => \$sessions,
55     'v|verbose' => \$verbose,
56     'zebraqueue:i' => \$zebraqueue_days,
57 ) || usage(1);
58
59 if ($help) {
60     usage(0);
61 }
62
63 if (!($sessions || $zebraqueue_days)){
64     print "You did not specify any cleanup work for the script to do.\n\n";
65     usage(1);
66 }
67
68 my $dbh = C4::Context->dbh();
69 my $query;
70 my $sth;
71 my $sth2;
72 my $count;
73
74 if ($sessions) {
75     if ($verbose){
76         print "Session purge triggered.\n";
77         $sth = $dbh->prepare("SELECT COUNT(*) FROM sessions");
78         $sth->execute() or die $dbh->errstr;
79         my @count_arr = $sth->fetchrow_array;
80         print "$count_arr[0] entries will be deleted.\n";
81     }
82     $sth = $dbh->prepare("TRUNCATE sessions");
83     $sth->execute() or die $dbh->errstr;;
84     if ($verbose){
85         print "Done with session purge.\n";
86     }
87 }
88
89 if ($zebraqueue_days){
90     $count = 0;
91     if ($verbose){
92         print "Zebraqueue purge triggered for $zebraqueue_days days.\n";
93     }
94     $sth = $dbh->prepare("SELECT id,biblio_auth_number,server,time FROM zebraqueue
95                           WHERE done=1 and time < date_sub(curdate(), interval ? day)");
96     $sth->execute($zebraqueue_days) or die $dbh->errstr;
97     $sth2 = $dbh->prepare("DELETE FROM zebraqueue WHERE id=?");
98     while (my $record = $sth->fetchrow_hashref){
99         $sth2->execute($record->{id}) or die $dbh->errstr;
100         $count++;
101     }
102     if ($verbose){
103         print "$count records were deleted.\nDone with zebraqueue purge.\n";
104     }
105 }
106 exit(0);