From fb1122c0d8509162aab452089e7599df55c2ff82 Mon Sep 17 00:00:00 2001 From: Mirko Tietgen Date: Tue, 28 May 2013 14:33:59 +0200 Subject: [PATCH] Bug 10361 Add Option to cleanup_database.pl to purge search_history entries Add an option to cleanup_database.pl to purge the search_history entries older than X days. Revised version, moved DELETE FROM search_history to Search.pm Test plan: - Apply patch - Check that your test DB has some entries a little older than 30 days and a few ones even older than that in search_history: SELECT * FROM search_history WHERE time < DATE_SUB( NOW(), INTERVAL 30 DAY ); If not, modify some existing entries. - Run cleanup_database with a fixed number of days (replace XX with something higher than 30) /misc/cronjobs/cleanup_database.pl --searchhistory XX - check that entries older than XX days got deleted from search_history - run without the day parameter /misc/cronjobs/cleanup_database.pl --searchhistory - check that entries older than 30 days got deleted from search_history - Signoff --- C4/Search.pm | 9 ++++- misc/cronjobs/cleanup_database.pl | 71 ++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index aec024b..b923d85 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -71,7 +71,7 @@ This module provides searching functions for Koha's bibliographic databases &AddSearchHistory &GetDistinctValues &enabled_staff_search_views - &SimpleSearch + &PurgeSearchHistory ); # make all your functions, whether exported or not; @@ -2217,6 +2217,13 @@ sub GetSearchHistory{ return $sth->fetchall_hashref({}); } +sub PurgeSearchHistory{ + my ($pSearchhistory)=@_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("DELETE FROM search_history WHERE time < DATE_SUB( NOW(), INTERVAL ? DAY )"); + $sth->execute($pSearchhistory) or die $dbh->errstr; +} + =head2 z3950_search_args $arrayref = z3950_search_args($matchpoints) diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 99310df..f49e296 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -23,6 +23,7 @@ use warnings; use constant DEFAULT_ZEBRAQ_PURGEDAYS => 30; use constant DEFAULT_IMPORT_PURGEDAYS => 60; use constant DEFAULT_LOGS_PURGEDAYS => 180; +use constant DEFAULT_SEARCHHISTORY_PURGEDAYS => 30; BEGIN { # find Koha's Perl modules @@ -34,57 +35,63 @@ BEGIN { use C4::Context; use C4::Dates; +use C4::Search; + use Getopt::Long; sub usage { print STDERR < \$help, - 'sessions' => \$sessions, - 'sessdays:i' => \$sess_days, - 'v|verbose' => \$verbose, - 'm|mail' => \$mail, - 'zebraqueue:i' => \$zebraqueue_days, - 'merged' => \$purge_merged, - 'import:i' => \$pImport, - 'logs:i' => \$pLogs, + 'h|help' => \$help, + 'sessions' => \$sessions, + 'sessdays:i' => \$sess_days, + 'v|verbose' => \$verbose, + 'm|mail' => \$mail, + 'zebraqueue:i' => \$zebraqueue_days, + 'merged' => \$purge_merged, + 'import:i' => \$pImport, + 'logs:i' => \$pLogs, + 'searchhistory:i' => \$pSearchhistory, ) || usage(1); $sessions=1 if $sess_days && $sess_days>0; -# if --import, --logs or --zebraqueue were passed without number of days, +# if --import, --logs, --zebraqueue or --searchhistory were passed without number of days, # use defaults $pImport= DEFAULT_IMPORT_PURGEDAYS if defined($pImport) && $pImport==0; $pLogs= DEFAULT_LOGS_PURGEDAYS if defined($pLogs) && $pLogs==0; $zebraqueue_days= DEFAULT_ZEBRAQ_PURGEDAYS if defined($zebraqueue_days) && $zebraqueue_days==0; +$pSearchhistory= DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhistory) && $pSearchhistory==0; if ($help) { usage(0); } -if ( !( $sessions || $zebraqueue_days || $mail || $purge_merged || $pImport || $pLogs) ) { +if ( !( $sessions || $zebraqueue_days || $mail || $purge_merged || $pImport || $pLogs || $pSearchhistory ) ) { print "You did not specify any cleanup work for the script to do.\n\n"; usage(1); } @@ -170,6 +177,12 @@ if($pLogs) { print "Done with purging action_logs.\n" if $verbose; } +if($pSearchhistory) { + print "Purging records older than $pSearchhistory from search_history.\n" if $verbose; + PurgeSearchHistory($pSearchhistory); + print "Done with purging search_history.\n" if $verbose; +} + exit(0); sub RemoveOldSessions { -- 1.7.2.5