[Koha-patches] [PATCH] Bug 6990 TransformKohaToMarc enhancement

Frédéric Demians f.demians at tamil.fr
Sat Oct 8 17:06:11 CEST 2011


TransformKohaToMarc function is called for each biblio and item that has
to be build. This function execute a DB statement for each Koha field
that has to be mapped to a MARC tag/letter. This impact deeply
performances for script like rebuild_zebra, especially since items are
not anymore in bilio records and have to be rebuild on the fly.

I'm proposing a patch which read Koha field to MARC field mapping just
one time and cache it. My test show a 30% execution time improvement on
rebuild_zebra.pl script.
---
 C4/Biblio.pm |   65 ++++++++++++++++++++++++++++-----------------------------
 1 files changed, 32 insertions(+), 33 deletions(-)
 mode change 100644 => 100755 Makefile.PL

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 915139e..823b4f2 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -1782,17 +1782,44 @@ sub GetFrameworkCode {
 This function builds partial MARC::Record from a hash
 Hash entries can be from biblio or biblioitems.
 
-This function is called in acquisition module, to create a basic catalogue entry from user entry
+This function is called in acquisition module, to create a basic catalogue
+entry from user entry
 
 =cut
 
+
+# A hashref giving for each Koha field its MARC tag/letter
+my $db_to_marc;
+
 sub TransformKohaToMarc {
-    my ($hash) = @_;
-    my $sth    = C4::Context->dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
+    my $hash = shift;
     my $record = MARC::Record->new();
     SetMarcUnicodeFlag( $record, C4::Context->preference("marcflavour") );
-    foreach ( keys %{$hash} ) {
-        &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
+    unless ($db_to_marc) {
+        my $dbh = C4::Context->dbh;
+        my $sth = $dbh->prepare(
+            "SELECT kohafield, tagfield, tagsubfield
+             FROM   marc_subfield_structure
+             WHERE  kohafield <> '' AND frameworkcode = ''" );
+        $sth->execute();
+        $db_to_marc = {}; 
+        while ( my ($name, $tag, $letter) = $sth->fetchrow ) { 
+            $db_to_marc->{$name} = [$tag, $letter];
+        }
+    }
+    while ( my ($name, $value) = each %$hash ) {
+        next unless my $dtm = $db_to_marc->{$name};
+        my ($tag, $letter) = @$dtm;
+        foreach my $value ( split(/\s?\|\s?/, $value, -1) ) {
+            if ( my $field = $record->field($tag) ) {
+                $field->add_subfields( $letter => $value );
+            }
+            else {
+                $record->insert_fields_ordered( MARC::Field->new(
+                    $tag, " ", " ", $letter => $value ) );
+            }
+        }
+
     }
     return $record;
 }
@@ -1803,34 +1830,6 @@ sub TransformKohaToMarc {
 
 =cut
 
-sub TransformKohaToMarcOneField {
-    my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
-    $frameworkcode = '' unless $frameworkcode;
-    my $tagfield;
-    my $tagsubfield;
-
-    if ( !defined $sth ) {
-        my $dbh = C4::Context->dbh;
-        $sth = $dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
-    }
-    $sth->execute( $frameworkcode, $kohafieldname );
-    if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
-        my @values = split(/\s?\|\s?/, $value, -1);
-        
-        foreach my $itemvalue (@values){
-        my $tag = $record->field($tagfield);
-        if ($tag) {
-                $tag->add_subfields( $tagsubfield => $itemvalue );
-            $record->delete_field($tag);
-            $record->insert_fields_ordered($tag);
-            }
-            else {
-                $record->add_fields( $tagfield, " ", " ", $tagsubfield => $itemvalue );
-            }
-        }
-    }
-    return $record;
-}
 
 =head2 TransformHtmlToXml
 
diff --git a/Makefile.PL b/Makefile.PL
old mode 100644
new mode 100755
-- 
1.7.6.1



More information about the Koha-patches mailing list