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