|
Lines 842-883
Comments : an improvement would be to return All the records that match.
Link Here
|
| 842 |
|
842 |
|
| 843 |
=cut |
843 |
=cut |
| 844 |
|
844 |
|
|
|
845 |
sub _find_duplicate_authority_try { |
| 846 |
my ($try, $authorityfield, $authtypecode) = @_; |
| 847 |
|
| 848 |
my $filtervalues = qr([\001-\040\Q!'"`#$%&*+,-./:;<=>?@(){[}_|~\E\]]); |
| 849 |
|
| 850 |
my $op = 'and'; |
| 851 |
if (C4::Context->preference('UseQueryParser')) { |
| 852 |
my $QParser = C4::Context->queryparser; |
| 853 |
if ($QParser) { |
| 854 |
$op = '&&'; |
| 855 |
} else { |
| 856 |
$op = 'and'; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
# build a request for SearchAuthorities |
| 861 |
my $query = "at=$authtypecode"; |
| 862 |
|
| 863 |
my $duplicaterecord; |
| 864 |
if ($try->{field} and $try->{subfields}) { |
| 865 |
foreach my $s (@{ $try->{subfields} }) { |
| 866 |
my $v = $authorityfield->subfield($s); |
| 867 |
if ($v) { |
| 868 |
$v =~ s/$filtervalues/ /g; |
| 869 |
$v =~ s/^\s*//; |
| 870 |
$v =~ s/\s*$//; |
| 871 |
$query .= qq/ $op $try->{index}="$v"/; |
| 872 |
} |
| 873 |
} |
| 874 |
my $maxresults = C4::Context->preference('FindDuplicateAuthorityMaxResults') || 10; |
| 875 |
my ($error, $results, $total_hits) = |
| 876 |
C4::Search::SimpleSearch($query, 0, $maxresults, ["authorityserver"]); |
| 877 |
return unless (!defined $error and @{$results}); |
| 878 |
|
| 879 |
foreach my $result (@$results) { |
| 880 |
my $marcrecord = |
| 881 |
C4::Search::new_record_from_zebra( 'authorityserver', $result ); |
| 882 |
my @marcfields = $marcrecord->field($try->{field}); |
| 883 |
next if (scalar @marcfields == 0); |
| 884 |
my $stillmatch = 1; |
| 885 |
foreach my $marcfield (@marcfields) { |
| 886 |
foreach my $subfield (@{ $try->{subfields} }) { |
| 887 |
my $authoritysubfield = $authorityfield->subfield($subfield); |
| 888 |
my $marcsubfield = $marcfield->subfield($subfield); |
| 889 |
if ( (defined $authoritysubfield xor defined $marcsubfield) |
| 890 |
or (defined $authoritysubfield |
| 891 |
and $authoritysubfield ne $marcsubfield) |
| 892 |
) { |
| 893 |
$stillmatch = 0; |
| 894 |
last; |
| 895 |
} |
| 896 |
} |
| 897 |
last unless $stillmatch; |
| 898 |
} |
| 899 |
if ($stillmatch) { |
| 900 |
$duplicaterecord = $marcrecord; |
| 901 |
last; |
| 902 |
} |
| 903 |
} |
| 904 |
} else { |
| 905 |
foreach my $s ($authorityfield->subfields()) { |
| 906 |
next unless ($s->[0] =~ /[A-z]/); |
| 907 |
$s->[1] =~ s/$filtervalues/ /g; |
| 908 |
$query .= qq/ $op $try->{index}="$s->[1]"/; |
| 909 |
} |
| 910 |
my ($error, $results, $total_hits) = |
| 911 |
C4::Search::SimpleSearch( $query, 0, 1, ["authorityserver"] ); |
| 912 |
if (!defined $error and @{$results}) { |
| 913 |
$duplicaterecord = |
| 914 |
C4::Search::new_record_from_zebra( 'authorityserver', |
| 915 |
$results->[0] ); |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
return $duplicaterecord; |
| 920 |
} |
| 921 |
|
| 845 |
sub FindDuplicateAuthority { |
922 |
sub FindDuplicateAuthority { |
|
|
923 |
my ($record, $authtypecode) = @_; |
| 924 |
|
| 925 |
return unless $record; |
| 926 |
|
| 927 |
my $opts = C4::Context->preference('FindDuplicateAuthorityOptions'); |
| 928 |
return unless $opts; |
| 846 |
|
929 |
|
| 847 |
my ($record,$authtypecode)=@_; |
|
|
| 848 |
# warn "IN for ".$record->as_formatted; |
| 849 |
my $dbh = C4::Context->dbh; |
930 |
my $dbh = C4::Context->dbh; |
| 850 |
# warn "".$record->as_formatted; |
931 |
my $sth = $dbh->prepare(" |
| 851 |
my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?"); |
932 |
SELECT auth_tag_to_report FROM auth_types WHERE authtypecode = ? |
|
|
933 |
"); |
| 852 |
$sth->execute($authtypecode); |
934 |
$sth->execute($authtypecode); |
| 853 |
my ($auth_tag_to_report) = $sth->fetchrow; |
935 |
my ($auth_tag_to_report) = $sth->fetchrow; |
| 854 |
$sth->finish; |
936 |
$sth->finish; |
| 855 |
# warn "record :".$record->as_formatted." auth_tag_to_report :$auth_tag_to_report"; |
937 |
|
| 856 |
# build a request for SearchAuthorities |
938 |
my $authorityfield = $record->field($auth_tag_to_report); |
| 857 |
my $QParser; |
939 |
return unless $authorityfield; |
| 858 |
$QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser')); |
940 |
|
| 859 |
my $op; |
941 |
my @tries; |
| 860 |
if ($QParser) { |
942 |
$opts =~ s/ //g; |
| 861 |
$op = '&&'; |
943 |
foreach my $try (split /\|/, $opts) { |
| 862 |
} else { |
944 |
my ($index, $matchcheck) = split m#/#, $try; |
| 863 |
$op = 'and'; |
945 |
my ($field, @subfields); |
| 864 |
} |
946 |
if ($matchcheck and length($matchcheck) > 3) { |
| 865 |
my $query='at:'.$authtypecode.' '; |
947 |
$field = substr $matchcheck, 0, 3; |
| 866 |
my $filtervalues=qr([\001-\040\Q!'"`#$%&*+,-./:;<=>?@(){[}_|~\E\]]); |
948 |
@subfields = split(//, substr($matchcheck, 3)); |
| 867 |
if ($record->field($auth_tag_to_report)) { |
|
|
| 868 |
foreach ($record->field($auth_tag_to_report)->subfields()) { |
| 869 |
$_->[1]=~s/$filtervalues/ /g; $query.= " $op he:\"".$_->[1]."\"" if ($_->[0]=~/[A-z]/); |
| 870 |
} |
949 |
} |
|
|
950 |
push @tries, { |
| 951 |
index => $index, |
| 952 |
field => $field, |
| 953 |
subfields => \@subfields, |
| 954 |
}; |
| 871 |
} |
955 |
} |
| 872 |
my ($error, $results, $total_hits) = C4::Search::SimpleSearch( $query, 0, 1, [ "authorityserver" ] ); |
956 |
|
| 873 |
# there is at least 1 result => return the 1st one |
957 |
my $duplicaterecord; |
| 874 |
if (!defined $error && @{$results} ) { |
958 |
foreach my $try (@tries) { |
| 875 |
my $marcrecord = C4::Search::new_record_from_zebra( |
959 |
$duplicaterecord = _find_duplicate_authority_try($try, $authorityfield, |
| 876 |
'authorityserver', |
960 |
$authtypecode); |
| 877 |
$results->[0] |
961 |
last if $duplicaterecord; |
| 878 |
); |
962 |
} |
| 879 |
return $marcrecord->field('001')->data,BuildSummary($marcrecord,$marcrecord->field('001')->data,$authtypecode); |
963 |
|
|
|
964 |
if ($duplicaterecord) { |
| 965 |
my $authid = $duplicaterecord->field('001')->data; |
| 966 |
return $authid, BuildSummary($duplicaterecord, $authid, $authtypecode); |
| 880 |
} |
967 |
} |
|
|
968 |
|
| 881 |
# no result, returns nothing |
969 |
# no result, returns nothing |
| 882 |
return; |
970 |
return; |
| 883 |
} |
971 |
} |