From be9f66b63a66c8a0593c9529a60fe1879c2e6304 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Sat, 25 Nov 2017 02:28:02 +0100 Subject: [PATCH] Bug 18458 - Merging authority record incorrectly orders subfields While doing a merge, if a subfield(s) precedes the controlled subfields (like $i before $a in 7XX, which comes before $a -- rare but will become more and more usual) after merging will be moved to the end. This is not right. The patch (with AuthorityMergeMode == loose) make merge consider the subfields order: all subfields which originally were found before the first controlled subfield (e.g. $i before $a in 7XX / MARC 21) will remain in the front, the rest of not controlled subfields that should remain in the field will come after the subfields copied from authority rec. As a bonus, $9 will be placed at the end. To test: 0) Have AuthorityMergeMode == loose; 1) Have some field in bibliorecord, controlled by an authority, with extra subfield(s) (i.e. not present in authority rec.) placed at the beginning of the field; 2) Open (not necessarily edit) and save the connected authority; 3) See that the extra subfieds were moved to the end of the field (and $9 is in the front); 4) Apply the patch; 5) Reorder subfields in biblio field; 6) Open (not necessarily edit) and save the connected authority; 7) See that the order has been conserved, additionally $9 the last subfield in the field. --- C4/AuthoritiesMarc.pm | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 6dbe516208..30c0952036 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -1473,25 +1473,41 @@ sub merge { my $newtag = $tags_new && @$tags_new ? _merge_newtag( $tag, $tags_new ) : $tag; - my $field_to = MARC::Field->new( - $newtag, - $field->indicator(1), - $field->indicator(2), - "9" => $mergeto, - ); - foreach my $subfield ( grep { $_->[0] ne '9' } @record_to ) { - $field_to->add_subfields( $subfield->[0] => $subfield->[1] ); - } + my (@prefix, @postfix); if ( !$overwrite ) { # add subfields back in loose mode, check skip_subfields + # check the relative position resp. controlled subfields + my $prefix_flag = 1; foreach my $subfield ( $field->subfields ) { - next if $skip_subfields->{ $subfield->[0] }; - $field_to->add_subfields( $subfield->[0], $subfield->[1] ); + if ( $skip_subfields->{ $subfield->[0] } ) { + $prefix_flag = 0; + next; + } + if ($prefix_flag) { + push @prefix, [ $subfield->[0], $subfield->[1] ] ; + } else { + push @postfix, [ $subfield->[0], $subfield->[1] ] ; + } + } + } + my $field_to; + foreach my $subfield ( grep { $_->[0] ne '9' } (@prefix, @record_to, @postfix) ) { + unless ($field_to) { + $field_to = MARC::Field->new( + $newtag, + $field->indicator(1), + $field->indicator(2), + $subfield->[0] => $subfield->[1], + ); + } else { + $field_to->add_subfields( $subfield->[0] => $subfield->[1] ); } } + $field_to->add_subfields( 9 => $mergeto ); + if ($tags_new && @$tags_new) { $marcrecord->delete_field($field); - append_fields_ordered( $marcrecord, $field_to ); + $marcrecord->insert_fields_ordered($field_to); } else { $field->replace_with($field_to); } -- 2.11.0