From 02d392a502571e14df9373782343cf5e3bbe83b6 Mon Sep 17 00:00:00 2001 From: =?utf8?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. Signed-off-by: Chris Cormack http://bugs.koha-community.org/show_bug.cgi?id=6990 Signed-off-by: Paul Poulain --- C4/Biblio.pm | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index ac78ae379e..ad17b2e50c 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1827,17 +1827,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; } @@ -1928,34 +1941,6 @@ sub PrepHostMarcField { =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 -- 2.39.5