From 36fc3fca3021413c64797af02f1b00f7f4c75ba2 Mon Sep 17 00:00:00 2001
From: BibLibre <dev_patches@biblibre.com>
Date: Wed, 8 Feb 2012 18:41:59 +0100
Subject: [PATCH] Bug 7513 Marc Import Hangs

On some record, the commit_biblio_file is creating wide character
because as_xml is not used with correct parameter.
This patch fixes that.
To test on a UNIMARC Koha, stage attachment 7510 and then import.
It hangs before the patch, it passes after.
---
 C4/ImportBatch.pm |   20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index b6db406..6662c56 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -308,7 +308,7 @@ sub AddItemsToImportBiblio {
                                         VALUES (?, ?, ?)");
         $sth->bind_param(1, $import_record_id);
         $sth->bind_param(2, 'staged');
-        $sth->bind_param(3, $item_marc->as_xml());
+        $sth->bind_param(3, $item_marc->as_xml(C4::Context->preference('marcflavour')));
         $sth->execute();
         push @import_items_ids, $dbh->{'mysql_insertid'};
         $sth->finish();
@@ -440,8 +440,11 @@ sub BatchCommitBibRecords {
             next;
         }
 
-        my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
+        my $marc_record = eval{MARC::Record->new_from_usmarc($rowref->{'marc'})};
 
+	if ($@ or !defined $marc_record ){
+	    next;
+	}
         # remove any item tags - rely on BatchCommitItems
         my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",'');
         foreach my $item_field ($marc_record->field($item_tag)) {
@@ -455,7 +458,12 @@ sub BatchCommitBibRecords {
 
         if ($bib_result eq 'create_new') {
             $num_added++;
-            my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, $framework);
+            my ($biblionumber, $biblioitemnumber) = eval{AddBiblio($marc_record, $framework)};
+	    if ($@ or !defined $biblionumber){
+	       $num_added--;
+	       $num_ignored++;
+	       next;
+	    }
             my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
             $sth->execute($biblionumber, $rowref->{'import_record_id'});
             $sth->finish();
@@ -480,7 +488,7 @@ sub BatchCommitBibRecords {
 
             ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
             my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
-            $sth->execute($old_marc->as_xml(), $rowref->{'import_record_id'});
+            $sth->execute($old_marc->as_xml(C4::Context->preference('marcflavour')), $rowref->{'import_record_id'});
             $sth->finish();
             my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
             $sth2->execute($biblionumber, $rowref->{'import_record_id'});
@@ -1137,7 +1145,7 @@ sub _create_import_record {
     my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, 
                                                          record_type, encoding, z3950random)
                                     VALUES (?, ?, ?, ?, ?, ?, ?)");
-    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
+    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(C4::Context->preference('marcflavour')),
                   $record_type, $encoding, $z3950random);
     my $import_record_id = $dbh->{'mysql_insertid'};
     $sth->finish();
@@ -1150,7 +1158,7 @@ sub _update_import_record_marc {
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
                              WHERE  import_record_id = ?");
-    $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
+    $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(C4::Context->preference('marcflavour')), $import_record_id);
     $sth->finish();
 }
 
-- 
1.7.2.5