JBAS-1437 Backstage 010z replace records
authorBill Erickson <berickxx@gmail.com>
Tue, 25 Oct 2016 21:06:03 +0000 (17:06 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Replace authority records whose 010a matches the 010z value for inbound
records.

Also avoid looking up deleted records when looking for matches.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/backstage/process-backstage-files.pl

index de0fa64..27d96dd 100755 (executable)
@@ -306,6 +306,8 @@ sub handle_modified_auths {
     while (my $record = $marc_batch->next()) {
 
         my @matches = find_matching_auths($record);
+        push(@matches, find_replaced_auths($record));
+
         my $marcxml = clean_marc($record->as_xml_record());
 
         if (@matches) {
@@ -351,6 +353,25 @@ sub insert_auth {
         if $new_auth_ctr % $log_mod == 0;
 }
 
+# Return ID's of authority records that should be replaced by the
+# current record.  Checks for records whose 010$a equals the 010$z of
+# the current record.
+# 010$z == Canceled/invalid LC control number
+sub find_replaced_auths {
+    my $record = shift;
+
+    my $subfield = $record->subfield('010', 'z');
+    return () unless $subfield;
+
+    $match_auth_sth->execute('010', $subfield);
+    my $matches = $match_auth_sth->fetchall_arrayref;
+    my @ids = map {$_->[0]} @$matches;
+
+    announce('INFO', "Auth 010z=$subfield matched records: @ids") if @ids;
+
+    return @ids;
+}
+
 # Return ID's of matching authority records.  Matching tries:
 # 001 -> 010a -> 035a.
 sub find_matching_auths {
@@ -365,12 +386,10 @@ sub find_matching_auths {
 
             $match_auth_001_sth->execute($subfield);
             my $matches = $match_auth_001_sth->fetchall_arrayref;
-
-            if (@$matches) { # DEBUGGING...
-                my @ids = map {$_->[0]} @$matches;
-                announce('INFO', "Auth 001=$subfield matched records: @ids");
-            }
-            return map {$_->[0]} @$matches if @$matches;
+            my @ids = map {$_->[0]} @$matches;
+            announce('INFO', 
+                "Auth 001=$subfield matched records: @ids") if @ids;
+            return @ids;
         }
     }
 
@@ -387,7 +406,10 @@ sub find_matching_auths {
     $match_auth_sth->execute($tag, $subfield);
     my $matches = $match_auth_sth->fetchall_arrayref;
 
-    return map {$_->[0]} @$matches;
+    my @ids = map {$_->[0]} @$matches;
+    announce('INFO', "Auth ${tag}a=$subfield matched records: @ids") if @ids;
+
+    return @ids;
 }
 
 sub prepare_statements {
@@ -419,18 +441,24 @@ sub prepare_statements {
     SQL
 
     $match_auth_sth = $db_handle->prepare(<<"    SQL");
-        SELECT DISTINCT(record) 
-        FROM authority.full_rec 
+        SELECT DISTINCT(rec.id)
+        FROM authority.record_entry rec
+            JOIN authority.full_rec frec ON (frec.record = rec.id)
         WHERE 
-            tag = ? 
-            AND subfield = 'a' 
-            AND value = NACO_NORMALIZE(?, 'a')
+            NOT rec.deleted
+            AND frec.tag = ? 
+            AND frec.subfield = 'a' 
+            AND frec.value = NACO_NORMALIZE(?, 'a')
     SQL
 
     $match_auth_001_sth = $db_handle->prepare(<<"    SQL");
-        SELECT DISTINCT(record)
-        FROM authority.full_rec
-        WHERE tag = '001' AND value = ?
+        SELECT DISTINCT(rec.id)
+        FROM authority.record_entry rec
+            JOIN authority.full_rec frec ON (frec.record = rec.id)
+        WHERE 
+            NOT rec.deleted
+            AND frec.tag = '001' 
+            AND frec.value = ?
     SQL
 }