7 use OpenSRF::Utils::Config;
11 use XML::LibXML::XPathContext;
12 use OpenSRF::AppSession;
14 use MARC::File::XML (BinaryEncoding => 'UTF-8');
16 require '/srv/openils/bin/oils_header.pl';
17 use vars qw/$apputils/;
21 my ($gather, $hostname, $core_config, $tmpdir) =
22 (0, Net::Domain::hostfqdn(), '/srv/openils/conf/opensrf_core.xml', '/tmp/');
24 my ($staff_username, $staff_password) = '';
28 'hostname=s' => \$hostname,
29 'config_file=s' => \$core_config,
30 'tempdir=s' => \$tmpdir,
31 'staff_username=s' => \$staff_username,
32 'staff_password=s' => \$staff_password,
35 (my $conf_dir = $core_config) =~ s#(.*)/.*#$1#;
36 OpenSRF::Utils::Config->load(config_file => $core_config);
37 my $conf = OpenSRF::Utils::Config->current;
38 my $settings_config = $conf->bootstrap->settings_config;
40 my $xmlparser = XML::LibXML->new();
41 my $confxml = $xmlparser->parse_file($core_config);
42 my $confxpc = XML::LibXML::XPathContext->new($confxml);
43 my $osrfxml = $xmlparser->parse_file($settings_config);
45 my $dbh = init_database_connections();
47 osrf_connect($core_config);
49 clean_date1_records($dbh);
53 sub clean_date1_records {
56 # Get list of server languages
57 my $sth = $dbh->prepare("SELECT DISTINCT mrfr.record
58 FROM metabib.real_full_rec mrfr INNER JOIN biblio.record_entry bre ON mrfr.record = bre.id
59 INNER JOIN asset.call_number acn ON bre.id = acn.record
60 INNER JOIN asset.copy ac ON acn.id = ac.call_number
61 INNER JOIN actor.org_unit aou ON ac.circ_lib = aou.id
62 WHERE mrfr.record IN (SELECT record
63 FROM metabib.real_full_rec
64 WHERE tag = '008' AND substring(value, 8, 4) ~ '(^\\s*\$|^\\d{1,3}\$|[^0-9\\su]|203[1-9])')
65 AND (tag = '260' OR tag = '264') AND subfield = 'c' AND value ~ '\\d{1,4}' AND bre.deleted = false AND aou.shortname NOT IN ('MWP', 'AB', 'LB', 'ITC', 'HBCA')");
67 my $records = $sth->fetchall_arrayref([0]);
70 my $authtoken = new_auth_token();
75 print "Getting MARC for record: " . $_->[0] . "\n";
77 $marc = MARC::Record->new_from_xml(get_marc_by_id($authtoken, $record_id), 'UTF-8');
79 my $field_260 = $marc->field('260');
80 my $field_264 = $marc->field('264');
84 $pubdate = $field_260->subfield('c');
87 if ($field_260 && !$pubdate) {
88 $pubdate = $field_260->subfield('c');
91 $pubdate =~ s/(\d{4}).*/$1/;
93 my $field_008 = $marc->field('008');
95 my $data_008 = $field_008->data();
97 my $data_008_00_to_06 = substr($data_008, 0, 7);
98 my $data_008_after_10 = substr($data_008, 11);
100 my $data_008_with_pubdate = "$data_008_00_to_06$pubdate$data_008_after_10";
102 $field_008->update($data_008_with_pubdate);
104 update_marc_by_id($authtoken, $record_id, $marc->as_xml());
110 sub init_database_connections {
111 print "\nInitializing database connection\n";
112 # Check database connections
113 my @databases = $osrfxml->findnodes('//database');
115 # If we have no database connections, this is probably the OpenSRF version
118 my $de = "* WARNING: There are no database connections defined in " .
119 "opensrf.xml. These are defined in services such as " .
120 "open-ils.cstore and open-ils.reporter. Please ensure that " .
121 "your opensrf_core.xml and opensrf.xml configuration files " .
122 "are based on the examples shipped with Evergreen instead of " .
128 foreach my $database (@databases) {
129 unless ($database->parentNode->parentNode->localname eq 'open-ils.cstore') {
133 my $db_name = $database->findvalue("./db");
135 $db_name = $database->findvalue("./name");
137 my $db_host = $database->findvalue("./host");
138 my $db_port = $database->findvalue("./port");
139 my $db_user = $database->findvalue("./user");
140 my $db_pw = $database->findvalue("./pw");
143 foreach my $node ($database->findnodes("ancestor::node()")) {
144 next unless $node->nodeType == XML::LibXML::XML_ELEMENT_NODE;
145 $osrf_xpath .= "/" . $node->nodeName;
148 my $dbh = db_connect($db_name, $db_host, $db_port, $db_user, $db_pw, $osrf_xpath);
155 my ($db_name, $db_host, $db_port, $db_user, $db_pw, $osrf_xpath) = @_;
157 my $dsn = "dbi:Pg:dbname=$db_name;host=$db_host;port=$db_port";
160 $dbh = DBI->connect($dsn, $db_user, $db_pw);
162 # Short-circuit if we didn't connect successfully
164 warn "* $osrf_xpath :: Unable to connect to database $dsn, user=$db_user, password=$db_pw\n";
172 if ($staff_username eq '' || $staff_password eq '') {
173 print "staff_username and staff_password need to be set at the command line\n";
176 my $authtoken = oils_login($staff_username, $staff_password, 'staff')
177 or die "Unable to login to Evergreen as user $staff_username";
181 sub clear_auth_token {
182 my ($authtoken) = @_;
183 $apputils->simplereq(
185 'open-ils.auth.session.delete',
191 my ($authtoken, $record_id) = @_;
192 my $bre = $apputils->simplereq(
194 'open-ils.pcrud.search.bre',
204 sub update_marc_by_id {
205 my ($authtoken, $record_id, $marc) = @_;
207 my $ret = $apputils->simplereq(
209 'open-ils.cat.biblio.record.marc.replace',