my $db_port = $ENV{PGPORT} || '5432';
my $db_user = $ENV{PGDATABASE} || 'evergreen';
my $db_pass = $ENV{PGPASSWORD};
+my $links_removed = 0;
+my $links_added = 0;
my %options;
my $result = GetOptions(
(sh2.atag = af2.id AND af2.main_entry IS NOT NULL
AND af2.linking_subfield IS NOT NULL)
%s -- where clause here
- EXCEPT SELECT target, source, field FROM authority.authority_linking
+ -- 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
};
for my $link (split ';', $links) {
my ($target, $field_id) = split ',', $link;
+ next if $target eq $src_rec->id;
+
announce("Target: $target, field_id: $field_id");
my $target_rec = $e->retrieve_authority_record_entry($target);
my $auth_target_thesaurus =
substr($target_marc->field('008')->data(), 11, 1);
+
announce("Target record thesaurus value=$auth_target_thesaurus");
- # warn here, cleanup invalid links below
- announce("Thesauri for source/target records do not match")
- if $auth_src_thesaurus ne $auth_target_thesaurus;
+ if ($auth_src_thesaurus ne $auth_target_thesaurus) {
+ announce("Thesauri for source/target records do not match. Skipping..");
+ next;
+ }
my $cni = $target_marc->field('003')->data;
my $acsaf = get_acsaf($e, $field_id);
next;
}
+ # start by removing all existing links for the current tag
for my $field ($src_marc->field($acsaf->tag)) {
-
- if ($auth_src_thesaurus ne $auth_target_thesaurus) {
- my @zeros = $field->subfield('0');
- announce("Existing links: @zeros");
- if (grep { $_ =~ qr/\)$target$/ } @zeros) {
- announce("Removing link(s) on ".$field->tag.
- " for src/target thesaurus mismatch");
- $field->delete_subfield(code => '0', match => qr/\)$target$/);
- $changed = 1;
- }
- next;
+ if (my $val = $field->subfield('0')) {
+ announce("Removing existing subfield 0 : $val");
+ $field->delete_subfield(code => '0');
+ $changed = 1;
+ $links_removed++;
}
+ }
+
+ # rebuild the links for the current tag
+ for my $field ($src_marc->field($acsaf->tag)) {
my $src_string = matchable_string(
$field, $acsaf->main_entry->display_sf_list,
my ($tfield) = $target_marc->field($acsaf->main_entry->tag);
if(defined $tfield) {
+
my $target_string = matchable_string(
$tfield, $acsaf->main_entry->display_sf_list,
$acsaf->main_entry->joiner
announce("Got a match");
$field->update('0' => "($cni)$target");
$changed = 1;
+ $links_added++;
}
}
}
announce("Processed all records; processed=$total_records; problems=$problems");
}
+announce("links removed: $links_removed");
+announce("links added: $links_added");
+announce("delta added: ".($links_added - $links_removed));
+
exit ($problems > 0);
__END__