Merge branch 'master' of http://manage-gmc.dev.kohalibrary.com/koha-installer
[koha.git] / misc / merge_authority.pl
index a046d83..a423ddc 100755 (executable)
@@ -2,6 +2,12 @@
 # script that rebuild thesaurus from biblio table.
 
 use strict;
+BEGIN {
+    # find Koha's Perl modules
+    # test carefully before changing this
+    use FindBin;
+    eval { require "$FindBin::Bin/kohalib.pl" };
+}
 
 # Koha modules used
 use MARC::File::USMARC;
@@ -19,11 +25,11 @@ GetOptions(
     'f:s' => \$mergefrom,
     't:s' => \$mergeto,
     'v' => \$verbose,
-       'n' => \$noconfirm,
+    'n' => \$noconfirm,
 );
 
 if ($version || ($mergefrom eq '')) {
-       print <<EOF
+    print <<EOF
 Script to merge an authority into another
 parameters :
 \th : this version/help screen
@@ -46,25 +52,25 @@ my $dbh = C4::Context->dbh;
 # my @subf = $subfields =~ /(##\d\d\d##.)/g;
 
 $|=1; # flushes output
-my $authfrom = AUTHgetauthority($dbh,$mergefrom);
-my $authto = AUTHgetauthority($dbh,$mergeto);
+my $authfrom = AUTHgetauthority($mergefrom);
+my $authto = AUTHgetauthority($mergeto);
 
-my $authtypecodefrom = AUTHfind_authtypecode($dbh,$mergefrom);
-my $authtypecodeto = AUTHfind_authtypecode($dbh,$mergeto);
+my $authtypecodefrom = AUTHfind_authtypecode($mergefrom);
+my $authtypecodeto = AUTHfind_authtypecode($mergeto);
 
 unless ($noconfirm) {
-       print "************\n";
-       print "You will merge authority : $mergefrom ($authtypecodefrom)\n".$authfrom->as_formatted;
-       print "\n*************\n";
-       print "Into authority : $mergeto ($authtypecodeto)\n".$authto->as_formatted;
-       print "\n\nDo you confirm (enter YES)?";
-       my $confirm = <STDIN>;
-       chop $confirm;
-       unless (uc($confirm) eq 'YES' and $authtypecodefrom eq $authtypecodeto) {
-               print "IMPOSSIBLE : authorities are not of the same type ($authtypecodefrom vs $authtypecodeto) !!!\n" if $authtypecodefrom ne $authtypecodeto;
-               print "Merge cancelled\n";
-               exit;
-       }
+    print "************\n";
+    print "You will merge authority : $mergefrom ($authtypecodefrom)\n".$authfrom->as_formatted;
+    print "\n*************\n";
+    print "Into authority : $mergeto ($authtypecodeto)\n".$authto->as_formatted;
+    print "\n\nDo you confirm (enter YES)?";
+    my $confirm = <STDIN>;
+    chop $confirm;
+    unless (uc($confirm) eq 'YES' and $authtypecodefrom eq $authtypecodeto) {
+        print "IMPOSSIBLE : authorities are not of the same type ($authtypecodefrom vs $authtypecodeto) !!!\n" if $authtypecodefrom ne $authtypecodeto;
+        print "Merge cancelled\n";
+        exit;
+    }
 }
 my $starttime = gettimeofday;
 print "Merging\n" unless $noconfirm;
@@ -83,7 +89,7 @@ $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure wher
 $sth->execute($authtypecodefrom);
 my $tags_using_authtype;
 while (my ($tagfield) = $sth->fetchrow) {
-       $tags_using_authtype.= "'".$tagfield."',";
+    $tags_using_authtype.= "'".$tagfield."',";
 }
 chop $tags_using_authtype;
 # now, find every biblio using this authority
@@ -93,24 +99,24 @@ $sth->execute;
 my $nbdone;
 # and delete entries before recreating them
 while (my ($bibid,$tag,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder) = $sth->fetchrow) {
-       my $biblio = MARCgetbiblio($dbh,$bibid);
-       print "BEFORE : ".$biblio->as_formatted."\n" if $verbose;
-       # now, we know what uses the authority & where.
-       # delete all subfields that are in the same tag/tagorder and that are in the authority (& that are not in tab ignore in the biblio)
-       # then recreate them with the new authority.
-       foreach my $subfield (@record_from) {
-               &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield->[0]);
-       }
-       &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,'9');
-       foreach my $subfield (@record_to) {
-               &MARCaddsubfield($dbh,$bibid,$tag,$tag_indicator,$tagorder,$subfield->[0],$subfieldorder,$subfield->[1]);
-       }
-       &MARCaddsubfield($dbh,$bibid,$tag,$tag_indicator,$tagorder,'9',$subfieldorder,$mergeto);
-       my $biblio = MARCgetbiblio($dbh,$bibid);
-       print "AFTER : ".$biblio->as_formatted."\n" if $verbose;
-       $nbdone++;
-#      &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfieldcode,$subfieldorder);
-       
+    my $biblio = GetMarcBiblio($bibid);
+    print "BEFORE : ".$biblio->as_formatted."\n" if $verbose;
+    # now, we know what uses the authority & where.
+    # delete all subfields that are in the same tag/tagorder and that are in the authority (& that are not in tab ignore in the biblio)
+    # then recreate them with the new authority.
+    foreach my $subfield (@record_from) {
+        &MARCdelsubfield($bibid,$tag,$tagorder,$subfield->[0]);
+    }
+    &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,'9');
+    foreach my $subfield (@record_to) {
+        &MARCaddsubfield($bibid,$tag,$tag_indicator,$tagorder,$subfield->[0],$subfieldorder,$subfield->[1]);
+    }
+    &MARCaddsubfield($bibid,$tag,$tag_indicator,$tagorder,'9',$subfieldorder,$mergeto);
+    $biblio = GetMarcBiblio($bibid);
+    print "AFTER : ".$biblio->as_formatted."\n" if $verbose;
+    $nbdone++;
+#     &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfieldcode,$subfieldorder);
+    
 }
 my $timeneeded = gettimeofday - $starttime;
-print "$nbdone authorities done in $timeneeded seconds" unless $noconfirm;
\ No newline at end of file
+print "$nbdone authorities done in $timeneeded seconds" unless $noconfirm;