Bug 11715: require authentication for the cataloging Z39.50 search
[koha.git] / misc / cronjobs / cleanup_database.pl
blobfa27e9b7f21edb4263cc9baf4111b0967eef5be6
1 #!/usr/bin/perl
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
10 # version.
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.
20 use strict;
21 use warnings;
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;
30 BEGIN {
31 # find Koha's Perl modules
32 # test carefully before changing this
33 use FindBin;
34 eval { require "$FindBin::Bin/../kohalib.pl" };
37 use C4::Context;
38 use C4::Dates;
40 use C4::Search;
42 use Getopt::Long;
44 sub usage {
45 print STDERR <<USAGE;
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
49 other options
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
54 about the run.
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
63 of Z39.50 searches
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.
70 USAGE
71 exit $_[0];
74 my (
75 $help, $sessions, $sess_days, $verbose,
76 $zebraqueue_days, $mail, $purge_merged, $pImport,
77 $pLogs, $pSearchhistory, $pZ3950,
78 $pListShareInvites,
81 GetOptions(
82 'h|help' => \$help,
83 'sessions' => \$sessions,
84 'sessdays:i' => \$sess_days,
85 'v|verbose' => \$verbose,
86 'm|mail:i' => \$mail,
87 'zebraqueue:i' => \$zebraqueue_days,
88 'merged' => \$purge_merged,
89 'import:i' => \$pImport,
90 'z3950' => \$pZ3950,
91 'logs:i' => \$pLogs,
92 'searchhistory:i' => \$pSearchhistory,
93 'list-invites:i' => \$pListShareInvites,
94 ) || usage(1);
96 $sessions=1 if $sess_days && $sess_days>0;
97 # if --import, --logs, --zebraqueue or --searchhistory were passed without number of days,
98 # use defaults
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;
106 if ($help) {
107 usage(0);
110 unless ( $sessions
111 || $zebraqueue_days
112 || $mail
113 || $purge_merged
114 || $pImport
115 || $pLogs
116 || $pSearchhistory
117 || $pZ3950
118 || $pListShareInvites )
120 print "You did not specify any cleanup work for the script to do.\n\n";
121 usage(1);
124 my $dbh = C4::Context->dbh();
125 my $query;
126 my $sth;
127 my $sth2;
128 my $count;
130 if ( $sessions && !$sess_days ) {
131 if ($verbose) {
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;
140 if ($verbose) {
141 print "Done with session purge.\n";
143 } elsif ( $sessions && $sess_days > 0 ) {
144 if ($verbose) {
145 print "Session purge triggered with days>$sess_days.\n";
147 RemoveOldSessions();
148 if ($verbose) {
149 print "Done with session purge with days>$sess_days.\n";
153 if ($zebraqueue_days) {
154 $count = 0;
155 if ($verbose) {
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;
166 $count++;
168 if ($verbose) {
169 print "$count records were deleted.\nDone with zebraqueue purge.\n";
173 if ($mail) {
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;
179 $sth->finish;
181 print "$count messages were deleted from the mail queue.\nDone with message_queue purge.\n" if ($verbose);
184 if($purge_merged) {
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;
191 if($pImport) {
192 print "Purging records from import tables.\n" if $verbose;
193 PurgeImportTables();
194 print "Done with purging import tables.\n" if $verbose;
197 if($pZ3950) {
198 print "Purging Z39.50 records from import tables.\n" if $verbose;
199 PurgeZ3950();
200 print "Done with purging Z39.50 records from import tables.\n" if $verbose;
203 if($pLogs) {
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;
227 exit(0);
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=?");
237 $count = 0;
239 while ( $sth->fetch ) {
240 $lasttime = 0;
241 if ( $a_session =~ /lasttime:\s+'?(\d+)/ ) {
242 $lasttime = $1;
243 } elsif ( $a_session =~ /(ATIME|CTIME):\s+'?(\d+)/ ) {
244 $lasttime = $2;
246 if ( $lasttime && $lasttime < $limit ) {
247 $sth2->execute($id) or die $dbh->errstr;
248 $count++;
251 if ($verbose) {
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;
276 sub PurgeZ3950 {
277 $sth = $dbh->prepare(q{
278 DELETE FROM import_batches WHERE batch_type = 'z3950'
280 $sth->execute() or die $dbh->errstr;