JBAS-980 auth-to-auth clear links once
authorBill Erickson <berickxx@gmail.com>
Mon, 4 Jan 2016 22:19:20 +0000 (14:19 -0800)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Only delete auth-to-auth links for a given subfield once per record.
Otherwise, we could only create one link per tag.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/linking/authority_authority_linker.pl

index 0aecb2f..a7f3ffc 100755 (executable)
@@ -106,7 +106,8 @@ my $query = q{
             -- Ignore authority.authority_linking rows since we want to
             -- rebuild all links, which may mean deleting bogus links.
             -- EXCEPT SELECT target, source, field FROM authority.authority_linking
-    ) x GROUP BY 1
+    -- order by source for consistent testing
+    ) x GROUP BY 1 ORDER BY source
 };
 
 my @bind_params;
@@ -162,6 +163,7 @@ while (my ($src, $links) = $sth->fetchrow_array) {
         announce("Source record thesaurus value=$auth_src_thesaurus");
 
         my $changed = 0;
+        my %tags_seen;
         for my $link (split ';', $links) {
             my ($target, $field_id) = split ',', $link;
 
@@ -194,14 +196,19 @@ while (my ($src, $links) = $sth->fetchrow_array) {
                 next;
             }
 
-            # start by removing all existing links for the current tag
-            for my $field ($src_marc->field($acsaf->tag)) {
-                if (my $val = $field->subfield('0')) {
-                    announce("Removing existing subfield 0 : $val");
-                    $field->delete_subfield(code => '0');
-                    $changed = 1;
-                    $links_removed++;
+            if (!$tags_seen{$acsaf->tag}) {
+                # the first time we process each tag for a given record,
+                # remove all existing auth-to-auth link subfields
+                # so they can be completely rebuilt.
+                for my $field ($src_marc->field($acsaf->tag)) {
+                    if (my $val = $field->subfield('0')) {
+                        announce("Removing existing subfield 0 : $val");
+                        $field->delete_subfield(code => '0');
+                        $changed = 1;
+                        $links_removed++;
+                    }
                 }
+                $tags_seen{$acsaf->tag} = 1;
             }
 
             # rebuild the links for the current tag