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.
23 use constant DEFAULT_ZEBRAQ_PURGEDAYS
=> 30;
24 use constant DEFAULT_MAIL_PURGEDAYS
=> 30;
25 use constant DEFAULT_IMPORT_PURGEDAYS
=> 60;
26 use constant DEFAULT_LOGS_PURGEDAYS
=> 180;
27 use constant DEFAULT_SEARCHHISTORY_PURGEDAYS
=> 30;
28 use constant DEFAULT_SHARE_INVITATION_EXPIRY_DAYS
=> 14;
31 # find Koha's Perl modules
32 # test carefully before changing this
34 eval { require "$FindBin::Bin/../kohalib.pl" };
46 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS]
48 -h --help prints this help message, and exits, ignoring all
50 --sessions purge the sessions table. If you use this while users
51 are logged into Koha, they will have to reconnect.
52 --sessdays DAYS purge only sessions older than DAYS days.
53 -v --verbose will cause the script to give you a bit more information
55 --zebraqueue DAYS purge completed zebraqueue entries older than DAYS days.
56 Defaults to 30 days if no days specified.
57 -m --mail DAYS purge items from the mail queue that are older than DAYS days.
58 Defaults to 30 days if no days specified.
59 --merged purged completed entries from need_merge_authorities.
60 --import DAYS purge records from import tables older than DAYS days.
61 Defaults to 60 days if no days specified.
62 --z3950 purge records from import tables that are the result
64 --logs DAYS purge entries from action_logs older than DAYS days.
65 Defaults to 180 days if no days specified.
66 --searchhistory DAYS purge entries from search_history older than DAYS days.
67 Defaults to 30 days if no days specified
68 --list-invites DAYS purge (unaccepted) list share invites older than DAYS
69 days. Defaults to 14 days if no days specified.
75 $help, $sessions, $sess_days, $verbose,
76 $zebraqueue_days, $mail, $purge_merged, $pImport,
77 $pLogs, $pSearchhistory, $pZ3950,
83 'sessions' => \
$sessions,
84 'sessdays:i' => \
$sess_days,
85 'v|verbose' => \
$verbose,
87 'zebraqueue:i' => \
$zebraqueue_days,
88 'merged' => \
$purge_merged,
89 'import:i' => \
$pImport,
92 'searchhistory:i' => \
$pSearchhistory,
93 'list-invites:i' => \
$pListShareInvites,
96 $sessions=1 if $sess_days && $sess_days>0;
97 # if --import, --logs, --zebraqueue or --searchhistory were passed without number of days,
99 $pImport= DEFAULT_IMPORT_PURGEDAYS
if defined($pImport) && $pImport==0;
100 $pLogs= DEFAULT_LOGS_PURGEDAYS
if defined($pLogs) && $pLogs==0;
101 $zebraqueue_days= DEFAULT_ZEBRAQ_PURGEDAYS
if defined($zebraqueue_days) && $zebraqueue_days==0;
102 $mail= DEFAULT_MAIL_PURGEDAYS
if defined($mail) && $mail==0;
103 $pSearchhistory= DEFAULT_SEARCHHISTORY_PURGEDAYS
if defined($pSearchhistory) && $pSearchhistory==0;
104 $pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS
if defined($pListShareInvites) && $pListShareInvites == 0;
118 || $pListShareInvites )
120 print "You did not specify any cleanup work for the script to do.\n\n";
124 my $dbh = C4
::Context
->dbh();
130 if ( $sessions && !$sess_days ) {
132 print "Session purge triggered.\n";
133 $sth = $dbh->prepare("SELECT COUNT(*) FROM sessions");
134 $sth->execute() or die $dbh->errstr;
135 my @count_arr = $sth->fetchrow_array;
136 print "$count_arr[0] entries will be deleted.\n";
138 $sth = $dbh->prepare("TRUNCATE sessions");
139 $sth->execute() or die $dbh->errstr;
141 print "Done with session purge.\n";
143 } elsif ( $sessions && $sess_days > 0 ) {
145 print "Session purge triggered with days>$sess_days.\n";
149 print "Done with session purge with days>$sess_days.\n";
153 if ($zebraqueue_days) {
156 print "Zebraqueue purge triggered for $zebraqueue_days days.\n";
158 $sth = $dbh->prepare(
159 "SELECT id,biblio_auth_number,server,time FROM zebraqueue
160 WHERE done=1 and time < date_sub(curdate(), interval ? day)"
162 $sth->execute($zebraqueue_days) or die $dbh->errstr;
163 $sth2 = $dbh->prepare("DELETE FROM zebraqueue WHERE id=?");
164 while ( my $record = $sth->fetchrow_hashref ) {
165 $sth2->execute( $record->{id
} ) or die $dbh->errstr;
169 print "$count records were deleted.\nDone with zebraqueue purge.\n";
174 print "Mail queue purge triggered for $mail days.\n" if ($verbose);
176 $sth = $dbh->prepare("DELETE FROM message_queue WHERE time_queued < date_sub(curdate(), interval ? day)");
177 $sth->execute($mail) or die $dbh->errstr;
178 my $count = $sth->rows;
181 print "$count messages were deleted from the mail queue.\nDone with message_queue purge.\n" if ($verbose);
185 print "Purging completed entries from need_merge_authorities.\n" if $verbose;
186 $sth = $dbh->prepare("DELETE FROM need_merge_authorities WHERE done=1");
187 $sth->execute() or die $dbh->errstr;
188 print "Done with purging need_merge_authorities.\n" if $verbose;
192 print "Purging records from import tables.\n" if $verbose;
194 print "Done with purging import tables.\n" if $verbose;
198 print "Purging Z39.50 records from import tables.\n" if $verbose;
200 print "Done with purging Z39.50 records from import tables.\n" if $verbose;
204 print "Purging records from action_logs.\n" if $verbose;
205 $sth = $dbh->prepare("DELETE FROM action_logs WHERE timestamp < date_sub(curdate(), interval ? DAY)");
206 $sth->execute($pLogs) or die $dbh->errstr;
207 print "Done with purging action_logs.\n" if $verbose;
210 if($pSearchhistory) {
211 print "Purging records older than $pSearchhistory from search_history.\n" if $verbose;
212 PurgeSearchHistory
($pSearchhistory);
213 print "Done with purging search_history.\n" if $verbose;
216 if ($pListShareInvites) {
217 print "Purging unaccepted list share invites older than $pListShareInvites days.\n" if $verbose;
218 $sth = $dbh->prepare("
219 DELETE FROM virtualshelfshares
220 WHERE invitekey IS NOT NULL
221 AND (sharedate + INTERVAL ? DAY) < NOW()
223 $sth->execute($pListShareInvites);
224 print "Done with purging unaccepted list share invites.\n" if $verbose;
229 sub RemoveOldSessions
{
230 my ( $id, $a_session, $limit, $lasttime );
231 $limit = time() - 24 * 3600 * $sess_days;
233 $sth = $dbh->prepare("SELECT id, a_session FROM sessions");
234 $sth->execute or die $dbh->errstr;
235 $sth->bind_columns( \
$id, \
$a_session );
236 $sth2 = $dbh->prepare("DELETE FROM sessions WHERE id=?");
239 while ( $sth->fetch ) {
241 if ( $a_session =~ /lasttime:\s+'?(\d+)/ ) {
243 } elsif ( $a_session =~ /(ATIME|CTIME):\s+'?(\d+)/ ) {
246 if ( $lasttime && $lasttime < $limit ) {
247 $sth2->execute($id) or die $dbh->errstr;
252 print "$count sessions were deleted.\n";
256 sub PurgeImportTables
{
257 #First purge import_records
258 #Delete cascades to import_biblios, import_items and import_record_matches
259 $sth = $dbh->prepare("DELETE FROM import_records WHERE upload_timestamp < date_sub(curdate(), interval ? DAY)");
260 $sth->execute($pImport) or die $dbh->errstr;
262 # Now purge import_batches
263 # Timestamp cannot be used here without care, because records are added
264 # continuously to batches without updating timestamp (Z39.50 search).
265 # So we only delete older empty batches.
266 # This delete will therefore not have a cascading effect.
267 $sth = $dbh->prepare("DELETE ba
268 FROM import_batches ba
269 LEFT JOIN import_records re ON re.import_batch_id=ba.import_batch_id
270 WHERE re.import_record_id IS NULL AND
271 ba.upload_timestamp < date_sub(curdate(), interval ? DAY)");
272 $sth->execute($pImport) or die $dbh->errstr;
277 $sth = $dbh->prepare(q{
278 DELETE FROM import_batches WHERE batch_type = 'z3950'
280 $sth->execute() or die $dbh->errstr;