JBAS-980 auth2auth link repairs/consistency
authorBill Erickson <berickxx@gmail.com>
Fri, 18 Dec 2015 15:57:37 +0000 (10:57 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
1. Use consistent DB connection logic -- removes a lot of
   unnecessary DB connection settings logic.

2. Use existing clean_marc utility function instead of inline version.

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

index 912e2b7..96dd9b6 100755 (executable)
@@ -13,7 +13,7 @@ use OpenSRF::Utils::SettingsClient;
 use OpenSRF::EX qw/:try/;
 use Encode;
 use Unicode::Normalize;
-use OpenILS::Application::AppUtils;
+use OpenILS::Utils::Normalize;
 use Data::Dumper;
 use Pod::Usage qw/ pod2usage /;
 
@@ -33,45 +33,6 @@ sub get_acsaf {
     return $acsaf_cache->{$id};
 }
 
-# Grab DB information from local settings. Return connected db handle (or die)
-sub connect_to_db {
-    my $sc = OpenSRF::Utils::SettingsClient->new;
-    my $db_driver = $sc->config_value( apps => 'open-ils.storage' => app_settings => databases => 'driver' );
-    my $db_host = $sc->config_value( apps => 'open-ils.storage' => app_settings => databases => database => 'host' );
-    my $db_port = $sc->config_value( apps => 'open-ils.storage' => app_settings => databases => database => 'port' );
-    my $db_name = $sc->config_value( apps => 'open-ils.storage' => app_settings => databases => database => 'db' );
-   
-    if (!$db_name) {
-        $db_name = $sc->config_value( reporter => setup => database => 'name' );
-        print STDERR "WARN: <database><name> is a deprecated setting for database name. For future compatibility, you should use <database><db> instead." if $db_name;
-    }
-    my $db_user = $sc->config_value( apps => 'open-ils.storage' => app_settings => databases => database => 'user' );
-    my $db_pw = $sc->config_value( apps => 'open-ils.storage' => app_settings => databases => database => 'pw' );
-   
-    die "Unable to retrieve database connection information from the settings server" unless ($db_driver && $db_host && $db_port && $db_name && $db_user);
-
-    my $dsn = "dbi:" . $db_driver . ":dbname=" . $db_name .';host=' . $db_host . ';port=' . $db_port;
-
-    return DBI->connect(
-        $dsn,$db_user,$db_pw, {
-            AutoCommit => 1, pg_enable_utf8 => 1, RaiseError => 1
-        }
-    ); # shouldn't need 'or die...' with RaiseError=>1
-}
-
-# I can't believe this isn't already in a sub somewhere?  We seem to repeat
-# these steps all over the place, which is very much "bad code smell."
-sub marcxml_eg {
-    my ($xml) = @_; # a string, not an object, to be clear
-
-    $xml =~ s/\n//sgo;
-    $xml =~ s/^<\?xml.+\?\s*>//go;
-    $xml =~ s/>\s+</></go;
-    $xml =~ s/\p{Cc}//go;
-
-    return OpenILS::Application::AppUtils->entityize($xml);
-}
-
 sub matchable_string {
     my ($field, $sf_list, $joiner) = @_;
     $joiner ||= ' ';
@@ -84,6 +45,10 @@ my ($start_id, $end_id);
 my $bootstrap = '/openils/conf/opensrf_core.xml';
 my @records;
 my $verbose;
+my $db_host = 'localhost';
+my $db_port = '5432';
+my $db_user = 'evergreen';
+my $db_pass = 'evergreen';
 
 my %options;
 my $result = GetOptions(
@@ -93,13 +58,17 @@ my $result = GetOptions(
     'all', 'help',
     'start_id=i' => \$start_id,
     'end_id=i' => \$end_id,
-    'verbose' => \$verbose
+    'verbose' => \$verbose,
+    "db-host=s" => \$db_host,
+    "db-user=s" => \$db_user,
+    "db-pass=s" => \$db_pass,
+    "db-port=s" => \$db_port
 );
 
 sub announce {
     my $msg = shift;
     return unless $verbose;
-    print DateTime->now->strftime('%F %T') . " $msg\n";
+    print DateTime->now(time_zone => 'local')->strftime('%F %T') . " $msg\n";
 }
 
 pod2usage(0) if not $result or $options{help};
@@ -148,7 +117,9 @@ if (@records) {
 }
 
 announce("SQL, params: ", Dumper($query, \@bind_params));
-my $dbh = connect_to_db; # dies if any problem
+
+my $dsn = "dbi:Pg:database=evergreen;host=$db_host;port=$db_port";
+my $dbh = DBI->connect($dsn, $db_user, $db_pass);
 $dbh->do('SET statement_timeout = 0');
 
 my $sth = $dbh->prepare($query);
@@ -255,7 +226,7 @@ while (my ($src, $links) = $sth->fetchrow_array) {
 
         if ($changed) {
             announce("Updating authority record ".$src_rec->id);
-            $src_rec->marc(marcxml_eg($src_marc->as_xml_record));
+            $src_rec->marc(OpenILS::Utils::Normalize::clean_marc($src_marc));
             $e->xact_begin;
             $e->update_authority_record_entry($src_rec) or die $e->die_event;
             $e->xact_commit;