new version of rebuild_zebra. Should work with Perl-ZOOM, but DOES NOT WORK for me.
[koha.git] / misc / migration_tools / rebuild_zebra.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 # Koha modules used
6 use MARC::File::USMARC;
7 use MARC::Record;
8 use MARC::Batch;
9 use C4::Context;
10 use C4::Biblio;
11 use ZOOM;
12 use Time::HiRes qw(gettimeofday);
13
14 use Getopt::Long;
15 my ( $input_marc_file, $number) = ('',0);
16 my ($confirm);
17 GetOptions(
18     'c' => \$confirm,
19 );
20
21 unless ($confirm) {
22         print <<EOF
23
24 Script to create the zebra DB from a Koha DB
25
26 EOF
27 ;#'
28 die;
29 }
30
31 $|=1; # flushes output
32
33 my $dbh = C4::Context->dbh;
34 my $Zconn;
35 eval {
36         $Zconn = new ZOOM::Connection('localhost','2100');
37 };
38 if ($@) {
39         print "Error ", $@->code()," : ",$@->message()."\n";
40         die;
41 }
42
43 # first, drop Zebra DB
44 eval {
45         my $Zpackage = $Zconn->package();
46         $Zpackage->option(databaseName => 'Koha');
47 #       $Zpackage->send("drop");
48 };
49
50 eval {
51         my $Zpackage = $Zconn->package();
52         $Zpackage->option(databaseName => 'Koha');
53         $Zpackage->send("create");
54 };
55 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
56 unless (opendir(DIR, "$cgidir")) {
57                 $cgidir = C4::Context->intranetdir."/";
58
59
60 my $starttime = gettimeofday;
61 my $sth = $dbh->prepare("select biblionumber from biblio");
62 $sth->execute;
63 my $i=0;
64 while ((my $biblionumber) = $sth->fetchrow) {
65         my $record = XMLgetbiblio($dbh,$biblionumber);
66         my $Zpackage = $Zconn->package();
67         $Zpackage->option(databaseName => 'Koha');
68         $Zpackage->option(action => "recordInsert");
69         $Zpackage->option(record => $record);
70         $Zpackage->send("update");
71         $Zpackage->destroy;
72         $i++;
73         print '.';
74         print "$i\r" unless ($i % 100);
75 #       exit if $i>100;
76 }
77 my $Zpackage = $Zconn->package();
78 $Zpackage->option(databaseName => 'Koha');
79 $Zpackage->send("commit");
80 my $timeneeded = gettimeofday - $starttime;
81 print "\n\n$i MARC record done in $timeneeded seconds\n";