From ff85c7b1aff3078899b74705adcc11f916c8d838 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Mon, 7 Aug 2017 21:24:48 +0200
Subject: [PATCH] Bug 19049: Restore import with to_marc plugin
Content-Type: text/plain; charset=utf-8

Still needs some polishing and testing.
---
 C4/ImportBatch.pm          | 44 +++++++++++++++++++++++++++++++++++++-------
 tools/stage-marc-import.pl |  4 +++-
 2 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index c4550eb..83a5b08 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -399,13 +399,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');
@@ -1556,6 +1549,43 @@ 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, $encoding) = @_;
+
+    # Read input file
+    open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
+    my ( $text, $marc, @return );
+    $/ = "\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,
+        method => 'to_marc',
+        params => { data => $text },
+    });
+
+    # Convert to array of MARC records
+    my $marc_type = C4::Context->preference('marcflavour');
+    foreach my $blob ( split(/\x1D/, $text) ) {
+        my ($marcrecord) = MarcToUTF8Record($blob, $marc_type, $encoding);
+        push @return, $marcrecord;
+    }
+    return \@return;
+}
+
 # internal functions
 
 sub _create_import_record {
diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl
index 63dd4e5..34bced4 100755
--- a/tools/stage-marc-import.pl
+++ b/tools/stage-marc-import.pl
@@ -92,8 +92,10 @@ if ($completedJobID) {
     my ( $errors, $marcrecords );
     if( $format eq 'MARCXML' ) {
         ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
-    } else {
+    } elsif( $format eq 'MARC' ) {
         ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
+    } else { # plugin
+        ( $marcrecords ) = C4::ImportBatch::RecordsFromMarcPlugin( $file, $to_marc_plugin, $encoding );
     }
     warn "$filename: " . ( join ',', @$errors ) if @$errors;
         # no need to exit if we have no records (or only errors) here
-- 
2.1.4