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