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