X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=misc%2Fmigration_tools%2Fmerge_authority.pl;h=8e0959489b5aaa3399b77156d22744bdfed5f43a;hb=3b0d4e04e09f37e8dcbaba71abd47980bfef7059;hp=a423ddc19231119087b07ef1392c6decbdbf7013;hpb=dd3f557f53f01dd6f5db51c044d792974e547720;p=koha_fer diff --git a/misc/migration_tools/merge_authority.pl b/misc/migration_tools/merge_authority.pl index a423ddc192..8e0959489b 100755 --- a/misc/migration_tools/merge_authority.pl +++ b/misc/migration_tools/merge_authority.pl @@ -2,6 +2,7 @@ # script that rebuild thesaurus from biblio table. use strict; +#use warnings; FIXME - Bug 2505 BEGIN { # find Koha's Perl modules # test carefully before changing this @@ -10,25 +11,24 @@ BEGIN { } # Koha modules used -use MARC::File::USMARC; -use MARC::Record; -use MARC::Batch; use C4::Context; +use C4::Search; use C4::Biblio; use C4::AuthoritiesMarc; use Time::HiRes qw(gettimeofday); use Getopt::Long; -my ($version, $verbose, $mergefrom,$mergeto,$noconfirm); +my ($version, $verbose, $mergefrom,$mergeto,$noconfirm,$batch); GetOptions( 'h' => \$version, 'f:s' => \$mergefrom, 't:s' => \$mergeto, 'v' => \$verbose, 'n' => \$noconfirm, + 'b' => \$batch, ); -if ($version || ($mergefrom eq '')) { +if ($version || ($mergefrom eq '' && !$batch)) { print <dbh; -# my @subf = $subfields =~ /(##\d\d\d##.)/g; $|=1; # flushes output -my $authfrom = AUTHgetauthority($mergefrom); -my $authto = AUTHgetauthority($mergeto); +my $authfrom = GetAuthority($mergefrom); +my $authto = GetAuthority($mergeto); -my $authtypecodefrom = AUTHfind_authtypecode($mergefrom); -my $authtypecodeto = AUTHfind_authtypecode($mergeto); +my $authtypecodefrom = GetAuthTypeCode($mergefrom); +my $authtypecodeto = GetAuthTypeCode($mergeto); -unless ($noconfirm) { +unless ($noconfirm || $batch) { print "************\n"; print "You will merge authority : $mergefrom ($authtypecodefrom)\n".$authfrom->as_formatted; print "\n*************\n"; @@ -74,49 +74,24 @@ unless ($noconfirm) { } my $starttime = gettimeofday; print "Merging\n" unless $noconfirm; - -# search the tag to report -my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?"); -$sth->execute($authtypecodefrom); -my ($auth_tag_to_report) = $sth->fetchrow; -# my $record_to_report = $authto->field($auth_tag_to_report); -print "Reporting authority tag $auth_tag_to_report :\n" if $verbose; -my @record_to = $authto->field($auth_tag_to_report)->subfields(); -my @record_from = $authfrom->field($auth_tag_to_report)->subfields(); - -# search all biblio tags using this authority. -$sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where authtypecode=?"); -$sth->execute($authtypecodefrom); -my $tags_using_authtype; -while (my ($tagfield) = $sth->fetchrow) { - $tags_using_authtype.= "'".$tagfield."',"; -} -chop $tags_using_authtype; -# now, find every biblio using this authority -my $query = "select bibid,tag,tag_indicator,tagorder,subfieldcode,subfieldorder from marc_subfield_table where tag in ($tags_using_authtype) and subfieldcode='9' and subfieldvalue='$mergefrom'"; -$sth = $dbh->prepare($query); -$sth->execute; -my $nbdone; -# and delete entries before recreating them -while (my ($bibid,$tag,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder) = $sth->fetchrow) { - 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); - +if ($batch) { + my $authref; + $dbh->do("update need_merge_authorities set done=2 where done=0"); #temporary status 2 means: selected for merge + $authref=$dbh->selectall_arrayref("select distinct authid from need_merge_authorities where done=2"); + foreach(@$authref) { + my $authid=$_->[0]; + print "managing $authid\n" if $verbose; + my $MARCauth = GetAuthority($authid) ; + next unless ($MARCauth); + merge($authid,$MARCauth,$authid,$MARCauth) if ($MARCauth); + } + $dbh->do("update need_merge_authorities set done=1 where done=2"); #DONE +} else { + my $MARCfrom = GetAuthority($mergefrom); + my $MARCto = GetAuthority($mergeto); + &merge($mergefrom,$MARCfrom,$mergeto,$MARCto); + #Could add mergefrom authority to mergeto rejected forms before deletion + DelAuthority($mergefrom) if ($mergefrom != $mergeto); } my $timeneeded = gettimeofday - $starttime; -print "$nbdone authorities done in $timeneeded seconds" unless $noconfirm; +print "Done in $timeneeded seconds" unless $noconfirm;