synch'ing 2.2 and head
[koha.git] / misc / migration_tools / bulkmarcimport.pl
1 #!/usr/bin/perl
2 # small script that import an iso2709 file into koha 2.0
3
4 use strict;
5
6 # Koha modules used
7 use MARC::File::USMARC;
8 use MARC::Record;
9 use MARC::Batch;
10 use C4::Context;
11 use C4::Biblio;
12 use Time::HiRes qw(gettimeofday);
13
14 use Getopt::Long;
15 my ( $input_marc_file, $number) = ('',0);
16 my ($version, $delete, $test_parameter,$char_encoding, $verbose);
17 GetOptions(
18     'file:s'    => \$input_marc_file,
19     'n' => \$number,
20     'h' => \$version,
21     'd' => \$delete,
22     't' => \$test_parameter,
23     'c:s' => \$char_encoding,
24     'v:s' => \$verbose,
25 );
26
27 if ($version || ($input_marc_file eq '')) {
28         print <<EOF
29 small script to import an iso2709 file into Koha.
30 parameters :
31 \th : this version/help screen
32 \tfile /path/to/file/to/dump : the file to dump
33 \tv : verbose mode. 1 means "some infos", 2 means "MARC dumping"
34 \tn : the number of the record to import. If missing, all the file is imported
35 \tt : test mode : parses the file, saying what he would do, but doing nothing.
36 \tc : the char encoding. At the moment, only MARC21 and UNIMARC supported. MARC21 by default.
37 \d : delete EVERYTHING related to biblio in koha-DB before import  :tables :
38 \t\tbiblio, \t\tbiblioitems, \t\tsubjects,\titems
39 \t\tadditionalauthors, \tbibliosubtitles, \tmarc_biblio,
40 \t\tmarc_subfield_table, \tmarc_word, \t\tmarc_blob_subfield
41 IMPORTANT : don't use this script before you've entered and checked twice (or more) your  MARC parameters tables.
42 If you fail this, the import won't work correctly and you will get invalid datas.
43
44 SAMPLE : ./bulkmarcimport.pl -file /home/paul/koha.dev/local/npl -n 1
45 EOF
46 ;#'
47 die;
48 }
49
50 my $dbh = C4::Context->dbh;
51
52 if ($delete) {
53         print "deleting biblios\n";
54         $dbh->do("delete from biblio");
55         $dbh->do("delete from biblioitems");
56         $dbh->do("delete from items");
57         $dbh->do("delete from bibliosubject");
58         $dbh->do("delete from additionalauthors");
59         $dbh->do("delete from bibliosubtitle");
60         $dbh->do("delete from marc_biblio");
61         $dbh->do("delete from marc_subfield_table");
62         $dbh->do("delete from marc_word");
63         $dbh->do("delete from marc_blob_subfield");
64 }
65 if ($test_parameter) {
66         print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
67 }
68
69 $char_encoding = 'MARC21' unless ($char_encoding);
70 print "CHAR : $char_encoding\n" if $verbose;
71 my $starttime = gettimeofday;
72 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
73 $batch->warnings_off();
74 $batch->strict_off();
75 my $i=0;
76 #1st of all, find item MARC tag.
77 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",'');
78 # $dbh->do("lock tables biblio write, biblioitems write, items write, marc_biblio write, marc_subfield_table write, marc_blob_subfield write, marc_word write, marc_subfield_structure write, stopwords write");
79 while ( my $record = $batch->next() ) {
80         $i++;
81         #now, parse the record, extract the item fields, and store them in somewhere else.
82
83     ## create an empty record object to populate
84     my $newRecord = MARC::Record->new();
85         $newRecord->leader($record->leader());
86
87     # go through each field in the existing record
88     foreach my $oldField ( $record->fields() ) {
89
90         # just reproduce tags < 010 in our new record
91         if ( $oldField->tag() < 10 ) {
92             $newRecord->append_fields( $oldField );
93             next();
94         }
95
96         # store our new subfield data in this list
97         my @newSubfields = ();
98
99         # go through each subfield code/data pair
100         foreach my $pair ( $oldField->subfields() ) { 
101                 $pair->[1] =~ s/\<//g;
102                 $pair->[1] =~ s/\>//g;
103                 push( @newSubfields, $pair->[0], char_decode($pair->[1],$char_encoding) );
104         }
105
106         # add the new field to our new record
107         my $newField = MARC::Field->new(
108             $oldField->tag(),
109             $oldField->indicator(1),
110             $oldField->indicator(2),
111             @newSubfields
112         );
113
114         $newRecord->append_fields( $newField );
115
116     }
117
118
119         warn "$i ==>".$newRecord->as_formatted() if $verbose eq 2;
120         my @fields = $newRecord->field($tagfield);
121         my @items;
122         my $nbitems=0;
123
124         foreach my $field (@fields) {
125                 my $item = MARC::Record->new();
126                 $item->append_fields($field);
127                 push @items,$item;
128                 $newRecord->delete_field($field);
129                 $nbitems++;
130         }
131         print "$i : $nbitems items found\n" if $verbose;
132         # now, create biblio and items with NEWnewXX call.
133         unless ($test_parameter) {
134                 my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$newRecord,'');
135                 warn "ADDED biblio NB $bibid in DB\n" if $verbose;
136                 for (my $i=0;$i<=$#items;$i++) {
137                         NEWnewitem($dbh,$items[$i],$bibid);
138                 }
139         }
140 }
141 # $dbh->do("unlock tables");
142 my $timeneeded = gettimeofday - $starttime;
143 print "$i MARC record done in $timeneeded seconds";