2 # script that rebuild thesaurus from biblio table.
5 #use warnings; FIXME - Bug 2505
7 # find Koha's Perl modules
8 # test carefully before changing this
10 eval { require "$FindBin::Bin/kohalib.pl" };
17 use C4::AuthoritiesMarc;
18 use Koha::Authorities;
19 use Koha::Authority::MergeRequests;
20 use Time::HiRes qw(gettimeofday);
23 my ($version, $verbose, $mergefrom,$mergeto,$noconfirm,$batch);
33 if ($version || ($mergefrom eq '' && !$batch)) {
35 Script to merge an authority into another
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)
44 All biblios with the authority in -t will be modified to be "connected" to authority -f
46 ./merge_authority.pl -f 2457 -t 531
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.
54 my $dbh = C4::Context->dbh;
56 $|=1; # flushes output
57 my $authfrom = GetAuthority($mergefrom);
58 my $authto = GetAuthority($mergeto);
60 die "Authority $mergefrom does not exist" unless $authfrom;
61 die "Authority $mergeto does not exist" unless $authto;
63 my $authtypecodefrom = Koha::Authorities->find($mergefrom)->authtypecode;
64 my $authtypecodeto = Koha::Authorities->find($mergeto)->authtypecode;
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>;
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";
80 my $starttime = gettimeofday;
81 print "Merging\n" unless $noconfirm;
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 });
95 $dbh->do("update need_merge_authorities set done=1 where done=2"); #DONE
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);
103 my $timeneeded = gettimeofday - $starttime;
104 print "Done in $timeneeded seconds" unless $noconfirm;