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