From a56795b386ec4ef61d8cc8f2dc30b794d8ffafe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Demians?= Date: Sat, 8 Oct 2011 17:03:18 +0200 Subject: [PATCH] TransformKohaToMarc enhancement 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. It uses already cached mapping in C4::Context. --- C4/Biblio.pm | 51 ++++++++++++++++++--------------------------------- 1 files changed, 18 insertions(+), 33 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 915139e..1e8443a 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1782,17 +1782,30 @@ 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 + 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->{$_}, '' ); + my $db_to_marc = C4::Context->marcfromkohafield; + 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 +1816,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 -- 1.7.6.1