Clean up before final commits
[koha.git] / misc / merge_authority.pl
1 #!/usr/bin/perl
2
3
4 use strict;
5
6 # Koha modules used
7 use C4::Context;
8 use C4::Biblio;
9 use C4::AuthoritiesMarc; ###merge routine moved to there
10 use Time::HiRes qw(gettimeofday);
11
12 use Getopt::Long;
13 my ($version, $verbose, $mergefrom,$mergeto,$noconfirm,$batch);
14 GetOptions(
15     'h' => \$version,
16     'f:s' => \$mergefrom,
17     't:s' => \$mergeto,
18     'v' => \$verbose,
19         'n' => \$noconfirm,
20         'b' => \$batch,
21 );
22
23 if ($version || ($mergefrom eq '' && !$batch)) {
24         print <<EOF
25 Script to merge an authority into another
26 parameters :
27 \th : this version/help screen
28 \tv : verbose mode (show many things on screen)
29 \tf : the authority number to merge (the one that can be deleted after the merge).
30 \tt : the authority number where to merge
31 \tb : batch merging.
32 \tn : don't ask for confirmation (useful for batch mergings, should not be used on command line)
33
34 All biblios with the authority in -t will be modified to be "connected" to authority -f
35 SAMPLE :
36 ./merge_authority.pl -f 2457 -t 531
37
38 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.
39
40 BATCH MODE :
41 The batch mode is done to report modifs. On every authority modif, a file is generated in KOHAROOT/localfile/modified_authorities/ If this script is called with -b, it parses the directory, finding & updating biblios using the modified authority.
42
43 ./merge_authority.pl -b
44
45 (don't forget to export PERL5LIB and KOHA_CONF. Here is my cron job :
46 SHELL=/bin/bash
47 */5 * * * *       export PERL5LIB=/home/httpd/koha;export KOHA_CONF=/etc/mykoha.conf;/home/httpd/koha/scripts/misc/merge_authority.pl -b -n
48
49 EOF
50 ;#
51 exit;
52 }#
53
54 my $dbh = C4::Context->dbh;
55 # my @subf = $subfields =~ /(##\d\d\d##.)/g;
56
57 $|=1; # flushes output
58 my $starttime = gettimeofday;
59 if ($batch) {
60         my @authlist;
61         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
62         unless (opendir(DIR, "$cgidir/localfile/modified_authorities")) {
63                 $cgidir = C4::Context->intranetdir;
64                 opendir(DIR, "$cgidir/localfile/modified_authorities") || die "can't opendir $cgidir/localfile/modified_authorities: $!";
65         } 
66         while (my $authid = readdir(DIR)) {
67                 if ($authid =~ /\.authid$/) {
68                         $authid =~ s/\.authid$//;
69                         print "managing $authid\n" if $verbose;
70                         my $MARCauth = XMLgetauthorityhash($dbh,$authid);
71                         &merge($dbh,$authid,$MARCauth,$authid,$MARCauth) if ($MARCauth);
72                         unlink $cgidir.'/localfile/modified_authorities/'.$authid.'.authid';
73                 }
74         }
75         closedir DIR;
76 } else {
77         my $MARCfrom = XMLgetauthorityhash($dbh,$mergefrom);
78         my $MARCto = XMLgetauthorityhash($dbh,$mergeto);
79         &merge($dbh,$mergefrom,$MARCfrom,$mergeto,$MARCto);
80 }
81 my $timeneeded = gettimeofday - $starttime;
82 print "Done in $timeneeded seconds" unless $noconfirm;
83