Bug 9988: Merge should have a parameter hash
[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 Time::HiRes qw(gettimeofday);
19
20 use Getopt::Long;
21 my ($version, $verbose, $mergefrom,$mergeto,$noconfirm,$batch);
22 GetOptions(
23     'h' => \$version,
24     'f:s' => \$mergefrom,
25     't:s' => \$mergeto,
26     'v' => \$verbose,
27     'n' => \$noconfirm,
28     'b' => \$batch, 
29 );
30
31 if ($version || ($mergefrom eq '' && !$batch)) {
32     print <<EOF
33 Script to merge an authority into another
34 parameters :
35 \th : this version/help screen
36 \tv : verbose mode (show many things on screen)
37 \tf : the authority number to merge (the one that can be deleted after the merge).
38 \tt : the authority number where to merge
39 \tn : don't ask for confirmation (useful for batch mergings, should not be used on command line)
40 \tb : batch Merging
41
42 All biblios with the authority in -t will be modified to be "connected" to authority -f
43 SAMPLE :
44 ./merge_authority.pl -f 2457 -t 531
45
46 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.
47 EOF
48 ;#
49 die;
50 }#/'
51
52 my $dbh = C4::Context->dbh;
53
54 $|=1; # flushes output
55 my $authfrom = GetAuthority($mergefrom);
56 my $authto = GetAuthority($mergeto);
57
58 die "Authority $mergefrom does not exist" unless $authfrom;
59 die "Authority $mergeto does not exist"   unless $authto;
60
61 my $authtypecodefrom = Koha::Authorities->find($mergefrom)->authtypecode;
62 my $authtypecodeto = Koha::Authorities->find($mergeto)->authtypecode;
63
64 unless ($noconfirm || $batch) {
65     print "************\n";
66     print "You will merge authority : $mergefrom ($authtypecodefrom)\n".$authfrom->as_formatted;
67     print "\n*************\n";
68     print "Into authority : $mergeto ($authtypecodeto)\n".$authto->as_formatted;
69     print "\n\nDo you confirm (enter YES)?";
70     my $confirm = <STDIN>;
71     chop $confirm;
72     unless (uc($confirm) eq 'YES' and $authtypecodefrom eq $authtypecodeto) {
73         print "IMPOSSIBLE : authorities are not of the same type ($authtypecodefrom vs $authtypecodeto) !!!\n" if $authtypecodefrom ne $authtypecodeto;
74         print "Merge cancelled\n";
75         exit;
76     }
77 }
78 my $starttime = gettimeofday;
79 print "Merging\n" unless $noconfirm;
80 if ($batch) {
81   my $authref;
82   $dbh->do("update need_merge_authorities set done=2 where done=0"); #temporary status 2 means: selected for merge
83   $authref=$dbh->selectall_arrayref("select distinct authid from need_merge_authorities where done=2");
84   foreach(@$authref) {
85       my $authid=$_->[0];
86       print "managing $authid\n" if $verbose;
87       my $MARCauth = GetAuthority( $authid );
88       if( $MARCauth ) {
89           merge({ mergefrom => $authid, MARCfrom => $MARCauth, mergeto => $authid, MARCto => $MARCauth });
90       } else {
91           merge({ mergefrom => $authid }); # handle a delete
92       }
93   }
94   $dbh->do("update need_merge_authorities set done=1 where done=2"); #DONE
95 } else {
96   my $MARCfrom = GetAuthority($mergefrom);
97   my $MARCto = GetAuthority($mergeto);
98   &merge({ mergefrom => $mergefrom, MARCfrom => $MARCfrom, mergeto => $mergeto, MARCto => $MARCto });
99   #Could add mergefrom authority to mergeto rejected forms before deletion 
100   DelAuthority({ authid => $mergefrom }) if ($mergefrom != $mergeto);
101 }
102 my $timeneeded = gettimeofday - $starttime;
103 print "Done in $timeneeded seconds" unless $noconfirm;