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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use constant DEFAULT_ZEBRAQ_PURGEDAYS => 30;
23 use constant DEFAULT_MAIL_PURGEDAYS => 30;
24 use constant DEFAULT_IMPORT_PURGEDAYS => 60;
25 use constant DEFAULT_LOGS_PURGEDAYS => 180;
26 use constant DEFAULT_SEARCHHISTORY_PURGEDAYS => 30;
27 use constant DEFAULT_SHARE_INVITATION_EXPIRY_DAYS => 14;
28 use constant DEFAULT_DEBARMENTS_PURGEDAYS => 30;
31 # find Koha's Perl modules
32 # test carefully before changing this
34 eval { require "$FindBin::Bin/../kohalib.pl" };
40 use C4::Search::History;
47 Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS]
49 -h --help prints this help message, and exits, ignoring all
51 --sessions purge the sessions table. If you use this while users
52 are logged into Koha, they will have to reconnect.
53 --sessdays DAYS purge only sessions older than DAYS days.
54 -v --verbose will cause the script to give you a bit more information
56 --zebraqueue DAYS purge completed zebraqueue entries older than DAYS days.
57 Defaults to 30 days if no days specified.
58 -m --mail DAYS purge items from the mail queue that are older than DAYS days.
59 Defaults to 30 days if no days specified.
60 --merged purged completed entries from need_merge_authorities.
61 --import DAYS purge records from import tables older than DAYS days.
62 Defaults to 60 days if no days specified.
63 --z3950 purge records from import tables that are the result
65 --fees DAYS purge entries accountlines older than DAYS days, where
66 amountoutstanding is 0 or NULL.
67 WARNING: Fees and payments may not be deleted together.
68 This will not affect the account balance but may be
70 --logs DAYS purge entries from action_logs older than DAYS days.
71 Defaults to 180 days if no days specified.
72 --searchhistory DAYS purge entries from search_history older than DAYS days.
73 Defaults to 30 days if no days specified
74 --list-invites DAYS purge (unaccepted) list share invites older than DAYS
75 days. Defaults to 14 days if no days specified.
76 --restrictions DAYS purge patrons restrictions expired since more than DAYS days.
77 Defaults to 30 days if no days specified.
78 --all-restrictions purge all expired patrons restrictions.
79 --del-exp-selfreg Delete expired self registration accounts
80 --del-unv-selfreg DAYS Delete unverified self registrations older than DAYS
103 $help, $sessions, $sess_days, $verbose, $zebraqueue_days,
104 $mail, $purge_merged, $pImport, $pLogs, $pSearchhistory,
105 $pZ3950, $pListShareInvites, $pDebarments, $allDebarments,
110 'sessions' => \$sessions,
111 'sessdays:i' => \$sess_days,
112 'v|verbose' => \$verbose,
113 'm|mail:i' => \$mail,
114 'zebraqueue:i' => \$zebraqueue_days,
115 'merged' => \$purge_merged,
116 'import:i' => \$pImport,
119 'fees:i' => \$fees_days,
120 'searchhistory:i' => \$pSearchhistory,
121 'list-invites:i' => \$pListShareInvites,
122 'restrictions:i' => \$pDebarments,
123 'all-restrictions' => \$allDebarments,
124 'del-exp-selfreg' => \$pExpSelfReg,
125 'del-unv-selfreg' => \$pUnvSelfReg,
129 $sessions = 1 if $sess_days && $sess_days > 0;
130 $pImport = DEFAULT_IMPORT_PURGEDAYS if defined($pImport) && $pImport == 0;
131 $pLogs = DEFAULT_LOGS_PURGEDAYS if defined($pLogs) && $pLogs == 0;
132 $zebraqueue_days = DEFAULT_ZEBRAQ_PURGEDAYS if defined($zebraqueue_days) && $zebraqueue_days == 0;
133 $mail = DEFAULT_MAIL_PURGEDAYS if defined($mail) && $mail == 0;
134 $pSearchhistory = DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhistory) && $pSearchhistory == 0;
135 $pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0;
136 $pDebarments = DEFAULT_DEBARMENTS_PURGEDAYS if defined($pDebarments) && $pDebarments == 0;
151 || $pListShareInvites
157 print "You did not specify any cleanup work for the script to do.\n\n";
161 if ($pDebarments && $allDebarments) {
162 print "You can not specify both --restrictions and --all-restrictions.\n\n";
168 my $dbh = C4::Context->dbh();
173 if ( $sessions && !$sess_days ) {
175 print "Session purge triggered.\n";
176 $sth = $dbh->prepare(q{ SELECT COUNT(*) FROM sessions });
177 $sth->execute() or die $dbh->errstr;
178 my @count_arr = $sth->fetchrow_array;
179 print "$count_arr[0] entries will be deleted.\n";
181 $sth = $dbh->prepare(q{ TRUNCATE sessions });
182 $sth->execute() or die $dbh->errstr;
184 print "Done with session purge.\n";
187 elsif ( $sessions && $sess_days > 0 ) {
188 print "Session purge triggered with days>$sess_days.\n" if $verbose;
190 print "Done with session purge with days>$sess_days.\n" if $verbose;
193 if ($zebraqueue_days) {
195 print "Zebraqueue purge triggered for $zebraqueue_days days.\n" if $verbose;
196 $sth = $dbh->prepare(
198 SELECT id,biblio_auth_number,server,time
200 WHERE done=1 AND time < date_sub(curdate(), INTERVAL ? DAY)
203 $sth->execute($zebraqueue_days) or die $dbh->errstr;
204 $sth2 = $dbh->prepare(q{ DELETE FROM zebraqueue WHERE id=? });
205 while ( my $record = $sth->fetchrow_hashref ) {
206 $sth2->execute( $record->{id} ) or die $dbh->errstr;
209 print "$count records were deleted.\nDone with zebraqueue purge.\n" if $verbose;
213 print "Mail queue purge triggered for $mail days.\n" if $verbose;
214 $sth = $dbh->prepare(
216 DELETE FROM message_queue
217 WHERE time_queued < date_sub(curdate(), INTERVAL ? DAY)
220 $sth->execute($mail) or die $dbh->errstr;
223 print "$count messages were deleted from the mail queue.\nDone with message_queue purge.\n" if $verbose;
227 print "Purging completed entries from need_merge_authorities.\n" if $verbose;
228 $sth = $dbh->prepare(q{ DELETE FROM need_merge_authorities WHERE done=1 });
229 $sth->execute() or die $dbh->errstr;
230 print "Done with purging need_merge_authorities.\n" if $verbose;
234 print "Purging records from import tables.\n" if $verbose;
236 print "Done with purging import tables.\n" if $verbose;
240 print "Purging Z39.50 records from import tables.\n" if $verbose;
242 print "Done with purging Z39.50 records from import tables.\n" if $verbose;
246 print "Purging records from action_logs.\n" if $verbose;
247 $sth = $dbh->prepare(
249 DELETE FROM action_logs
250 WHERE timestamp < date_sub(curdate(), INTERVAL ? DAY)
253 $sth->execute($pLogs) or die $dbh->errstr;
254 print "Done with purging action_logs.\n" if $verbose;
258 print "Purging records from accountlines.\n" if $verbose;
259 purge_zero_balance_fees( $fees_days );
260 print "Done purging records from accountlines.\n" if $verbose;
263 if ($pSearchhistory) {
264 print "Purging records older than $pSearchhistory from search_history.\n" if $verbose;
265 C4::Search::History::delete({ interval => $pSearchhistory });
266 print "Done with purging search_history.\n" if $verbose;
269 if ($pListShareInvites) {
270 print "Purging unaccepted list share invites older than $pListShareInvites days.\n" if $verbose;
271 $sth = $dbh->prepare(
273 DELETE FROM virtualshelfshares
274 WHERE invitekey IS NOT NULL
275 AND (sharedate + INTERVAL ? DAY) < NOW()
278 $sth->execute($pListShareInvites);
279 print "Done with purging unaccepted list share invites.\n" if $verbose;
283 print "Expired patrons restrictions purge triggered for $pDebarments days.\n" if $verbose;
284 $count = PurgeDebarments($pDebarments);
285 print "$count restrictions were deleted.\nDone with restrictions purge.\n" if $verbose;
289 print "All expired patrons restrictions purge triggered.\n" if $verbose;
290 $count = PurgeDebarments(0);
291 print "$count restrictions were deleted.\nDone with all restrictions purge.\n" if $verbose;
295 DeleteExpiredSelfRegs();
298 DeleteUnverifiedSelfRegs( $pUnvSelfReg );
303 sub RemoveOldSessions {
304 my ( $id, $a_session, $limit, $lasttime );
305 $limit = time() - 24 * 3600 * $sess_days;
307 $sth = $dbh->prepare(q{ SELECT id, a_session FROM sessions });
308 $sth->execute or die $dbh->errstr;
309 $sth->bind_columns( \$id, \$a_session );
310 $sth2 = $dbh->prepare(q{ DELETE FROM sessions WHERE id=? });
313 while ( $sth->fetch ) {
315 if ( $a_session =~ /lasttime:\s+'?(\d+)/ ) {
318 elsif ( $a_session =~ /(ATIME|CTIME):\s+'?(\d+)/ ) {
321 if ( $lasttime && $lasttime < $limit ) {
322 $sth2->execute($id) or die $dbh->errstr;
327 print "$count sessions were deleted.\n";
331 sub PurgeImportTables {
333 #First purge import_records
334 #Delete cascades to import_biblios, import_items and import_record_matches
335 $sth = $dbh->prepare(
337 DELETE FROM import_records
338 WHERE upload_timestamp < date_sub(curdate(), INTERVAL ? DAY)
341 $sth->execute($pImport) or die $dbh->errstr;
343 # Now purge import_batches
344 # Timestamp cannot be used here without care, because records are added
345 # continuously to batches without updating timestamp (Z39.50 search).
346 # So we only delete older empty batches.
347 # This delete will therefore not have a cascading effect.
348 $sth = $dbh->prepare(
351 FROM import_batches ba
352 LEFT JOIN import_records re ON re.import_batch_id=ba.import_batch_id
353 WHERE re.import_record_id IS NULL AND
354 ba.upload_timestamp < date_sub(curdate(), INTERVAL ? DAY)
357 $sth->execute($pImport) or die $dbh->errstr;
361 $sth = $dbh->prepare(
363 DELETE FROM import_batches
364 WHERE batch_type = 'z3950'
367 $sth->execute() or die $dbh->errstr;
370 sub PurgeDebarments {
371 require Koha::Borrower::Debarments;
374 $sth = $dbh->prepare(
376 SELECT borrower_debarment_id
377 FROM borrower_debarments
378 WHERE expiration < date_sub(curdate(), INTERVAL ? DAY)
381 $sth->execute($days) or die $dbh->errstr;
382 while ( my ($borrower_debarment_id) = $sth->fetchrow_array ) {
383 Koha::Borrower::Debarments::DelDebarment($borrower_debarment_id);
389 sub DeleteExpiredSelfRegs {
390 my $cnt= C4::Members::DeleteExpiredOpacRegistrations();
391 print "Removed $cnt expired self-registered borrowers\n" if $verbose;
394 sub DeleteUnverifiedSelfRegs {
395 my $cnt= C4::Members::DeleteUnverifiedOpacRegistrations( $_[0] );
396 print "Removed $cnt unverified self-registrations\n" if $verbose;