Bug 22532: Remove Z39.50 random
[koha-equinox.git] / C4 / ImportBatch.pm
index c4550eb..bb3393c 100644 (file)
@@ -27,6 +27,7 @@ use C4::Items;
 use C4::Charset;
 use C4::AuthoritiesMarc;
 use C4::MarcModificationTemplates;
+use Koha::Items;
 use Koha::Plugins::Handler;
 use Koha::Logger;
 
@@ -278,7 +279,7 @@ sub GetImportBatch {
 =head2 AddBiblioToBatch 
 
   my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, 
-                $marc_record, $encoding, $z3950random, $update_counts);
+                $marc_record, $encoding, $update_counts);
 
 =cut
 
@@ -287,10 +288,9 @@ sub AddBiblioToBatch {
     my $record_sequence = shift;
     my $marc_record = shift;
     my $encoding = shift;
-    my $z3950random = shift;
     my $update_counts = @_ ? shift : 1;
 
-    my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random, C4::Context->preference('marcflavour'));
+    my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, C4::Context->preference('marcflavour'));
     _add_biblio_fields($import_record_id, $marc_record);
     _update_batch_record_counts($batch_id) if $update_counts;
     return $import_record_id;
@@ -313,7 +313,7 @@ sub ModBiblioInBatch {
 =head2 AddAuthToBatch
 
   my $import_record_id = AddAuthToBatch($batch_id, $record_sequence,
-                $marc_record, $encoding, $z3950random, $update_counts, [$marc_type]);
+                $marc_record, $encoding, $update_counts, [$marc_type]);
 
 =cut
 
@@ -322,13 +322,12 @@ sub AddAuthToBatch {
     my $record_sequence = shift;
     my $marc_record = shift;
     my $encoding = shift;
-    my $z3950random = shift;
     my $update_counts = @_ ? shift : 1;
     my $marc_type = shift || C4::Context->preference('marcflavour');
 
     $marc_type = 'UNIMARCAUTH' if $marc_type eq 'UNIMARC';
 
-    my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $z3950random, $marc_type);
+    my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'auth', $encoding, $marc_type);
     _add_auth_fields($import_record_id, $marc_record);
     _update_batch_record_counts($batch_id) if $update_counts;
     return $import_record_id;
@@ -352,8 +351,8 @@ sub ModAuthInBatch {
 
 ( $batch_id, $num_records, $num_items, @invalid_records ) =
   BatchStageMarcRecords(
-    $encoding,                   $marc_records,
-    $file_name,                  $to_marc_plugin,
+    $record_type,                $encoding,
+    $marc_records,               $file_name,
     $marc_modification_template, $comments,
     $branch_code,                $parse_items,
     $leave_as_staging,           $progress_interval,
@@ -367,7 +366,6 @@ sub BatchStageMarcRecords {
     my $encoding = shift;
     my $marc_records = shift;
     my $file_name = shift;
-    my $to_marc_plugin = shift;
     my $marc_modification_template = shift;
     my $comments = shift;
     my $branch_code = shift;
@@ -399,13 +397,6 @@ sub BatchStageMarcRecords {
         SetImportBatchItemAction($batch_id, 'ignore');
     }
 
-    $marc_records = Koha::Plugins::Handler->run(
-        {
-            class  => $to_marc_plugin,
-            method => 'to_marc',
-            params => { data => $marc_records }
-        }
-    ) if $to_marc_plugin && @$marc_records;
 
     my $marc_type = C4::Context->preference('marcflavour');
     $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth');
@@ -755,7 +746,7 @@ sub BatchCommitItems {
 
         my $item = TransformMarcToKoha( $item_marc );
 
-        my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode( $item->{'barcode'} );
+        my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
         my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
 
         my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
@@ -769,7 +760,7 @@ sub BatchCommitItems {
             $updsth->finish();
             $num_items_replaced++;
         } elsif ( $action eq "replace" && $duplicate_barcode ) {
-            my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} );
+            my $itemnumber = $duplicate_barcode->itemnumber;
             ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
@@ -1126,7 +1117,7 @@ sub GetImportRecordsRange {
     ( $order_by ) = grep( /^$order_by$/, qw( import_record_id title status overlay_status ) ) ? $order_by : 'import_record_id';
 
     my $order_by_direction =
-      uc( $parameters->{order_by_direction} ) eq 'DESC' ? 'DESC' : 'ASC';
+      uc( $parameters->{order_by_direction} // 'ASC' ) eq 'DESC' ? 'DESC' : 'ASC';
 
     $order_by .= " $order_by_direction, authorized_heading" if $order_by eq 'title';
 
@@ -1556,17 +1547,58 @@ sub RecordsFromMARCXMLFile {
     return (\@errors, \@marcRecords);
 }
 
+=head2 RecordsFromMarcPlugin
+
+    Converts text of input_file into array of MARC records with to_marc plugin
+
+=cut
+
+sub RecordsFromMarcPlugin {
+    my ($input_file, $plugin_class, $encoding) = @_;
+    my ( $text, @return );
+    return \@return if !$input_file || !$plugin_class;
+
+    # Read input file
+    open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+    $/ = "\035";
+    while (<IN>) {
+        s/^\s+//;
+        s/\s+$//;
+        next unless $_;
+        $text .= $_;
+    }
+    close IN;
+
+    # Convert to large MARC blob with plugin
+    $text = Koha::Plugins::Handler->run({
+        class  => $plugin_class,
+        method => 'to_marc',
+        params => { data => $text },
+    }) if $text;
+
+    # Convert to array of MARC records
+    if( $text ) {
+        my $marc_type = C4::Context->preference('marcflavour');
+        foreach my $blob ( split(/\x1D/, $text) ) {
+            next if $blob =~ /^\s*$/;
+            my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
+            push @return, $marcrecord;
+        }
+    }
+    return \@return;
+}
+
 # internal functions
 
 sub _create_import_record {
-    my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random, $marc_type) = @_;
+    my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $marc_type) = @_;
 
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, 
-                                                         record_type, encoding, z3950random)
+    my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, marcxml_old,
+                                                         record_type, encoding)
                                     VALUES (?, ?, ?, ?, ?, ?, ?)");
-    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type),
-                  $record_type, $encoding, $z3950random);
+    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml($marc_type), '',
+                  $record_type, $encoding);
     my $import_record_id = $dbh->{'mysql_insertid'};
     $sth->finish();
     return $import_record_id;