]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Evergreen/src/extras/import/import_clean_marc.pl
2c45eb2ef4c0442a4b65fa3a4d1ce29951d29c19
[working/Evergreen.git] / Evergreen / src / extras / import / import_clean_marc.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use XML::LibXML;
4 use Time::HiRes qw/time/;
5 use Getopt::Long;
6 use Data::Dumper;
7 use Error qw/:try/;
8 use open qw/:utf8/;
9
10 $|=1;
11
12 my ($userid, $sourceid, $rec_id, $entry_file, $map_file) = (1, 2, 1, 'record_entry.sql','record_id_map.pl');
13
14 GetOptions (    
15         "sourceid"              => \$sourceid,
16         "entry_file=s"          => \$entry_file,
17         "tcn_map_file=s"        => \$map_file,
18         "userid=i"              => \$userid,
19         "first=i"               => \$rec_id,
20 );
21
22 my $tcn_map;
23
24 open RE, ">$entry_file" or die "Can't open $entry_file!  $!\n";
25
26 print RE <<SQL;
27 SET CLIENT_ENCODING TO 'UNICODE';
28 COPY biblio.record_entry (id,editor,creator,source,tcn_value,marc,last_xact_id) FROM STDIN;
29 SQL
30
31 my $xact_id = time;
32
33 my $parser = XML::LibXML->new;
34
35 my $xml = '';
36 while ( $xml .= <STDIN> ) {
37         chomp $xml;
38         next unless $xml;
39
40         my $tcn;
41         my $success = 0;
42         try {
43                 my $doc = $parser->parse_string($xml);;
44                 $tcn = $doc->documentElement->findvalue( '/*/*[@tag="035"][1]' );
45                 $success = 1;
46         } catch Error with {
47                 my $e = shift;
48                 warn $e;
49                 warn $xml;
50         };      
51         next unless $success;
52
53         $xml =~ s/\\/\\\\/go;
54         $xml =~ s/\t/\\t/go;
55
56         $tcn =~ s/^.*?(\w+)\s*$/$1/go;
57         
58         unless ($tcn) {
59                 warn "\nNo TCN found for rec # $rec_id\n";
60                 $xml = '';
61                 $rec_id++;
62                 next;
63         }
64
65         if (exists($$tcn_map{$tcn})) {
66                 warn "\n !! TCN $tcn already exists!\n";
67                 $xml = '';
68                 next;
69         }
70
71         print ".";
72         $$tcn_map{$tcn} = $rec_id;
73
74         print RE join("\t", ($rec_id,$userid,$userid,$sourceid,$tcn,$xml,$xact_id))."\n";
75
76         $rec_id++;
77         $xml = '';
78 }
79
80 print RE "\\.\n";
81 print RE "SELECT setval('biblio.record_entry_id_seq'::TEXT, $rec_id);\n";
82
83 open MAP, ">$map_file" or die "Can't open $map_file!  $!\n";
84 print MAP Data::Dumper->Dump([$tcn_map],['tcn_map']);
85
86
87