Itemtypes, branch, and categories are displayed using their respective description...
[koha.git] / misc / 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
13 use Getopt::Long;
14 my ( $input_marc_file, $number) = ('',0);
15 my ($version, $delete, $test_parameter,$char_encoding);
16 GetOptions(
17     'file:s'    => \$input_marc_file,
18     'n' => \$number,
19     'v' => \$version,
20     'd' => \$delete,
21     't' => \$test_parameter,
22     'c:s' => \$char_encoding,
23 );
24
25 if ($version || ($input_marc_file eq '')) {
26         print <<EOF
27 small script to import an iso2709 file into Koha.
28 parameters :
29 \tv : this version/help screen
30 \tfile /path/to/file/to/dump : the file to dump
31 \tn : the number of the record to import. If missing, all the file is imported
32 \tt : test mode : parses the file, saying what he would do, but doing nothing.
33 \tc : the char encoding. At the moment, only USMARC and UNIMARC supported. USMARC by default.
34 \d : delete EVERYTHING related to biblio in koha-DB before import  :tables :
35 \t\tbiblio, \t\tbiblioitems, \t\tsubjects,\titems
36 \t\tadditionalauthors, \tbibliosubtitles, \tmarc_biblio,
37 \t\tmarc_subfield_table, \tmarc_word, \t\tmarc_blob_subfield
38 IMPORTANT : don't use this script before you've entered and checked twice (or more) your  MARC parameters tables.
39 If you fail this, the import won't work correctly and you will get invalid datas.
40
41 SAMPLE : ./bulkmarcimport.pl -file /home/paul/koha.dev/local/npl -n 1
42 EOF
43 ;#'
44 die;
45 }
46
47 my $dbh = C4::Context->dbh;
48
49 if ($delete) {
50         print "deleting biblios\n";
51         $dbh->do("delete from biblio");
52         $dbh->do("delete from biblioitems");
53         $dbh->do("delete from items");
54         $dbh->do("delete from bibliosubject");
55         $dbh->do("delete from additionalauthors");
56         $dbh->do("delete from bibliosubtitle");
57         $dbh->do("delete from marc_biblio");
58         $dbh->do("delete from marc_subfield_table");
59         $dbh->do("delete from marc_word");
60         $dbh->do("delete from marc_blob_subfield");
61 }
62 if ($test_parameter) {
63         print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
64 }
65 $char_encoding = 'USMARC' unless ($char_encoding);
66 print "CHAR : $char_encoding\n";
67 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
68 $batch->warnings_off();
69 $batch->strict_off();
70 my $i=1;
71 #1st of all, find item MARC tag.
72 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
73
74 while ( my $record = $batch->next() ) {
75         $i++;
76         #now, parse the record, extract the item fields, and store them in somewhere else.
77         $record = MARC::File::USMARC::decode(char_decode($record->as_usmarc(),$char_encoding));
78         my @fields = $record->field($tagfield);
79         print "biblio $i";
80         my @items;
81         my $nbitems;
82
83         foreach my $field (@fields) {
84                 my $item = MARC::Record->new();
85                 $item->append_fields($field);
86                 push @items,$item;
87                 $record->delete_field($field);
88                 $nbitems++;
89         }
90         print " : $nbitems items found\n";
91         # now, create biblio and items with NEWnewXX call.
92         unless ($test_parameter) {
93                 my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbiblio($dbh,$record);
94                 warn "ADDED biblio NB $bibid in DB\n";
95                 for (my $i=0;$i<=$#items;$i++) {
96                         NEWnewitem($dbh,$items[$i],$bibid);
97                 }
98         }
99 }