3 # Copyright 2009 PTFS, Inc.
5 # This file is part of Koha.
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
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.
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.
25 # find Koha's Perl modules
26 # test carefully before changing this
28 eval { require "$FindBin::Bin/../kohalib.pl" };
40 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS]
42 -h --help prints this help message, and exits, ignoring all
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
49 --zebraqueue DAYS purge completed entries from the zebraqueue from
50 more than DAYS days ago.
51 -m --mail purge the mail queue.
56 my ($help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail);
60 'sessions' => \$sessions,
61 'sessdays:i' => \$sess_days,
62 'v|verbose' => \$verbose,
64 'zebraqueue:i' => \$zebraqueue_days,
71 if (!($sessions || $zebraqueue_days || $mail)){
72 print "You did not specify any cleanup work for the script to do.\n\n";
76 my $dbh = C4::Context->dbh();
82 if ($sessions && !$sess_days) { #old behavior
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";
90 $sth = $dbh->prepare("TRUNCATE sessions");
91 $sth->execute() or die $dbh->errstr;;
93 print "Done with session purge.\n";
96 elsif($sessions && $sess_days>0) { #new behavior with number of days old
98 print "Session purge triggered with days>$sess_days.\n";
102 print "Done with session purge with days>$sess_days.\n";
106 if ($zebraqueue_days){
109 print "Zebraqueue purge triggered for $zebraqueue_days days.\n";
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;
120 print "$count records were deleted.\nDone with zebraqueue purge.\n";
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";
131 $sth = $dbh->prepare("TRUNCATE message_queue");
132 $sth->execute() or $dbh->errstr;
133 print "Done with purging the mail queue.\n" if ($verbose);
137 sub RemoveOldSessions {
138 my ($id, $a_session, $limit, $lasttime);
139 $limit= time() - 24*3600*$sess_days;
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=?");
147 while ($sth->fetch) {
149 if($a_session =~ /lasttime:\s+(\d+)/) {
152 elsif($a_session =~ /(ATIME|CTIME):\s+(\d+)/ ) {
155 if($lasttime && $lasttime < $limit) {
156 $sth2->execute($id) or die $dbh->errstr;
161 print "$count sessions were deleted.\n";