fixing nomenclature on files in misc/, adding a few new utilities
[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 my  $input_marc_file = '';
23 my ($version);
24 GetOptions(
25     'file:s'    => \$input_marc_file,
26     'h' => \$version,
27 );
28
29 if ($version || ($input_marc_file eq '')) {
30         print <<EOF
31 If your ISO2709 file already has biblionumbers, you can use this script
32 to import the MARC into your database.
33 parameters :
34 \th : this version/help screen
35 \tfile /path/to/file/to/dump : the file to dump
36 SAMPLE : 
37 \t\$ export KOHA_CONF=/etc/koha.conf
38 \t\$ perl misc/marcimport_to_biblioitems.pl  -file /home/jmf/koha.mrc 
39 EOF
40 ;#'
41         die;
42 }
43 my $starttime = gettimeofday;
44 my $timeneeded;
45 my $dbh = C4::Context->dbh;
46
47 my $sth2=$dbh->prepare("update biblioitems  set marc=? where biblionumber=?");
48 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
49 $batch->warnings_off();
50 $batch->strict_off();
51 my ($tagfield,$biblionumtagsubfield) = &GetMarcFromKohaField("biblio.biblionumber","");
52
53 my $i=0;
54 while ( my $record = $batch->next() ) {
55         my $biblionumber=$record->field($tagfield)->subfield($biblionumtagsubfield);
56         $i++;
57         $sth2->execute($record->as_usmarc,$biblionumber) if $biblionumber;
58         print "$biblionumber \n";
59 }
60
61 $timeneeded = gettimeofday - $starttime ;
62 print "$i records in $timeneeded s\n" ;
63
64 END;
65 # IS THIS SUPPOSED TO BE __END__ ??  If not, then what is it?  --JBA
66
67 sub search {
68         my ($query)=@_;
69         my $nquery="\ \@attr 1=1007  ".$query;
70         my $oAuth=C4::Context->Zconn("biblioserver");
71         if ($oAuth eq "error"){
72                 warn "Error/CONNECTING \n";
73                 return("error",undef);
74         }
75         my $oAResult;
76         my $Anewq= new ZOOM::Query::PQF($nquery);
77         eval {
78         $oAResult= $oAuth->search_pqf($nquery) ; 
79         };
80         if($@){
81                 warn " /Cannot search:", $@->code()," /MSG:",$@->message(),"\n";
82                 return("error",undef);
83         }
84         my $authrecord;
85         my $nbresults="0";
86         $nbresults=$oAResult->size();
87         if ($nbresults eq "1" ){
88                 my $rec=$oAResult->record(0);
89                 my $marcdata=$rec->raw();
90                 $authrecord = MARC::File::USMARC::decode($marcdata);
91         }
92         return ($authrecord,$nbresults);
93 }