From 102432b47481d72df019de5f0181e3bb45ded9a6 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 17 Feb 2015 06:23:35 -0500 Subject: [PATCH] Bug 13760 - Authorities merge sometimes create duplicate fields in biblio When merging two authorities the biblio records linked to those authorities are updated. It is directly done if syspref "dontmerge" is on "Do". Actually some cases create a duplicate field. For example, a biblio record as : 600 $9 11 $a Son Goku 600 $9 12 $a Vegeta If you merge the authorities id=11 and id=12 into id=11 (creating Gogeta), you actually get : 600 $9 11 $a Son Goku 600 $9 11 $a Son Goku Having two identical fields linked to the same authority is useless. The same problem is even more frequent when merging two authorities of different type. This patch corrects this behavior by searching already existing field with the link to the autority, using flag $field_to_exists. This flag is not used in case the merge is after a single authority modification. Note that this patch changes the minium of code in order to rebase it easyly. A followup will do the code formatting. Test plan : - Set syspref dontmerge to 'Do' - Create a biblio record - Link a field to an authority (the field gets a $9). For example : 600 $9 11 $a Son Goku - Link a field to another authority (the field gets a $9). For example : 600 $9 12 $a Vegeta - Save the record - Index Zebra queue - Search for autority "Vegeta" - Click on "Merge" - Search for autority "Son Goku" - Click on "Merge" - Click on "Next" - Click on "Merge" - Go to biblio record => Without the patch : you see 2 fields linked to the same authority "Son Goku" => With the patch : you see 1 field linked to the authority "Son Goku" - Test also with autoritites of different type. - Check that modifying the autority "Son Goku" impacts the biblio record --- C4/AuthoritiesMarc.pm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 34151fc..a58ab73 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -1518,6 +1518,21 @@ sub merge { my $auth_number=$field->subfield("9"); my $tag=$field->tag(); if ($auth_number==$mergefrom) { + # If merging distinct authorities, + # look if field to add already exists in the record + # in order to avoid a duplicate. + my $field_to_exists; + if ( $mergefrom != $mergeto ) { + foreach ( $marcrecord->field( $tag_to ? $tag_to : $tag ) ) { + if ( $_->subfield('9') && $_->subfield('9') == $mergeto ) { + $field_to_exists = 1; + last; + } + } + } + # Go editing + $marcrecord->delete_field($field); + unless ($field_to_exists) { my $field_to=MARC::Field->new(($tag_to?$tag_to:$tag),$field->indicator(1),$field->indicator(2),"9"=>$mergeto); my $exclude='9'; foreach my $subfield (grep {$_->[0] ne '9'} @record_to) { @@ -1530,8 +1545,8 @@ sub merge { foreach my $subfield (@restore) { $field_to->add_subfields($subfield->[0] =>$subfield->[1]); } - $marcrecord->delete_field($field); - $marcrecord->insert_grouped_field($field_to); + $marcrecord->insert_grouped_field($field_to); + } $update=1; } }#for each tag -- 2.1.0