2 # Import an iso2709 file into Koha 3
7 # find Koha's Perl modules
8 # test carefully before changing this
10 eval { require "$FindBin::Bin/../kohalib.pl" };
14 use MARC::File::USMARC;
15 # Uncomment the line below and use MARC::File::XML again when it works better.
17 # use MARC::File::XML;
26 use Unicode::Normalize;
27 use Time::HiRes qw(gettimeofday);
29 binmode(STDOUT, ":utf8");
33 my ( $input_marc_file, $number) = ('',0);
34 my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format);
39 'commit:f' => \$commit,
40 'file:s' => \$input_marc_file,
44 't' => \$test_parameter,
45 's' => \$skip_marc8_conversion,
46 'c:s' => \$char_encoding,
52 if ($version || ($input_marc_file eq '')) {
54 small script to import an iso2709 file into Koha.
56 \th : this version/help screen
57 \tfile /path/to/file/to/dump : the file to import
58 \tv : verbose mode. 1 means "some infos", 2 means "MARC dumping"
59 \tfk : Turn off foreign key checks during import.
60 \tn : the number of records to import. If missing, all the file is imported
61 \tcommit : the number of records to wait before performing a 'commit' operation
62 \tt : test mode : parses the file, saying what he would do, but doing nothing.
63 \ts : skip automatic conversion of MARC-8 to UTF-8. This option is
64 \t provided for debugging.
65 \tc : the characteristic MARC flavour. At the moment, only MARC21 and UNIMARC
66 \tsupported. MARC21 by default.
67 \td : delete EVERYTHING related to biblio in koha-DB before import :tables :
68 \t\tbiblio, \tbiblioitems,\titems
69 IMPORTANT : don't use this script before you've entered and checked your MARC parameters tables twice (or more!).
70 Otherwise, the import won't work correctly and you will get invalid data.
73 \t\$ export KOHA_CONF=/etc/koha.conf
74 \t\$ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 -file /home/jmf/koha.mrc -n 3000
80 my $dbh = C4::Context->dbh;
82 # save the CataloguingLog property : we don't want to log a bulkmarcimport. It will slow the import &
83 # will create problems in the action_logs table, that can't handle more than 1 entry per second per user.
84 my $CataloguingLog = C4::Context->preference('CataloguingLog');
85 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
88 $dbh->do("SET FOREIGN_KEY_CHECKS = 0");
93 print "deleting biblios\n";
94 $dbh->do("truncate biblio");
95 $dbh->do("truncate biblioitems");
96 $dbh->do("truncate items");
97 $dbh->do("truncate zebraqueue");
102 if ($test_parameter) {
103 print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
106 my $marcFlavour = C4::Context->preference('marcflavour') || 'MARC21';
108 print "Characteristic MARC flavour: $marcFlavour\n" if $verbose;
109 my $starttime = gettimeofday;
111 if ($format =~ /XML/i) {
112 $batch = MARC::Batch->new( 'XML', $input_marc_file );
114 $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
116 $batch->warnings_off();
117 $batch->strict_off();
123 $commitnum = $commit;
127 $dbh->{AutoCommit} = 0;
128 RECORD: while ( my $record = $batch->next() ) {
131 print "\r$i" unless $i % 100;
133 if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
134 # FIXME update condition
135 my ($guessed_charset, $charset_errors);
136 ($record, $guessed_charset, $charset_errors) = MarcToUTF8Record($record, $marcFlavour);
137 if ($guessed_charset eq 'failed') {
138 warn "ERROR: failed to perform character conversion for record $i\n";
143 unless ($test_parameter) {
144 my ( $biblionumber, $biblioitemnumber, $itemnumbers_ref, $errors_ref );
145 eval { ( $biblionumber, $biblioitemnumber ) = AddBiblio($record, '', { defer_marc_save => 1 }) };
147 warn "ERROR: Adding biblio $biblionumber failed: $@\n";
150 eval { ( $itemnumbers_ref, $errors_ref ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
152 warn "ERROR: Adding items to bib $biblionumber failed: $@\n";
153 # if we failed because of an exception, assume that
154 # the MARC columns in biblioitems were not set.
155 ModBiblioMarc( $record, $biblionumber, '' );
158 if ($#{ $errors_ref } > -1) {
159 report_item_errors($biblionumber, $errors_ref);
162 $dbh->commit() if (0 == $i % $commitnum);
164 last if $i == $number;
170 $dbh->do("SET FOREIGN_KEY_CHECKS = 1");
173 # restore CataloguingLog
174 $dbh->do("UPDATE systempreferences SET value=$CataloguingLog WHERE variable='CataloguingLog'");
176 my $timeneeded = gettimeofday - $starttime;
177 print "\n$i MARC records done in $timeneeded seconds\n";
181 sub report_item_errors {
182 my $biblionumber = shift;
183 my $errors_ref = shift;
185 foreach my $error (@{ $errors_ref }) {
186 my $msg = "Item not added (bib $biblionumber, item tag #$error->{'item_sequence'}, barcode $error->{'item_barcode'}): ";
187 my $error_code = $error->{'error_code'};
188 $error_code =~ s/_/ /g;
189 $msg .= "$error_code $error->{'error_information'}";