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