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;
22 # According to kados, an undocumented feature of setting MARC::Charset to
23 # ignore_errors(1) is that errors are not ignored. Instead of deleting the
24 # whole subfield when a character does not translate properly from MARC8 into
25 # UTF-8, just the problem characters are deleted. This should solve at least
26 # some of the fixme problems for fMARC8ToUTF8().
28 # Problems remain if there are MARC 21 records where 000/09 is set incorrectly.
30 # MARC::Charset->ignore_errors(1);
35 use Unicode::Normalize;
36 use Time::HiRes qw(gettimeofday);
38 binmode(STDOUT, ":utf8");
42 my ( $input_marc_file, $number) = ('',0);
43 my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off);
48 'commit:f' => \$commit,
49 'file:s' => \$input_marc_file,
53 't' => \$test_parameter,
54 's' => \$skip_marc8_conversion,
55 'c:s' => \$char_encoding,
60 # FIXME: Management of error conditions needed for record parsing problems
61 # and MARC8 character sets with mappings to Unicode not yet included in
62 # MARC::Charset. The real world rarity of these problems is not fully tested.
63 # Unmapped character sets will throw a warning currently and processing will
64 # continue with the error condition. A fairly trivial correction should
65 # address some record parsing and unmapped character set problems but I need
66 # time to implement a test and correction for undef subfields and revert to
67 # MARC8 if mappings are missing. -- thd
68 sub fMARC8ToUTF8($$) {
70 my ($verbose) = shift;
72 foreach my $field ($record->fields()) {
73 if ($field->is_control_field()) {
74 ; # do nothing -- control fields should not contain non-ASCII characters
77 my $fieldName = $field->tag();
78 my $indicator1Value = $field->indicator(1);
79 my $indicator2Value = $field->indicator(2);
80 foreach my $subfield ($field->subfields()) {
81 my $subfieldName = $subfield->[0];
82 my $subfieldValue = $subfield->[1];
83 my $utf8sf = MARC::Charset::marc8_to_utf8($subfieldValue);
84 unless (defined $utf8sf) {
85 # For now, we're being very strict about
86 # error during the MARC8 conversion, so return
87 # if there's a problem.
90 $subfieldValue = NFC($utf8sf); # Normalization Form C to assist
91 # some browswers (e.g., Firefox on OS X)
92 # that have issues with decomposed characters
95 # Alas, MARC::Field::update() does not work correctly.
96 ## push (@subfieldsArray, $subfieldName, $subfieldValue);
98 push @subfieldsArray, [$subfieldName, $subfieldValue];
101 # Alas, MARC::Field::update() does not work correctly.
103 # The first instance in the field of a of a repeated subfield
104 # overwrites the content from later instances with the content
105 # from the first instance.
106 ## $field->update(@subfieldsArray);
108 foreach my $subfieldRow(@subfieldsArray) {
109 my $subfieldName = $subfieldRow->[0];
110 $field->delete_subfields($subfieldName);
112 foreach my $subfieldRow(@subfieldsArray) {
113 $field->add_subfields(@$subfieldRow);
118 # Reading the indicator values again is not necessary.
119 # They were not converted.
120 # $indicator1Value = $field->indicator(1);
121 # $indicator2Value = $field->indicator(2);
122 # $indicator1Value =~ s/ /#/;
123 # $indicator2Value =~ s/ /#/;
124 print "\nCONVERTED TO UTF-8:\n" . $fieldName . ' ' .
127 foreach my $subfield ($field->subfields()) {
128 my $subfieldName = $subfield->[0];
129 my $subfieldValue = $subfield->[1];
130 print " \$" . $subfieldName . ' ' . $subfieldValue;
136 print "\n" if $verbose;
142 # must set Leader/09 to 'a' to indicate that
143 # record is now in UTF-8
144 my $leader = $record->leader();
145 substr($leader, 9, 1) = 'a';
146 $record->leader($leader);
148 $record->encoding('UTF-8');
153 if ($version || ($input_marc_file eq '')) {
155 small script to import an iso2709 file into Koha.
157 \th : this version/help screen
158 \tfile /path/to/file/to/dump : the file to import
159 \tv : verbose mode. 1 means "some infos", 2 means "MARC dumping"
160 \tfk : Turn off foreign key checks during import.
161 \tn : the number of records to import. If missing, all the file is imported
162 \tcommit : the number of records to wait before performing a 'commit' operation
163 \tt : test mode : parses the file, saying what he would do, but doing nothing.
164 \ts : skip automatic conversion of MARC-8 to UTF-8. This option is
165 \t provided for debugging.
166 \tc : the characteristic MARC flavour. At the moment, only MARC21 and UNIMARC
167 \tsupported. MARC21 by default.
168 \td : delete EVERYTHING related to biblio in koha-DB before import :tables :
169 \t\tbiblio, \tbiblioitems,\titems
170 IMPORTANT : don't use this script before you've entered and checked your MARC parameters tables twice (or more!).
171 Otherwise, the import won't work correctly and you will get invalid data.
174 \t\$ export KOHA_CONF=/etc/koha.conf
175 \t\$ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 -file /home/jmf/koha.mrc -n 3000
181 my $dbh = C4::Context->dbh;
183 # save the CataloguingLog property : we don't want to log a bulkmarcimport. It will slow the import &
184 # will create problems in the action_logs table, that can't handle more than 1 entry per second per user.
185 my $CataloguingLog = C4::Context->preference('CataloguingLog');
186 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
189 print "deleting biblios\n";
190 $dbh->do("truncate biblio");
191 $dbh->do("truncate biblioitems");
192 $dbh->do("truncate items");
193 $dbh->do("truncate zebraqueue");
196 $dbh->do("SET FOREIGN_KEY_CHECKS = 0");
198 if ($test_parameter) {
199 print "TESTING MODE ONLY\n DOING NOTHING\n===============\n";
202 my $marcFlavour = C4::Context->preference('marcflavour') || 'MARC21';
204 print "Characteristic MARC flavour: $marcFlavour\n" if $verbose;
205 my $starttime = gettimeofday;
206 my $batch = MARC::Batch->new( 'USMARC', $input_marc_file );
207 $batch->warnings_off();
208 $batch->strict_off();
214 $commitnum = $commit;
218 $dbh->{AutoCommit} = 0;
219 RECORD: while ( my $record = $batch->next() ) {
222 print "\r$i" unless $i % 100;
224 if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
225 unless (fMARC8ToUTF8($record, $verbose)) {
226 warn "ERROR: failed to perform character conversion for record $i\n";
231 unless ($test_parameter) {
232 my ( $biblionumber, $biblioitemnumber, $itemnumbers_ref, $errors_ref );
233 eval { ( $biblionumber, $biblioitemnumber ) = AddBiblio($record, '', { defer_marc_save => 1 }) };
235 warn "ERROR: Adding biblio $biblionumber failed: $@\n";
238 eval { ( $itemnumbers_ref, $errors_ref ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
240 warn "ERROR: Adding items to bib $biblionumber failed: $@\n";
241 # if we failed because of an exception, assume that
242 # the MARC columns in biblioitems were not set.
243 ModBiblioMarc( $record, $biblionumber, '' );
246 if ($#{ $errors_ref } > -1) {
247 report_item_errors($biblionumber, $errors_ref);
250 $dbh->commit() if (0 == $i % $commitnum);
252 last if $i == $number;
258 $dbh->do("SET FOREIGN_KEY_CHECKS = 1");
261 # restore CataloguingLog
262 $dbh->do("UPDATE systempreferences SET value=$CataloguingLog WHERE variable='CataloguingLog'");
264 my $timeneeded = gettimeofday - $starttime;
265 print "\n$i MARC records done in $timeneeded seconds\n";
269 sub report_item_errors {
270 my $biblionumber = shift;
271 my $errors_ref = shift;
273 foreach my $error (@{ $errors_ref }) {
274 my $msg = "Item not added (bib $biblionumber, item tag #$error->{'item_sequence'}, barcode $error->{'item_barcode'}): ";
275 my $error_code = $error->{'error_code'};
276 $error_code =~ s/_/ /g;
277 $msg .= "$error_code $error->{'error_information'}";