From 2fa5e41a50aff5e9225c8869a32e39b099506e51 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Wed, 29 Jan 2025 00:23:41 +0100 Subject: [PATCH] Bug 39503: Bug 39503 - Linker should always respect thesaurus with LinkerConsiderThesaurus on With bug 30280 and subsequent patches we attempt to respect the thesaurus indication when linking with LinkerConsiderThesaurus on. However, there are still cases when we link wrong. This happens when thesaurus in bibliographic field 6XX is defined by subfield $2. In such a case the authority record should have 008/11 = 'z' and, according to https://www.loc.gov/marc/authority/ad008.html (cf. comment for 008/11 = 'z': "A MARC code for the conventions used to formulate the heading *may* be contained in subfield $f"), may or may have thesaurus declared in 040 $f. This is why we make a second search attempt in C4::Heading::_search, but doing so we have to make sure that the resulting authority record has not a different thesaurus declared in 040 $f. (Note that 008/11 and 040 $f are indexed with the same search field: subject-heading-thesaurus. So we search for the second time in the same search field but with 'notdefined' == 008/11='z' instead of the original thesaurus taken from subfield $2 of the bibliographic field 6XX.) This means we have to explicitly check the content of 040 $f of the resulting authority record. Test plan: ========== 1. Turn on the LinkerConsiderThesaurus systempreference. 2. Create an authority record (e.g. TOPIC_TERM) with 008/11 = 'z' and 040 $f containing a thesaurus indication (e.g. 'kaba'), and a term in field 150 $a (e.g. Early music). 3. Edit a bibliographic record: in field 650 put 2nd indicator '7', the term 'Early music' in $a, and a different thesaurus code in $2 (e.g. 'dbn'). Click the 'Link authorities automatically' button. Note that 650 'Early music' (dbn) has been linked with and authority record 'Early music' (kaba). This is wrong. 4. Apply the patch ; restart_all. 5. Repeat p. 3. Now the 650 'Early music' (dbn) has not been linked to an authority record (650 - No matching authority found.) Signed-off-by: Roman Dolny --- C4/Heading.pm | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/C4/Heading.pm b/C4/Heading.pm index 79001ecf62..3eb2bbcec1 100644 --- a/C4/Heading.pm +++ b/C4/Heading.pm @@ -232,7 +232,7 @@ sub _search { && none { $_ eq $thesaurus } ( 'lcsh', 'lcac', 'mesh', 'nal', 'notspecified', 'cash', 'rvm', 'sears', - 'aat' + 'aat', 'notdefined' ) ) { @@ -244,6 +244,26 @@ sub _search { 'AuthidAsc' ); ( $matched_auths, $total ) = $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata ); + + # We have to make sure that we do not link with an authority record + # controlling the same term but from a different thesaurus. + # This means we have to check the content of 040 $f of the authority record. + my $matched_auths_exact = []; + for my $matched_auth (@$matched_auths) { + my $auth = Koha::Authorities->find( $matched_auth->{authid} ); + unless ($auth) { + $total--; + next; + } + my $auth_rec = $auth->record; + my $s040f = $auth_rec->subfield( '040', 'f' ) // ''; + if ( !$s040f || $s040f eq $thesaurus ) { + push @{$matched_auths_exact}, $matched_auth; + } else { + $total--; + } + } + $matched_auths = $matched_auths_exact; } return ( $matched_auths, $total ); -- 2.39.5