5 use lib '/openils/lib/perl5/';
8 use OpenSRF::Application;
9 use OpenSRF::EX qw/:try/;
10 use OpenSRF::AppSession;
11 use OpenSRF::MultiSession;
12 use OpenSRF::Utils::SettingsClient;
13 use OpenILS::Application::AppUtils;
14 use OpenILS::Utils::Fieldmapper;
15 use Digest::MD5 qw/md5_hex/;
16 use OpenSRF::Utils::JSON;
18 use Unicode::Normalize;
20 use Time::HiRes qw/time/;
23 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
26 MARC::Charset->ignore_errors(1);
28 my ($idfield, $count, $user, $password, $config, $marctype, $idsubfield, @files, @trash_fields, $quiet, $libmap) =
29 ('001', 1, 'admin', 'open-ils', '/openils/conf/opensrf_core.xml', 'USMARC');
32 'idfield=s' => \$idfield,
33 'idsubfield=s' => \$idsubfield,
34 'startid=i' => \$count,
36 'password=s' => \$password,
37 'config=s' => \$config,
38 'marctype=s' => \$marctype,
40 'libmap=s' => \$libmap,
44 @files = @ARGV if (!@files);
51 $lib_id_map = map_libraries_to_ID($libmap);
54 OpenSRF::System->bootstrap_client( config_file => $config );
55 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
57 $user = OpenILS::Application::AppUtils->check_user_session( login($user,$password) )->id;
59 select STDERR; $| = 1;
60 select STDOUT; $| = 1;
62 my $batch = new MARC::Batch ( $marctype, @files );
64 $batch->warnings_off();
68 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
73 $record_field = $rec->field($idfield, $idsubfield);
75 $record_field = $rec->field($idfield);
79 # On some systems, the 001 actually points to the record ID
80 # We need to attach to the call number to handle holdings in different libraries
81 # but we can work out call numbers later in SQL by the record ID + call number text
83 $record = $record_field->data;
84 $record =~ s/^.*?(\d+).*?$/$1/o;
87 (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
88 $xml =~ s/^<\?xml.+\?\s*>//go;
89 $xml =~ s/>\s+</></go;
91 $xml = OpenILS::Application::AppUtils->entityize($xml);
92 $xml =~ s/[\x00-\x1f]//go;
94 my $bib = new Fieldmapper::serial::record_entry;
96 $bib->record($record);
100 $bib->creator($user);
101 $bib->create_date('now');
103 $bib->edit_date('now');
104 $bib->last_xact_id('IMPORT-'.$starttime);
107 my $lib_id = get_library_id($rec);
109 $bib->owning_lib($lib_id);
113 print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
117 if (!$quiet && !($count % 20)) {
118 print STDERR "\r$count\t". $count / (time - $starttime);
123 my( $username, $password, $type ) = @_;
127 my $seed = OpenILS::Application::AppUtils->simplereq(
129 'open-ils.auth.authenticate.init',
133 die("No auth seed. Couldn't talk to the auth server") unless $seed;
135 my $response = OpenILS::Application::AppUtils->simplereq(
137 'open-ils.auth.authenticate.complete',
138 { username => $username,
139 password => md5_hex($seed . md5_hex($password)),
142 die("No auth response returned on login.") unless $response;
144 my $authtime = $response->{payload}->{authtime};
145 my $authtoken = $response->{payload}->{authtoken};
147 die("Login failed for user $username!") unless $authtoken;
156 Parses a file to return a hash of library names to integers representing
157 the actor.org_unit.id value of the library. This enables us to generate
158 an ingest file that does not subsequently need to manually manipulated.
160 The library name must correspond to the 'b' subfield of the 852 field.
161 Well, it does not have to, but you will have to modify this script
164 The format of the map file should be the name of the library, followed
165 by a tab, followed by the desired numeric ID of the library. For example:
172 sub map_libraries_to_ID {
173 my $map_filename = shift;
177 open(MAP_FH, '<', $map_filename) or die "Could not load [$map_filename] $!";
179 my ($lib, $id) = $_ =~ /^(.*?)\t(.*?)$/;
180 $lib_id_map{$lib} = $id;
189 my $lib_name = $record->field('852')->subfield('b');
190 my $lib_id = $lib_id_map->{$lib_name};