use Net::Domain;
use XML::LibXML;
use XML::LibXML::XPathContext;
+use OpenSRF::AppSession;
+use MARC::Record;
+use MARC::File::XML (BinaryEncoding => 'UTF-8');
+
+require '/srv/openils/bin/oils_header.pl';
+use vars qw/$apputils/;
my $output = '';
my ($gather, $hostname, $core_config, $tmpdir) =
(0, Net::Domain::hostfqdn(), '/srv/openils/conf/opensrf_core.xml', '/tmp/');
+my ($staff_username, $staff_password) = '';
+
GetOptions(
'gather' => \$gather,
'hostname=s' => \$hostname,
'config_file=s' => \$core_config,
'tempdir=s' => \$tmpdir,
+ 'staff_username=s' => \$staff_username,
+ 'staff_password=s' => \$staff_password,
);
(my $conf_dir = $core_config) =~ s#(.*)/.*#$1#;
my $dbh = init_database_connections();
-get_date1_records($dbh);
+osrf_connect($core_config);
+
+clean_date1_records($dbh);
$dbh->disconnect;
-sub get_date1_records {
+sub clean_date1_records {
my ($dbh) = @_;
# Get list of server languages
my $records = $sth->fetchall_arrayref([0]);
$sth->finish;
+ my $authtoken = new_auth_token();
+
+ my $marc = '';
+ my $record_id = '';
for (@$records) {
- print $_->[0] . "\n";
+ print "Getting MARC for record: " . $_->[0] . "\n";
+ $record_id = $_->[0];
+ $marc = MARC::Record->new_from_xml(get_marc_by_id($authtoken, $record_id), 'UTF-8');
+
+ my $field_260 = $marc->field('260');
+ my $field_264 = $marc->field('264');
+ my $pubdate = '';
+
+ if ($field_264) {
+ $pubdate = $field_260->subfield('c');
+ }
+
+ if ($field_260 && !$pubdate) {
+ $pubdate = $field_260->subfield('c');
+ }
+
+ $pubdate =~ s/(\d{4}).*/$1/;
+
+ my $field_008 = $marc->field('008');
+
+ my $data_008 = $field_008->data();
+
+ my $data_008_00_to_06 = substr($data_008, 0, 7);
+ my $data_008_after_10 = substr($data_008, 11);
+
+ my $data_008_with_pubdate = "$data_008_00_to_06$pubdate$data_008_after_10";
+
+ $field_008->update($data_008_with_pubdate);
+
+ update_marc_by_id($authtoken, $record_id, $marc->as_xml());
+
+ exit;
}
}
return $dbh;
}
+
+sub new_auth_token {
+ if ($staff_username eq '' || $staff_password eq '') {
+ print "staff_username and staff_password need to be set at the command line\n";
+ exit;
+ }
+ my $authtoken = oils_login($staff_username, $staff_password, 'staff')
+ or die "Unable to login to Evergreen as user $staff_username";
+ return $authtoken;
+}
+
+sub clear_auth_token {
+ my ($authtoken) = @_;
+ $apputils->simplereq(
+ 'open-ils.auth',
+ 'open-ils.auth.session.delete',
+ $authtoken
+ );
+}
+
+sub get_marc_by_id {
+ my ($authtoken, $record_id) = @_;
+ my $bre = $apputils->simplereq(
+ 'open-ils.pcrud',
+ 'open-ils.pcrud.search.bre',
+ $authtoken,
+ {
+ id => $record_id
+ }
+ );
+
+ return $bre->marc;
+}
+
+sub update_marc_by_id {
+ my ($authtoken, $record_id, $marc) = @_;
+
+ my $ret = $apputils->simplereq(
+ 'open-ils.cat',
+ 'open-ils.cat.biblio.record.marc.replace',
+ $authtoken,
+ $record_id,
+ $marc
+ );
+}