From c284285d11c4862b5b093c023cf3bd9bf231bbd9 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 18 Dec 2019 18:27:33 +0000 Subject: [PATCH] Bug 24267: Improve ImportBreedingAuth git grep ImportBreedingAuth - there is only one call to this routine from SearchZ3950Auth We pass it a MARC record, '2' for overwrite_auth We then check for this record in the DB and get the breeding id, however, when overwrite_auth is 2 we always add the auth to the batch and return the new breeding id. We don't actually use any of the other parameters returned here either To recreate: 1 - Browse to Authorities 2 - Select New form Z3950 3 - Perform a search that returns results 4 - SELECT COUNT(*) FROM import_auths 5 - Repeat the search 6 - SELECT COUNT(*) FROM import_auths 7 - There are 20 more records 8 - SELECT * FROM import_auths - note the repeated rows Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- C4/Breeding.pm | 63 +++++++++++++--------------------------------------------- 1 file changed, 14 insertions(+), 49 deletions(-) diff --git a/C4/Breeding.pm b/C4/Breeding.pm index 8f3e4a8c69..59cad532c7 100644 --- a/C4/Breeding.pm +++ b/C4/Breeding.pm @@ -485,13 +485,12 @@ sub _translate_query { #SRU query adjusted per server cf. srufields column ImportBreedingAuth($marcrecords,$overwrite_auth,$filename,$encoding); - ImportBreedingAuth imports MARC records in the reservoir (import_records table). - ImportBreedingAuth is based on the ImportBreeding subroutine. + ImportBreedingAuth imports MARC records in the reservoir (import_records table) or returns their id if they already exist. =cut sub ImportBreedingAuth { - my ($marcrecord,$overwrite_auth,$filename,$encoding) = @_; + my ($marcrecord,$filename,$encoding) = @_; my $dbh = C4::Context->dbh; my $batch_id = GetZ3950BatchId($filename); @@ -500,56 +499,22 @@ sub ImportBreedingAuth { my $marcflavour = C4::Context->preference('marcflavour'); my $marc_type = $marcflavour eq 'UNIMARC' ? 'UNIMARCAUTH' : $marcflavour; - # fields used for import results - my $imported=0; - my $alreadyindb = 0; - my $alreadyinfarm = 0; - my $notmarcrecord = 0; - my $breedingid; + my $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord }); - # Normalize the record so it doesn't have separated diacritics - SetUTF8Flag($marcrecord); - - if (scalar($marcrecord->fields()) == 0) { - $notmarcrecord++; - } else { - my $heading; - $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord }); + my $heading_authtype_code = GuessAuthTypeCode($marcrecord); - my $heading_authtype_code; - $heading_authtype_code = GuessAuthTypeCode($marcrecord); + my $controlnumber = $marcrecord->field('001')->data; + my $breedingid; - my $controlnumber; - $controlnumber = $marcrecord->field('001')->data; + # Normalize the record so it doesn't have separated diacritics + SetUTF8Flag($marcrecord); - #Check if the authority record already exists in the database... - my ($duplicateauthid,$duplicateauthvalue); - if ($marcrecord && $heading_authtype_code) { - ($duplicateauthid,$duplicateauthvalue) = FindDuplicateAuthority( $marcrecord, $heading_authtype_code); - } + $searchbreeding->execute($controlnumber,$heading); + ($breedingid) = $searchbreeding->fetchrow; - if ($duplicateauthid && $overwrite_auth ne 2) { - #If the authority record exists and $overwrite_auth doesn't equal 2, then mark it as already in the DB - $alreadyindb++; - } else { - if ($controlnumber && $heading) { - $searchbreeding->execute($controlnumber,$heading); - ($breedingid) = $searchbreeding->fetchrow; - } - if ($breedingid && $overwrite_auth eq '0') { - $alreadyinfarm++; - } else { - if ($breedingid && $overwrite_auth eq '1') { - ModAuthorityInBatch($breedingid, $marcrecord); - } else { - my $import_id = AddAuthToBatch($batch_id, $imported, $marcrecord, $encoding); - $breedingid = $import_id; - } - $imported++; - } - } - } - return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid); + return $breedingid if $breedingid; + $breedingid = AddAuthToBatch($batch_id, 0, $marcrecord, $encoding); + return $breedingid; } =head2 Z3950SearchAuth @@ -649,7 +614,7 @@ sub Z3950SearchAuth { $heading_authtype_code = GuessAuthTypeCode($marcrecord); $heading = C4::AuthoritiesMarc::GetAuthorizedHeading({ record => $marcrecord }); - my ($notmarcrecord, $alreadyindb, $alreadyinfarm, $imported, $breedingid)= ImportBreedingAuth( $marcrecord, 2, $serverhost[$k], $encoding[$k]); + my $breedingid = ImportBreedingAuth( $marcrecord, $serverhost[$k], $encoding[$k]); my %row_data; $row_data{server} = $servers[$k]->{'servername'}; $row_data{breedingid} = $breedingid; -- 2.11.0