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