Bug 17952 - Lost items not skipped by overdue_notices.pl
[koha.git] / misc / cronjobs / merge_authorities.pl
blob46826944332e3e2086fd87fcf01c4223e7401c96
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Getopt::Long;
5 use Pod::Usage;
6 use Time::HiRes qw(gettimeofday);
8 use C4::AuthoritiesMarc;
9 use Koha::Authority::MergeRequests;
11 use constant RESET_HOURS => 24;
12 use constant REMOVE_DAYS => 30;
14 my ( $params );
15 GetOptions(
16 'h' => \$params->{help},
17 'v' => \$params->{verbose},
18 'b' => \$params->{batch},
21 $|=1; # flushes output
22 if( $params->{batch} ) {
23 handle_batch( $params );
24 } else {
25 pod2usage(1);
28 sub handle_batch {
29 my $params = shift;
30 my $verbose = $params->{verbose};
32 my $starttime = gettimeofday;
33 print "Started merging\n" if $verbose;
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
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.
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;
59 my $timeneeded = gettimeofday - $starttime;
60 print "Done in $timeneeded seconds\n" if $verbose;
63 =head1 NAME
65 merge_authorities.pl
67 =head1 DESCRIPTION
69 Cron script to handle authority merge requests
71 =head1 SYNOPSIS
73 merge_authorities.pl -h
75 merge_authorities.pl -b -v
77 =head1 OPTIONS
79 -b : batch mode (You need to pass this parameter from crontab file)
81 -h : print usage statement
83 -v : verbose mode
85 =head1 AUTHOR
87 Koha Development Team
89 =cut