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" };
41 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged]
43 -h --help prints this help message, and exits, ignoring all
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
50 --zebraqueue DAYS purge completed entries from the zebraqueue from
51 more than DAYS days ago.
52 -m --mail purge the mail queue.
53 --merged purged completed entries from need_merge_authorities.
58 my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail, $purge_merged);
62 'sessions' => \$sessions,
63 'sessdays:i' => \$sess_days,
64 'v|verbose' => \$verbose,
66 'zebraqueue:i' => \$zebraqueue_days,
67 'merged' => \$purge_merged,
74 if ( !( $sessions || $zebraqueue_days || $mail || $purge_merged) ) {
75 print "You did not specify any cleanup work for the script to do.\n\n";
79 my $dbh = C4::Context->dbh();
85 if ( $sessions && !$sess_days ) {
87 print "Session purge triggered.\n";
88 $sth = $dbh->prepare("SELECT COUNT(*) FROM sessions");
89 $sth->execute() or die $dbh->errstr;
90 my @count_arr = $sth->fetchrow_array;
91 print "$count_arr[0] entries will be deleted.\n";
93 $sth = $dbh->prepare("TRUNCATE sessions");
94 $sth->execute() or die $dbh->errstr;
96 print "Done with session purge.\n";
98 } elsif ( $sessions && $sess_days > 0 ) {
100 print "Session purge triggered with days>$sess_days.\n";
104 print "Done with session purge with days>$sess_days.\n";
108 if ($zebraqueue_days) {
111 print "Zebraqueue purge triggered for $zebraqueue_days days.\n";
113 $sth = $dbh->prepare(
114 "SELECT id,biblio_auth_number,server,time FROM zebraqueue
115 WHERE done=1 and time < date_sub(curdate(), interval ? day)"
117 $sth->execute($zebraqueue_days) or die $dbh->errstr;
118 $sth2 = $dbh->prepare("DELETE FROM zebraqueue WHERE id=?");
119 while ( my $record = $sth->fetchrow_hashref ) {
120 $sth2->execute( $record->{id} ) or die $dbh->errstr;
124 print "$count records were deleted.\nDone with zebraqueue purge.\n";
130 $sth = $dbh->prepare("SELECT COUNT(*) FROM message_queue");
131 $sth->execute() or die $dbh->errstr;
132 my @count_arr = $sth->fetchrow_array;
133 print "Deleting $count_arr[0] entries from the mail queue.\n";
135 $sth = $dbh->prepare("TRUNCATE message_queue");
136 $sth->execute() or $dbh->errstr;
137 print "Done with purging the mail queue.\n" if ($verbose);
141 print "Purging completed entries from need_merge_authorities.\n" if $verbose;
142 $sth = $dbh->prepare("DELETE FROM need_merge_authorities WHERE done=1");
143 $sth->execute() or die $dbh->errstr;
144 print "Done with purging need_merge_authorities.\n" if $verbose;
149 sub RemoveOldSessions {
150 my ( $id, $a_session, $limit, $lasttime );
151 $limit = time() - 24 * 3600 * $sess_days;
153 $sth = $dbh->prepare("SELECT id, a_session FROM sessions");
154 $sth->execute or die $dbh->errstr;
155 $sth->bind_columns( \$id, \$a_session );
156 $sth2 = $dbh->prepare("DELETE FROM sessions WHERE id=?");
159 while ( $sth->fetch ) {
161 if ( $a_session =~ /lasttime:\s+(\d+)/ ) {
163 } elsif ( $a_session =~ /(ATIME|CTIME):\s+(\d+)/ ) {
166 if ( $lasttime && $lasttime < $limit ) {
167 $sth2->execute($id) or die $dbh->errstr;
172 print "$count sessions were deleted.\n";