synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
[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 if ($@) {
50         print "Error dropping /CODE:", $@->code()," /MSG: ",$@->message(),"\n";
51 #       die;
52 }
53 # then recreate it
54 eval {
55         my $Zpackage = $Zconn->package();
56         $Zpackage->option(databaseName => 'Koha');
57 #       $Zpackage->send("create");
58 };
59 if ($@) {
60         print "Error creating /CODE:", $@->code(),"\n /MSG:",$@->message(),"\n\n";
61 #       die;
62 }
63
64 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
65 unless (opendir(DIR, "$cgidir")) {
66                 $cgidir = C4::Context->intranetdir."/";
67
68
69 my $starttime = gettimeofday;
70 my $sth = $dbh->prepare("select biblionumber from biblio");
71 $sth->execute;
72 my $i=0;
73 while ((my $biblionumber) = $sth->fetchrow) {
74         my $record = MARCgetbiblio($dbh,$biblionumber);
75 #       my $filename = $cgidir."/zebra/biblios/BIBLIO".$biblionumber."iso2709";
76 #       open F,"> $filename";
77 #       print F $record->as_usmarc();
78 #       close F;
79         my $Zpackage = $Zconn->package();
80 #       print "=>".$record->as_xml()."\n";
81         $Zpackage->option(action => "recordInsert");
82         $Zpackage->option(record => $record->as_usmarc());
83         eval {
84                 $Zpackage->send("update");
85         };
86         if ($@) {
87                 print "Error updating /CODE:", $@->code()," /MSG:",$@->message(),"\n";
88                 die;
89         }
90         $Zpackage->destroy;
91         $i++;
92         print "\r$i" unless ($i % 100);
93 }
94 my $timeneeded = gettimeofday - $starttime;
95 print "\n$i MARC record done in $timeneeded seconds\n";