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