Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # update an existing marc record
- use OpenSRF::System;
- use OpenSRF::AppSession;
- use OpenILS::Application::AppUtils;
- use Digest::MD5 qw(md5_hex);
- use Getopt::Long;
- use Data::Dumper;
- binmode(STDOUT, ":utf8");
- # Feed this script a list of bibids and it will clean up the MARCXML in those records.
- # Usage: perl marc-cleanup.pl --file bibids.txt
- my ($file, $help, @modified);
- my $count = 0;
- GetOptions('file=s' => \$file, 'help' => \$help);
- if (!$file || $help) {
- die q/Usage: perl marc-cleanup.pl --file bibids.txt/
- };
- OpenSRF::System->bootstrap_client( config_file => '/openils/conf/opensrf_core.xml');
- Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
- my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
- my $cat = OpenSRF::AppSession->create('open-ils.cat');
- # osrf auth crap
- print "srfsh username: ";
- chomp( my $username = <> );
- print "srfsh password: ";
- chomp( my $password = <> );
- my $auth = oils_login($username, $password);
- die("Login unsuccessful!\n") unless $auth;
- open (BIBS, "<", $file) or die "Could not open $file: $!\n";
- while (<BIBS>) {
- chomp(my $bibid = $_); # nom nom nom
- $count++;
- my $bib = $cstore->request( "open-ils.cstore.direct.biblio.record_entry.retrieve", $bibid )->gather(1);
- if (!$bib) {
- print STDERR "ZOMG BIB $bibid DOES NOT EXIST WTF! Aborting.\n";
- next;
- }
- my $xml = $bib->marc;
- utf8::encode($xml); # ugh.
- $xml = marc_to_eg($xml); # this is the part that matters.
- my $source = $bib->source;
- my $result = $cat->request('open-ils.cat.biblio.record.xml.update', $auth, $bibid, $xml, $source)->gather(1);
- push @modified, $bibid;
- }
- # free the children!
- $cstore->disconnect();
- $cat->disconnect();
- print join("\n", @modified) . "\n";
- print scalar @modified . " bib records modified\n";
- sub marc_to_eg {
- my $xml = shift;
- $xml =~ s/\n//sog;
- $xml =~ s/^<\?xml.+\?\s*>//go;
- $xml =~ s/>\s+</></go;
- $xml =~ s/\p{Cc}//go;
- $xml = OpenILS::Application::AppUtils->entityize($xml);
- $xml =~ s/[\x00-\x1f]//go;
- return $xml;
- }
- sub oils_login {
- my( $username, $password, $type ) = @_;
- $type |= "staff";
- my $nametype = 'username';
- $nametype = 'barcode' if ($username =~ /^\d+$/o);
- my $seed = OpenSRF::AppSession
- ->create("open-ils.auth")
- ->request( 'open-ils.auth.authenticate.init', $username )
- ->gather(1);
- return undef unless $seed;
- my $response = OpenSRF::AppSession
- ->create("open-ils.auth")
- ->request( 'open-ils.auth.authenticate.complete', {
- $nametype => $username,
- password => md5_hex($seed . md5_hex($password)),
- type => $type
- })
- ->gather(1);
- return undef unless $response;
- return $response->{payload}->{authtoken};
- }
Add Comment
Please, Sign In to add comment