Bug 9988: Check the merge limit in sub merge
[koha.git] / misc / migration_tools / merge_authority.pl
1 #!/usr/bin/perl
2 # script that rebuild thesaurus from biblio table.
3
4 use strict;
5 #use warnings; FIXME - Bug 2505
6 BEGIN {
7     # find Koha's Perl modules
8     # test carefully before changing this
9     use FindBin;
10     eval { require "$FindBin::Bin/kohalib.pl" };
11 }
12
13 # Koha modules used
14 use C4::Context;
15 use C4::Search;
16 use C4::Biblio;
17 use C4::AuthoritiesMarc;
18 use Koha::Authorities;
19 use Koha::Authority::MergeRequests;
20 use Time::HiRes qw(gettimeofday);
21
22 use Getopt::Long;
23 my ($version, $verbose, $mergefrom,$mergeto,$noconfirm,$batch);
24 GetOptions(
25     'h' => \$version,
26     'f:s' => \$mergefrom,
27     't:s' => \$mergeto,
28     'v' => \$verbose,
29     'n' => \$noconfirm,
30     'b' => \$batch, 
31 );
32
33 if ($version || ($mergefrom eq '' && !$batch)) {
34     print <<EOF
35 Script to merge an authority into another
36 parameters :
37 \th : this version/help screen
38 \tv : verbose mode (show many things on screen)
39 \tf : the authority number to merge (the one that can be deleted after the merge).
40 \tt : the authority number where to merge
41 \tn : don't ask for confirmation (useful for batch mergings, should not be used on command line)
42 \tb : batch Merging
43
44 All biblios with the authority in -t will be modified to be "connected" to authority -f
45 SAMPLE :
46 ./merge_authority.pl -f 2457 -t 531
47
48 Before doing anything, the script will show both authorities and ask for confirmation. Of course, you can merge only 2 authorities of the same kind.
49 EOF
50 ;#
51 die;
52 }#/'
53
54 my $dbh = C4::Context->dbh;
55
56 $|=1; # flushes output
57 my $authfrom = GetAuthority($mergefrom);
58 my $authto = GetAuthority($mergeto);
59
60 die "Authority $mergefrom does not exist" unless $authfrom;
61 die "Authority $mergeto does not exist"   unless $authto;
62
63 my $authtypecodefrom = Koha::Authorities->find($mergefrom)->authtypecode;
64 my $authtypecodeto = Koha::Authorities->find($mergeto)->authtypecode;
65
66 unless ($noconfirm || $batch) {
67     print "************\n";
68     print "You will merge authority : $mergefrom ($authtypecodefrom)\n".$authfrom->as_formatted;
69     print "\n*************\n";
70     print "Into authority : $mergeto ($authtypecodeto)\n".$authto->as_formatted;
71     print "\n\nDo you confirm (enter YES)?";
72     my $confirm = <STDIN>;
73     chop $confirm;
74     unless (uc($confirm) eq 'YES' and $authtypecodefrom eq $authtypecodeto) {
75         print "IMPOSSIBLE : authorities are not of the same type ($authtypecodefrom vs $authtypecodeto) !!!\n" if $authtypecodefrom ne $authtypecodeto;
76         print "Merge cancelled\n";
77         exit;
78     }
79 }
80 my $starttime = gettimeofday;
81 print "Merging\n" unless $noconfirm;
82 if ($batch) {
83   my $authref;
84   $dbh->do("update need_merge_authorities set done=2 where done=0"); #temporary status 2 means: selected for merge
85   $authref=$dbh->selectall_arrayref("select id,authid,authid_new from need_merge_authorities where done=2");
86   foreach my $row ( @$authref ) {
87       my $req = Koha::Authority::MergeRequests->find( $row->[0] );
88       my $marc = $req ? $req->oldmarc : undef;
89       my $authid = $row->[1];
90       my $authid_new = $row->[2];
91       print "managing $authid\n" if $verbose;
92       # Following merge call handles both modifications and deletes
93       merge({ mergefrom => $authid, MARCfrom => $marc, mergeto => $authid_new, MARCto => GetAuthority($authid_new), override_limit => 1 });
94   }
95   $dbh->do("update need_merge_authorities set done=1 where done=2"); #DONE
96 } else {
97   my $MARCfrom = GetAuthority($mergefrom);
98   my $MARCto = GetAuthority($mergeto);
99   &merge({ mergefrom => $mergefrom, MARCfrom => $MARCfrom, mergeto => $mergeto, MARCto => $MARCto });
100   #Could add mergefrom authority to mergeto rejected forms before deletion 
101   DelAuthority({ authid => $mergefrom }) if ($mergefrom != $mergeto);
102 }
103 my $timeneeded = gettimeofday - $starttime;
104 print "Done in $timeneeded seconds" unless $noconfirm;