Lines 896-926
Comments : an improvement would be to return All the records that match.
Link Here
|
896 |
|
896 |
|
897 |
=cut |
897 |
=cut |
898 |
|
898 |
|
|
|
899 |
sub _find_duplicate_authority_try { |
900 |
my ($try, $authorityfield, $authtypecode) = @_; |
901 |
|
902 |
my $filtervalues = |
903 |
qr([\001-\040\!\'\"\`\#\$\%\&\*\+,\-\./:;<=>\?\@\(\)\{\[\]\}_\|\~]); |
904 |
|
905 |
# build a request for SearchAuthorities |
906 |
my $query = "at=$authtypecode"; |
907 |
|
908 |
foreach my $s ($authorityfield->subfields()) { |
909 |
next unless ($s->[0] =~ /[A-z]/); |
910 |
$s->[1] =~ s/$filtervalues/ /g; |
911 |
$query .= qq{ and $try->{index}="$s->[1]"}; |
912 |
} |
913 |
|
914 |
my $duplicaterecord; |
915 |
if ($try->{field} and $try->{subfields}) { |
916 |
my $maxresults = C4::Context->preference('FindDuplicateMaxResults') || 10; |
917 |
my ($error, $results, $total_hits) = |
918 |
C4::Search::SimpleSearch($query, 0, $maxresults, ["authorityserver"]); |
919 |
return unless (!defined $error and @{$results}); |
920 |
|
921 |
foreach my $result (@$results) { |
922 |
my $marcrecord = MARC::File::USMARC::decode($result); |
923 |
my @marcfields = $marcrecord->field($try->{field}); |
924 |
next if (scalar @marcfields == 0); |
925 |
my $stillmatch = 1; |
926 |
foreach my $marcfield (@marcfields) { |
927 |
foreach my $subfield (@{ $try->{subfields} }) { |
928 |
my $authoritysubfield = $authorityfield->subfield($subfield); |
929 |
my $marcsubfield = $marcfield->subfield($subfield); |
930 |
if ( (defined $authoritysubfield xor defined $marcsubfield) |
931 |
or (defined $authoritysubfield |
932 |
and $authoritysubfield ne $marcsubfield) |
933 |
) { |
934 |
$stillmatch = 0; |
935 |
last; |
936 |
} |
937 |
} |
938 |
last unless $stillmatch; |
939 |
} |
940 |
if ($stillmatch) { |
941 |
$duplicaterecord = $marcrecord; |
942 |
last; |
943 |
} |
944 |
} |
945 |
} else { |
946 |
my ($error, $results, $total_hits) = |
947 |
C4::Search::SimpleSearch( $query, 0, 1, ["authorityserver"] ); |
948 |
if (!defined $error and @{$results}) { |
949 |
$duplicaterecord = MARC::File::USMARC::decode($results->[0]); |
950 |
} |
951 |
} |
952 |
|
953 |
return $duplicaterecord; |
954 |
} |
955 |
|
899 |
sub FindDuplicateAuthority { |
956 |
sub FindDuplicateAuthority { |
|
|
957 |
my ($record, $authtypecode) = @_; |
958 |
|
959 |
return unless $record; |
960 |
|
961 |
my $opts = C4::Context->preference('FindDuplicateOptions'); |
962 |
return unless $opts; |
900 |
|
963 |
|
901 |
my ($record,$authtypecode)=@_; |
|
|
902 |
# warn "IN for ".$record->as_formatted; |
903 |
my $dbh = C4::Context->dbh; |
964 |
my $dbh = C4::Context->dbh; |
904 |
# warn "".$record->as_formatted; |
965 |
my $sth = $dbh->prepare(" |
905 |
my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?"); |
966 |
SELECT auth_tag_to_report FROM auth_types WHERE authtypecode = ? |
|
|
967 |
"); |
906 |
$sth->execute($authtypecode); |
968 |
$sth->execute($authtypecode); |
907 |
my ($auth_tag_to_report) = $sth->fetchrow; |
969 |
my ($auth_tag_to_report) = $sth->fetchrow; |
908 |
$sth->finish; |
970 |
$sth->finish; |
909 |
# warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report"; |
971 |
|
910 |
# build a request for SearchAuthorities |
972 |
my $authorityfield = $record->field($auth_tag_to_report); |
911 |
my $query='at='.$authtypecode.' '; |
973 |
return unless $authorityfield; |
912 |
my $filtervalues=qr([\001-\040\!\'\"\`\#\$\%\&\*\+,\-\./:;<=>\?\@\(\)\{\[\]\}_\|\~]); |
974 |
|
913 |
if ($record->field($auth_tag_to_report)) { |
975 |
my @tries; |
914 |
foreach ($record->field($auth_tag_to_report)->subfields()) { |
976 |
$opts =~ s/ //g; |
915 |
$_->[1]=~s/$filtervalues/ /g; $query.= " and he,wrdl=\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/); |
977 |
foreach my $try (split /\|/, $opts) { |
916 |
} |
978 |
my ($index, $matchcheck) = split m#/#, $try; |
|
|
979 |
my ($field, @subfields); |
980 |
if ($matchcheck and length($matchcheck) > 3) { |
981 |
$field = substr $matchcheck, 0, 3; |
982 |
@subfields = split(//, substr($matchcheck, 3)); |
983 |
} |
984 |
push @tries, { |
985 |
index => $index, |
986 |
field => $field, |
987 |
subfields => \@subfields, |
988 |
}; |
917 |
} |
989 |
} |
918 |
my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, 0, 1, [ "authorityserver" ] ); |
990 |
|
919 |
# there is at least 1 result => return the 1st one |
991 |
my $duplicaterecord; |
920 |
if (!defined $error && @{$results} ) { |
992 |
foreach my $try (@tries) { |
921 |
my $marcrecord = MARC::File::USMARC::decode($results->[0]); |
993 |
$duplicaterecord = _find_duplicate_authority_try($try, $authorityfield, |
922 |
return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode); |
994 |
$authtypecode); |
|
|
995 |
last if $duplicaterecord; |
923 |
} |
996 |
} |
|
|
997 |
|
998 |
if ($duplicaterecord) { |
999 |
my $authid = $duplicaterecord->field('001')->data; |
1000 |
return $authid, BuildSummary($duplicaterecord, $authid, $authtypecode); |
1001 |
} |
1002 |
|
924 |
# no result, returns nothing |
1003 |
# no result, returns nothing |
925 |
return; |
1004 |
return; |
926 |
} |
1005 |
} |