5 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
9 use Unicode::Normalize;
11 use Digest::MD5 qw(md5_hex);
14 use OpenSRF::Utils::SettingsClient;
15 use OpenSRF::MultiSession;
16 use OpenSRF::Utils::JSON;
17 use OpenILS::Utils::Fieldmapper;
18 use OpenILS::Application::AppUtils;
22 my $apputils = "OpenILS::Application::AppUtils";
23 my $username = 'admin';
24 my $password = 'open-ils';
25 my $configfile = '/srv/openils/conf/opensrf_core.xml';
34 'username=s' => \$username,
35 'password=s' => \$password,
37 'configfile=s' => \$configfile,
39 'verbose' => \$verbose
44 --username : username for EG [admin]
45 --password : password for EG [open-ils]
46 --verbose : more stuff to STDERR
47 --quiet : less stuff to STDOUT
48 --overlaycap : MultiSession cap for open-ils.cat overlay calls
49 --mergecap : MultiSession cap for open-ils.cat merge calls
50 --configfile : path to opensrf_core.xml [/srv/openils/conf/opensrf_core.xml]
55 OpenSRF::System->bootstrap_client( config_file => $configfile);
56 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
59 print STDERR "login..." if $verbose;
60 my $auth = oils_login($username,$password);
61 print STDERR "ok\n" if $verbose;
63 print STDERR "opening batch..." if $verbose;
65 my $batch = MARC::Batch->new( 'USMARC', $fh );
66 print STDERR "ok\n" if $verbose;
68 print STDERR "initialize osrf:ms..." if $verbose;
69 my $cat = OpenSRF::MultiSession->new(
70 app => 'open-ils.cat',
72 success_handler => sub {
75 print STDERR $req->{meth} . " record: " . $req->{params}->[1] . " ok\n" if $verbose;
77 failure_handler => sub {
80 warn "record $req->{params}->[0] failed: " . OpenSRF::Utils::JSON->perl2JSON($req->{response});
82 session_hash_function => sub {
85 return $_[1]; # last parameter is the ID of the metarecord associated with the
86 # request's target; using this as the hash function value ensures
87 # that parallel targeters won't try to simultaneously handle two
88 # hold requests that have overlapping pools of copies that could
93 print STDERR "ok\n" if $verbose;
95 print STDERR "connecting to open-ils.cat..." if $verbose;
97 print STDERR "ok\n" if $verbose;
99 print STDERR "begin MARC loop\n" if $verbose;
102 while(my $rec = $batch->next()){
104 my @mergerecs = $rec->field("901");
105 my $main901 = shift @mergerecs;
106 my $recid = $main901->subfield('c');
108 print "processing rec . $recid\n" if $verbose;
110 my @mergeids = map($_->subfield('c'), @mergerecs);
112 $rec->delete_fields( $_ );
115 my $xml = $rec->as_xml;
118 $cat->request("open-ils.cat.biblio.record.marc.replace.override", $auth, $recid, eg_clean_xml($xml));
121 $cat->request( "open-ils.cat.biblio.records.merge", $auth, $recid, \@mergeids);
123 if (!$quiet && !($count % 50)) {
124 my $timediff = ( time - $starttime);
125 $timediff = 1 if $timediff < 1;
126 print STDERR "\r$count processed\t". $count / $timediff . " recs per sec ";
130 print STDERR "disconnecting catoverlay..." if $verbose;
131 $cat->session_wait(1);
133 print STDERR "ok\n" if $verbose;
136 print STDERR "done\n\n" if $verbose;
142 $xml =~ s/^<\?xml.+\?\s*>//go;
143 $xml =~ s/>\s+</></go;
144 $xml =~ s/\p{Cc}//go;
146 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
147 $xml =~ s/[\x00-\x1f]//go;
150 #----------------------------------------------------------------
152 my( $username, $password, $type ) = @_;
156 my $seed = $apputils->simplereq( 'open-ils.auth',
157 'open-ils.auth.authenticate.init', $username );
158 err("No auth seed") unless $seed;
160 my $response = $apputils->simplereq( 'open-ils.auth',
161 'open-ils.auth.authenticate.complete',
162 { username => $username,
163 password => md5_hex($seed . md5_hex($password)),
166 err("No auth response returned on login") unless $response;
168 # die Dumper($response);
170 $authtime = $response->{payload}->{authtime};
171 $authtoken = $response->{payload}->{authtoken};
176 #----------------------------------------------------------------
177 # Destroys the login session on the server
178 #----------------------------------------------------------------
180 $apputils->simplereq(
182 'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );