Merge branch 'translation' of git://git.workbuffer.org/git/koha into master
[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         [-m|--mail]
42    -h --help          prints this help message, and exits, ignoring all
43                       other options
44    --sessions         purge the sessions table.  If you use this while users 
45                       are logged into Koha, they will have to reconnect.
46    -v --verbose       will cause the script to give you a bit more information
47                       about the run.
48    --zebraqueue DAYS  purge completed entries from the zebraqueue from 
49                       more than DAYS days ago.
50    -m --mail          purge the mail queue. 
51 USAGE
52     exit $_[0];
53 }
54
55 my ($help, $sessions, $verbose, $zebraqueue_days, $mail);
56
57 GetOptions(
58     'h|help'    => \$help,
59     'sessions'  => \$sessions,
60     'v|verbose' => \$verbose,
61     'm|mail'    => \$mail,
62     'zebraqueue:i' => \$zebraqueue_days,
63 ) || usage(1);
64
65 if ($help) {
66     usage(0);
67 }
68
69 if (!($sessions || $zebraqueue_days || $mail)){
70     print "You did not specify any cleanup work for the script to do.\n\n";
71     usage(1);
72 }
73
74 my $dbh = C4::Context->dbh();
75 my $query;
76 my $sth;
77 my $sth2;
78 my $count;
79
80 if ($sessions) {
81     if ($verbose){
82         print "Session purge triggered.\n";
83         $sth = $dbh->prepare("SELECT COUNT(*) FROM sessions");
84         $sth->execute() or die $dbh->errstr;
85         my @count_arr = $sth->fetchrow_array;
86         print "$count_arr[0] entries will be deleted.\n";
87     }
88     $sth = $dbh->prepare("TRUNCATE sessions");
89     $sth->execute() or die $dbh->errstr;;
90     if ($verbose){
91         print "Done with session purge.\n";
92     }
93 }
94
95 if ($zebraqueue_days){
96     $count = 0;
97     if ($verbose){
98         print "Zebraqueue purge triggered for $zebraqueue_days days.\n";
99     }
100     $sth = $dbh->prepare("SELECT id,biblio_auth_number,server,time FROM zebraqueue
101                           WHERE done=1 and time < date_sub(curdate(), interval ? day)");
102     $sth->execute($zebraqueue_days) or die $dbh->errstr;
103     $sth2 = $dbh->prepare("DELETE FROM zebraqueue WHERE id=?");
104     while (my $record = $sth->fetchrow_hashref){
105         $sth2->execute($record->{id}) or die $dbh->errstr;
106         $count++;
107     }
108     if ($verbose){
109         print "$count records were deleted.\nDone with zebraqueue purge.\n";
110     }
111 }
112
113 if ($mail) {
114     if ($verbose) {
115         $sth = $dbh->prepare("SELECT COUNT(*) FROM message_queue");
116         $sth->execute() or die $dbh->errstr;
117         my @count_arr = $sth->fetchrow_array;
118         print "Deleting $count_arr[0] entries from the mail queue.\n";
119     }
120     $sth = $dbh->prepare("TRUNCATE message_queue");
121     $sth->execute() or $dbh->errstr;
122     print "Done with purging the mail queue.\n" if ($verbose);
123 }
124 exit(0);