-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.pl
executable file
·301 lines (252 loc) · 7.38 KB
/
importer.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl
use WWW::Mechanize;
use JSON -support_by_pp;
use vars qw/$libpath/;
use FindBin qw($Bin);
BEGIN { $libpath="$Bin" };
use lib "$libpath";
use lib "$libpath/../lib";
use lib '/usr/src/Evergreen-ILS-2.0.3/Open-ILS/src/perlmods';
use MARC::Record;
use MARC::File::XML (BinaryEncoding => 'UTF-8');
use MARC::Charset;
use OpenSRF::System;
use OpenILS::Utils::Fieldmapper;
use OpenSRF::Utils::SettingsClient;
use OpenSRF::EX qw/:try/;
use Encode;
use Unicode::Normalize;
use OpenILS::Application::AppUtils;
use OpenILS::Application::Cat::BibCommon;
use DBI;
MARC::Charset->assume_unicode(1);
my %dbconfig = loadconfig("$Bin/config/db.config");
my ($dbname, $dbhost, $dblogin, $dbpassword) = ($dbconfig{dbname}, $dbconfig{dbhost}, $dbconfig{dblogin}, $dbconfig{dbpassword});
my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$dbhost",$dblogin,$dbpassword,{AutoCommit=>1,RaiseError=>1,PrintError=>0});
my ($start_id, $end_id);
my $bootstrap = '/openils/conf/opensrf_core.xml';
$leaderfile = "$Bin/config/leaders.map";
OpenSRF::System->bootstrap_client(config_file => $bootstrap);
Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
# must be loaded and initialized after the IDL is parsed
use OpenILS::Utils::CStoreEditor;
OpenILS::Utils::CStoreEditor::init();
# Control parameters
$SAVE = 1;
$DEBUG = 1;
$file = $ARGV[0];
$leader = $ARGV[1];
print "$file\n";
$command = "xls2csv -s 8859-1 -c '|' -d utf8 $file";
$csv = `$command`;
unless ($leader)
{
$leader = loadleader($file, $leaderfile);
}
# "Call number","100","245a ","245b","245c","260e","260f","260g","300","500","700","710","740"
my @items = split(/\n/, $csv);
$final = $#items;
push(@test, $items[0]);
$true = 1;
for ($k=0; $k<=$final; $k++)
{
if ($true)
{
push(@test, $items[$k]);
};
}
foreach $item (@test)
{
print "CSV $item\n" if ($DEBUG);
$item=~s/^\"|\"$//g;
$item = decode('iso-8859-1', $item);
my @fields = split(/\|/, $item);
$itemlength = length($item);
my $callnumber = $fields[0];
if ($callnumber=~/Call/i)
{
for ($i=0; $i<=$#fields; $i++)
{
my ($field) = $fields[$i];
$field=~s/\"//g;
$field=~s/^\s+|\s+$//g;
my ($subfield);
if ($field=~/(\d+)(\D+)/)
{
$field = $1;
$subfield = $2;
}
my $outfield = $field;
$subfield="a" if (!$subfield && $outfield!~/call/i);
$outfield.="|$subfield" if ($subfield);
$map{$i} = $outfield;
print "$i => $outfield\n" if ($DEBUG);
}
}
else
{
# Real data
my $record = MARC::Record->new();
## add the leader to the record
$showleader = $leader || '00620nam a22 a 4500';
$record->leader($showleader);
my $controlfield = MARC::Field->new( '008', '110701s d' );
$record->insert_fields_ordered($controlfield);
my ($title, $author, $holding, $thisbarcode);
#for ($i=0; $i<=$#fields; $i++)
for ($i=$#fields; $i>=0; $i--)
{
my $mapvalue = $fields[$i];
my $uname = $map{$i};
if ($uname=~/barcode/i)
{
$thisbarcode = $mapvalue;
}
else
{
my ($field, $subfield) = split(/\|/, $map{$i});
$mapvalue=~s/^\s*\"\s*|\s*\"\s*$//g;
print "$map{$i} => $mapvalue\n" if ($DEBUG);
$title = $mapvalue if ($map{$i} eq '245|a');
$author = $mapvalue if ($map{$i} eq '700|a');
$holding = $mapvalue if ($field=~/call/i);
if ($field && $mapvalue && $field!~/call/i)
{
print "M $field $mapvalue\n" if ($DEBUG);
my $newfield = MARC::Field->new($field,'','',$subfield => $mapvalue);
$record->insert_fields_ordered($newfield);
}
};
}
$xml = $record->as_xml_record();
print "Metadata: $title $author -$holding- Len:$itemlength\n" if ($DEBUG);
print "Stored: $xml\n" if ($DEBUG);
if ($SAVE && $itemlength > 10 && $holding)
{
my $editor = OpenILS::Utils::CStoreEditor->new(xact=>1);
my $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($editor, $xml); #, $source, $auto_tcn, 1, 1);
print "$record\n" if ($DEBUG);
$status = $editor->commit();
# Adding holdings
if ($status)
{
# Retreive latest inserted ID
my $thisid = receive_id($dbh, $title, $author);
print "$thisid $thisbarcode\n" if ($DEBUG);
# Store new holding for record with ID
add_holding($dbh, $thisid, $holding, $thisbarcode);
}
}
}
};
sub add_holding
{
my ($dbh, $id, $holding, $thisbarcode, $DEBUG) = @_;
my $barcode;
return if (!$id || !$holding);
$str_holding = $holding;
$str_holding=~s/\W/\_/g;
#insert into asset.call_number (creator, editor, record, owning_lib, label, label_sortkey) values (5, 5, 1446572, 4, 'Brok 5927/4', 'Brok_5927_4');
$dbh->do("insert into asset.call_number (creator, editor, record, owning_lib, label, label_sortkey) values (1, 1, $id, 4, '$holding', '$str_holding')");
# Get id
$sqlquery = "select id from asset.call_number order by id desc limit 1";
my $sth = $dbh->prepare("$sqlquery");
$sth->execute();
my $call_id = $sth->fetchrow_array();
unless ($thisbarcode)
{
$barcode = check_barcode($dbh);
# barcode=30051002988267
$barcode+=1;
while (check_barcode($dbh, $barcode))
{
$barcode++;
}
}
else
{
$thisbarcode=~s/\"//g;
$barcode = "$thisbarcode"."0";
}
print "$call_id $barcode\n" if ($DEBUG);
$barcode=~s/\"|\'//g;
$dbh->do("insert into asset.copy (circ_lib, creator, call_number, editor, location, loan_duration, fine_level, barcode) values (4, 5, $call_id, 5, 105, 2, 2, $barcode)");
return $barcode;
}
sub check_barcode
{
my ($dbh, $barcode) = @_;
my $sqlquery = "select barcode from asset.copy";
$sqlquery.=" where barcode='$barcode'" if ($barcode);
$sqlquery.=" order by id desc limit 100";
my $sth = $dbh->prepare("$sqlquery");
$sth->execute();
while (my $tmpbarcodedb = $sth->fetchrow_array())
{
if ($tmpbarcodedb!~/^3005/ && $tmpbarcode=~/^\d+$/)
{
$barcodedb = $tmpbarcodedb unless ($barcodedb);
}
};
return $barcodedb;
}
sub receive_id
{
my ($dbh, $title, $author) = @_;
my $thisid;
my $sqlquery = "select id, marc from biblio.record_entry order by id desc limit 1";
my $sth = $dbh->prepare("$sqlquery");
$sth->execute();
my $firstid;
while (my ($id, $marc) = $sth->fetchrow_array())
{
print "$id $marc\n";
$firstid = $id unless ($firstid);
if ($title!~/\(/)
{
$thisid = $id if ($marc=~/$title/);
$thisid = $id if ($marc=~/$author/);
};
last if ($thisid);
};
$thisid = $firstid unless ($thisid);
print "[DEBUG] $thisid\n";
return $thisid;
}
sub loadleader
{
my ($file, $leaderfile) = @_;
my ($leader, %leaders);
open(lfile, $leaderfile);
@leaders = <lfile>;
close(lfile);
foreach $str (@leaders)
{
$str=~s/\r|\n//g;
my ($name, $leader) = split(/\;\;/, $str);
$leaders{$name} = $leader;
}
foreach $name (sort keys %leaders)
{
if ($file=~/$name/i)
{
$leader = $leaders{$name};
}
}
return $leader;
}
sub loadconfig
{
my ($configfile, $DEBUG) = @_;
my %config;
open(conf, $configfile);
while (<conf>)
{
my $str = $_;
$str=~s/\r|\n//g;
my ($name, $value) = split(/\s*\=\s*/, $str);
$config{$name} = $value;
}
close(conf);
return %config;
}