Bug 10419: (followup) - tidy up cronjob for deleting patrons
authorJonathan Druart <jonathan.druart@biblibre.com>
Fri, 7 Jun 2013 13:51:43 +0000 (15:51 +0200)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 3 Oct 2013 21:41:14 +0000 (21:41 +0000)
This followup adds:
- execute flag (+x) for the cronjob script.
- replaces --dry-run with --confirm (according with existing scripts).
- changes output text (remove 'first person' style).
- updates the doc and simplifies the dates parameters.
- changes flags PrintError and RaiseError for the dbh in order to catch
  something if an error occurs...

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
misc/cronjobs/delete_patrons.pl [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index ad3c875..2d869f5
@@ -9,14 +9,14 @@ use C4::Members;
 use Koha::DateUtils;
 
 my ( $help, $verbose, $not_borrowered_since, $expired_before, $category_code,
-    $dryrun );
+    $confirm );
 GetOptions(
     'h|help'                 => \$help,
     'v|verbose'              => \$verbose,
     'not_borrowered_since:s' => \$not_borrowered_since,
     'expired_before:s'       => \$expired_before,
     'category_code:s'        => \$category_code,
-    'dry-run'                => \$dryrun,
+    'c|confirm'              => \$confirm,
 ) || pod2usage(1);
 
 if ($help) {
@@ -42,20 +42,25 @@ my $members = GetBorrowersToExpunge(
     }
 );
 
-say "I found " . scalar(@$members) . " patrons to delete";
+say scalar(@$members) . " patrons to delete";
+
+my $dbh = C4::Context->dbh;
+$dbh->{RaiseError} = 1;
+$dbh->{PrintError} = 0;
+
 for my $member (@$members) {
     print "Trying to delete patron " . $member->{borrowernumber} . "... ";
     eval {
         C4::Members::MoveMemberToDeleted( $member->{borrowernumber} )
-          unless $dryrun;
+          if $confirm;
     };
     if ($@) {
-        say "Failed, cannot move this patron ($@)";
+        say "Failed, cannot move this patron ($@)";
         next;
     }
-    eval { C4::Members::DelMember( $member->{borrowernumber} ) unless $dryrun; };
+    eval { C4::Members::DelMember( $member->{borrowernumber} ) if $confirm; };
     if ($@) {
-        say "Failed, cannot delete this patron ($@)";
+        say "Failed, cannot delete this patron ($@)";
         next;
     }
     say "OK";
@@ -67,7 +72,9 @@ delete_patrons - This script deletes patrons
 
 =head1 SYNOPSIS
 
-delete_patrons.pl [-h -v] --not_borrowered_since=`date -d '-3 month' "+%Y-%m-%d"` --expired_before=`date -d '-3 month' "+%Y-%m-%d"` --category_code=CAT
+delete_patrons.pl [-h -v -c] --not_borrowered_since=2013-07-21 --expired_before=2013-07-21 --category_code=CAT
+
+dates can be generated with `date -d '-3 month' "+%Y-%m-%d"`
 
 Options are cumulatives.
 
@@ -91,9 +98,9 @@ Delete patrons with an account expired before this date.
 
 Delete patrons who have this category code.
 
-=item B<--dry-run>
+=item B<-c|--confirm>
 
-Dry run mode. To use with the verbose mode.
+Without this flag set, this script will do nothing.
 
 =item B<-v|--verbose>