Merge remote branch 'kc/new/merge_help' into kcmaster
[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] [--sessdays DAYS] [-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    --sessdays DAYS   purge only sessions older than DAYS days (use together with sessions parameter).
47    -v --verbose       will cause the script to give you a bit more information
48                       about the run.
49    --zebraqueue DAYS  purge completed entries from the zebraqueue from 
50                       more than DAYS days ago.
51    -m --mail          purge the mail queue. 
52 USAGE
53     exit $_[0];
54 }
55
56 my ($help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail);
57
58 GetOptions(
59     'h|help'    => \$help,
60     'sessions'  => \$sessions,
61     'sessdays:i' => \$sess_days,
62     'v|verbose' => \$verbose,
63     'm|mail'    => \$mail,
64     'zebraqueue:i' => \$zebraqueue_days,
65 ) || usage(1);
66
67 if ($help) {
68     usage(0);
69 }
70
71 if (!($sessions || $zebraqueue_days || $mail)){
72     print "You did not specify any cleanup work for the script to do.\n\n";
73     usage(1);
74 }
75
76 my $dbh = C4::Context->dbh();
77 my $query;
78 my $sth;
79 my $sth2;
80 my $count;
81
82 if ($sessions && !$sess_days) { #old behavior
83     if ($verbose){
84         print "Session purge triggered.\n";
85         $sth = $dbh->prepare("SELECT COUNT(*) FROM sessions");
86         $sth->execute() or die $dbh->errstr;
87         my @count_arr = $sth->fetchrow_array;
88         print "$count_arr[0] entries will be deleted.\n";
89     }
90     $sth = $dbh->prepare("TRUNCATE sessions");
91     $sth->execute() or die $dbh->errstr;;
92     if ($verbose){
93         print "Done with session purge.\n";
94     }
95 }
96 elsif($sessions && $sess_days>0) { #new behavior with number of days old
97     if ($verbose){
98         print "Session purge triggered with days>$sess_days.\n";
99     }
100     RemoveOldSessions();
101     if ($verbose){
102         print "Done with session purge with days>$sess_days.\n";
103     }
104 }
105
106 if ($zebraqueue_days){
107     $count = 0;
108     if ($verbose){
109         print "Zebraqueue purge triggered for $zebraqueue_days days.\n";
110     }
111     $sth = $dbh->prepare("SELECT id,biblio_auth_number,server,time FROM zebraqueue
112                           WHERE done=1 and time < date_sub(curdate(), interval ? day)");
113     $sth->execute($zebraqueue_days) or die $dbh->errstr;
114     $sth2 = $dbh->prepare("DELETE FROM zebraqueue WHERE id=?");
115     while (my $record = $sth->fetchrow_hashref){
116         $sth2->execute($record->{id}) or die $dbh->errstr;
117         $count++;
118     }
119     if ($verbose){
120         print "$count records were deleted.\nDone with zebraqueue purge.\n";
121     }
122 }
123
124 if ($mail) {
125     if ($verbose) {
126         $sth = $dbh->prepare("SELECT COUNT(*) FROM message_queue");
127         $sth->execute() or die $dbh->errstr;
128         my @count_arr = $sth->fetchrow_array;
129         print "Deleting $count_arr[0] entries from the mail queue.\n";
130     }
131     $sth = $dbh->prepare("TRUNCATE message_queue");
132     $sth->execute() or $dbh->errstr;
133     print "Done with purging the mail queue.\n" if ($verbose);
134 }
135 exit(0);
136
137 sub RemoveOldSessions {
138   my ($id, $a_session, $limit, $lasttime);
139   $limit= time() - 24*3600*$sess_days;
140
141   $sth= $dbh->prepare("SELECT id, a_session FROM sessions");
142   $sth->execute or die $dbh->errstr;
143   $sth->bind_columns(\$id, \$a_session);
144   $sth2 = $dbh->prepare("DELETE FROM sessions WHERE id=?");
145   $count=0;
146
147   while ($sth->fetch) {
148     $lasttime=0;
149     if($a_session =~ /lasttime:\s+(\d+)/) {
150         $lasttime= $1;
151     }
152     elsif($a_session =~ /(ATIME|CTIME):\s+(\d+)/ ) {
153         $lasttime= $2;
154     }
155     if($lasttime && $lasttime < $limit) {
156         $sth2->execute($id) or die $dbh->errstr;
157         $count++;
158     }
159   }
160   if ($verbose){
161       print "$count sessions were deleted.\n";
162   }
163 }