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