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