do not let MARC::Batch open MARC files
[koha.git] / misc / batchImportMARCWithBiblionumbers.pl
1 #!/usr/bin/perl
2 # load records that already have biblionumber set into a koha system
3 # Written by TG on 10/04/2006
4 use strict;
5 BEGIN {
6     # find Koha's Perl modules
7     # test carefully before changing this
8     use FindBin;
9     eval { require "$FindBin::Bin/kohalib.pl" };
10 }
11
12 # Koha modules used
13
14 use C4::Context;
15 use C4::Biblio;
16 use MARC::Record;
17 use MARC::File::USMARC;
18 use MARC::File::XML;
19 use MARC::Batch;
20 use Time::HiRes qw(gettimeofday);
21 use Getopt::Long;
22 use IO::File;
23
24 my  $input_marc_file = '';
25 my ($version);
26 GetOptions(
27     'file:s'    => \$input_marc_file,
28     'h' => \$version,
29 );
30
31 if ($version || ($input_marc_file eq '')) {
32         print <<EOF
33 If your ISO2709 file already has biblionumbers, you can use this script
34 to import the MARC into your database.
35 parameters :
36 \th : this version/help screen
37 \tfile /path/to/file/to/dump : the file to dump
38 SAMPLE : 
39 \t\$ export KOHA_CONF=/etc/koha.conf
40 \t\$ perl misc/marcimport_to_biblioitems.pl  -file /home/jmf/koha.mrc 
41 EOF
42 ;#'
43         die;
44 }
45 my $starttime = gettimeofday;
46 my $timeneeded;
47 my $dbh = C4::Context->dbh;
48
49 my $sth2=$dbh->prepare("update biblioitems  set marc=? where biblionumber=?");
50 my $fh = IO::File->new($input_marc_file); # don't let MARC::Batch open the file, as it applies the ':utf8' IO layer
51 my $batch = MARC::Batch->new( 'USMARC', $fh );
52 $batch->warnings_off();
53 $batch->strict_off();
54 my ($tagfield,$biblionumtagsubfield) = &GetMarcFromKohaField("biblio.biblionumber","");
55
56 my $i=0;
57 while ( my $record = $batch->next() ) {
58         my $biblionumber=$record->field($tagfield)->subfield($biblionumtagsubfield);
59         $i++;
60         $sth2->execute($record->as_usmarc,$biblionumber) if $biblionumber;
61         print "$biblionumber \n";
62 }
63
64 $timeneeded = gettimeofday - $starttime ;
65 print "$i records in $timeneeded s\n" ;
66
67 END;
68 # IS THIS SUPPOSED TO BE __END__ ??  If not, then what is it?  --JBA
69
70 sub search {
71         my ($query)=@_;
72         my $nquery="\ \@attr 1=1007  ".$query;
73         my $oAuth=C4::Context->Zconn("biblioserver");
74         if ($oAuth eq "error"){
75                 warn "Error/CONNECTING \n";
76                 return("error",undef);
77         }
78         my $oAResult;
79         my $Anewq= new ZOOM::Query::PQF($nquery);
80         eval {
81         $oAResult= $oAuth->search_pqf($nquery) ; 
82         };
83         if($@){
84                 warn " /Cannot search:", $@->code()," /MSG:",$@->message(),"\n";
85                 return("error",undef);
86         }
87         my $authrecord;
88         my $nbresults="0";
89         $nbresults=$oAResult->size();
90         if ($nbresults eq "1" ){
91                 my $rec=$oAResult->record(0);
92                 my $marcdata=$rec->raw();
93                 $authrecord = MARC::File::USMARC::decode($marcdata);
94         }
95         return ($authrecord,$nbresults);
96 }