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