New and hopefully last version of the MARC-DB. Is the fastest in benchmark, everybody...
[koha_fer] / acqui.simple / bulkmarcimport.pl
1 #!/usr/bin/perl
2 #
3 # Tool for importing bulk marc records
4 #
5 # WARNING!!
6 #
7 # Do not use this script on a production system, it is still in development
8 #
9 #
10
11
12
13
14 $file=$ARGV[0];
15
16 $branchname='MAIN';
17
18 unless ($file) {
19     print "USAGE: ./bulkmarcimport.pl filename\n";
20     exit;
21 }
22
23
24
25
26 my $lc1='#dddddd';
27 my $lc2='#ddaaaa';
28
29
30 use C4::Database;
31 use CGI;
32 use DBI;
33 #use strict;
34 use C4::Acquisitions;
35 use C4::Output;
36 my $dbh=C4Connect;
37 my $userid=$ENV{'REMOTE_USER'};
38 %tagtext = (
39     '001' => 'Control number',
40     '003' => 'Control number identifier',
41     '005' => 'Date and time of latest transaction',
42     '006' => 'Fixed-length data elements -- additional material characteristics',
43     '007' => 'Physical description fixed field',
44     '008' => 'Fixed length data elements',
45     '010' => 'LCCN',
46     '015' => 'LCCN Cdn',
47     '020' => 'ISBN',
48     '022' => 'ISSN',
49     '037' => 'Source of acquisition',
50     '040' => 'Cataloging source',
51     '041' => 'Language code',
52     '043' => 'Geographic area code',
53     '050' => 'Library of Congress call number',
54     '060' => 'National Library of Medicine call number',
55     '082' => 'Dewey decimal call number',
56     '100' => 'Main entry -- Personal name',
57     '110' => 'Main entry -- Corporate name',
58     '130' => 'Main entry -- Uniform title',
59     '240' => 'Uniform title',
60     '245' => 'Title statement',
61     '246' => 'Varying form of title',
62     '250' => 'Edition statement',
63     '256' => 'Computer file characteristics',
64     '260' => 'Publication, distribution, etc.',
65     '263' => 'Projected publication date',
66     '300' => 'Physical description',
67     '306' => 'Playing time',
68     '440' => 'Series statement / Added entry -- Title',
69     '490' => 'Series statement',
70     '500' => 'General note',
71     '504' => 'Bibliography, etc. note',
72     '505' => 'Formatted contents note',
73     '508' => 'Creation/production credits note',
74     '510' => 'Citation/references note',
75     '511' => 'Participant or performer note',
76     '520' => 'Summary, etc. note',
77     '521' => 'Target audience note (ie age)',
78     '530' => 'Additional physical form available note',
79     '538' => 'System details note',
80     '586' => 'Awards note',
81     '600' => 'Subject added entry -- Personal name',
82     '610' => 'Subject added entry -- Corporate name',
83     '650' => 'Subject added entry -- Topical term',
84     '651' => 'Subject added entry -- Geographic name',
85     '656' => 'Index term -- Occupation',
86     '700' => 'Added entry -- Personal name',
87     '710' => 'Added entry -- Corporate name',
88     '730' => 'Added entry -- Uniform title',
89     '740' => 'Added entry -- Uncontrolled related/analytical title',
90     '800' => 'Series added entry -- Personal name',
91     '830' => 'Series added entry -- Uniform title',
92     '852' => 'Location',
93     '856' => 'Electronic location and access',
94 );
95
96
97 my $dbh=C4Connect;
98 if ($file) {
99     open (F, "$file");
100     my $data=<F>;
101     close F;
102     $splitchar=chr(29);
103
104
105 # Cycle through all of the records in the file
106
107
108 RECORD:
109     foreach $record (split(/$splitchar/, $data)) {
110         $leader=substr($record,0,24);
111         print "\n\n---------------------------------------------------------------------------\n";
112         print "Leader: $leader\n";
113         $record=substr($record,24);
114         $splitchar2=chr(30);
115         my $directory=0;
116         my $tagcounter=0;
117         my %tag;
118         my @record;
119         my %record;
120         foreach $field (split(/$splitchar2/, $record)) {
121             my %field;
122             unless ($directory) {
123                 # Parse the MARC directory and store the cotents in the %tag hash
124                 $directory=$field;
125                 my $itemcounter=1;
126                 $counter=0;
127                 while ($item=substr($directory,0,12)) {
128                     $tag=substr($directory,0,3);
129                     $length=substr($directory,3,4);
130                     $start=substr($directory,7,6);
131                     $directory=substr($directory,12);
132                     $tag{$counter}=$tag;
133                     $counter++;
134                 }
135                 $directory=1;
136                 next;
137             }
138             $tag=$tag{$tagcounter};
139             $tagcounter++;
140             $field{'tag'}=$tag;
141             printf "%4s %-40s ",$tag, $tagtext{$tag};
142             $splitchar3=chr(31);
143             my @subfields=split(/$splitchar3/, $field);
144             $indicator=$subfields[0];
145             $field{'indicator'}=$indicator;
146             my $firstline=1;
147             if ($#subfields==0) {
148                 print "$indicator\n";
149             } else {
150                 print "\n";
151                 my %subfields;
152                 for ($i=1; $i<=$#subfields; $i++) {
153                     my $text=$subfields[$i];
154                     my $subfieldcode=substr($text,0,1);
155                     my $subfield=substr($text,1);
156                     print "   $subfieldcode $subfield\n";
157                     if ($subfields{$subfieldcode}) {
158                         my $subfieldlist=$subfields{$subfieldcode};
159                         my @subfieldlist=@$subfieldlist;
160                         if ($#subfieldlist>=0) {
161                             push (@subfieldlist, $subfield);
162                         } else {
163                             @subfieldlist=($subfields{$subfieldcode}, $subfield);
164                         }
165                         $subfields{$subfieldcode}=\@subfieldlist;
166                     } else {
167                         $subfields{$subfieldcode}=$subfield;
168                     }
169                 }
170                 $field{'subfields'}=\%subfields;
171             }
172             if ($record{$tag}) {
173                 my $fieldlist=$record{$tag};
174                 if ($fieldlist->{'tag'}) {
175                     @fieldlist=($fieldlist, \%field);
176                     $fieldlist=\@fieldlist;
177                 } else {
178                     push (@$fieldlist,\%field);
179                 }
180                 $record{$tag}=$fieldlist;
181             } else {
182                 $record{$tag}=[\%field];
183             }
184             push (@record, \%field);
185         }
186         $rec=\@record;
187         $counter++;
188         my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $additionalauthors, $illustrator, $copyrightdate, $barcode, $itemtype, $seriestitle, @barcodes);
189         my $marc=$record;
190         foreach $field (sort {$a->{'tag'} cmp $b->{'tag'}} @$rec) {
191             # LCCN is stored in field 010 a
192             if ($field->{'tag'} eq '010') {
193                 $lccn=$field->{'subfields'}->{'a'};
194                 $lccn=~s/^\s*//;
195                 $lccn=~s/cn //;
196                 $lccn=~s/^\s*//;
197                 ($lccn) = (split(/\s+/, $lccn))[0];
198             }
199             # LCCN is stored in field 015 a
200             if ($field->{'tag'} eq '015') {
201                 $lccn=$field->{'subfields'}->{'a'};
202                 $lccn=~s/^\s*//;
203                 $lccn=~s/^C//;
204                 ($lccn) = (split(/\s+/, $lccn))[0];
205             }
206             # ISBN is stored in field 020 a
207             if ($field->{'tag'} eq '020') {
208                 $isbn=$field->{'subfields'}->{'a'};
209                 $isbn=~s/^\s*//;
210                 ($isbn) = (split(/\s+/, $isbn))[0];
211             }
212             # ISSN is stored in field 022 a
213             if ($field->{'tag'} eq '022') {
214                 $issn=$field->{'subfields'}->{'a'};
215                 $issn=~s/^\s*//;
216                 ($issn) = (split(/\s+/, $issn))[0];
217             }
218             # Dewey number stored in field 082 a
219             # If there is more than one dewey number (more than one 'a'
220             # subfield) I just take the first one
221             if ($field->{'tag'} eq '082') {
222                 $dewey=$field->{'subfields'}->{'a'};
223                 $dewey=~s/\///g;
224                 if (@$dewey) {
225                     $dewey=$$dewey[0];
226                 }
227             }
228             # Author is stored in field 100 a
229             if ($field->{'tag'} eq '100') {
230                 $author=$field->{'subfields'}->{'a'};
231             }
232             # Title is stored in field 245 a
233             # Subtitle in field 245 b
234             # Illustrator in field 245 c
235             if ($field->{'tag'} eq '245') {
236                 $title=$field->{'subfields'}->{'a'};
237                 $title=~s/ \/$//;
238                 $subtitle=$field->{'subfields'}->{'b'};
239                 $subtitle=~s/ \/$//;
240                 my $name=$field->{'subfields'}->{'c'};
241                 if ($name=~/illustrated by]*\s+(.*)/) {
242                     $illustrator=$1;
243                 }
244             }
245             # Publisher Info in field 260
246             #   a = place
247             #   b = publisher
248             #   c = publication date
249             #     (also store as copyright date if date starts with a 'c' as in c1995)
250             if ($field->{'tag'} eq '260') {
251                 $place=$field->{'subfields'}->{'a'};
252                 if (@$place) {
253                     $place=$$place[0];
254                 }
255                 $place=~s/\s*:$//g;
256                 $publisher=$field->{'subfields'}->{'b'};
257                 if (@$publisher) {
258                     $publisher=$$publisher[0];
259                 }
260                 $publisher=~s/\s*:$//g;
261                 $publicationyear=$field->{'subfields'}->{'c'};
262                 if ($publicationyear=~/c(\d\d\d\d)/) {
263                     $copyrightdate=$1;
264                 }
265                 if ($publicationyear=~/[^c](\d\d\d\d)/) {
266                     $publicationyear=$1;
267                 } elsif ($copyrightdate) {
268                     $publicationyear=$copyrightdate;
269                 } else {
270                     $publicationyear=~/(\d\d\d\d)/;
271                     $publicationyear=$1;
272                 }
273             }
274             # Physical Dimensions in field 300
275             #   a = pages
276             #   c = size
277             if ($field->{'tag'} eq '300') {
278                 $pages=$field->{'subfields'}->{'a'};
279                 $pages=~s/ \;$//;
280                 $size=$field->{'subfields'}->{'c'};
281                 $pages=~s/\s*:$//g;
282                 $size=~s/\s*:$//g;
283             }
284             # Vol/No in field 362 a
285             if ($field->{'tag'} eq '362') {
286                 if ($field->{'subfields'}->{'a'}=~/(\d+).*(\d+)/) {
287                     $volume=$1;
288                     $number=$2;
289                 }
290             }
291             # Series Title in field 440 a
292             # Vol/No in field 440 v
293             if ($field->{'tag'} eq '440') {
294                 $seriestitle=$field->{'subfields'}->{'a'};
295                 if ($field->{'subfields'}->{'v'}=~/(\d+).*(\d+)/) {
296                     $volume=$1;
297                     $number=$2;
298                 }
299             }
300             # BARCODES!!!
301             # 852 p stores barcodes
302             # 852 h stores dewey field
303             # 852 9 stores replacement price
304             #   I check for an itemtype identifier in 852h as well... pb or pbk means PBK
305             #   also if $dewey is > 0, then I assign JNF, otherwise JF.
306             #   Note that my libraries are school libraries, so I assume Junior.
307             if ($field->{'tag'} eq '852') {
308                 $barcode=$field->{'subfields'}->{'p'};
309                 push (@barcodes, $barcode);
310                 my $q_barcode=$dbh->quote($barcode);
311                 my $deweyfield=$field->{'subfields'}->{'h'};
312                 $deweyfield=~/^([\d\.]*)/;
313                 $dewey=$1;
314                 if (($deweyfield=~/pbk/) || ($deweyfield=~/pb$/)) {
315                     $itemtype='PBK';
316                 } elsif ($dewey) {
317                     $itemtype='JNF';
318                 } else {
319                     $itemtype='JF';
320                 }
321
322                 $replacementprice=$field->{'subfields'}->{'9'};
323             }
324             # 700 a stores additional authors / illustrator info
325             # 700 c will contain 'ill' if it's an illustrator
326             if ($field->{'tag'} eq '700') {
327                 my $name=$field->{'subfields'}->{'a'};
328                 if ($field->{'subfields'}->{'c'}=~/ill/) {
329                     $illustrator=$name;
330                 } else {
331                     $additionalauthors.="$name\n";
332                 }
333             }
334             # I concatenate all 5XX a entries as notes
335             if ($field->{'tag'} =~/^5/) {
336                 $note.="$field->{'subfields'}->{'a'}\n";
337             }
338             # 6XX entries are subject entries
339             #   Not sure why I'm skipping 691 tags
340             #   691 a contains the subject.
341             # I take subfield a, and append entries from subfield x (general
342             # subdivision) y (Chronological subdivision) and z (geographic
343             # subdivision)
344             if ($field->{'tag'} =~/6\d\d/) {
345                 (next) if ($field->{'tag'} eq '691');
346                 my $subject=$field->{'subfields'}->{'a'};
347                 print "SUBJECT: $subject\n";
348                 $subject=~s/\.$//;
349                 if ($gensubdivision=$field->{'subfields'}->{'x'}) {
350                     my @sub=@$gensubdivision;
351                     if ($#sub>=0) {
352                         foreach $s (@sub) {
353                             $s=~s/\.$//;
354                             $subject.=" -- $s";
355                         }
356                     } else {
357                         $gensubdivision=~s/\.$//;
358                         $subject.=" -- $gensubdivision";
359                     }
360                 }
361                 if ($chronsubdivision=$field->{'subfields'}->{'y'}) {
362                     my @sub=@$chronsubdivision;
363                     if ($#sub>=0) {
364                         foreach $s (@sub) {
365                             $s=~s/\.$//;
366                             $subject.=" -- $s";
367                         }
368                     } else {
369                         $chronsubdivision=~s/\.$//;
370                         $subject.=" -- $chronsubdivision";
371                     }
372                 }
373                 if ($geosubdivision=$field->{'subfields'}->{'z'}) {
374                     my @sub=@$geosubdivision;
375                     if ($#sub>=0) {
376                         foreach $s (@sub) {
377                             $s=~s/\.$//;
378                             $subject.=" -- $s";
379                         }
380                     } else {
381                         $geosubdivision=~s/\.$//;
382                         $subject.=" -- $geosubdivision";
383                     }
384                 }
385                 push @subjects, $subject;
386             }
387         }
388
389         my $q_isbn=$dbh->quote($isbn);
390         my $q_issn=$dbh->quote($issn);
391         my $q_lccn=$dbh->quote($lccn);
392         my $sth=$dbh->prepare("select biblionumber,biblioitemnumber from biblioitems where issn=$q_issn or isbn=$q_isbn or lccn=$q_lccn");
393         $sth->execute;
394         my $biblionumber=0;
395         my $biblioitemnumber=0;
396         if ($sth->rows) {
397             ($biblionumber, $biblioitemnumber) = $sth->fetchrow;
398             my $title=$title;
399 #title already in the database
400         } else {
401             my $q_title=$dbh->quote("$title");
402             my $q_subtitle=$dbh->quote("$subtitle");
403             my $q_author=$dbh->quote($author);
404             my $q_copyrightdate=$dbh->quote($copyrightdate);
405             my $q_seriestitle=$dbh->quote($seriestitle);
406             $sth=$dbh->prepare("select biblionumber from biblio where title=$q_title and author=$q_author and copyrightdate=$q_copyrightdate and seriestitle=$q_seriestitle");
407             $sth->execute;
408             if ($sth->rows) {
409                 ($biblionumber) = $sth->fetchrow;
410 #title already in the database
411             } else {
412                 $sth=$dbh->prepare("select max(biblionumber) from biblio");
413                 $sth->execute;
414                 ($biblionumber) = $sth->fetchrow;
415                 $biblionumber++;
416                 my $q_notes=$dbh->quote($note);
417                 $sth=$dbh->prepare("insert into biblio (biblionumber, title, author, copyrightdate, seriestitle, notes) values ($biblionumber, $q_title, $q_author, $q_copyrightdate, $q_seriestitle, $q_notes)");
418                 $sth->execute;
419                 $sth=$dbh->prepare("insert into bibliosubtitle values ($q_subtitle, $biblionumber)");
420                 $sth->execute;
421             }
422             $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
423             $sth->execute;
424             ($biblioitemnumber) = $sth->fetchrow;
425             $biblioitemnumber++;
426             my $q_isbn=$dbh->quote($isbn);
427             my $q_issn=$dbh->quote($issn);
428             my $q_lccn=$dbh->quote($lccn);
429             my $q_volume=$dbh->quote($volume);
430             my $q_number=$dbh->quote($number);
431             my $q_itemtype=$dbh->quote($itemtype);
432             my $q_dewey=$dbh->quote($dewey);
433             $cleanauthor=$author;
434             $cleanauthor=~s/[^A-Za-z]//g;
435             $subclass=uc(substr($cleanauthor,0,3));
436             my $q_subclass=$dbh->quote($subclass);
437             my $q_publicationyear=$dbh->quote($publicationyear);
438             my $q_publishercode=$dbh->quote($publishercode);
439             my $q_volumedate=$dbh->quote($volumedate);
440             my $q_volumeddesc=$dbh->quote($volumeddesc);
441             my $q_illus=$dbh->quote($illustrator);
442             my $q_pages=$dbh->quote($pages);
443             my $q_notes=$dbh->quote($note);
444             ($q_notes) || ($q_notes="''");
445             my $q_size=$dbh->quote($size);
446             my $q_place=$dbh->quote($place);
447             my $q_marc=$dbh->quote($marc);
448
449             $sth=$dbh->prepare("insert into biblioitems (biblioitemnumber, biblionumber, volume, number, itemtype, isbn, issn, dewey, subclass, publicationyear, publishercode, volumedate, volumeddesc, illus, pages, size, place, lccn, marc) values ($biblioitemnumber, $biblionumber, $q_volume, $q_number, $q_itemtype, $q_isbn, $q_issn, $q_dewey, $q_subclass, $q_publicationyear, $q_publishercode, $q_volumedate, $q_volumeddesc, $q_illus, $q_pages, $q_size, $q_place, $q_lccn, $q_marc)");
450             $sth->execute;
451             my $subjectheading;
452             foreach $subjectheading (@subjects) {
453                 # convert to upper case
454                 $subjectheading=uc($subjectheading);
455                 # quote value
456                 my $q_subjectheading=$dbh->quote($subjectheading);
457                 $sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject)
458                     values ($biblionumber, $q_subjectheading)");
459                 $sth->execute;
460             }
461             my @additionalauthors=split(/\n/,$additionalauthors);
462             my $additionalauthor;
463             foreach $additionalauthor (@additionalauthors) {
464                 # remove any line ending characters (Ctrl-L or Ctrl-M)
465                 $additionalauthor=~s/\013//g;
466                 $additionalauthor=~s/\010//g;
467                 # convert to upper case
468                 $additionalauthor=uc($additionalauthor);
469                 # quote value
470                 my $q_additionalauthor=$dbh->quote($additionalauthor);
471                 $sth=$dbh->prepare("insert into additionalauthors (biblionumber,author) values ($biblionumber, $q_additionalauthor)");
472                 $sth->execute;
473             }
474         }
475         my $q_barcode=$dbh->quote($barcode);
476         my $q_homebranch="'$branchname'";
477         my $q_notes="''";
478         #my $replacementprice=0;
479         my $sth=$dbh->prepare("select max(itemnumber) from items");
480         $sth->execute;
481         my ($itemnumber) = $sth->fetchrow;
482         $itemnumber++;
483         my @datearr=localtime(time);
484         my $date=(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
485 BARCODE:
486         foreach $barcode (@barcodes) {
487             my $q_barcode=$dbh->quote($barcode);
488             my $sti=$dbh->prepare("select barcode from items where barcode=$q_barcode");
489             $sti->execute;
490             if ($sti->rows) {
491                 print "Skipping $barcode\n";
492                 next BARCODE;
493             }
494             $replacementprice=~s/^p//;
495             ($replacementprice) || ($replacementprice=0);
496             $replacementprice=~s/\$//;
497             $task="insert into items (itemnumber, biblionumber, biblioitemnumber, barcode, itemnotes, homebranch, holdingbranch, dateaccessioned, replacementprice) values ($itemnumber, $biblionumber, $biblioitemnumber, $q_barcode, $q_notes, $q_homebranch, '$branchname', '$date', $replacementprice)";
498             $sth=$dbh->prepare($task);
499             print "$task\n";
500             $sth->execute;
501         }
502     }
503 }
504 $dbh->disconnect;