Bug 21336: Adjust cleanup_database.pl
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Wed, 12 Sep 2018 12:25:26 +0000 (14:25 +0200)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 17 Apr 2019 12:25:24 +0000 (12:25 +0000)
Add the new Patron routines to this cron job. Actions are performed only if
preferences are set.

Note: No specific command line flags for these actions are added here
(and probably not needed too). So no crontab changes too.

Test plan:
Add a new patron.
Enable GDPR_Policy and refuse consent on OPAC for this patron.
Set only the first delay to zero (0) for immediate action.
Run cleanup_database.pl --logs (or any other flag) for the first time.
Check lock and expiration.
Set the second delay to zero (0) for immediate action.
Run cleanup_database.pl --logs for the second time. Check anonymization.
Set the third delay to zero (0) for immediate action.
Run cleanup_database.pl --logs for the third time. Check removal.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Amended: Added the warn $@ line in cleanup_database.pl

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Patrons.pm
misc/cronjobs/cleanup_database.pl

index 7da0655..b35e6cc 100644 (file)
@@ -210,7 +210,7 @@ sub anonymise_issue_history {
 
 =head3 delete
 
-    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
+    Koha::Patrons->search({ some filters here })->delete({ move => 1, verbose => 1 });
 
     Delete passed set of patron objects.
     Wrapper for Koha::Patron->delete. (We do not want to bypass Koha::Patron
@@ -227,11 +227,13 @@ sub delete {
     my $patrons_deleted;
     $self->_resultset->result_source->schema->txn_do( sub {
         my ( $set, $params ) = @_;
+        my $count = $set->count;
         while( my $patron = $set->next ) {
             $patron->move_to_deleted if $params->{move};
             $patron->delete == 1 || Koha::Exceptions::Patron::FailedDelete->throw;
             $patrons_deleted++;
         }
+        warn "Deleted $count patrons\n" if $params->{verbose};
     }, $self, $params );
     return $patrons_deleted;
 }
@@ -328,34 +330,44 @@ sub search_anonymized {
 
 =head3 lock
 
-    Koha::Patrons->search({ some filters })->lock({ expire => 1, remove => 1 })
+    Koha::Patrons->search({ some filters })->lock({ expire => 1, remove => 1, verbose => 1 })
 
     Lock the passed set of patron objects. Optionally expire and remove holds.
+    Optional verbose flag is used in cron job.
     Wrapper around Koha::Patron->lock.
 
 =cut
 
 sub lock {
     my ( $self, $params ) = @_;
+    my $count = $self->count;
     while( my $patron = $self->next ) {
         $patron->lock($params);
     }
+    if( $params->{verbose} ) {
+        warn "Locked $count patrons\n";
+    }
 }
 
 =head3 anonymize
 
-    Koha::Patrons->search({ some filters })->anonymize;
+    Koha::Patrons->search({ some filters })->anonymize({ verbose => 1 });
 
     Anonymize passed set of patron objects.
+    Optional verbose flag is used in cron job.
     Wrapper around Koha::Patron->anonymize.
 
 =cut
 
 sub anonymize {
-    my ( $self ) = @_;
+    my ( $self, $params ) = @_;
+    my $count = $self->count;
     while( my $patron = $self->next ) {
         $patron->anonymize;
     }
+    if( $params->{verbose} ) {
+        warn "Anonymized $count patrons\n";
+    }
 }
 
 =head3 _type
index 6ddb1ba..975aaab 100755 (executable)
@@ -305,6 +305,14 @@ if($allDebarments) {
     print "$count restrictions were deleted.\nDone with all restrictions purge.\n" if $verbose;
 }
 
+# Handle unsubscribe requests from GDPR consent form, depends on UnsubscribeReflectionDelay preference
+Koha::Patrons->search_unsubscribed->lock({ expire => 1, remove => 1, verbose => $verbose });
+# Anonymize patron data, depending on PatronAnonymizeDelay
+Koha::Patrons->search_anonymize_candidates({ locked => 1 })->anonymize({ verbose => $verbose });
+# Remove patron data, depending on PatronRemovalDelay (will raise an exception if problem encountered
+eval { Koha::Patrons->search_anonymized->delete({ move => 1, verbose => $verbose }) };
+warn $@ if $@;
+
 if( $pExpSelfReg ) {
     DeleteExpiredSelfRegs();
 }