Bug 7298: (follow-up) fix uninitialized variable warning
[koha.git] / misc / cronjobs / cleanup_database.pl
index 4b6444f..c190a80 100755 (executable)
@@ -24,6 +24,7 @@ use constant DEFAULT_ZEBRAQ_PURGEDAYS => 30;
 use constant DEFAULT_MAIL_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
@@ -35,11 +36,13 @@ BEGIN {
 use C4::Context;
 use C4::Dates;
 
+use C4::Search;
+
 use Getopt::Long;
 
 sub usage {
     print STDERR <<USAGE;
-Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS]
+Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS]
 
    -h --help          prints this help message, and exits, ignoring all
                       other options
@@ -57,11 +60,13 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu
                       Defaults to 60 days if no days specified.
    --logs DAYS        purge entries from action_logs older than DAYS days.
                       Defaults to 180 days if no days specified.
+   --searchhistory DAYS  purge entries from search_history older than DAYS days.
+                         Defaults to 30 days if no days specified
 USAGE
     exit $_[0];
 }
 
-my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail, $purge_merged, $pImport, $pLogs);
+my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail, $purge_merged, $pImport, $pLogs, $pSearchhistory);
 
 GetOptions(
     'h|help'       => \$help,
@@ -73,21 +78,23 @@ GetOptions(
     '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;
 $mail= DEFAULT_MAIL_PURGEDAYS if defined($mail) && $mail==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);
 }
@@ -172,6 +179,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 {