Bug 9811: Remove useless orderby management
[koha_fer] / misc / migration_tools / bulkmarcimport.pl
index 113aaf2..4a47de0 100755 (executable)
@@ -35,10 +35,11 @@ use open qw( :std :encoding(UTF-8) );
 binmode( STDOUT, ":encoding(UTF-8)" );
 my ( $input_marc_file, $number, $offset) = ('',0,0);
 my ($version, $delete, $test_parameter, $skip_marc8_conversion, $char_encoding, $verbose, $commit, $fk_off,$format,$biblios,$authorities,$keepids,$match, $isbn_check, $logfile);
-my ( $insert, $filters, $update, $all, $yamlfile, $authtypes );
+my ( $insert, $filters, $update, $all, $yamlfile, $authtypes, $append );
 my $cleanisbn = 1;
 my ($sourcetag,$sourcesubfield,$idmapfl, $dedup_barcode);
 my $framework = '';
+my $localcust;
 
 $|=1;
 
@@ -52,10 +53,11 @@ GetOptions(
     't|test' => \$test_parameter,
     's' => \$skip_marc8_conversion,
     'c:s' => \$char_encoding,
-    'v:s' => \$verbose,
+    'v:+' => \$verbose,
     'fk' => \$fk_off,
     'm:s' => \$format,
     'l:s' => \$logfile,
+    'append' => \$append,
     'k|keepids:s' => \$keepids,
     'b|biblios' => \$biblios,
     'a|authorities' => \$authorities,
@@ -73,9 +75,11 @@ GetOptions(
     'yaml:s'        => \$yamlfile,
     'dedupbarcode' => \$dedup_barcode,
     'framework=s' => \$framework,
+    'custom:s'    => \$localcust,
 );
 $biblios ||= !$authorities;
 $insert  ||= !$update;
+my $writemode = ($append) ? "a" : "w";
 
 if ($all) {
     $insert = 1;
@@ -87,6 +91,24 @@ if ($version || ($input_marc_file eq '')) {
     exit;
 }
 
+if(defined $localcust) { #local customize module
+    if(!-e $localcust) {
+        $localcust= $localcust||'LocalChanges'; #default name
+        $localcust=~ s/^.*\/([^\/]+)$/$1/; #extract file name only
+        $localcust=~ s/\.pm$//;           #remove extension
+        my $fqcust= $FindBin::Bin."/$localcust.pm"; #try migration_tools dir
+        if(-e $fqcust) {
+            $localcust= $fqcust;
+        }
+        else {
+            print "WARNING: customize module $localcust.pm not found!\n";
+            exit 1;
+        }
+    }
+    require $localcust if $localcust;
+    $localcust=\&customize if $localcust;
+}
+
 my $dbh = C4::Context->dbh;
 my $heading_fields=get_heading_fields();
 
@@ -182,7 +204,7 @@ my $sth_isbn = $dbh->prepare("SELECT biblionumber,biblioitemnumber FROM biblioit
 $dbh->{AutoCommit} = 0;
 my $loghandle;
 if ($logfile){
-   $loghandle= IO::File->new($logfile,"w") ;
+   $loghandle= IO::File->new($logfile, $writemode) ;
    print $loghandle "id;operation;status\n";
 }
 RECORD: while (  ) {
@@ -202,9 +224,10 @@ RECORD: while (  ) {
     # skip if we get an empty record (that is MARC valid, but will result in AddBiblio failure
     last unless ( $record );
     $i++;
-    print ".";
-    print "\n$i" unless $i % 100;
-    
+    if( ($verbose//1)==1 ) { #no dot for verbose==2
+        print "." . ( $i % 100==0 ? "\n$i" : '' );
+    }
+
     # transcode the record to UTF8 if needed & applicable.
     if ($record->encoding() eq 'MARC-8' and not $skip_marc8_conversion) {
         # FIXME update condition
@@ -216,6 +239,7 @@ RECORD: while (  ) {
         }
     }
     SetUTF8Flag($record);
+    &$localcust($record) if $localcust;
     my $isbn;
     # remove trailing - in isbn (only for biblios, of course)
     if ($biblios && $cleanisbn) {
@@ -240,12 +264,14 @@ RECORD: while (  ) {
         die "unable to search the database for duplicates : $error" if ( defined $error );
         $debug && warn "$query $server : $totalhits";
         if ( $results && scalar(@$results) == 1 ) {
-            my $marcrecord = MARC::File::USMARC::decode( $results->[0] );
+            my $marcrecord = C4::Search::new_record_from_zebra( $server, $results->[0] );
             SetUTF8Flag($marcrecord);
             $id = GetRecordId( $marcrecord, $tagid, $subfieldid );
             if ( $authorities && $marcFlavour ) {
                 #Skip if authority in database is the same as the on in database
-                if ( $marcrecord->field('005')->data >= $record->field('005')->data ) {
+                if ( $marcrecord->field('005') && $record->field('005') &&
+                     $marcrecord->field('005')->data && $record->field('005')->data &&
+                     $marcrecord->field('005')->data >= $record->field('005')->data ) {
                     if ($yamlfile) {
                         $yamlhash->{$originalid}->{'authid'} = $id;
 
@@ -457,10 +483,11 @@ RECORD: while (  ) {
         }
         $dbh->commit() if (0 == $i % $commitnum);
     }
+    print $record->as_formatted()."\n" if ($verbose//0)==2;
     last if $i == $number;
 }
 $dbh->commit();
-
+$dbh->{AutoCommit} = 1;
 
 
 if ($fk_off) {
@@ -630,6 +657,10 @@ The I<NUMBER> of records to wait before performing a 'commit' operation
 
 File logs actions done for each record and their status into file
 
+=item B<-append>
+
+If specified, data will be appended to the logfile. If not, the logfile will be erased for each execution.
+
 =item B<-t, -test>
 
 Test mode: parses the file, saying what he would do, but doing nothing.
@@ -732,6 +763,14 @@ This is the code for the framework that the requested records will have attached
 to them when they are created. If not specified, then the default framework
 will be used.
 
+=item B<-custom>=I<MODULE>
+
+This parameter allows you to use a local module with a customize subroutine
+that is called for each MARC record.
+If no filename is passed, LocalChanges.pm is assumed to be in the
+migration_tools subdirectory. You may pass an absolute file name or a file name
+from the migration_tools directory.
+
 =back
 
 =cut