Corrected some subfield visibility values and improved the ease of
[koha.git] / misc / bulkauthimport.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::AuthoritiesMarc;
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 authorities\n";
54         $dbh->do("delete from auth_header");
55         $dbh->do("delete from auth_subfield_table");
56         $dbh->do("delete from auth_word");
57 }
58 if ($test_parameter) {
59         print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
60 }
61
62 $char_encoding = 'MARC21' unless ($char_encoding);
63 print "CHAR : $char_encoding\n" if $verbose;
64 my $starttime = gettimeofday;
65 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
66 $batch->warnings_off();
67 $batch->strict_off();
68 my $i=0;
69 while ( my $record = $batch->next() ) {
70         $i++;
71         #now, parse the record, extract the item fields, and store them in somewhere else.
72
73     ## create an empty record object to populate
74     my $newRecord = MARC::Record->new();
75         $newRecord->leader($record->leader);
76     # go through each field in the existing record
77     foreach my $oldField ( $record->fields() ) {
78                 # just reproduce tags < 010 in our new record
79                 if ( $oldField->tag() < 10 ) {
80                         $newRecord->append_fields( $oldField );
81                         next();
82                 }
83                 # store our new subfield data in this list
84                 my @newSubfields = ();
85         
86                 # go through each subfield code/data pair
87                 foreach my $pair ( $oldField->subfields() ) { 
88                         $pair->[1] =~ s/\<//g;
89                         $pair->[1] =~ s/\>//g;
90                         push( @newSubfields, $pair->[0], char_decode($pair->[1],$char_encoding) );
91                 }
92         
93                 # add the new field to our new record
94                 my $newField = MARC::Field->new(
95                         $oldField->tag(),
96                         $oldField->indicator(1),
97                         $oldField->indicator(2),
98                         @newSubfields
99                 );
100                 $newRecord->append_fields( $newField );
101     }
102         warn "$i ==>".$newRecord->as_formatted() if $verbose eq 2;
103         my $authtypecode=substr($newRecord->leader(),9,1);
104         $authtypecode="NP" if ($authtypecode eq 'a'); # personnes
105         $authtypecode="CO" if ($authtypecode eq 'b'); # collectivités
106         $authtypecode="NG" if ($authtypecode eq 'c'); # géographique
107         $authtypecode="NM" if ($authtypecode eq 'd'); # marque
108         $authtypecode="NF" if ($authtypecode eq 'e'); # famille
109         $authtypecode="TI" if ($authtypecode eq 'f'); # Titre uniforme
110         $authtypecode="TI" if ($authtypecode eq 'h'); # auteur/titre
111         $authtypecode="MM" if ($authtypecode eq 'j'); # mot matière
112         warn "XX => $authtypecode";
113         # now, create biblio and items with NEWnewXX call.
114         unless ($test_parameter) {
115                 my ($authid) = AUTHaddauthority($dbh,$newRecord,0,$authtypecode);
116                 warn "ADDED authority NB $authid in DB\n" if $verbose;
117         }
118 }
119 # $dbh->do("unlock tables");
120 my $timeneeded = gettimeofday - $starttime;
121 print "$i MARC record done in $timeneeded seconds";