Guest User

Untitled

a guest
Mar 9th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.10 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use OpenILS::Utils::Cronscript;
  4. use MARC::File::XML;
  5. use MARC::Record;
  6. use UNIVERSAL qw/isa/;
  7. use Data::Dumper;
  8.  
  9. my $script = OpenILS::Utils::Cronscript->new();
  10.  
  11. my $authtoken = $script->authenticate(
  12.     {
  13.      username => $ARGV[0],
  14.      password => $ARGV[1],
  15.      workstation => $ARGV[2],
  16.      type => $ARGV[3] || 'staff'
  17.     }
  18. );
  19.  
  20. die("Failed to authenticate") unless ($authtoken);
  21.  
  22. my $query =
  23.     {
  24.      'select' => { 'mfr' => [ 'record', 'value',
  25.                               { 'column' => 'value', 'alias' => 'length',
  26.                                 'transform' => 'character_length' } ],
  27.                    'bre' => [ 'create_date', 'edit_date' ] },
  28.      'from' => { 'mfr' => { 'bre' => { 'filter' => { 'deleted' => 'f' } } } },
  29.      'where' =>
  30.      {
  31.       'tag' => '008',
  32.       '-or' =>
  33.       {
  34.       'value' => { '!~' => '^([0-9]{6}| {6}|\|{6})' },
  35.       'value'=> { '<>' => { 'transform' => 'character_length', 'value' => 40 } }
  36.       }
  37.      }
  38.     };
  39.  
  40. my $editor = $script->editor(authtoken=>$authtoken);
  41.  
  42. my $results = $editor->json_query($query);
  43.  
  44. if ($results) {
  45.     foreach my $row (@$results) {
  46.         fix_row($row);
  47.     }
  48. } else {
  49.     print STDERR Dumper $editor->event;
  50. }
  51. $editor->finish();
  52.  
  53. sub fix_row {
  54.     my $row = shift;
  55.     if ($row->{length} == 34 || $row->{length} == 38 || $row->{length} == 40) {
  56.         my $yr = substr($row->{create_date}, 2,2);
  57.         my $mo = substr($row->{create_date}, 5,2);
  58.         my $da = substr($row->{create_date}, 8,2);
  59.         my $e = $script->editor(authtoken => $script->authtoken, xact=>1);
  60.         die $e->event unless ($e->checkauth);
  61.         my $bre = $e->retrieve_biblio_record_entry($row->{record});
  62.         my $marc = MARC::Record->new_from_xml($bre->marc, 'UTF-8');
  63.         my $field = $marc->field('008');
  64.         my $value = $field->data();
  65.         if ($row->{length} == 34) {
  66.             $value = $yr . $mo . $da . $value;
  67.         } elsif ($row->{length} == 38) {
  68.             $value .= "  ";
  69.         } elsif ($row->{length} == 40) {
  70.             $value = substr($value, 6);
  71.             $value = $yr . $mo . $da . $value;
  72.         }
  73.         $field->update($value);
  74.         $bre->marc(clean_marc($marc));
  75.         if ($e->update_biblio_record_entry($bre)) {
  76.             print STDERR "Updated " . $row->{record} . "\n";
  77.         } else {
  78.             print STDERR "Failed to update " . $row->{record} . "\n";
  79.         }
  80.         $e->finish;
  81.     } else {
  82.         print $row->{record} . "\n";
  83.     }
  84. }
  85.  
  86. # Cleans up a MARC::Record or MARCXML string for storage in the
  87. # Open-ILS database.
  88. #
  89. # Takes either a MARC::Record or a string of MARCXML.
  90. #
  91. # Returns a string of MARCXML as Open-ILS likes to store it.
  92. #
  93. # Assumes input is already in UTF-8.
  94. sub clean_marc {
  95.     my $input = shift;
  96.     my $xml = (isa $input, 'MARC::Record') ? $input->as_xml_record() : $input;
  97.     $xml =~ s/\n//sog;
  98.     $xml =~ s/^<\?xml.+\?\s*>//go;
  99.     $xml =~ s/>\s+</></go;
  100.     $xml =~ s/\p{Cc}//go;
  101.     $xml = OpenILS::Application::AppUtils->entityize($xml);
  102.     $xml =~ s/[\x00-\x1f]//go;
  103.     return $xml;
  104. }
Add Comment
Please, Sign In to add comment