diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 35feed6033..c77b719bed 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -1619,8 +1619,25 @@ sub merge { my $auth_tag_to_report_to = $authtypeto ? $authtypeto->auth_tag_to_report : ''; my @record_to; - @record_to = $MARCto->field($auth_tag_to_report_to)->subfields() - if $auth_tag_to_report_to && $MARCto && $MARCto->field($auth_tag_to_report_to); + + my $short_langcode = C4::Context->preference('LanguageToReportOnMerge'); + if ( $auth_tag_to_report_to && $MARCto && $MARCto->field($auth_tag_to_report_to) ) { + my $chosen_field = $MARCto->field($auth_tag_to_report_to); + if ($short_langcode) { + foreach my $field ( $MARCto->field($auth_tag_to_report_to) ) { + my $subfield = $field->subfield('7'); + if ( + defined $subfield + && ( ( length($subfield) == 2 && $subfield eq $short_langcode ) + || ( length($subfield) == 8 && substr( $subfield, 4, 2 ) eq $short_langcode ) ) + ) + { + $chosen_field = $field; + } + } + } + @record_to = $chosen_field->subfields(); + } # Exceptional: If MARCto and authtypeto exist but $auth_tag_to_report_to # is empty, make sure that $9 and $a remain (instead of clearing the diff --git a/installer/data/mysql/atomicupdate/Bug38142.pl b/installer/data/mysql/atomicupdate/Bug38142.pl new file mode 100755 index 0000000000..f94ba2012b --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug38142.pl @@ -0,0 +1,18 @@ +use Modern::Perl; + +return { + bug_number => "38142", + description => "LanguageToReportOnMerge", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + $dbh->do( + q{ + INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES + ('LanguageToReportOnMerge','','','If set, the authority field having the given language code in its $7 subfield will be reported to the bibliographic record if it exists, rather than the first field. The code can be in a short, 2 characters long form (example: ba for latin) or in a long, 8 characters long form, with the short form in position 5 and 6 starting from 1 (example: ba0yba0y for latin). A list of available codes can be found here: https://documentation.abes.fr/sudoc/formats/unmb/DonneesCodees/CodesZone104.htm#$d','Free'); + } + ); + + say $out "Added new syspref 'LanguageToReportOnMerge'"; + } +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index ab0bc8ee80..ab730e17bc 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -371,6 +371,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('KohaManualBaseURL','https://koha-community.org/manual/','','Where is the Koha manual/documentation located?','Free'), ('KohaManualLanguage','en','en|ar|cs|de|es|fr|it|pt_BR|tr|zh_TW','What is the language of the online manual you want to use?','Choice'), ('LabelMARCView','standard','standard|economical','Define how a MARC record will display','Choice'), +('LanguageToReportOnMerge','',NULL,'If set, the authority field having the given language code in its $7 subfield will be reported to the bibliographic record if it exists, rather than the first field. The code can be in a short, 2 characters long form (example: ba for latin) or in a long, 8 characters long form, with the short form in position 5 and 6 starting from 1 (example: ba0yba0y for latin). A list of available codes can be found here: https://documentation.abes.fr/sudoc/formats/unmb/DonneesCodees/CodesZone104.htm#$d','Free'), ('LibraryName','','','Define the library name as displayed on the OPAC',''), ('LibraryThingForLibrariesEnabled','0','','Enable or Disable Library Thing for Libraries Features','YesNo'), ('LibraryThingForLibrariesID','','','See:http://librarything.com/forlibraries/','free'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/authorities.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/authorities.pref index 39b044b30c..57c4cadb2c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/authorities.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/authorities.pref @@ -103,7 +103,9 @@ Authorities: 0: "Don't" - consider the intended use of MARC21 authority records (008/14-16) during cataloging and linking. - "
NOTE: If you enable this preference, 'No attempt to code' in 008/14-16 is excluded from search results too." - + - + - "If set, the authority field having the given language code in its $7 subfield will be reported to the bibliographic record if it exists, rather than the first field. The code can be in a short, 2 characters long form (example: ba for latin) or in a long, 8 characters long form, with the short form in position 5 and 6 starting from 1 (example: ba0yba0y for latin). A list of available codes can be found here: https://documentation.abes.fr/sudoc/formats/unmb/DonneesCodees/CodesZone104.htm#$d" + - pref: LanguageToReportOnMerge Linker: - - Use the diff --git a/t/db_dependent/Authority/Merge.t b/t/db_dependent/Authority/Merge.t index 885e7fdaaf..fb65f65401 100755 --- a/t/db_dependent/Authority/Merge.t +++ b/t/db_dependent/Authority/Merge.t @@ -3,7 +3,6 @@ # Tests for C4::AuthoritiesMarc::merge use Modern::Perl; - use Test::NoWarnings; use Test::More tests => 14; @@ -211,6 +210,78 @@ subtest 'Test merge A1 to modified A1, test strict mode' => sub { # Note: the +1 comes from the added subfield $9 in the biblio }; +subtest 'Test merge A1 to B1 (choosing language)' => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference( 'LanguageToReportOnMerge', '' ); + + my $auth1 = MARC::Record->new; + $auth1->append_fields( + MARC::Field->new( '109', '0', '0', 'a' => 'language da', '7' => 'da' ), + MARC::Field->new( '109', '0', '0', 'a' => 'language ba', '7' => 'ba' ), + ); + my $authid1 = AddAuthority( $auth1, undef, $authtype1 ); + + my $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '003', 'some_003' ), + MARC::Field->new( '245', '', '', a => 'My title' ), + MARC::Field->new( '609', '', '', a => 'language ba', '7' => 'ba', 9 => $authid1 ), + ); + my ($biblionumber) = C4::Biblio::AddBiblio( $marc, '' ); + + @linkedrecords = ($biblionumber); + C4::AuthoritiesMarc::merge( { mergefrom => $authid1, MARCfrom => $auth1, mergeto => $authid1, MARCto => $auth1 } ); + my $biblio = Koha::Biblios->find($biblionumber)->metadata->record; + + is( + $biblio->subfield( '609', 'a' ), 'language da', + 'When LanguageToReportOnMerge is not set, the first authority field is used' + ); + + t::lib::Mocks::mock_preference( 'LanguageToReportOnMerge', 'ba' ); + C4::AuthoritiesMarc::merge( { mergefrom => $authid1, MARCfrom => $auth1, mergeto => $authid1, MARCto => $auth1 } ); + $biblio = Koha::Biblios->find($biblionumber)->metadata->record; + is( + $biblio->subfield( '609', 'a' ), 'language ba', + 'When LanguageToReportOnMerge is set, the field with the matching language is reported' + ); + + t::lib::Mocks::mock_preference( 'LanguageToReportOnMerge', 'xx' ); + C4::AuthoritiesMarc::merge( { mergefrom => $authid1, MARCfrom => $auth1, mergeto => $authid1, MARCto => $auth1 } ); + $biblio = Koha::Biblios->find($biblionumber)->metadata->record; + is( + $biblio->subfield( '609', 'a' ), 'language da', + 'When LanguageToReportOnMerge is set to a value that does not exist in the authority, the first authority field is used' + ); + + # Long form of lang code + t::lib::Mocks::mock_preference( 'LanguageToReportOnMerge', 'ba' ); + my $auth2 = MARC::Record->new; + $auth2->append_fields( + MARC::Field->new( '109', '0', '0', 'a' => 'language da (long form)', '7' => 'ba0yda0y' ), + MARC::Field->new( '109', '0', '0', 'a' => 'language ba (long form)', '7' => 'ba0yba0y' ), + ); + my $authid2 = AddAuthority( $auth2, undef, $authtype1 ); + + $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '003', 'some_003' ), + MARC::Field->new( '245', '', '', a => 'My title' ), + MARC::Field->new( '609', '', '', a => 'language ba', '7' => 'ba', 9 => $authid2 ), + ); + my ($biblionumber2) = C4::Biblio::AddBiblio( $marc, '' ); + + @linkedrecords = ($biblionumber2); + C4::AuthoritiesMarc::merge( { mergefrom => $authid2, MARCfrom => $auth2, mergeto => $authid2, MARCto => $auth2 } ); + my $biblio2 = Koha::Biblios->find($biblionumber2)->metadata->record; + is( + $biblio2->subfield( '609', 'a' ), 'language ba (long form)', + 'When LanguageToReportOnMerge is set, the field with the matching language in its long form is reported' + ); + +}; + subtest 'Test merge A1 to B1 (changing authtype)' => sub { # Tests were aimed for bug 9988, moved to 17909 in adjusted form