View | Details | Raw Unified | Return to bug 26831
Collapse All | Expand All

(-)a/misc/cronjobs/cleanup_database.pl (-157 / +167 lines)
Lines 172-178 my @jobs_types; Link Here
172
my $reports;
172
my $reports;
173
my $edifact_msg_days;
173
my $edifact_msg_days;
174
174
175
my $command_line_options = join(" ",@ARGV);
175
my $command_line_options = join( " ", @ARGV );
176
176
177
GetOptions(
177
GetOptions(
178
    'h|help'                     => \$help,
178
    'h|help'                     => \$help,
Lines 221-237 GetOptions( Link Here
221
) || usage(1);
221
) || usage(1);
222
222
223
# Use default values
223
# Use default values
224
$sessions          = 1                                    if $sess_days                  && $sess_days > 0;
224
$sessions         = 1                               if $sess_days                 && $sess_days > 0;
225
$pImport           = DEFAULT_IMPORT_PURGEDAYS             if defined($pImport)           && $pImport == 0;
225
$pImport          = DEFAULT_IMPORT_PURGEDAYS        if defined($pImport)          && $pImport == 0;
226
$pLogs             = DEFAULT_LOGS_PURGEDAYS               if defined($pLogs)             && $pLogs == 0;
226
$pLogs            = DEFAULT_LOGS_PURGEDAYS          if defined($pLogs)            && $pLogs == 0;
227
$zebraqueue_days   = DEFAULT_ZEBRAQ_PURGEDAYS             if defined($zebraqueue_days)   && $zebraqueue_days == 0;
227
$zebraqueue_days  = DEFAULT_ZEBRAQ_PURGEDAYS        if defined($zebraqueue_days)  && $zebraqueue_days == 0;
228
$mail              = DEFAULT_MAIL_PURGEDAYS               if defined($mail)              && $mail == 0;
228
$mail             = DEFAULT_MAIL_PURGEDAYS          if defined($mail)             && $mail == 0;
229
$pSearchhistory    = DEFAULT_SEARCHHISTORY_PURGEDAYS      if defined($pSearchhistory)    && $pSearchhistory == 0;
229
$pSearchhistory   = DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhistory)   && $pSearchhistory == 0;
230
$pDebarments       = DEFAULT_DEBARMENTS_PURGEDAYS         if defined($pDebarments)       && $pDebarments == 0;
230
$pDebarments      = DEFAULT_DEBARMENTS_PURGEDAYS    if defined($pDebarments)      && $pDebarments == 0;
231
$pMessages         = DEFAULT_MESSAGES_PURGEDAYS           if defined($pMessages)         && $pMessages == 0;
231
$pMessages        = DEFAULT_MESSAGES_PURGEDAYS      if defined($pMessages)        && $pMessages == 0;
232
$jobs_days         = DEFAULT_JOBS_PURGEDAYS               if defined($jobs_days)         && $jobs_days == 0;
232
$jobs_days        = DEFAULT_JOBS_PURGEDAYS          if defined($jobs_days)        && $jobs_days == 0;
233
@jobs_types        = (DEFAULT_JOBS_PURGETYPES)            if $jobs_days                  && @jobs_types == 0;
233
@jobs_types       = (DEFAULT_JOBS_PURGETYPES)       if $jobs_days                 && @jobs_types == 0;
234
$edifact_msg_days  = DEFAULT_EDIFACT_MSG_PURGEDAYS        if defined($edifact_msg_days)  && $edifact_msg_days == 0;
234
$edifact_msg_days = DEFAULT_EDIFACT_MSG_PURGEDAYS   if defined($edifact_msg_days) && $edifact_msg_days == 0;
235
235
236
# Choose the number of days at which to purge unaccepted list invites:
236
# Choose the number of days at which to purge unaccepted list invites:
237
# - DAYS defined in the list-invites parameter is prioritised first
237
# - DAYS defined in the list-invites parameter is prioritised first
Lines 283-302 unless ( $sessions Link Here
283
    || $return_claims
283
    || $return_claims
284
    || $jobs_days
284
    || $jobs_days
285
    || $reports
285
    || $reports
286
    || $edifact_msg_days
286
    || $edifact_msg_days )
287
) {
287
{
288
    print "You did not specify any cleanup work for the script to do.\n\n";
288
    print "You did not specify any cleanup work for the script to do.\n\n";
289
    usage(1);
289
    usage(1);
290
}
290
}
291
291
292
if ($pDebarments && $allDebarments) {
292
if ( $pDebarments && $allDebarments ) {
293
    print "You can not specify both --restrictions and --all-restrictions.\n\n";
293
    print "You can not specify both --restrictions and --all-restrictions.\n\n";
294
    usage(1);
294
    usage(1);
295
}
295
}
296
296
297
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
297
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
298
298
299
cronlogaction({ info => $command_line_options });
299
cronlogaction( { info => $command_line_options } );
300
300
301
my $dbh = C4::Context->dbh();
301
my $dbh = C4::Context->dbh();
302
my $sth;
302
my $sth;
Lines 310-326 if ( $sessions && !$sess_days ) { Link Here
310
        my @count_arr = $sth->fetchrow_array;
310
        my @count_arr = $sth->fetchrow_array;
311
        say $confirm ? "$count_arr[0] entries will be deleted." : "$count_arr[0] entries would be deleted.";
311
        say $confirm ? "$count_arr[0] entries will be deleted." : "$count_arr[0] entries would be deleted.";
312
    }
312
    }
313
    if ( $confirm ) {
313
    if ($confirm) {
314
        $sth = $dbh->prepare(q{ TRUNCATE sessions });
314
        $sth = $dbh->prepare(q{ TRUNCATE sessions });
315
        $sth->execute() or die $dbh->errstr;
315
        $sth->execute() or die $dbh->errstr;
316
    }
316
    }
317
    if ($verbose) {
317
    if ($verbose) {
318
        print "Done with session purge.\n";
318
        print "Done with session purge.\n";
319
    }
319
    }
320
}
320
} elsif ( $sessions && $sess_days > 0 ) {
321
elsif ( $sessions && $sess_days > 0 ) {
322
    print "Session purge triggered with days>$sess_days.\n" if $verbose;
321
    print "Session purge triggered with days>$sess_days.\n" if $verbose;
323
    RemoveOldSessions() if $confirm;
322
    RemoveOldSessions()                                     if $confirm;
324
    print "Done with session purge with days>$sess_days.\n" if $verbose;
323
    print "Done with session purge with days>$sess_days.\n" if $verbose;
325
}
324
}
326
325
Lines 337-348 if ($zebraqueue_days) { Link Here
337
    $sth->execute($zebraqueue_days) or die $dbh->errstr;
336
    $sth->execute($zebraqueue_days) or die $dbh->errstr;
338
    $sth2 = $dbh->prepare(q{ DELETE FROM zebraqueue WHERE id=? });
337
    $sth2 = $dbh->prepare(q{ DELETE FROM zebraqueue WHERE id=? });
339
    while ( my $record = $sth->fetchrow_hashref ) {
338
    while ( my $record = $sth->fetchrow_hashref ) {
340
        if ( $confirm ) {
339
        if ($confirm) {
341
            $sth2->execute( $record->{id} ) or die $dbh->errstr;
340
            $sth2->execute( $record->{id} ) or die $dbh->errstr;
342
        }
341
        }
343
        $count++;
342
        $count++;
344
    }
343
    }
345
    if ( $verbose ) {
344
    if ($verbose) {
346
        say $confirm ? "$count records were deleted." : "$count records would have been deleted.";
345
        say $confirm ? "$count records were deleted." : "$count records would have been deleted.";
347
        say "Done with zebraqueue purge.";
346
        say "Done with zebraqueue purge.";
348
    }
347
    }
Lines 357-375 if ($mail) { Link Here
357
            WHERE time_queued < date_sub(curdate(), INTERVAL ? DAY)
356
            WHERE time_queued < date_sub(curdate(), INTERVAL ? DAY)
358
        }
357
        }
359
    );
358
    );
360
    if ( $confirm ) {
359
    if ($confirm) {
361
        $sth->execute($mail) or die $dbh->errstr;
360
        $sth->execute($mail) or die $dbh->errstr;
362
        $count = $sth->rows;
361
        $count = $sth->rows;
363
    }
362
    }
364
    if ( $verbose ) {
363
    if ($verbose) {
365
        say $confirm ? "$count messages were deleted from the mail queue." : "Message from message_queue would have been deleted";
364
        say $confirm
365
            ? "$count messages were deleted from the mail queue."
366
            : "Message from message_queue would have been deleted";
366
        say "Done with message_queue purge.";
367
        say "Done with message_queue purge.";
367
    }
368
    }
368
}
369
}
369
370
370
if ($purge_merged) {
371
if ($purge_merged) {
371
    print "Purging completed entries from need_merge_authorities.\n" if $verbose;
372
    print "Purging completed entries from need_merge_authorities.\n" if $verbose;
372
    if ( $confirm ) {
373
    if ($confirm) {
373
        $sth = $dbh->prepare(q{ DELETE FROM need_merge_authorities WHERE done=1 });
374
        $sth = $dbh->prepare(q{ DELETE FROM need_merge_authorities WHERE done=1 });
374
        $sth->execute() or die $dbh->errstr;
375
        $sth->execute() or die $dbh->errstr;
375
    }
376
    }
Lines 378-390 if ($purge_merged) { Link Here
378
379
379
if ($pImport) {
380
if ($pImport) {
380
    print "Purging records from import tables.\n" if $verbose;
381
    print "Purging records from import tables.\n" if $verbose;
381
    PurgeImportTables() if $confirm;
382
    PurgeImportTables()                           if $confirm;
382
    print "Done with purging import tables.\n" if $verbose;
383
    print "Done with purging import tables.\n"    if $verbose;
383
}
384
}
384
385
385
if ($pZ3950) {
386
if ($pZ3950) {
386
    print "Purging Z39.50 records from import tables.\n" if $verbose;
387
    print "Purging Z39.50 records from import tables.\n"           if $verbose;
387
    PurgeZ3950() if $confirm;
388
    PurgeZ3950()                                                   if $confirm;
388
    print "Done with purging Z39.50 records from import tables.\n" if $verbose;
389
    print "Done with purging Z39.50 records from import tables.\n" if $verbose;
389
}
390
}
390
391
Lines 395-415 if ($pLogs) { Link Here
395
            WHERE timestamp < date_sub(curdate(), INTERVAL ? DAY)
396
            WHERE timestamp < date_sub(curdate(), INTERVAL ? DAY)
396
    };
397
    };
397
    my @query_params = ();
398
    my @query_params = ();
398
    if( @preserve_logs ){
399
    if (@preserve_logs) {
399
        $log_query .= " AND module NOT IN (" . join(',',('?') x @preserve_logs ) . ")";
400
        $log_query .= " AND module NOT IN (" . join( ',', ('?') x @preserve_logs ) . ")";
400
        push @query_params, @preserve_logs;
401
        push @query_params, @preserve_logs;
401
    }
402
    }
402
    if( @log_modules ){
403
    if (@log_modules) {
403
        $log_query .= " AND module IN (" . join(',',('?') x @log_modules ) . ")";
404
        $log_query .= " AND module IN (" . join( ',', ('?') x @log_modules ) . ")";
404
        push @query_params, @log_modules;
405
        push @query_params, @log_modules;
405
    }
406
    }
406
    if( @log_actions ){
407
    if (@log_actions) {
407
        $log_query .= " AND action IN (" . join(',',('?') x @log_actions ) . ")";
408
        $log_query .= " AND action IN (" . join( ',', ('?') x @log_actions ) . ")";
408
        push @query_params, @log_actions;
409
        push @query_params, @log_actions;
409
    }
410
    }
410
    $sth = $dbh->prepare( $log_query );
411
    $sth = $dbh->prepare($log_query);
411
    if ( $confirm ) {
412
    if ($confirm) {
412
        $sth->execute($pLogs, @query_params) or die $dbh->errstr;
413
        $sth->execute( $pLogs, @query_params ) or die $dbh->errstr;
413
    }
414
    }
414
    print "Done with purging action_logs.\n" if $verbose;
415
    print "Done with purging action_logs.\n" if $verbose;
415
}
416
}
Lines 420-442 if ($pMessages) { Link Here
420
        { timestamp_column_name => 'message_date', days => $pMessages } );
421
        { timestamp_column_name => 'message_date', days => $pMessages } );
421
    my $count = $messages->count;
422
    my $count = $messages->count;
422
    $messages->delete if $confirm;
423
    $messages->delete if $confirm;
423
    if ( $verbose ) {
424
    if ($verbose) {
424
        say $confirm
425
        say $confirm
425
          ? sprintf( "Done with purging %d messages", $count )
426
            ? sprintf( "Done with purging %d messages",       $count )
426
          : sprintf( "%d messages would have been removed", $count );
427
            : sprintf( "%d messages would have been removed", $count );
427
    }
428
    }
428
}
429
}
429
430
430
if ($fees_days) {
431
if ($fees_days) {
431
    print "Purging records from accountlines.\n" if $verbose;
432
    print "Purging records from accountlines.\n"      if $verbose;
432
    purge_zero_balance_fees( $fees_days ) if $confirm;
433
    purge_zero_balance_fees($fees_days)               if $confirm;
433
    print "Done purging records from accountlines.\n" if $verbose;
434
    print "Done purging records from accountlines.\n" if $verbose;
434
}
435
}
435
436
436
if ($pSearchhistory) {
437
if ($pSearchhistory) {
437
    print "Purging records older than $pSearchhistory from search_history.\n" if $verbose;
438
    print "Purging records older than $pSearchhistory from search_history.\n" if $verbose;
438
    C4::Search::History::delete({ interval => $pSearchhistory }) if $confirm;
439
    C4::Search::History::delete( { interval => $pSearchhistory } )            if $confirm;
439
    print "Done with purging search_history.\n" if $verbose;
440
    print "Done with purging search_history.\n"                               if $verbose;
440
}
441
}
441
442
442
if ($pListShareInvites) {
443
if ($pListShareInvites) {
Lines 448-454 if ($pListShareInvites) { Link Here
448
            AND (sharedate + INTERVAL ? DAY) < NOW()
449
            AND (sharedate + INTERVAL ? DAY) < NOW()
449
        }
450
        }
450
    );
451
    );
451
    if ( $confirm ) {
452
    if ($confirm) {
452
        $sth->execute($pListShareInvites);
453
        $sth->execute($pListShareInvites);
453
    }
454
    }
454
    print "Done with purging unaccepted list share invites.\n" if $verbose;
455
    print "Done with purging unaccepted list share invites.\n" if $verbose;
Lines 456-563 if ($pListShareInvites) { Link Here
456
457
457
if ($pDebarments) {
458
if ($pDebarments) {
458
    print "Expired patrons restrictions purge triggered for $pDebarments days.\n" if $verbose;
459
    print "Expired patrons restrictions purge triggered for $pDebarments days.\n" if $verbose;
459
    my $count = PurgeDebarments($pDebarments, $confirm);
460
    my $count = PurgeDebarments( $pDebarments, $confirm );
460
    if ( $verbose ) {
461
    if ($verbose) {
461
        say $confirm ? "$count restrictions were deleted." : "$count restrictions would have been deleted";
462
        say $confirm ? "$count restrictions were deleted." : "$count restrictions would have been deleted";
462
        say "Done with restrictions purge.";
463
        say "Done with restrictions purge.";
463
    }
464
    }
464
}
465
}
465
466
466
if($allDebarments) {
467
if ($allDebarments) {
467
    print "All expired patrons restrictions purge triggered.\n" if $verbose;
468
    print "All expired patrons restrictions purge triggered.\n" if $verbose;
468
    my $count = PurgeDebarments(0, $confirm);
469
    my $count = PurgeDebarments( 0, $confirm );
469
    if ( $verbose ) {
470
    if ($verbose) {
470
        say $confirm ? "$count restrictions were deleted." : "$count restrictions would have been deleted";
471
        say $confirm ? "$count restrictions were deleted." : "$count restrictions would have been deleted";
471
        say "Done with all restrictions purge.";
472
        say "Done with all restrictions purge.";
472
    }
473
    }
473
}
474
}
474
475
475
# Lock expired patrons?
476
# Lock expired patrons?
476
if( defined $lock_days && $lock_days ne q{} ) {
477
if ( defined $lock_days && $lock_days ne q{} ) {
477
    say "Start locking expired patrons" if $verbose;
478
    say "Start locking expired patrons" if $verbose;
478
    my $expired_patrons = Koha::Patrons->filter_by_expiration_date({ days => $lock_days })->search({ login_attempts => { '!=' => -1 } });
479
    my $expired_patrons = Koha::Patrons->filter_by_expiration_date( { days => $lock_days } )
480
        ->search( { login_attempts => { '!=' => -1 } } );
479
    my $count = $expired_patrons->count;
481
    my $count = $expired_patrons->count;
480
    $expired_patrons->lock({ remove => 1 }) if $confirm;
482
    $expired_patrons->lock( { remove => 1 } ) if $confirm;
481
    if( $verbose ) {
483
    if ($verbose) {
482
        say $confirm ? sprintf("Locked %d patrons", $count) : sprintf("Found %d patrons", $count);
484
        say $confirm ? sprintf( "Locked %d patrons", $count ) : sprintf( "Found %d patrons", $count );
483
    }
485
    }
484
}
486
}
485
487
486
# Handle unsubscribe requests from GDPR consent form, depends on UnsubscribeReflectionDelay preference
488
# Handle unsubscribe requests from GDPR consent form, depends on UnsubscribeReflectionDelay preference
487
say "Start lock unsubscribed, anonymize and delete" if $verbose;
489
say "Start lock unsubscribed, anonymize and delete" if $verbose;
488
my $unsubscribed_patrons = Koha::Patrons->search_unsubscribed;
490
my $unsubscribed_patrons = Koha::Patrons->search_unsubscribed;
489
my $count = $unsubscribed_patrons->count;
491
my $count                = $unsubscribed_patrons->count;
490
$unsubscribed_patrons->lock( { expire => 1, remove => 1 } ) if $confirm;
492
$unsubscribed_patrons->lock( { expire => 1, remove => 1 } ) if $confirm;
491
say $confirm ? sprintf("Locked %d patrons", $count) : sprintf("%d patrons would have been locked", $count) if $verbose;
493
say $confirm ? sprintf( "Locked %d patrons", $count ) : sprintf( "%d patrons would have been locked", $count )
494
    if $verbose;
492
495
493
# Anonymize patron data, depending on PatronAnonymizeDelay
496
# Anonymize patron data, depending on PatronAnonymizeDelay
494
my $anonymize_candidates = Koha::Patrons->search_anonymize_candidates( { locked => 1 } );
497
my $anonymize_candidates = Koha::Patrons->search_anonymize_candidates( { locked => 1 } );
495
$count = $anonymize_candidates->count;
498
$count = $anonymize_candidates->count;
496
$anonymize_candidates->anonymize if $confirm;
499
$anonymize_candidates->anonymize if $confirm;
497
say $confirm ? sprintf("Anonymized %d patrons", $count) : sprintf("%d patrons would have been anonymized", $count) if $verbose;
500
say $confirm ? sprintf( "Anonymized %d patrons", $count ) : sprintf( "%d patrons would have been anonymized", $count )
501
    if $verbose;
498
502
499
# Remove patron data, depending on PatronRemovalDelay (will raise an exception if problem encountered
503
# Remove patron data, depending on PatronRemovalDelay (will raise an exception if problem encountered
500
my $anonymized_patrons = Koha::Patrons->search_anonymized;
504
my $anonymized_patrons = Koha::Patrons->search_anonymized;
501
$count = $anonymized_patrons->count;
505
$count = $anonymized_patrons->count;
502
if ( $confirm ) {
506
if ($confirm) {
503
    $anonymized_patrons->delete( { move => 1 } );
507
    $anonymized_patrons->delete( { move => 1 } );
504
    if ($@) {
508
    if ($@) {
505
        warn $@;
509
        warn $@;
506
    }
510
    }
507
}
511
}
508
if ($verbose) {
512
if ($verbose) {
509
    say $confirm ? sprintf("Deleted %d patrons", $count) : sprintf("%d patrons would have been deleted", $count);
513
    say $confirm ? sprintf( "Deleted %d patrons", $count ) : sprintf( "%d patrons would have been deleted", $count );
510
}
514
}
511
515
512
# FIXME The output for dry-run mode needs to be improved
516
# FIXME The output for dry-run mode needs to be improved
513
# But non trivial changes to C4::Members need to be done before.
517
# But non trivial changes to C4::Members need to be done before.
514
if( $pExpSelfReg ) {
518
if ($pExpSelfReg) {
515
    if ( $confirm ) {
519
    if ($confirm) {
516
        DeleteExpiredSelfRegs();
520
        DeleteExpiredSelfRegs();
517
    } elsif ( $verbose ) {
521
    } elsif ($verbose) {
518
        say "self-registered borrowers may be deleted";
522
        say "self-registered borrowers may be deleted";
519
    }
523
    }
520
}
524
}
521
if( $pUnvSelfReg ) {
525
if ($pUnvSelfReg) {
522
    if ( $confirm ) {
526
    if ($confirm) {
523
        DeleteUnverifiedSelfRegs( $pUnvSelfReg );
527
        DeleteUnverifiedSelfRegs($pUnvSelfReg);
524
    } elsif ( $verbose ) {
528
    } elsif ($verbose) {
525
        say "unverified self-registrations may be deleted";
529
        say "unverified self-registrations may be deleted";
526
    }
530
    }
527
}
531
}
528
532
529
if ($special_holidays_days) {
533
if ($special_holidays_days) {
530
    if ( $confirm ) {
534
    if ($confirm) {
531
        DeleteSpecialHolidays( abs($special_holidays_days) );
535
        DeleteSpecialHolidays( abs($special_holidays_days) );
532
    } elsif ( $verbose ) {
536
    } elsif ($verbose) {
533
        say "self-registered borrowers may be deleted";
537
        say "self-registered borrowers may be deleted";
534
    }
538
    }
535
}
539
}
536
540
537
if( $temp_uploads ) {
541
if ($temp_uploads) {
542
538
    # Delete temporary uploads, governed by a pref (unless you override)
543
    # Delete temporary uploads, governed by a pref (unless you override)
539
    print "Purging temporary uploads.\n" if $verbose;
544
    print "Purging temporary uploads.\n" if $verbose;
540
    if ( $confirm ) {
545
    if ($confirm) {
541
        Koha::UploadedFiles->delete_temporary({
546
        Koha::UploadedFiles->delete_temporary(
542
            defined($temp_uploads_days)
547
            {
548
                defined($temp_uploads_days)
543
                ? ( override_pref => $temp_uploads_days )
549
                ? ( override_pref => $temp_uploads_days )
544
                : ()
550
                : ()
545
        });
551
            }
552
        );
546
    }
553
    }
547
    print "Done purging temporary uploads.\n" if $verbose;
554
    print "Done purging temporary uploads.\n" if $verbose;
548
}
555
}
549
556
550
if( defined $uploads_missing ) {
557
if ( defined $uploads_missing ) {
551
    print "Looking for missing uploads\n" if $verbose;
558
    print "Looking for missing uploads\n" if $verbose;
552
    if ( $confirm ) {
559
    if ($confirm) {
553
        my $keep = $uploads_missing == 1 ? 0 : 1;
560
        my $keep  = $uploads_missing == 1 ? 0 : 1;
554
        my $count = Koha::UploadedFiles->delete_missing({ keep_record => $keep });
561
        my $count = Koha::UploadedFiles->delete_missing( { keep_record => $keep } );
555
        if( $keep ) {
562
        if ($keep) {
556
            print "Counted $count missing uploaded files\n";
563
            print "Counted $count missing uploaded files\n";
557
        } else {
564
        } else {
558
            print "Removed $count records for missing uploads\n";
565
            print "Removed $count records for missing uploads\n";
559
        }
566
        }
560
    } else {
567
    } else {
568
561
        # FIXME need to create a filter_by_missing method (then call ->delete) instead of delete_missing
569
        # FIXME need to create a filter_by_missing method (then call ->delete) instead of delete_missing
562
        say "Dry-run mode cannot guess how many uploads would have been deleted";
570
        say "Dry-run mode cannot guess how many uploads would have been deleted";
563
    }
571
    }
Lines 566-601 if( defined $uploads_missing ) { Link Here
566
if ($oauth_tokens) {
574
if ($oauth_tokens) {
567
    require Koha::OAuthAccessTokens;
575
    require Koha::OAuthAccessTokens;
568
576
569
    my $tokens = Koha::OAuthAccessTokens->search({ expires => { '<=', time } });
577
    my $tokens = Koha::OAuthAccessTokens->search( { expires => { '<=', time } } );
570
    my $count = $tokens->count;
578
    my $count  = $tokens->count;
571
    $tokens->delete if $confirm;
579
    $tokens->delete if $confirm;
572
    if ( $verbose ) {
580
    if ($verbose) {
573
        say $confirm
581
        say $confirm
574
          ? sprintf( "Removed %d expired OAuth2 tokens", $count )
582
            ? sprintf( "Removed %d expired OAuth2 tokens",                $count )
575
          : sprintf( "%d expired OAuth tokens would have been removed", $count );
583
            : sprintf( "%d expired OAuth tokens would have been removed", $count );
576
    }
584
    }
577
}
585
}
578
586
579
if ($pStatistics) {
587
if ($pStatistics) {
580
    print "Purging statistics older than $pStatistics days.\n" if $verbose;
588
    print "Purging statistics older than $pStatistics days.\n" if $verbose;
581
    my $statistics = Koha::Statistics->filter_by_last_update(
589
    my $statistics =
582
        { timestamp_column_name => 'datetime', days => $pStatistics } );
590
        Koha::Statistics->filter_by_last_update( { timestamp_column_name => 'datetime', days => $pStatistics } );
583
    my $count = $statistics->count;
591
    my $count = $statistics->count;
584
    $statistics->delete if $confirm;
592
    $statistics->delete if $confirm;
585
    if ( $verbose ) {
593
    if ($verbose) {
586
        say $confirm
594
        say $confirm
587
          ? sprintf( "Done with purging %d statistics", $count )
595
            ? sprintf( "Done with purging %d statistics",       $count )
588
          : sprintf( "%d statistics would have been removed", $count );
596
            : sprintf( "%d statistics would have been removed", $count );
589
    }
597
    }
590
}
598
}
591
599
592
if( $return_claims && ( my $days = C4::Context->preference('CleanUpDatabaseReturnClaims') )) {
600
if ( $return_claims && ( my $days = C4::Context->preference('CleanUpDatabaseReturnClaims') ) ) {
593
    print "Purging return claims older than $days days.\n" if $verbose;
601
    print "Purging return claims older than $days days.\n" if $verbose;
594
602
595
    $return_claims = Koha::Checkouts::ReturnClaims->filter_by_last_update(
603
    $return_claims = Koha::Checkouts::ReturnClaims->filter_by_last_update(
596
        {
604
        {
597
            timestamp_column_name => 'resolved_on',
605
            timestamp_column_name => 'resolved_on',
598
            days => $days,
606
            days                  => $days,
599
        }
607
        }
600
    );
608
    );
601
609
Lines 611-622 if( $return_claims && ( my $days = C4::Context->preference('CleanUpDatabaseRetur Link Here
611
619
612
if ($pDeletedCatalog) {
620
if ($pDeletedCatalog) {
613
    print "Purging deleted catalog older than $pDeletedCatalog days.\n"
621
    print "Purging deleted catalog older than $pDeletedCatalog days.\n"
614
      if $verbose;
622
        if $verbose;
615
    my $old_items = Koha::Old::Items->filter_by_last_update( { days => $pDeletedCatalog } );
623
    my $old_items       = Koha::Old::Items->filter_by_last_update( { days => $pDeletedCatalog } );
616
    my $old_biblioitems = Koha::Old::Biblioitems->filter_by_last_update( { days => $pDeletedCatalog } );
624
    my $old_biblioitems = Koha::Old::Biblioitems->filter_by_last_update( { days => $pDeletedCatalog } );
617
    my $old_biblios = Koha::Old::Biblios->filter_by_last_update( { days => $pDeletedCatalog } );
625
    my $old_biblios     = Koha::Old::Biblios->filter_by_last_update( { days => $pDeletedCatalog } );
618
    my ( $c_i, $c_bi, $c_b ) =
626
    my ( $c_i, $c_bi, $c_b ) = ( $old_items->count, $old_biblioitems->count, $old_biblios->count );
619
      ( $old_items->count, $old_biblioitems->count, $old_biblios->count );
620
    if ($confirm) {
627
    if ($confirm) {
621
        $old_items->delete;
628
        $old_items->delete;
622
        $old_biblioitems->delete;
629
        $old_biblioitems->delete;
Lines 627-633 if ($pDeletedCatalog) { Link Here
627
            $confirm
634
            $confirm
628
            ? "Done with purging deleted catalog (%d items, %d biblioitems, %d biblios)."
635
            ? "Done with purging deleted catalog (%d items, %d biblioitems, %d biblios)."
629
            : "Deleted catalog would have been removed (%d items, %d biblioitems, %d biblios).",
636
            : "Deleted catalog would have been removed (%d items, %d biblioitems, %d biblios).",
630
        $c_i, $c_bi, $c_b);
637
            $c_i, $c_bi, $c_b
638
        );
631
    }
639
    }
632
}
640
}
633
641
Lines 639-670 if ($pDeletedPatrons) { Link Here
639
    $old_patrons->delete if $confirm;
647
    $old_patrons->delete if $confirm;
640
    if ($verbose) {
648
    if ($verbose) {
641
        say $confirm
649
        say $confirm
642
          ? sprintf "Done with purging %d deleted patrons.", $count
650
            ? sprintf "Done with purging %d deleted patrons.", $count
643
          : sprintf "%d deleted patrons would have been purged.", $count;
651
            : sprintf "%d deleted patrons would have been purged.", $count;
644
    }
652
    }
645
}
653
}
646
654
647
if ($pOldIssues) {
655
if ($pOldIssues) {
648
    print "Purging old checkouts older than $pOldIssues days.\n" if $verbose;
656
    print "Purging old checkouts older than $pOldIssues days.\n" if $verbose;
649
    my $old_checkouts = Koha::Old::Checkouts->filter_by_last_update( { days => $pOldIssues } );
657
    my $old_checkouts = Koha::Old::Checkouts->filter_by_last_update( { days => $pOldIssues } );
650
    my $count = $old_checkouts->count;
658
    my $count         = $old_checkouts->count;
651
    $old_checkouts->delete if $confirm;
659
    $old_checkouts->delete if $confirm;
652
    if ($verbose) {
660
    if ($verbose) {
653
        say $confirm
661
        say $confirm
654
          ? sprintf "Done with purging %d old checkouts.", $count
662
            ? sprintf "Done with purging %d old checkouts.", $count
655
          : sprintf "%d old checkouts would have been purged.", $count;
663
            : sprintf "%d old checkouts would have been purged.", $count;
656
    }
664
    }
657
}
665
}
658
666
659
if ($pOldReserves) {
667
if ($pOldReserves) {
660
    print "Purging old reserves older than $pOldReserves days.\n" if $verbose;
668
    print "Purging old reserves older than $pOldReserves days.\n" if $verbose;
661
    my $old_reserves = Koha::Old::Holds->filter_by_last_update( { days => $pOldReserves } );
669
    my $old_reserves = Koha::Old::Holds->filter_by_last_update( { days => $pOldReserves } );
662
    my $count = $old_reserves->count;
670
    my $count        = $old_reserves->count;
663
    $old_reserves->delete if $confirm;
671
    $old_reserves->delete if $confirm;
664
    if ($verbose) {
672
    if ($verbose) {
665
        say $confirm
673
        say $confirm
666
          ? sprintf "Done with purging %d old reserves.", $count
674
            ? sprintf "Done with purging %d old reserves.", $count
667
          : sprintf "%d old reserves would have been purged.", $count;
675
            : sprintf "%d old reserves would have been purged.", $count;
668
    }
676
    }
669
}
677
}
670
678
Lines 673-691 if ($pTransfers) { Link Here
673
    my $transfers = Koha::Item::Transfers->filter_by_last_update(
681
    my $transfers = Koha::Item::Transfers->filter_by_last_update(
674
        {
682
        {
675
            timestamp_column_name => 'datearrived',
683
            timestamp_column_name => 'datearrived',
676
            days => $pTransfers,
684
            days                  => $pTransfers,
677
        }
685
        }
678
    );
686
    );
679
    my $count = $transfers->count;
687
    my $count = $transfers->count;
680
    $transfers->delete if $confirm;
688
    $transfers->delete if $confirm;
681
    if ($verbose) {
689
    if ($verbose) {
682
        say $confirm
690
        say $confirm
683
          ? sprintf "Done with purging %d transfers.", $count
691
            ? sprintf "Done with purging %d transfers.", $count
684
          : sprintf "%d transfers would have been purged.", $count;
692
            : sprintf "%d transfers would have been purged.", $count;
685
    }
693
    }
686
}
694
}
687
695
688
if (defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransactionsTo ) {
696
if ( defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransactionsTo ) {
689
    print "Purging pseudonymized transactions\n" if $verbose;
697
    print "Purging pseudonymized transactions\n" if $verbose;
690
    if ($pPseudoTransactionsTo) {
698
    if ($pPseudoTransactionsTo) {
691
        $pPseudoTransactionsTo = dt_from_string($pPseudoTransactionsTo);
699
        $pPseudoTransactionsTo = dt_from_string($pPseudoTransactionsTo);
Lines 696-738 if (defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransact Link Here
696
    my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update(
704
    my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update(
697
        {
705
        {
698
            timestamp_column_name => 'datetime',
706
            timestamp_column_name => 'datetime',
699
            ( defined $pPseudoTransactions  ? ( days => $pPseudoTransactions     ) : () ),
707
            ( defined $pPseudoTransactions ? ( days => $pPseudoTransactions )     : () ),
700
            ( $pPseudoTransactionsFrom      ? ( from => $pPseudoTransactionsFrom ) : () ),
708
            ( $pPseudoTransactionsFrom     ? ( from => $pPseudoTransactionsFrom ) : () ),
701
            ( $pPseudoTransactionsTo        ? ( to   => $pPseudoTransactionsTo   ) : () ),
709
            ( $pPseudoTransactionsTo       ? ( to   => $pPseudoTransactionsTo )   : () ),
702
        }
710
        }
703
    );
711
    );
704
    my $count = $anonymized_transactions->count;
712
    my $count = $anonymized_transactions->count;
705
    $anonymized_transactions->delete if $confirm;
713
    $anonymized_transactions->delete if $confirm;
706
    if ($verbose) {
714
    if ($verbose) {
707
        say $confirm
715
        say $confirm
708
          ? sprintf "Done with purging %d pseudonymized transactions.", $count
716
            ? sprintf "Done with purging %d pseudonymized transactions.", $count
709
          : sprintf "%d pseudonymized transactions would have been purged.", $count;
717
            : sprintf "%d pseudonymized transactions would have been purged.", $count;
710
    }
718
    }
711
}
719
}
712
720
713
if ($labels) {
721
if ($labels) {
714
    print "Purging item label batches last added to more than $labels days ago.\n" if $verbose;
722
    print "Purging item label batches last added to more than $labels days ago.\n" if $verbose;
715
    my $count = PurgeCreatorBatches($labels, 'labels', $confirm);
723
    my $count = PurgeCreatorBatches( $labels, 'labels', $confirm );
716
    if ($verbose) {
724
    if ($verbose) {
717
        say $confirm
725
        say $confirm
718
          ? sprintf "Done with purging %d item label batches last added to more than %d days ago.\n", $count, $labels
726
            ? sprintf "Done with purging %d item label batches last added to more than %d days ago.\n", $count, $labels
719
          : sprintf "%d item label batches would have been purged.", $count;
727
            : sprintf "%d item label batches would have been purged.", $count;
720
    }
728
    }
721
}
729
}
722
730
723
if ($cards) {
731
if ($cards) {
724
    print "Purging card creator batches last added to more than $cards days ago.\n" if $verbose;
732
    print "Purging card creator batches last added to more than $cards days ago.\n" if $verbose;
725
    my $count = PurgeCreatorBatches($labels, 'patroncards', $confirm);
733
    my $count = PurgeCreatorBatches( $labels, 'patroncards', $confirm );
726
    if ($verbose) {
734
    if ($verbose) {
727
        say $confirm
735
        say $confirm
728
          ? sprintf "Done with purging %d card creator batches last added to more than %d days ago.\n", $count, $labels
736
            ? sprintf "Done with purging %d card creator batches last added to more than %d days ago.\n", $count,
729
          : sprintf "%d card creator batches would have been purged.", $count;
737
            $labels
738
            : sprintf "%d card creator batches would have been purged.", $count;
730
    }
739
    }
731
}
740
}
732
741
733
if ($jobs_days) {
742
if ($jobs_days) {
734
    print "Purging background jobs more than $jobs_days days ago.\n"
743
    print "Purging background jobs more than $jobs_days days ago.\n"
735
      if $verbose;
744
        if $verbose;
736
    my $jobs = Koha::BackgroundJobs->search(
745
    my $jobs = Koha::BackgroundJobs->search(
737
        {
746
        {
738
            status => 'finished',
747
            status => 'finished',
Lines 741-779 if ($jobs_days) { Link Here
741
    )->filter_by_last_update(
750
    )->filter_by_last_update(
742
        {
751
        {
743
            timestamp_column_name => 'ended_on',
752
            timestamp_column_name => 'ended_on',
744
            days => $jobs_days,
753
            days                  => $jobs_days,
745
        }
754
        }
746
    );
755
    );
747
    my $count = $jobs->count;
756
    my $count = $jobs->count;
748
    $jobs->delete if $confirm;
757
    $jobs->delete if $confirm;
749
    if ($verbose) {
758
    if ($verbose) {
750
        say $confirm
759
        say $confirm
751
          ? sprintf "Done with purging %d background jobs of type(s): %s added more than %d days ago.\n",
760
            ? sprintf "Done with purging %d background jobs of type(s): %s added more than %d days ago.\n",
752
          $count, join( ',', @jobs_types ), $jobs_days
761
            $count, join( ',', @jobs_types ), $jobs_days
753
          : sprintf "%d background jobs of type(s): %s added more than %d days ago would have been purged.",
762
            : sprintf "%d background jobs of type(s): %s added more than %d days ago would have been purged.",
754
          $count, join( ',', @jobs_types ), $jobs_days;
763
            $count, join( ',', @jobs_types ), $jobs_days;
755
    }
764
    }
756
}
765
}
757
766
758
if ($reports) {
767
if ($reports) {
759
    if ( $confirm ) {
768
    if ($confirm) {
760
        PurgeSavedReports($reports);
769
        PurgeSavedReports($reports);
761
    } if ( $verbose ) {
770
    }
771
    if ($verbose) {
762
        say "Purging reports data saved more than $reports days ago.\n";
772
        say "Purging reports data saved more than $reports days ago.\n";
763
    }
773
    }
764
}
774
}
765
775
766
if($edifact_msg_days) {
776
if ($edifact_msg_days) {
767
    print "Purging EDIFACT messages older than $edifact_msg_days days.\n" if $verbose;
777
    print "Purging EDIFACT messages older than $edifact_msg_days days.\n" if $verbose;
768
    my $count = PurgeEdifactMessages($edifact_msg_days, $confirm);
778
    my $count = PurgeEdifactMessages( $edifact_msg_days, $confirm );
769
    if ( $verbose ) {
779
    if ($verbose) {
770
        say $confirm
780
        say $confirm
771
          ? sprintf( "Done with purging %d EDIFACT messages", $count )
781
            ? sprintf( "Done with purging %d EDIFACT messages",       $count )
772
          : sprintf( "%d EDIFACT messages would have been removed", $count );
782
            : sprintf( "%d EDIFACT messages would have been removed", $count );
773
    }
783
    }
774
}
784
}
775
785
776
cronlogaction({ action => 'End', info => "COMPLETED" });
786
cronlogaction( { action => 'End', info => "COMPLETED" } );
777
787
778
exit(0);
788
exit(0);
779
789
Lines 784-798 sub RemoveOldSessions { Link Here
784
    $sth = $dbh->prepare(q{ SELECT id, a_session FROM sessions });
794
    $sth = $dbh->prepare(q{ SELECT id, a_session FROM sessions });
785
    $sth->execute or die $dbh->errstr;
795
    $sth->execute or die $dbh->errstr;
786
    $sth->bind_columns( \$id, \$a_session );
796
    $sth->bind_columns( \$id, \$a_session );
787
    $sth2  = $dbh->prepare(q{ DELETE FROM sessions WHERE id=? });
797
    $sth2 = $dbh->prepare(q{ DELETE FROM sessions WHERE id=? });
788
    my $count = 0;
798
    my $count = 0;
789
799
790
    while ( $sth->fetch ) {
800
    while ( $sth->fetch ) {
791
        $lasttime = 0;
801
        $lasttime = 0;
792
        if ( $a_session =~ /lasttime:\s+'?(\d+)/ ) {
802
        if ( $a_session =~ /lasttime:\s+'?(\d+)/ ) {
793
            $lasttime = $1;
803
            $lasttime = $1;
794
        }
804
        } elsif ( $a_session =~ /(ATIME|CTIME):\s+'?(\d+)/ ) {
795
        elsif ( $a_session =~ /(ATIME|CTIME):\s+'?(\d+)/ ) {
796
            $lasttime = $2;
805
            $lasttime = $2;
797
        }
806
        }
798
        if ( $lasttime && $lasttime < $limit ) {
807
        if ( $lasttime && $lasttime < $limit ) {
Lines 848-854 sub PurgeDebarments { Link Here
848
    require Koha::Patron::Debarments;
857
    require Koha::Patron::Debarments;
849
    my ( $days, $doit ) = @_;
858
    my ( $days, $doit ) = @_;
850
    my $count = 0;
859
    my $count = 0;
851
    $sth   = $dbh->prepare(
860
    $sth = $dbh->prepare(
852
        q{
861
        q{
853
            SELECT borrower_debarment_id
862
            SELECT borrower_debarment_id
854
            FROM borrower_debarments
863
            FROM borrower_debarments
Lines 891-937 sub PurgeCreatorBatches { Link Here
891
}
900
}
892
901
893
sub DeleteExpiredSelfRegs {
902
sub DeleteExpiredSelfRegs {
894
    my $cnt= C4::Members::DeleteExpiredOpacRegistrations();
903
    my $cnt = C4::Members::DeleteExpiredOpacRegistrations();
895
    print "Removed $cnt expired self-registered borrowers\n" if $verbose;
904
    print "Removed $cnt expired self-registered borrowers\n" if $verbose;
896
}
905
}
897
906
898
sub DeleteUnverifiedSelfRegs {
907
sub DeleteUnverifiedSelfRegs {
899
    my $cnt= C4::Members::DeleteUnverifiedOpacRegistrations( $_[0] );
908
    my $cnt = C4::Members::DeleteUnverifiedOpacRegistrations( $_[0] );
900
    print "Removed $cnt unverified self-registrations\n" if $verbose;
909
    print "Removed $cnt unverified self-registrations\n" if $verbose;
901
}
910
}
902
911
903
sub DeleteSpecialHolidays {
912
sub DeleteSpecialHolidays {
904
    my ( $days ) = @_;
913
    my ($days) = @_;
905
914
906
    my $sth = $dbh->prepare(q{
915
    my $sth = $dbh->prepare(
916
        q{
907
        DELETE FROM special_holidays
917
        DELETE FROM special_holidays
908
        WHERE DATE( CONCAT( year, '-', month, '-', day ) ) < DATE_SUB( CAST(NOW() AS DATE), INTERVAL ? DAY );
918
        WHERE DATE( CONCAT( year, '-', month, '-', day ) ) < DATE_SUB( CAST(NOW() AS DATE), INTERVAL ? DAY );
909
    });
919
    }
910
    my $count = $sth->execute( $days ) + 0;
920
    );
921
    my $count = $sth->execute($days) + 0;
911
    print "Removed $count unique holidays\n" if $verbose;
922
    print "Removed $count unique holidays\n" if $verbose;
912
}
923
}
913
924
914
sub PurgeSavedReports {
925
sub PurgeSavedReports {
915
    my ( $reports ) = @_;
926
    my ($reports) = @_;
916
927
917
    my $sth = $dbh->prepare(q{
928
    my $sth = $dbh->prepare(
929
        q{
918
            DELETE FROM saved_reports
930
            DELETE FROM saved_reports
919
            WHERE date(date_run) < DATE_SUB(CURDATE(),INTERVAL ? DAY );
931
            WHERE date(date_run) < DATE_SUB(CURDATE(),INTERVAL ? DAY );
920
        });
932
        }
921
    $sth->execute( $reports );
933
    );
934
    $sth->execute($reports);
922
}
935
}
923
936
924
sub PurgeEdifactMessages {
937
sub PurgeEdifactMessages {
925
    my ( $days, $doit ) = @_;
938
    my ( $days, $doit ) = @_;
926
939
927
    my $schema = Koha::Database->new()->schema();
940
    my $schema    = Koha::Database->new()->schema();
928
    my $dtf = $schema->storage->datetime_parser;
941
    my $dtf       = $schema->storage->datetime_parser;
929
    my $resultset = $schema->resultset('EdifactMessage')->search(
942
    my $resultset = $schema->resultset('EdifactMessage')->search(
930
        {
943
        {
931
            transfer_date => {
944
            transfer_date => { '<'  => $dtf->format_datetime( dt_from_string->subtract( days => $days ) ) },
932
                '<' => $dtf->format_datetime(dt_from_string->subtract( days => $days ))
945
            status        => { '!=' => 'new' },
933
            },
934
            status => { '!=' => 'new' },
935
        }
946
        }
936
    );
947
    );
937
    my $count = $resultset->count;
948
    my $count = $resultset->count;
938
- 

Return to bug 26831