]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/marc2bre.pl
adding simpler loader scripts
[Evergreen.git] / Open-ILS / src / extras / import / marc2bre.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use lib '/openils/lib/perl5/';
6
7 #use OpenSRF::System;
8 #use OpenILS::Utils::Fieldmapper;
9 #use OpenSRF::Utils::SettingsClient;
10 #
11 #OpenSRF::System->bootstrap_client(config_file =>
12 #'/openils/conf/bootstrap.conf');
13 #Fieldmapper->import(IDL =>
14 #  OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
15 #
16 #  # do this after bootstrapping/importing IDL
17 #  require OpenILS::Application::Search;
18 #
19 #  my $meth = OpenSRF::Application->method_lookup(
20 #    'open-ils.search.biblio.metarecord.mods_slim.retrieve');
21 #    my @data = $meth->run(1);
22 #    my $mods = shift @data;
23 #    print "Got mvr: " . $mods->title . "\n";
24
25
26
27 use OpenSRF::System;
28 use OpenSRF::Application;
29 use OpenSRF::EX qw/:try/;
30 use OpenSRF::AppSession;
31 use OpenSRF::MultiSession;
32 use OpenSRF::Utils::SettingsClient;
33 use OpenILS::Application::AppUtils;
34 use OpenILS::Utils::Fieldmapper;
35 use Digest::MD5 qw/md5_hex/;
36 use JSON;
37 use Data::Dumper;
38
39 use Time::HiRes qw/time/;
40 use Getopt::Long;
41 use MARC::Batch;
42 use MARC::File::XML;
43 use MARC::Charset;
44 use UNIVERSAL::require;
45
46 MARC::Charset->ignore_errors(1);
47
48 my ($id_field, $count, $user, $password, $config, $keyfile,  @files) =
49         ('998', 1, 'admin', 'open-ils', '/openils/conf/bootstrap.conf');
50
51 GetOptions(
52         'startid=i'     => \$count,
53         'idfield=s'     => \$id_field,
54         'user=s'        => \$user,
55         'password=s'    => \$password,
56         'keyfile=s'     => \$keyfile,
57         'config=s'      => \$config,
58         'file=s'        => \@files,
59 );
60
61 @files = @ARGV if (!@files);
62
63 my @ses;
64 my @req;
65 my %processing_cache;
66
67 my %source_map = (      
68         o  => 'OCLC',
69         i  => 'ISxN',    
70         l  => 'Local',
71         s  => 'System',  
72         g  => 'Gutenberg',  
73 );                              
74
75
76 OpenSRF::System->bootstrap_client( config_file => $config );
77 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
78
79 $user = OpenILS::Application::AppUtils->check_user_session( login($user,$password) )->id;
80
81 my %keymap;
82 if ($keyfile) {
83         open F, $keyfile or die "Couldn't open key file $keyfile";
84         while (<F>) {
85                 if ( /^(\d+)\|(\S+)/o ) {
86                         $keymap{$1} = $2;
87                 }
88         }
89 }
90
91 select STDERR; $| = 1;
92 select STDOUT; $| = 1;
93
94 my $batch = new MARC::Batch ( 'USMARC', @files );
95 $batch->strict_off();
96 $batch->warnings_off();
97
98 my $starttime = time;
99 while ( my $rec = $batch->next ) {
100
101         my $id = $rec->subfield($id_field => 'a') || $count;
102         if ($id =~ /(\d+)/o) {
103                 $id = $1;
104         }
105
106         if ($keyfile) {
107                 if (my $tcn = $keymap{$id}) {
108                         $rec->delete_field( $_ ) for ($rec->field('035'));
109                         $rec->append_fields( MARC::Field->new( '035', '', '', 'a', $tcn ) );
110                 } else {
111                         $count++;
112                         next;
113                 }
114         }
115
116         $rec = preprocess($rec, $id);
117
118         if (!$rec) {
119                 next;
120         }
121
122         my $tcn_value = $rec->subfield('039' => 'a');
123         my $tcn_source = $rec->subfield('039' => 'b');
124
125         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
126         $xml =~ s/^<\?xml.+\?\s*>//go;
127         $xml =~ s/>\s+</></go;
128
129         my $bib = new Fieldmapper::biblio::record_entry;
130         $bib->id($id);
131         $bib->active('t');
132         $bib->deleted('f');
133         $bib->marc($xml);
134         $bib->creator($user);
135         $bib->create_date('now');
136         $bib->editor($user);
137         $bib->edit_date('now');
138         $bib->tcn_source($tcn_source);
139         $bib->tcn_value($tcn_value);
140         $bib->last_xact_id('IMPORT-'.$starttime);
141
142         print JSON->perl2JSON($bib)."\n";
143
144         $count++;
145
146         if (!($count % 20)) {
147                 print STDERR "\r$count\t". $count / (time - $starttime);
148         }
149 }
150
151 sub preprocess {
152         my $rec = shift;
153
154         my ($id, $source, $value);
155
156         if (!$id) {
157                 my $f = $rec->field('001');
158                 $id = $f->data if ($f);
159         }
160
161         if (!$id) {
162                 my $f = $rec->field('000');
163                 $id = 'g'.$f->data if ($f);
164                 $source = 'g';
165         }
166
167         if (!$id) {
168                 my $f = $rec->field('020');
169                 $id = $f->subfield('a') if ($f);
170                 $source = 'i';
171         }
172
173         if (!$id) {
174                 my $f = $rec->field('022');
175                 $id = $f->subfield('a') if ($f);
176                 $source = 'i';
177         }
178
179         if (!$id) {
180                 my $f = $rec->field('010');
181                 $id = $f->subfield('a') if ($f);
182                 $source = 'l';
183         }
184
185         if (!$id) {
186                 my $f = $rec->field('035');
187                 $id = $f->subfield('a') if ($f);
188                 $source = 's';
189         }
190
191         if (!$id) {
192                 $count++;
193                 warn "\n !!! Record with no TCN : $count\n".$rec->as_formatted;
194                 return undef;
195         }
196
197         $id =~ s/\s*$//o;
198         $id =~ s/^\s*//o;
199         $id =~ s/(\S+)$/$1/o;
200
201         $id = $source.$id if ($source);
202
203         ($source, $value) = $id =~ /^(.)(.+)$/o;
204         if ($id =~ /^o(\d+)$/o) {
205                 $id = "ocm$1";
206                 $source = 'o';
207         }
208
209         my $tcn = MARC::Field->new(
210                 '039',
211                 '', '',
212                 'a', $id,
213                 'b', do { $source_map{$source} || 'System' },
214         );
215
216         $rec->delete_field($_) for ($rec->field('035','948','998'));
217         $rec->append_fields($tcn);
218
219         return $rec;
220 }
221
222 sub login {        
223         my( $username, $password, $type ) = @_;
224
225         $type |= "staff"; 
226
227         my $seed = OpenILS::Application::AppUtils->simplereq(
228                 'open-ils.auth',
229                 'open-ils.auth.authenticate.init',
230                 $username
231         );
232
233         die("No auth seed. Couldn't talk to the auth server") unless $seed;
234
235         my $response = OpenILS::Application::AppUtils->simplereq(
236                 'open-ils.auth',
237                 'open-ils.auth.authenticate.complete',
238                 {       username => $username,
239                         password => md5_hex($seed . md5_hex($password)),
240                         type => $type });
241
242         die("No auth response returned on login.") unless $response;
243
244         my $authtime = $response->{payload}->{authtime};
245         my $authtoken = $response->{payload}->{authtoken};
246
247         die("Login failed for user $username!") unless $authtoken;
248
249         return $authtoken;
250 }       
251