Revert "Merging hdl's Search.pm changes"
[koha.git] / misc / bulkauthimport.pl
blobcd80aba024f2fad70cf64b1e08a44039c323a4e7
1 #!/usr/bin/perl
2 # small script that import an iso2709 file into koha 2.0
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" };
12 # Koha modules used
13 use MARC::File::USMARC;
14 use MARC::Record;
15 use MARC::Batch;
16 use C4::Context;
17 use C4::AuthoritiesMarc;
18 use Time::HiRes qw(gettimeofday);
20 use Getopt::Long;
21 my ( $input_marc_file, $number) = ('',0);
22 my ($version, $delete, $test_parameter,$char_encoding, $verbose);
23 GetOptions(
24 'file:s' => \$input_marc_file,
25 'n' => \$number,
26 'h' => \$version,
27 'd' => \$delete,
28 't' => \$test_parameter,
29 'c:s' => \$char_encoding,
30 'v:s' => \$verbose,
33 if ($version || ($input_marc_file eq '')) {
34 print <<EOF
35 small script to import an iso2709 file into Koha.
36 parameters :
37 \th : this version/help screen
38 \tfile /path/to/file/to/dump : the file to dump
39 \tv : verbose mode. 1 means "some infos", 2 means "MARC dumping"
40 \tn : the number of the record to import. If missing, all the file is imported
41 \tt : test mode : parses the file, saying what he would do, but doing nothing.
42 \tc : the char encoding. At the moment, only MARC21 and UNIMARC supported. MARC21 by default.
43 \td : delete EVERYTHING related to biblio in koha-DB before import :tables :
44 \t\tbiblio, \t\tbiblioitems, \t\tsubjects,\titems
45 \tmarc_biblio,
46 \t\tmarc_subfield_table, \tmarc_word, \t\tmarc_blob_subfield
47 IMPORTANT : don't use this script before you've entered and checked twice (or more) your MARC parameters tables.
48 If you fail this, the import won't work correctly and you will get invalid datas.
50 SAMPLE : ./bulkmarcimport.pl -file /home/paul/koha.dev/local/npl -n 1
51 EOF
52 ;#'
53 die;
56 my $dbh = C4::Context->dbh;
58 if ($delete) {
59 print "deleting authorities\n";
60 $dbh->do("delete from auth_header");
62 if ($test_parameter) {
63 print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
66 $char_encoding = 'MARC21' unless ($char_encoding);
67 print "CHAR : $char_encoding\n" if $verbose;
68 my $starttime = gettimeofday;
69 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
70 $batch->warnings_off();
71 $batch->strict_off();
72 my $i=0;
73 while ( my $record = $batch->next() ) {
74 $i++;
75 #now, parse the record, extract the item fields, and store them in somewhere else.
77 ## create an empty record object to populate
78 my $newRecord = MARC::Record->new();
79 $newRecord->leader($record->leader);
80 # go through each field in the existing record
81 foreach my $oldField ( $record->fields() ) {
82 # just reproduce tags < 010 in our new record
83 if ( $oldField->tag() < 10 ) {
84 $newRecord->append_fields( $oldField );
85 next();
87 # store our new subfield data in this list
88 my @newSubfields = ();
90 # go through each subfield code/data pair
91 foreach my $pair ( $oldField->subfields() ) {
92 $pair->[1] =~ s/\<//g;
93 $pair->[1] =~ s/\>//g;
94 push( @newSubfields, $pair->[0], char_decode($pair->[1],$char_encoding) );
97 # add the new field to our new record
98 my $newField = MARC::Field->new(
99 $oldField->tag(),
100 $oldField->indicator(1),
101 $oldField->indicator(2),
102 @newSubfields
104 $newRecord->append_fields( $newField );
106 warn "$i ==>".$newRecord->as_formatted() if $verbose eq 2;
107 my $authtypecode=substr($newRecord->leader(),9,1);
108 $authtypecode="NP" if ($authtypecode eq 'a'); # personnes
109 $authtypecode="CO" if ($authtypecode eq 'b'); # collectivit�
110 $authtypecode="NG" if ($authtypecode eq 'c'); # g�graphique
111 $authtypecode="NM" if ($authtypecode eq 'd'); # marque
112 $authtypecode="NF" if ($authtypecode eq 'e'); # famille
113 $authtypecode="TI" if ($authtypecode eq 'f'); # Titre uniforme
114 $authtypecode="TI" if ($authtypecode eq 'h'); # auteur/titre
115 $authtypecode="MM" if ($authtypecode eq 'j'); # mot mati�e
116 warn "XX => $authtypecode";
117 # now, create biblio and items with NEWnewXX call.
118 unless ($test_parameter) {
119 my ($authid) = AddAuthority($newRecord,0,$authtypecode);
120 warn "ADDED authority NB $authid in DB\n" if $verbose;
123 # $dbh->do("unlock tables");
124 my $timeneeded = gettimeofday - $starttime;
125 print "$i MARC record done in $timeneeded seconds";