Merge branch 'bug_7368' into 3.14-master
[koha.git] / misc / migration_tools / bulkmarcimport.pl
1 #!/usr/bin/perl
2 # Import an iso2709 file into Koha 3
3
4 use strict;
5 use warnings;
6 #use diagnostics;
7 BEGIN {
8     # find Koha's Perl modules
9     # test carefully before changing this
10     use FindBin;
11     eval { require "$FindBin::Bin/../kohalib.pl" };
12 }
13
14 # Koha modules used
15 use MARC::File::USMARC;
16 use MARC::File::XML;
17 use MARC::Record;
18 use MARC::Batch;
19 use MARC::Charset;
20
21 use C4::Context;
22 use C4::Biblio;
23 use C4::Koha;
24 use C4::Debug;
25 use C4::Charset;
26 use C4::Items;
27 use YAML;
28 use Unicode::Normalize;
29 use Time::HiRes qw(gettimeofday);
30 use Getopt::Long;
31 use IO::File;
32 use Pod::Usage;
33
34 use open qw( :std :encoding(UTF-8) );
35 binmode( STDOUT, ":encoding(UTF-8)" );
36 my ( $input_marc_file, $number, $offset) = ('',0,0);
37 my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
38 my ( $insert, $filters, $update, $all, $yamlfile, $authtypes );
39 my $cleanisbn = 1;
40 my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
41 my $framework = '';
42
43 $|=1;
44
45 GetOptions(
46     'commit:f'    => \$commit,
47     'file:s'    => \$input_marc_file,
48     'n:f' => \$number,
49     'o|offset:f' => \$offset,
50     'h' => \$version,
51     'd' => \$delete,
52     't|test' => \$test_parameter,
53     's' => \$skip_marc8_conversion,
54     'c:s' => \$char_encoding,
55     'v:s' => \$verbose,
56     'fk' => \$fk_off,
57     'm:s' => \$format,
58     'l:s' => \$logfile,
59     'k|keepids:s' => \$keepids,
60     'b|biblios' => \$biblios,
61     'a|authorities' => \$authorities,
62     'authtypes:s' => \$authtypes,
63     'filter=s@'     => \$filters,
64     'insert'        => \$insert,
65     'update'        => \$update,
66     'all'           => \$all,
67     'match=s@'    => \$match,
68     'i|isbn' => \$isbn_check,
69     'x:s' => \$sourcetag,
70     'y:s' => \$sourcesubfield,
71     'idmap:s' => \$idmapfl,
72     'cleanisbn!'     => \$cleanisbn,
73     'yaml:s'        => \$yamlfile,
74     'dedupbarcode' => \$dedup_barcode,
75     'framework=s' => \$framework,
76 );
77 $biblios ||= !$authorities;
78 $insert  ||= !$update;
79
80 if ($all) {
81     $insert = 1;
82     $update = 1;
83 }
84
85 if ($version || ($input_marc_file eq '')) {
86     pod2usage( -verbose => 2 );
87     exit;
88 }
89
90 my $dbh = C4::Context->dbh;
91 my $heading_fields=get_heading_fields();
92
93 if (defined $idmapfl) {
94   open(IDMAP,">$idmapfl") or die "cannot open $idmapfl \n";
95 }
96
97 if ((not defined $sourcesubfield) && (not defined $sourcetag)){
98   $sourcetag="910";
99   $sourcesubfield="a";
100 }
101
102 # save the CataloguingLog property : we don't want to log a bulkmarcimport. It will slow the import & 
103 # will create problems in the action_logs table, that can't handle more than 1 entry per second per user.
104 my $CataloguingLog = C4::Context->preference('CataloguingLog');
105 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
106
107 if ($fk_off) {
108         $dbh->do("SET FOREIGN_KEY_CHECKS = 0");
109 }
110
111
112 if ($delete) {
113         if ($biblios){
114         print "deleting biblios\n";
115         $dbh->do("truncate biblio");
116         $dbh->do("truncate biblioitems");
117         $dbh->do("truncate items");
118         }
119         else {
120         print "deleting authorities\n";
121         $dbh->do("truncate auth_header");
122         }
123     $dbh->do("truncate zebraqueue");
124 }
125
126
127
128 if ($test_parameter) {
129     print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
130 }
131
132 my $marcFlavour = C4::Context->preference('marcflavour') || 'MARC21';
133
134 print "Characteristic MARC flavour: $marcFlavour\n" if $verbose;
135 my $starttime = gettimeofday;
136 my $batch;
137 my $fh = IO::File->new($input_marc_file); # don't let MARC::Batch open the file, as it applies the ':utf8' IO layer
138 if (defined $format && $format =~ /XML/i) {
139     # ugly hack follows -- MARC::File::XML, when used by MARC::Batch,
140     # appears to try to convert incoming XML records from MARC-8
141     # to UTF-8.  Setting the BinaryEncoding key turns that off
142     # TODO: see what happens to ISO-8859-1 XML files.
143     # TODO: determine if MARC::Batch can be fixed to handle
144     #       XML records properly -- it probably should be
145     #       be using a proper push or pull XML parser to
146     #       extract the records, not using regexes to look
147     #       for <record>.*</record>.
148     $MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
149     my $recordformat= ($marcFlavour eq "MARC21"?"USMARC":uc($marcFlavour));
150 #UNIMARC Authorities have a different way to manage encoding than UNIMARC biblios.
151     $recordformat=$recordformat."AUTH" if ($authorities and $marcFlavour ne "MARC21");
152     $MARC::File::XML::_load_args{RecordFormat} = $recordformat;
153     $batch = MARC::Batch->new( 'XML', $fh );
154 } else {
155     $batch = MARC::Batch->new( 'USMARC', $fh );
156 }
157 $batch->warnings_off();
158 $batch->strict_off();
159 my $i=0;
160 my $commitnum = $commit ? $commit : 50;
161 my $yamlhash;
162
163 # Skip file offset
164 if ( $offset ) {
165     print "Skipping file offset: $offset records\n";
166     $batch->next() while ($offset--);
167 }
168
169 my ($tagid,$subfieldid);
170 if ($authorities){
171           $tagid='001';
172 }
173 else {
174    ( $tagid, $subfieldid ) =
175             GetMarcFromKohaField( "biblio.biblionumber", $framework );
176         $tagid||="001";
177 }
178
179 # the SQL query to search on isbn
180 my $sth_isbn = $dbh->prepare("SELECT biblionumber,biblioitemnumber FROM biblioitems WHERE isbn=?");
181
182 $dbh->{AutoCommit} = 0;
183 my $loghandle;
184 if ($logfile){
185    $loghandle= IO::File->new($logfile,"w") ;
186    print $loghandle "id;operation;status\n";
187 }
188 RECORD: while (  ) {
189     my $record;
190     # get records
191     eval { $record = $batch->next() };
192     if ( $@ ) {
193         print "Bad MARC record $i: $@ skipped\n";
194         # FIXME - because MARC::Batch->next() combines grabbing the next
195         # blob and parsing it into one operation, a correctable condition
196         # such as a MARC-8 record claiming that it's UTF-8 can't be recovered
197         # from because we don't have access to the original blob.  Note
198         # that the staging import can deal with this condition (via
199         # C4::Charset::MarcToUTF8Record) because it doesn't use MARC::Batch.
200         next;
201     }
202     # skip if we get an empty record (that is MARC valid, but will result in AddBiblio failure
203     last unless ( $record );
204     $i++;
205     print ".";
206     print "\n$i" unless $i % 100;
207     
208     # transcode the record to UTF8 if needed & applicable.
209     if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
210         # FIXME update condition
211         my ($guessed_charset, $charset_errors);
212          ($record, $guessed_charset, $charset_errors) = MarcToUTF8Record($record, $marcFlavour.(($authorities and $marcFlavour ne "MARC21")?'AUTH':''));
213         if ($guessed_charset eq 'failed') {
214             warn "ERROR: failed to perform character conversion for record $i\n";
215             next RECORD;            
216         }
217     }
218     SetUTF8Flag($record);
219     my $isbn;
220     # remove trailing - in isbn (only for biblios, of course)
221     if ($biblios && $cleanisbn) {
222         my $tag = $marcFlavour eq 'UNIMARC' ? '010' : '020';
223         my $field = $record->field($tag);
224         my $isbn = $field && $field->subfield('a');
225         if ( $isbn ) {
226             $isbn =~ s/-//g;
227             $field->update('a' => $isbn);
228         }
229     }
230     my $id;
231     # search for duplicates (based on Local-number)
232     my $originalid;
233     $originalid = GetRecordId( $record, $tagid, $subfieldid );
234     if ($match) {
235         require C4::Search;
236         my $query = build_query( $match, $record );
237         my $server = ( $authorities ? 'authorityserver' : 'biblioserver' );
238         $debug && warn $query;
239         my ( $error, $results, $totalhits ) = C4::Search::SimpleSearch( $query, 0, 3, [$server] );
240         die "unable to search the database for duplicates : $error" if ( defined $error );
241         $debug && warn "$query $server : $totalhits";
242         if ( $results && scalar(@$results) == 1 ) {
243             my $marcrecord = MARC::File::USMARC::decode( $results->[0] );
244             SetUTF8Flag($marcrecord);
245             $id = GetRecordId( $marcrecord, $tagid, $subfieldid );
246             if ( $authorities && $marcFlavour ) {
247                 #Skip if authority in database is the same as the on in database
248                 if ( $marcrecord->field('005')->data >= $record->field('005')->data ) {
249                     if ($yamlfile) {
250                         $yamlhash->{$originalid}->{'authid'} = $id;
251
252                         # we recover all subfields of the heading authorities
253                         my @subfields;
254                         foreach my $field ( $marcrecord->field("2..") ) {
255                             push @subfields, map { ( $_->[0] =~ /[a-z]/ ? $_->[1] : () ) } $field->subfields();
256                         }
257                         $yamlhash->{$originalid}->{'subfields'} = \@subfields;
258                     }
259                     next;
260                 }
261             }
262         } elsif ( $results && scalar(@$results) > 1 ) {
263             $debug && warn "more than one match for $query";
264         } else {
265             $debug && warn "nomatch for $query";
266         }
267     }
268     if ($keepids && $originalid) {
269             my $storeidfield;
270             if ( length($keepids) == 3 ) {
271                 $storeidfield = MARC::Field->new( $keepids, $originalid );
272             } else {
273                 $storeidfield = MARC::Field->new( substr( $keepids, 0, 3 ), "", "", substr( $keepids, 3, 1 ), $originalid );
274             }
275             $record->insert_fields_ordered($storeidfield);
276             $record->delete_field( $record->field($tagid) );
277     }
278     foreach my $stringfilter (@$filters) {
279         if ( length($stringfilter) == 3 ) {
280             foreach my $field ( $record->field($stringfilter) ) {
281                 $record->delete_field($field);
282                 $debug && warn "removed : ", $field->as_string;
283             }
284         } elsif ($stringfilter =~ /([0-9]{3})([a-z0-9])(.*)/) {
285             my $removetag = $1;
286             my $removesubfield = $2;
287             my $removematch = $3;
288             if ( ( $removetag > "010" ) && $removesubfield ) {
289                 foreach my $field ( $record->field($removetag) ) {
290                     $field->delete_subfield( code => "$removesubfield", match => $removematch );
291                     $debug && warn "Potentially removed : ", $field->subfield($removesubfield);
292                 }
293             }
294         }
295     }
296     unless ($test_parameter) {
297         if ($authorities){
298             use C4::AuthoritiesMarc;
299             my $authtypecode=GuessAuthTypeCode($record, $heading_fields);
300             my $authid= ($id?$id:GuessAuthId($record));
301             if ($authid && GetAuthority($authid) && $update ){
302             ## Authority has an id and is in database : Replace
303                 eval { ( $authid ) = ModAuthority($authid,$record, $authtypecode) };
304                 if ($@){
305                     warn "Problem with authority $authid Cannot Modify";
306                                         printlog({id=>$originalid||$id||$authid, op=>"edit",status=>"ERROR"}) if ($logfile);
307                 }
308                                 else{
309                                         printlog({id=>$originalid||$id||$authid, op=>"edit",status=>"ok"}) if ($logfile);
310                                 }
311             }  
312             elsif (defined $authid) {
313             ## An authid is defined but no authority in database : add
314                 eval { ( $authid ) = AddAuthority($record,$authid, $authtypecode) };
315                 if ($@){
316                     warn "Problem with authority $authid Cannot Add ".$@;
317                                         printlog({id=>$originalid||$id||$authid, op=>"insert",status=>"ERROR"}) if ($logfile);
318                 }
319                                 else{
320                                         printlog({id=>$originalid||$id||$authid, op=>"insert",status=>"ok"}) if ($logfile);
321                                 }
322             }
323                 else {
324             ## True insert in database
325                 eval { ( $authid ) = AddAuthority($record,"", $authtypecode) };
326                 if ($@){
327                     warn "Problem with authority $authid Cannot Add".$@;
328                                         printlog({id=>$originalid||$id||$authid, op=>"insert",status=>"ERROR"}) if ($logfile);
329                 }
330                                 else{
331                                         printlog({id=>$originalid||$id||$authid, op=>"insert",status=>"ok"}) if ($logfile);
332                                 }
333                 }
334             if ($yamlfile) {
335             $yamlhash->{$originalid}->{'authid'} = $authid;
336             my @subfields;
337             foreach my $field ( $record->field("2..") ) {
338                 push @subfields, map { ( $_->[0] =~ /[a-z]/ ? $_->[1] : () ) } $field->subfields();
339             }
340             $yamlhash->{$originalid}->{'subfields'} = \@subfields;
341             }
342         }
343         else {
344             my ( $biblionumber, $biblioitemnumber, $itemnumbers_ref, $errors_ref );
345             $biblionumber = $id;
346             # check for duplicate, based on ISBN (skip it if we already have found a duplicate with match parameter
347             if (!$biblionumber && $isbn_check && $isbn) {
348     #         warn "search ISBN : $isbn";
349                 $sth_isbn->execute($isbn);
350                 ($biblionumber,$biblioitemnumber) = $sth_isbn->fetchrow;
351             }
352                 if (defined $idmapfl) {
353                                 if ($sourcetag < "010"){
354                                         if ($record->field($sourcetag)){
355                                           my $source = $record->field($sourcetag)->data();
356                                           printf(IDMAP "%s|%s\n",$source,$biblionumber);
357                                         }
358                             } else {
359                                         my $source=$record->subfield($sourcetag,$sourcesubfield);
360                                         printf(IDMAP "%s|%s\n",$source,$biblionumber);
361                           }
362                         }
363                                         # create biblio, unless we already have it ( either match or isbn )
364             if ($biblionumber) {
365                 eval{$biblioitemnumber=GetBiblioData($biblionumber)->{biblioitemnumber};};
366                 if ($update) {
367                     eval { ( $biblionumber, $biblioitemnumber ) = ModBiblio( $record, $biblionumber, GetFrameworkcode($biblionumber) ) };
368                     if ($@) {
369                         warn "ERROR: Edit biblio $biblionumber failed: $@\n";
370                         printlog( { id => $id || $originalid || $biblionumber, op => "update", status => "ERROR" } ) if ($logfile);
371                         next RECORD;
372                     } else {
373                         printlog( { id => $id || $originalid || $biblionumber, op => "update", status => "ok" } ) if ($logfile);
374                     }
375                 } else {
376                     printlog( { id => $id || $originalid || $biblionumber, op => "insert", status => "warning : already in database" } ) if ($logfile);
377                 }
378             } else {
379                 if ($insert) {
380                     eval { ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '', { defer_marc_save => 1 } ) };
381                     if ($@) {
382                         warn "ERROR: Adding biblio $biblionumber failed: $@\n";
383                         printlog( { id => $id || $originalid || $biblionumber, op => "insert", status => "ERROR" } ) if ($logfile);
384                         next RECORD;
385                     } else {
386                         printlog( { id => $id || $originalid || $biblionumber, op => "insert", status => "ok" } ) if ($logfile);
387                     }
388                 } else {
389                     printlog( { id => $id || $originalid || $biblionumber, op => "update", status => "warning : not in database" } ) if ($logfile);
390                 }
391             }
392             eval { ( $itemnumbers_ref, $errors_ref ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
393             my $error_adding = $@;
394             # Work on a clone so that if there are real errors, we can maybe
395             # fix them up later.
396                         my $clone_record = $record->clone();
397             C4::Biblio::_strip_item_fields($clone_record, '');
398             # This sets the marc fields if there was an error, and also calls
399             # defer_marc_save.
400             ModBiblioMarc( $clone_record, $biblionumber, $framework );
401             if ( $error_adding ) {
402                 warn "ERROR: Adding items to bib $biblionumber failed: $error_adding";
403                                 printlog({id=>$id||$originalid||$biblionumber, op=>"insertitem",status=>"ERROR"}) if ($logfile);
404                 # if we failed because of an exception, assume that 
405                 # the MARC columns in biblioitems were not set.
406                 next RECORD;
407             }
408                         else{
409                                 printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
410                         }
411             if ($dedup_barcode && grep { exists $_->{error_code} && $_->{error_code} eq 'duplicate_barcode' } @$errors_ref) {
412                 # Find the record called 'barcode'
413                 my ($tag, $sub) = C4::Biblio::GetMarcFromKohaField('items.barcode', $framework);
414                 # Now remove any items that didn't have a duplicate_barcode error,
415                 # erase the barcodes on items that did, and re-add those items.
416                 my %dupes;
417                 foreach my $i (0 .. $#{$errors_ref}) {
418                     my $ref = $errors_ref->[$i];
419                     if ($ref && ($ref->{error_code} eq 'duplicate_barcode')) {
420                         $dupes{$ref->{item_sequence}} = 1;
421                         # Delete the error message because we're going to
422                         # retry this one.
423                         delete $errors_ref->[$i];
424                     }
425                 }
426                 my $seq = 0;
427                 foreach my $field ($record->field($tag)) {
428                     $seq++;
429                     if ($dupes{$seq}) {
430                         # Here we remove the barcode
431                         $field->delete_subfield(code => $sub);
432                     } else {
433                         # otherwise we delete the field because we don't want
434                         # two of them
435                         $record->delete_fields($field);
436                     }
437                 }
438                 # Now re-add the record as before, adding errors to the prev list
439                 my $more_errors;
440                 eval { ( $itemnumbers_ref, $more_errors ) = AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); };
441                 if ( $@ ) {
442                     warn "ERROR: Adding items to bib $biblionumber failed: $@\n";
443                     printlog({id=>$id||$originalid||$biblionumber, op=>"insertitem",status=>"ERROR"}) if ($logfile);
444                     # if we failed because of an exception, assume that
445                     # the MARC columns in biblioitems were not set.
446                     ModBiblioMarc( $record, $biblionumber, $framework );
447                     next RECORD;
448                 } else {
449                     printlog({id=>$id||$originalid||$biblionumber, op=>"insert",status=>"ok"}) if ($logfile);
450                 }
451                 push @$errors_ref, @{ $more_errors };
452             }
453             if ($#{ $errors_ref } > -1) {
454                 report_item_errors($biblionumber, $errors_ref);
455             }
456             $yamlhash->{$originalid} = $biblionumber if ($yamlfile);
457         }
458         $dbh->commit() if (0 == $i % $commitnum);
459     }
460     last if $i == $number;
461 }
462 $dbh->commit();
463
464
465
466 if ($fk_off) {
467         $dbh->do("SET FOREIGN_KEY_CHECKS = 1");
468 }
469
470 # restore CataloguingLog
471 $dbh->do("UPDATE systempreferences SET value=$CataloguingLog WHERE variable='CataloguingLog'");
472
473 my $timeneeded = gettimeofday - $starttime;
474 print "\n$i MARC records done in $timeneeded seconds\n";
475 if ($logfile){
476   print $loghandle "file : $input_marc_file\n";
477   print $loghandle "$i MARC records done in $timeneeded seconds\n";
478   $loghandle->close;
479 }
480 if ($yamlfile) {
481     open my $yamlfileout, q{>}, "$yamlfile" or die "cannot open $yamlfile \n";
482     print $yamlfileout Dump($yamlhash);
483 }
484 exit 0;
485
486 sub GetRecordId{
487         my $marcrecord=shift;
488         my $tag=shift;
489         my $subfield=shift;
490         my $id;
491         if ($tag lt "010"){
492                 return $marcrecord->field($tag)->data() if $marcrecord->field($tag);
493         } 
494         elsif ($subfield){
495                 if ($marcrecord->field($tag)){
496                         return $marcrecord->subfield($tag,$subfield);
497                 }
498         }
499         return $id;
500 }
501 sub build_query {
502         my $match = shift;
503         my $record=shift;
504         my @searchstrings;
505         foreach my $matchingpoint (@$match){
506           my $string = build_simplequery($matchingpoint,$record);
507           push @searchstrings,$string if (length($string)>0);
508         }
509     my $QParser;
510     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
511     my $op;
512     if ($QParser) {
513         $op = '&&';
514     } else {
515         $op = 'and';
516     }
517     return join(" $op ",@searchstrings);
518 }
519 sub build_simplequery {
520         my $element=shift;
521         my $record=shift;
522     my @searchstrings;
523     my ($index,$recorddata)=split /,/,$element;
524     if ($recorddata=~/(\d{3})(.*)/) {
525         my ($tag,$subfields) =($1,$2);
526         foreach my $field ($record->field($tag)){
527                   if (length($field->as_string("$subfields"))>0){
528               push @searchstrings,"$index:\"".$field->as_string("$subfields")."\"";
529                   }
530         }
531     }
532     my $QParser;
533     $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
534     my $op;
535     if ($QParser) {
536         $op = '&&';
537     } else {
538         $op = 'and';
539     }
540     return join(" $op ",@searchstrings);
541 }
542 sub report_item_errors {
543     my $biblionumber = shift;
544     my $errors_ref = shift;
545
546     foreach my $error (@{ $errors_ref }) {
547         next if !$error;
548         my $msg = "Item not added (bib $biblionumber, item tag #$error->{'item_sequence'}, barcode $error->{'item_barcode'}): ";
549         my $error_code = $error->{'error_code'};
550         $error_code =~ s/_/ /g;
551         $msg .= "$error_code $error->{'error_information'}";
552         print $msg, "\n";
553     }
554 }
555 sub printlog{
556         my $logelements=shift;
557     print $loghandle join( ";", map { defined $_ ? $_ : "" } @$logelements{qw<id op status>} ), "\n";
558 }
559 sub get_heading_fields{
560     my $headingfields;
561     if ($authtypes){
562         $headingfields=YAML::LoadFile($authtypes);
563         $headingfields={C4::Context->preference('marcflavour')=>$headingfields};
564         $debug && warn YAML::Dump($headingfields);
565     }
566     unless ($headingfields){
567         $headingfields=$dbh->selectall_hashref("SELECT auth_tag_to_report, authtypecode from auth_types",'auth_tag_to_report',{Slice=>{}});
568         $headingfields={C4::Context->preference('marcflavour')=>$headingfields};
569     }
570     return $headingfields;
571 }
572
573 =head1 NAME
574
575 bulkmarcimport.pl - Import bibliographic/authority records into Koha
576
577 =head1 USAGE
578
579  $ export KOHA_CONF=/etc/koha.conf
580  $ perl misc/migration_tools/bulkmarcimport.pl -d -commit 1000 \\
581     -file /home/jmf/koha.mrc -n 3000
582
583 =head1 WARNING
584
585 Don't use this script before you've entered and checked your MARC parameters
586 tables twice (or more!). Otherwise, the import won't work correctly and you
587 will get invalid data.
588
589 =head1 DESCRIPTION
590
591 =over
592
593 =item  B<-h>
594
595 This version/help screen
596
597 =item B<-b, -biblios>
598
599 Type of import: bibliographic records
600
601 =item B<-a, -authorities>
602
603 Type of import: authority records
604
605 =item B<-file>=I<FILE>
606
607 The I<FILE> to import
608
609 =item  B<-v>
610
611 Verbose mode. 1 means "some infos", 2 means "MARC dumping"
612
613 =item B<-fk>
614
615 Turn off foreign key checks during import.
616
617 =item B<-n>=I<NUMBER>
618
619 The I<NUMBER> of records to import. If missing, all the file is imported
620
621 =item B<-o, -offset>=I<NUMBER>
622
623 File offset before importing, ie I<NUMBER> of records to skip.
624
625 =item B<-commit>=I<NUMBER>
626
627 The I<NUMBER> of records to wait before performing a 'commit' operation
628
629 =item B<-l>
630
631 File logs actions done for each record and their status into file
632
633 =item B<-t, -test>
634
635 Test mode: parses the file, saying what he would do, but doing nothing.
636
637 =item B<-s>
638
639 Skip automatic conversion of MARC-8 to UTF-8.  This option is provided for
640 debugging.
641
642 =item B<-c>=I<CHARACTERISTIC>
643
644 The I<CHARACTERISTIC> MARC flavour. At the moment, only I<MARC21> and
645 I<UNIMARC> are supported. MARC21 by default.
646
647 =item B<-d>
648
649 Delete EVERYTHING related to biblio in koha-DB before import. Tables: biblio,
650 biblioitems, items
651
652 =item B<-m>=I<FORMAT>
653
654 Input file I<FORMAT>: I<MARCXML> or I<ISO2709> (defaults to ISO2709)
655
656 =item B<-authtypes>
657
658 file yamlfile with authoritiesTypes and distinguishable record field in order
659 to store the correct authtype
660
661 =item B<-yaml>
662
663 yaml file  format a yaml file with ids
664
665 =item B<-filter>
666
667 list of fields that will not be imported. Can be any from 000 to 999 or field,
668 subfield and subfield's matching value such as 200avalue
669
670 =item B<-insert>
671
672 if set, only insert when possible
673
674 =item B<-update>
675
676 if set, only updates (any biblio should have a matching record)
677
678 =item B<-all>
679
680 if set, do whatever is required
681
682 =item B<-k, -keepids>=<FIELD>
683
684 Field store ids in I<FIELD> (usefull for authorities, where 001 contains the
685 authid for Koha, that can contain a very valuable info for authorities coming
686 from LOC or BNF. useless for biblios probably)
687
688 =item B<-match>=<FIELD>
689
690 I<FIELD> matchindex,fieldtomatch matchpoint to use to deduplicate fieldtomatch
691 can be either 001 to 999 or field and list of subfields as such 100abcde
692
693 =item B<-i,-isbn>
694
695 If set, a search will be done on isbn, and, if the same isbn is found, the
696 biblio is not added. It's another method to deduplicate.  B<-match> & B<-isbn>
697 can be both set.
698
699 =item B<-cleanisbn>
700
701 Clean ISBN fields from entering biblio records, ie removes hyphens. By default,
702 ISBN are cleaned. --nocleanisbn will keep ISBN unchanged.
703
704 =item B<-x>=I<TAG>
705
706 Source bib I<TAG> for reporting the source bib number
707
708 =item B<-y>=I<SUBFIELD>
709
710 Source I<SUBFIELD> for reporting the source bib number
711
712 =item B<-idmap>=I<FILE>
713
714 I<FILE> for the koha bib and source id
715
716 =item B<-keepids>
717
718 Store ids in 009 (usefull for authorities, where 001 contains the authid for
719 Koha, that can contain a very valuable info for authorities coming from LOC or
720 BNF. useless for biblios probably)
721
722 =item B<-dedupbarcode>
723
724 If set, whenever a duplicate barcode is detected, it is removed and the attempt
725 to add the record is retried, thereby giving the record a blank barcode. This
726 is useful when something has set barcodes to be a biblio ID, or similar
727 (usually other software.)
728
729 =item B<-framework>
730
731 This is the code for the framework that the requested records will have attached
732 to them when they are created. If not specified, then the default framework
733 will be used.
734
735 =back
736
737 =cut
738