Guest User

Untitled

a guest
May 18th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.87 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # update an existing marc record
  3. use OpenSRF::System;
  4. use OpenSRF::AppSession;
  5. use OpenILS::Application::AppUtils;
  6. use Digest::MD5 qw(md5_hex);
  7. use Getopt::Long;
  8. use Data::Dumper;
  9. binmode(STDOUT, ":utf8");
  10.  
  11. # Feed this script a list of bibids and it will clean up the MARCXML in those records.
  12. # Usage: perl marc-cleanup.pl --file bibids.txt
  13.  
  14. my ($file, $help, @modified);
  15. my $count = 0;
  16.  
  17. GetOptions('file=s' => \$file, 'help' => \$help);
  18. if (!$file || $help) {
  19.     die q/Usage: perl marc-cleanup.pl --file bibids.txt/
  20. };
  21.  
  22. OpenSRF::System->bootstrap_client( config_file => '/openils/conf/opensrf_core.xml');
  23. Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
  24.  
  25. my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
  26. my $cat = OpenSRF::AppSession->create('open-ils.cat');
  27.  
  28. # osrf auth crap
  29. print "srfsh username: ";
  30. chomp( my $username = <> );
  31. print "srfsh password: ";
  32. chomp( my $password = <> );
  33. my $auth = oils_login($username, $password);
  34. die("Login unsuccessful!\n") unless $auth;
  35.  
  36. open (BIBS, "<", $file) or die "Could not open $file: $!\n";
  37. while (<BIBS>) {
  38.     chomp(my $bibid = $_); # nom nom nom
  39.     $count++;
  40.  
  41.     my $bib = $cstore->request( "open-ils.cstore.direct.biblio.record_entry.retrieve", $bibid )->gather(1);
  42.     if (!$bib) {
  43.         print STDERR "ZOMG BIB $bibid DOES NOT EXIST WTF! Aborting.\n";
  44.         next;
  45.     }
  46.     my $xml = $bib->marc;
  47.     utf8::encode($xml); # ugh.
  48.     $xml = marc_to_eg($xml); # this is the part that matters.
  49.     my $source = $bib->source;
  50.  
  51.     my $result = $cat->request('open-ils.cat.biblio.record.xml.update', $auth, $bibid, $xml, $source)->gather(1);
  52.     push @modified, $bibid;
  53. }
  54.  
  55. # free the children!
  56. $cstore->disconnect();
  57. $cat->disconnect();
  58.  
  59. print join("\n", @modified) . "\n";
  60. print scalar @modified . " bib records modified\n";
  61.  
  62. sub marc_to_eg {
  63.     my $xml = shift;
  64.     $xml    =~ s/\n//sog;
  65.     $xml =~ s/^<\?xml.+\?\s*>//go;
  66.     $xml =~ s/>\s+</></go;
  67.     $xml =~ s/\p{Cc}//go;
  68.     $xml = OpenILS::Application::AppUtils->entityize($xml);
  69.     $xml =~ s/[\x00-\x1f]//go;
  70.     return $xml;
  71. }
  72.  
  73. sub oils_login {
  74.     my( $username, $password, $type ) = @_;
  75.  
  76.     $type |= "staff";
  77.     my $nametype = 'username';
  78.     $nametype = 'barcode' if ($username =~ /^\d+$/o);
  79.  
  80.     my $seed = OpenSRF::AppSession
  81.         ->create("open-ils.auth")
  82.         ->request( 'open-ils.auth.authenticate.init', $username )
  83.         ->gather(1);
  84.  
  85.     return undef unless $seed;
  86.  
  87.     my $response = OpenSRF::AppSession
  88.         ->create("open-ils.auth")
  89.         ->request( 'open-ils.auth.authenticate.complete', {
  90.             $nametype => $username,
  91.             password => md5_hex($seed . md5_hex($password)),
  92.             type => $type
  93.         })
  94.         ->gather(1);
  95.  
  96.     return undef unless $response;
  97.  
  98.     return $response->{payload}->{authtoken};
  99. }
Add Comment
Please, Sign In to add comment