Bug 19231: Add number of attached items to confirm message when deleting course
[koha.git] / misc / cronjobs / merge_authorities.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Getopt::Long;
5 use Pod::Usage;
6 use Time::HiRes qw(gettimeofday);
7
8 use C4::AuthoritiesMarc;
9 use Koha::Authority::MergeRequests;
10
11 use constant RESET_HOURS => 24;
12 use constant REMOVE_DAYS => 30;
13
14 my ( $params );
15 GetOptions(
16     'h' => \$params->{help},
17     'v' => \$params->{verbose},
18     'b' => \$params->{batch},
19 );
20
21 $|=1; # flushes output
22 if( $params->{batch} ) {
23     handle_batch( $params );
24 } else {
25     pod2usage(1);
26 }
27
28 sub handle_batch {
29     my $params = shift;
30     my $verbose = $params->{verbose};
31
32     my $starttime = gettimeofday;
33     print "Started merging\n" if $verbose;
34
35     Koha::Authority::MergeRequests->cron_cleanup({ reset_hours => RESET_HOURS, remove_days => REMOVE_DAYS });
36     my $rs = Koha::Authority::MergeRequests->search(
37         { done => 0 },
38         { order_by => { -asc => 'id' }}, # IMPORTANT
39     );
40     # For best results, postponed merges should be applied in right order.
41     # Similarly, we do not only select the last one for a specific id.
42
43     while( my $req = $rs->next ) {
44         $req->done(2)->store;
45         print "Merging auth " . $req->authid . " to " . ( $req->authid_new // 'NULL' ) . ".\n" if $verbose;
46         my $newmarc = $req->authid_new
47             ? GetAuthority( $req->authid_new )
48             : undef;
49         # Following merge call handles both modifications and deletes
50         merge({
51             mergefrom => $req->authid,
52             MARCfrom => scalar $req->oldmarc,
53             mergeto => $req->authid_new,
54             MARCto => $newmarc,
55             override_limit => 1,
56         });
57         $req->done(1)->store;
58     }
59     my $timeneeded = gettimeofday - $starttime;
60     print "Done in $timeneeded seconds\n" if $verbose;
61 }
62
63 =head1 NAME
64
65 merge_authorities.pl
66
67 =head1 DESCRIPTION
68
69 Cron script to handle authority merge requests
70
71 =head1 SYNOPSIS
72
73 merge_authorities.pl -h
74
75 merge_authorities.pl -b -v
76
77 =head1 OPTIONS
78
79 -b : batch mode (You need to pass this parameter from crontab file)
80
81 -h : print usage statement
82
83 -v : verbose mode
84
85 =head1 AUTHOR
86
87 Koha Development Team
88
89 =cut