Bug 17144: Fix variable scope issues in edi_account.pl
[koha.git] / misc / migration_tools / merge_authority.pl
blob405b66ae140420b6a55068568ca8af164322a0f9
1 #!/usr/bin/perl
2 # script that rebuild thesaurus from biblio table.
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" };
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);
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,
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
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
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
49 die;
50 }#/'
52 my $dbh = C4::Context->dbh;
54 $|=1; # flushes output
55 my $authfrom = GetAuthority($mergefrom);
56 my $authto = GetAuthority($mergeto);
58 die "Authority $mergefrom does not exist" unless $authfrom;
59 die "Authority $mergeto does not exist" unless $authto;
61 my $authtypecodefrom = Koha::Authorities->find($mergefrom)->authtypecode;
62 my $authtypecodeto = Koha::Authorities->find($mergeto)->authtypecode;
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;
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 next unless ($MARCauth);
89 merge($authid,$MARCauth,$authid,$MARCauth) if ($MARCauth);
91 $dbh->do("update need_merge_authorities set done=1 where done=2"); #DONE
92 } else {
93 my $MARCfrom = GetAuthority($mergefrom);
94 my $MARCto = GetAuthority($mergeto);
95 &merge($mergefrom,$MARCfrom,$mergeto,$MARCto);
96 #Could add mergefrom authority to mergeto rejected forms before deletion
97 DelAuthority($mergefrom) if ($mergefrom != $mergeto);
99 my $timeneeded = gettimeofday - $starttime;
100 print "Done in $timeneeded seconds" unless $noconfirm;