From d25ad4d949ecc5fd40bfbe95bcd682e97c13f58c Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 24 May 2023 14:57:46 +0000 Subject: [PATCH] Bug 33404: Z3950SearchAuth: Save in UTF-8 encoding Test plan: [1] If you have access to a Z3950 MARC8 auth server, search for an authority record and import it. [2] If you have access to a Z3950 UTF8 auth server, search for an authority record and import it. Signed-off-by: Nick Clemens Signed-off-by: Marcel de Rooy Signed-off-by: Nick Clemens Signed-off-by: Jonathan Druart Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 1233480ffaecb307ae7e9279acee7b516e42776f) Signed-off-by: Martin Renvoize (cherry picked from commit 52a243e3cae0afc9d1cc9cc97f5746ba6384bf01) Signed-off-by: Matt Blenkinsop --- C4/Breeding.pm | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/C4/Breeding.pm b/C4/Breeding.pm index efc9029f48..45a0563dcc 100644 --- a/C4/Breeding.pm +++ b/C4/Breeding.pm @@ -478,9 +478,6 @@ sub ImportBreedingAuth { my $controlnumber = $marcrecord->field('001')->data; - # Normalize the record so it doesn't have separated diacritics - SetUTF8Flag($marcrecord); - $searchbreeding->execute($controlnumber,$heading); my ($breedingid) = $searchbreeding->fetchrow; @@ -578,31 +575,34 @@ sub Z3950SearchAuth { for ($i = ($page-1)*20; $i < (($numresults < ($page*20)) ? $numresults : ($page*20)); $i++) { my $rec = $oResult[$k]->record($i); if ($rec) { + my $marcdata = $rec->raw(); my $marcrecord; - my $marcdata; - $marcdata = $rec->raw(); - - my ($charset_result, $charset_errors); if( $servers[$k]->{servertype} eq 'sru' ) { - $marcrecord = MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} ); - $marcrecord->encoding('UTF-8'); + $marcrecord = eval { MARC::Record->new_from_xml( $marcdata, 'UTF-8', $servers[$k]->{syntax} ) }; + if( !$marcrecord || $@ ) { + _dump_conversion_error( $servers[$k]->{servername}, $marcdata, $@ ); + next; # skip this one + } } else { + my ($charset_result, $charset_errors); ( $marcrecord, $charset_result, $charset_errors ) = MarcToUTF8Record( $marcdata, $marc_type, $encoding[$k] ); + if( !$marcrecord || @$charset_errors ) { + _dump_conversion_error( $servers[$k]->{servername}, $marcdata, $charset_result, $charset_errors ); + next; # skip this one + } } - my $heading; - my $heading_authtype_code; - $heading_authtype_code = GuessAuthTypeCode($marcrecord); - next if ( not defined $heading_authtype_code ) ; + $marcrecord->encoding('UTF-8'); + SetUTF8Flag($marcrecord); - $heading = GetAuthorizedHeading({ record => $marcrecord }); - - my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], $encoding[$k], $heading ); + my $heading_authtype_code = GuessAuthTypeCode($marcrecord) or next; + my $heading = GetAuthorizedHeading({ record => $marcrecord }); + my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], 'UTF-8', $heading ); my %row_data; $row_data{server} = $servers[$k]->{'servername'}; $row_data{breedingid} = $breedingid; $row_data{heading} = $heading; $row_data{authid} = $authid; - $row_data{heading_code} = $heading_authtype_code; + $row_data{heading_code} = $heading_authtype_code; push( @breeding_loop, \%row_data ); } else { @@ -639,6 +639,11 @@ sub Z3950SearchAuth { ); } +sub _dump_conversion_error { + require Data::Dumper; + warn Data::Dumper->new([ 'Z3950SearchAuth conversion error', @_ ])->Indent(0)->Dump; +} + 1; __END__ -- 2.39.5