Z39.50 now stores list of servers to query in a database table.
[koha-ffzg.git] / acqui.simple / marcimport.pl
1 #!/usr/bin/perl
2
3
4 my $lc1='#dddddd';
5 my $lc2='#ddaaaa';
6
7
8 use C4::Database;
9 use CGI;
10 use DBI;
11 #use strict;
12 use C4::Acquisitions;
13 use C4::Output;
14 my $dbh=C4Connect;
15 my $userid=$ENV{'REMOTE_USER'};
16 %tagtext = (
17     '001' => 'Control number',
18     '003' => 'Control number identifier',
19     '005' => 'Date and time of latest transaction',
20     '006' => 'Fixed-length data elements -- additional material characteristics',
21     '007' => 'Physical description fixed field',
22     '008' => 'Fixed length data elements',
23     '010' => 'LCCN',
24     '015' => 'LCCN Cdn',
25     '020' => 'ISBN',
26     '022' => 'ISSN',
27     '037' => 'Source of acquisition',
28     '040' => 'Cataloging source',
29     '041' => 'Language code',
30     '043' => 'Geographic area code',
31     '050' => 'Library of Congress call number',
32     '060' => 'National Library of Medicine call number',
33     '082' => 'Dewey decimal call number',
34     '100' => 'Main entry -- Personal name',
35     '110' => 'Main entry -- Corporate name',
36     '130' => 'Main entry -- Uniform title',
37     '240' => 'Uniform title',
38     '245' => 'Title statement',
39     '246' => 'Varying form of title',
40     '250' => 'Edition statement',
41     '256' => 'Computer file characteristics',
42     '260' => 'Publication, distribution, etc.',
43     '263' => 'Projected publication date',
44     '300' => 'Physical description',
45     '306' => 'Playing time',
46     '440' => 'Series statement / Added entry -- Title',
47     '490' => 'Series statement',
48     '500' => 'General note',
49     '504' => 'Bibliography, etc. note',
50     '505' => 'Formatted contents note',
51     '508' => 'Creation/production credits note',
52     '510' => 'Citation/references note',
53     '511' => 'Participant or performer note',
54     '520' => 'Summary, etc. note',
55     '521' => 'Target audience note (ie age)',
56     '530' => 'Additional physical form available note',
57     '538' => 'System details note',
58     '586' => 'Awards note',
59     '600' => 'Subject added entry -- Personal name',
60     '610' => 'Subject added entry -- Corporate name',
61     '650' => 'Subject added entry -- Topical term',
62     '651' => 'Subject added entry -- Geographic name',
63     '656' => 'Index term -- Occupation',
64     '700' => 'Added entry -- Personal name',
65     '710' => 'Added entry -- Corporate name',
66     '730' => 'Added entry -- Uniform title',
67     '740' => 'Added entry -- Uncontrolled related/analytical title',
68     '800' => 'Series added entry -- Personal name',
69     '830' => 'Series added entry -- Uniform title',
70     '852' => 'Location',
71     '856' => 'Electronic location and access',
72 );
73
74
75 my $input = new CGI;
76 my $dbh=C4Connect;
77
78 print $input->header;
79 print startpage();
80 print startmenu('acquisitions');
81 my $file=$input->param('file');
82
83 if ($input->param('z3950queue')) {
84     my $query=$input->param('query');
85     my $type=$input->param('type');
86     my @serverlist;
87     foreach ($input->param) {
88         if (/S-(.*)/) {
89             my $server=$1;
90             if ($server eq 'MAN') {
91                 push @serverlist, "MAN/".$input->param('manualz3950server')."//";
92             } else {
93                 my $sth=$dbh->prepare("select host,port,db,userid,password from z3950servers where id=$server");
94                 $sth->execute;
95                 my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
96                 push @serverlist, "$server/$host\:$port/$db/$userid/$password";
97             }
98         }
99     }
100     my $isbnfailed=0;
101     if ($type eq 'isbn') {
102         my $q=$query;
103         $q=~s/[^X\d]//g;
104         $q=~s/X.//g;
105         if (length($q)==10) {
106             my $checksum=substr($q,9,1);
107             my $isbn=substr($q,0,9);
108             my $i;
109             my $c=0;
110             for ($i=0; $i<9; $i++) {
111                 my $digit=substr($q,$i,1);
112                 $c+=$digit*(10-$i);
113             }
114             $c=int(11-($c/11-int($c/11))*11+.1);
115             ($c==10) && ($c='X');
116             if ($c eq $checksum) {
117             } else {
118                 print "<font color=red size=+1>$query is not a valid ISBN
119                 Number</font><p>\n";
120                 $isbnfailed=1;
121             }
122         } else {
123             print "<font color=red size=+1>$query is not a valid ISBN
124             Number</font><p>\n";
125             $isbnfailed=1;
126         }
127     }
128     unless ($isbnfailed) {
129         my $q_term=$dbh->quote($query);
130         my $serverlist='';
131         foreach (@serverlist) {
132             $serverlist.="$_ ";
133         }
134         chop $serverlist;
135         my $q_serverlist=$dbh->quote($serverlist);
136         my $sth=$dbh->prepare("insert into z3950queue (term,type,servers) values ($q_term, '$type', $q_serverlist)");
137         $sth->execute;
138     }
139 }
140
141 if (my $data=$input->param('uploadmarc')) {
142     my $name=$input->param('name');
143     ($name) || ($name=$data);
144     my $marcrecord='';
145     if (length($data)>0) {
146         while (<$data>) {
147             $marcrecord.=$_;
148         }
149     }
150     my $q_marcrecord=$dbh->quote($marcrecord);
151     my $q_name=$dbh->quote($name);
152     my $sth=$dbh->prepare("insert into uploadedmarc (marc,name) values ($q_marcrecord, $q_name)");
153     $sth->execute;
154 }
155
156
157 if ($input->param('insertnewrecord')) {
158     my $isbn=$input->param('isbn');
159     my $issn=$input->param('issn');
160     my $lccn=$input->param('lccn');
161     my $q_origisbn=$dbh->quote($input->param('origisbn'));
162     my $q_origissn=$dbh->quote($input->param('origissn'));
163     my $q_origlccn=$dbh->quote($input->param('origlccn'));
164     my $q_origcontrolnumber=$dbh->quote($input->param('origcontrolnumber'));
165     my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
166     my $q_issn=$dbh->quote((($issn) || ('NIL')));
167     my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
168     $sth=$dbh->prepare("insert into marcrecorddone values ($q_origisbn, $q_origissn, $q_origlccn, $q_origcontrolnumber)");
169     $sth->execute;
170     my $sth=$dbh->prepare("select biblionumber,biblioitemnumber from biblioitems where issn=$q_issn or isbn=$q_isbn or lccn=$q_lccn");
171     $sth->execute;
172     my $biblionumber=0;
173     my $biblioitemnumber=0;
174     print "<center>\n";
175     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file>New Record</a> | <a href=marcimport.pl>New File</a><br>\n";
176     if ($sth->rows) {
177         ($biblionumber, $biblioitemnumber) = $sth->fetchrow;
178         my $title=$input->param('title');
179         print << "EOF";
180         <table border=0 width=50% cellpadding=10 cellspacing=0>
181         <tr><th bgcolor=black><font color=white>Record already in database</font></th></tr>
182         <tr><td bgcolor=#dddddd>$title is already in the database with biblionumber $biblionumber and biblioitemnumber $biblioitemnumber</td></tr>
183         </table>
184         <p>
185 EOF
186     } else {
187         my $q_title=$dbh->quote($input->param('title'));
188         my $q_subtitle=$dbh->quote($input->param('subtitle'));
189         my $q_author=$dbh->quote($input->param('author'));
190         my $q_copyrightdate=$dbh->quote($input->param('copyrightdate'));
191         my $q_seriestitle=$dbh->quote($input->param('seriestitle'));
192         $sth=$dbh->prepare("select biblionumber from biblio where title=$q_title and author=$q_author and copyrightdate=$q_copyrightdate and seriestitle=$q_seriestitle");
193         $sth->execute;
194         if ($sth->rows) {
195             ($biblionumber) = $sth->fetchrow;
196         } else {
197             $sth=$dbh->prepare("select max(biblionumber) from biblio");
198             $sth->execute;
199             ($biblionumber) = $sth->fetchrow;
200             $biblionumber++;
201             $sth=$dbh->prepare("insert into biblio (biblionumber, title, author, copyrightdate, seriestitle) values ($biblionumber, $q_title, $q_author, $q_copyrightdate, $q_seriestitle)");
202             $sth->execute;
203             $sth=$dbh->prepare("insert into bibliosubtitle (biblionumber,subtitle) values ($biblionumber, $q_subtitle)");
204             $sth->execute;
205         }
206         $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
207         $sth->execute;
208         ($biblioitemnumber) = $sth->fetchrow;
209         $biblioitemnumber++;
210         my $q_isbn=$dbh->quote($isbn);
211         my $q_issn=$dbh->quote($issn);
212         my $q_lccn=$dbh->quote($lccn);
213         my $q_volume=$dbh->quote($input->param('volume'));
214         my $q_number=$dbh->quote($input->param('number'));
215         my $q_itemtype=$dbh->quote($input->param('itemtype'));
216         my $q_dewey=$dbh->quote($input->param('dewey'));
217         my $q_subclass=$dbh->quote($input->param('subclass'));
218         my $q_publicationyear=$dbh->quote($input->param('publicationyear'));
219         my $q_publishercode=$dbh->quote($input->param('publishercode'));
220         my $q_volumedate=$dbh->quote($input->param('volumedate'));
221         my $q_volumeddesc=$dbh->quote($input->param('volumeddesc'));
222         my $q_illus=$dbh->quote($input->param('illustrator'));
223         my $q_pages=$dbh->quote($input->param('pages'));
224         my $q_notes=$dbh->quote($input->param('note'));
225         my $q_size=$dbh->quote($input->param('size'));
226         my $q_place=$dbh->quote($input->param('place'));
227         my $q_marc=$dbh->quote($input->param('marc'));
228
229         $sth=$dbh->prepare("insert into biblioitems (biblioitemnumber, biblionumber, volume, number, itemtype, isbn, issn, dewey, subclass, publicationyear, publishercode, volumedate, volumeddesc, illus, pages, notes, 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_notes, $q_size, $q_place, $q_lccn, $q_marc)");
230         $sth->execute;
231         my $subjectheadings=$input->param('subject');
232         my $additionalauthors=$input->param('additionalauthors');
233         my @subjectheadings=split(/\n/,$subjectheadings);
234         my $subjectheading;
235         foreach $subjectheading (@subjectheadings) {
236             # remove any line ending characters (Ctrl-J or M)
237             $subjectheading=~s/\013//g;
238             $subjectheading=~s/\010//g;
239             # convert to upper case
240             $subjectheading=uc($subjectheading);
241             chomp ($subjectheading);
242             while (ord(substr($subjectheading, length($subjectheading)-1, 1))<14) {
243                 chop $subjectheading;
244             }
245             # quote value
246             my $q_subjectheading=$dbh->quote($subjectheading);
247             $sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject)
248                 values ($biblionumber, $q_subjectheading)");
249             $sth->execute;
250         }
251         my @additionalauthors=split(/\n/,$additionalauthors);
252         my $additionalauthor;
253         foreach $additionalauthor (@additionalauthors) {
254             # remove any line ending characters (Ctrl-L or Ctrl-M)
255             $additionalauthor=~s/\013//g;
256             $additionalauthor=~s/\010//g;
257             # convert to upper case
258             $additionalauthor=uc($additionalauthor);
259             # quote value
260             my $q_additionalauthor=$dbh->quote($additionalauthor);
261             $sth=$dbh->prepare("insert into additionalauthors (biblionumber,author) values ($biblionumber, $q_additionalauthor)");
262             $sth->execute;
263         }
264
265         my $title=$input->param('title');
266         print << "EOF";
267         <table cellpadding=10 cellspacing=0 border=0 width=50%>
268         <tr><th bgcolor=black><font color=white>Record entered into database</font></th></tr>
269         <tr><td bgcolor=#dddddd>$title has been entered into the database with biblionumber
270         $biblionumber and biblioitemnumber $biblioitemnumber</td></tr>
271         </table>
272 EOF
273     }
274     my $title=$input->param('title');
275     $sth=$dbh->prepare("select max(barcode) from items");
276     $sth->execute;
277     my ($barcode) = $sth->fetchrow;
278     $barcode++;
279     if ($barcode==1) {
280         $barcode=int(rand()*1000000);
281     }
282     print << "EOF";
283     <table border=0 cellpadding=10 cellspacing=0>
284     <tr><th bgcolor=black><font color=white>
285 Add a New Item for $title
286 </font>
287 </th></tr>
288 <tr><td bgcolor=#dddddd>
289 <form>
290 <input type=hidden name=newitem value=1>
291 <input type=hidden name=biblionumber value=$biblionumber>
292 <input type=hidden name=biblioitemnumber value=$biblioitemnumber>
293 <input type=hidden name=file value=$file>
294 <table border=0>
295 <tr><td>BARCODE</td><td><input name=barcode size=10 value=$barcode> Home Branch: <select name=homebranch><option value='STWE'>Stewart Elementary<option value='MEZ'>Meziadin Elementary</select></td></tr>
296 </tr><td>Replacement Price:</td><td><input name=replacementprice size=10></td></tr>
297 <tr><td>Notes</td><td><textarea name=notes rows=4 cols=40
298 wrap=physical></textarea></td></tr>
299 </table>
300 </td></tr>
301 </table>
302 <p>
303 <input type=submit value="Add Item">
304 </form>
305 EOF
306 print endmenu();
307 print endpage();
308
309 exit;
310 }
311
312 if ($input->param('newitem')) {
313     my $barcode=$input->param('barcode');
314     my $q_barcode=$dbh->quote($barcode);
315     my $q_notes=$dbh->quote($input->param('notes'));
316     my $q_homebranch=$dbh->quote($input->param('homebranch'));
317     my $biblionumber=$input->param('biblionumber');
318     my $biblioitemnumber=$input->param('biblioitemnumber');
319     my $replacementprice=($input->param('replacementprice') || 0);
320     my $sth=$dbh->prepare("select barcode from items where
321     barcode=$q_barcode");
322     $sth->execute;
323     if ($sth->rows) {
324         print "<font color=red>Barcode '$barcode' has already been assigned.</font><p>\n";
325     } else {
326         $sth=$dbh->prepare("select max(itemnumber) from items");
327         $sth->execute;
328         my ($itemnumber) = $sth->fetchrow;
329         $itemnumber++;
330         my @datearr=localtime(time);
331         my $date=(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
332         $sth=$dbh->prepare("insert into items (itemnumber, biblionumber, biblioitemnumber, barcode, itemnotes, homebranch, holdingbranch, dateaccessioned, replacementprice) values ($itemnumber, $biblionumber, $biblioitemnumber, $q_barcode, $q_notes, $q_homebranch, 'STWE', '$date', $replacementprice)");
333         $sth->execute;
334     }
335 }
336
337
338 my $menu = $input->param('menu');
339 if ($file) {
340     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
341     my $qisbn=$input->param('isbn');
342     my $qissn=$input->param('issn');
343     my $qlccn=$input->param('lccn');
344     my $qcontrolnumber=$input->param('controlnumber');
345     if ($qisbn || $qissn || $qlccn || $qcontrolnumber) {
346         print "<a href=$ENV{'SCRIPT_NAME'}>New File</a><hr>\n";
347         #open (F, "$file");
348         #my $data=<F>;
349         my $data;
350         if ($file=~/Z-(\d+)/) {
351             my $id=$1;
352             my $resultsid=$input->param('resultsid');
353             my $sth=$dbh->prepare("select results from z3950results where id=$resultsid");
354             $sth->execute;
355             ($data) = $sth->fetchrow;
356         } else {
357             my $sth=$dbh->prepare("select marc from uploadedmarc where id=$file");
358             $sth->execute;
359             ($data) = $sth->fetchrow;
360         }
361
362         $splitchar=chr(29);
363         my @records;
364         foreach $record (split(/$splitchar/, $data)) {
365             my $marctext="<table border=0 cellspacing=0>\n";
366             $marctext.="<tr><th colspan=3 bgcolor=black><font color=white>MARC RECORD</font></th></tr>\n";
367             $leader=substr($record,0,24);
368             $marctext.="<tr><td>Leader:</td><td colspan=2>$leader</td></tr>\n";
369             $record=substr($record,24);
370             $splitchar2=chr(30);
371             my $directory=0;
372             my $tagcounter=0;
373             my %tag;
374             my @record;
375             foreach $field (split(/$splitchar2/, $record)) {
376                 my %field;
377                 ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
378                 unless ($directory) {
379                     $directory=$field;
380                     my $itemcounter=1;
381                     $counter=0;
382                     while ($item=substr($directory,0,12)) {
383                         $tag=substr($directory,0,3);
384                         $length=substr($directory,3,4);
385                         $start=substr($directory,7,6);
386                         $directory=substr($directory,12);
387                         $tag{$counter}=$tag;
388                         $counter++;
389                     }
390                     $directory=1;
391                     next;
392                 }
393                 $tag=$tag{$tagcounter};
394                 $tagcounter++;
395                 $field{'tag'}=$tag;
396                 $marctext.="<tr><td bgcolor=$color valign=top>$tagtext{$tag}</td><td bgcolor=$color valign=top>$tag</td>";
397                 $splitchar3=chr(31);
398                 my @subfields=split(/$splitchar3/, $field);
399                 $indicator=$subfields[0];
400                 $field{'indicator'}=$indicator;
401                 my $firstline=1;
402                 if ($#subfields==0) {
403                     $marctext.="<td bgcolor=$color valign=top>$indicator</td></tr>";
404                 } else {
405                     my %subfields;
406                     $marctext.="<td bgcolor=$color valign=top><table border=0 cellspacing=0>\n";
407                     my $color2=$color;
408                     for ($i=1; $i<=$#subfields; $i++) {
409                         ($color2 eq $lc1) ? ($color2=$lc2) : ($color2=$lc1);
410                         my $text=$subfields[$i];
411                         my $subfieldcode=substr($text,0,1);
412                         my $subfield=substr($text,1);
413                         $marctext.="<tr><td colour=$color2><table border=0 cellpadding=0 cellspacing=0><tr><td>$subfieldcode </td></tr></table></td><td colour=$color2>$subfield</td></tr>\n";
414                         if ($subfields{$subfieldcode}) {
415                             my $subfieldlist=$subfields{$subfieldcode};
416                             my @subfieldlist=@$subfieldlist;
417                             if ($#subfieldlist>=0) {
418                                 push (@subfieldlist, $subfield);
419                             } else {
420                                 @subfieldlist=($subfields{$subfieldcode}, $subfield);
421                             }
422                             $subfields{$subfieldcode}=\@subfieldlist;
423                         } else {
424                             $subfields{$subfieldcode}=$subfield;
425                         }
426                     }
427                     $marctext.="</table></td></tr>\n";
428                     $field{'subfields'}=\%subfields;
429                 }
430                 push (@record, \%field);
431             }
432             $marctext.="</table>\n";
433             $marctext{\@record}=$marctext;
434             $marc{\@record}=$record;
435             push (@records, \@record);
436             $counter++;
437         }
438 RECORD:
439         foreach $record (@records) {
440             my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $additionalauthors, $illustrator, $copyrightdate, $seriestitle);
441             my $marctext=$marctext{$record};
442             my $marc=$marc{$record};
443             foreach $field (@$record) {
444                 if ($field->{'tag'} eq '001') {
445                     $controlnumber=$field->{'indicator'};
446                 }
447                 if ($field->{'tag'} eq '010') {
448                     $lccn=$field->{'subfields'}->{'a'};
449                     $lccn=~s/^\s*//;
450                     ($lccn) = (split(/\s+/, $lccn))[0];
451                 }
452                 if ($field->{'tag'} eq '015') {
453                     $lccn=$field->{'subfields'}->{'a'};
454                     $lccn=~s/^\s*//;
455                     $lccn=~s/^C//;
456                     ($lccn) = (split(/\s+/, $lccn))[0];
457                 }
458                 if ($field->{'tag'} eq '020') {
459                     $isbn=$field->{'subfields'}->{'a'};
460                     ($isbn=~/^ARRAY/) && ($isbn=$$isbn[0]);
461                     $isbn=~s/[^\d]*//g;
462                 }
463                 if ($field->{'tag'} eq '022') {
464                     $issn=$field->{'subfields'}->{'a'};
465                     $issn=~s/^\s*//;
466                     ($issn) = (split(/\s+/, $issn))[0];
467                 }
468                 if ($field->{'tag'} eq '082') {
469                     $dewey=$field->{'subfields'}->{'a'};
470                     $dewey=~s/\///g;
471                     if (@$dewey) {
472                         $dewey=$$dewey[0];
473                     }
474                     #$dewey=~s/\///g;
475                 }
476                 if ($field->{'tag'} eq '100') {
477                     $author=$field->{'subfields'}->{'a'};
478                 }
479                 if ($field->{'tag'} eq '245') {
480                     $title=$field->{'subfields'}->{'a'};
481                     $title=~s/ \/$//;
482                     $subtitle=$field->{'subfields'}->{'b'};
483                     $subtitle=~s/ \/$//;
484                 }
485                 if ($field->{'tag'} eq '260') {
486                     $place=$field->{'subfields'}->{'a'};
487                     if (@$place) {
488                         $place=$$place[0];
489                     }
490                     $place=~s/\s*:$//g;
491                     $publisher=$field->{'subfields'}->{'b'};
492                     if (@$publisher) {
493                         $publisher=$$publisher[0];
494                     }
495                     $publisher=~s/\s*:$//g;
496                     $publicationyear=$field->{'subfields'}->{'c'};
497                     if ($publicationyear=~/c(\d\d\d\d)/) {
498                         $copyrightdate=$1;
499                     }
500                     if ($publicationyear=~/[^c](\d\d\d\d)/) {
501                         $publicationyear=$1;
502                     } elsif ($copyrightdate) {
503                         $publicationyear=$copyrightdate;
504                     } else {
505                         $publicationyear=~/(\d\d\d\d)/;
506                         $publicationyear=$1;
507                     }
508                 }
509                 if ($field->{'tag'} eq '300') {
510                     $pages=$field->{'subfields'}->{'a'};
511                     $pages=~s/ \;$//;
512                     $size=$field->{'subfields'}->{'c'};
513                     $pages=~s/\s*:$//g;
514                     $size=~s/\s*:$//g;
515                 }
516                 if ($field->{'tag'} eq '362') {
517                     if ($field->{'subfields'}->{'a'}=~/(\d+).*(\d+)/) {
518                         $volume=$1;
519                         $number=$2;
520                     }
521                 }
522                 if ($field->{'tag'} eq '440') {
523                     $seriestitle=$field->{'subfields'}->{'a'};
524                     if ($field->{'subfields'}->{'v'}=~/(\d+).*(\d+)/) {
525                         $volume=$1;
526                         $number=$2;
527                     }
528                 }
529                 if ($field->{'tag'} eq '700') {
530                     my $name=$field->{'subfields'}->{'a'};
531                     if ($field->{'subfields'}->{'c'}=~/ill/) {
532                         $additionalauthors.="$name\n";
533                     } else {
534                         $illustrator=$name;
535                     }
536                 }
537                 if ($field->{'tag'} =~/^5/) {
538                     $note.="$field->{'subfields'}->{'a'}\n";
539                 }
540                 if ($field->{'tag'} =~/65\d/) {
541                     my $subject=$field->{'subfields'}->{'a'};
542                     $subject=~s/\.$//;
543                     if ($gensubdivision=$field->{'subfields'}->{'x'}) {
544                         my @sub=@$gensubdivision;
545                         if ($#sub>=0) {
546                             foreach $s (@sub) {
547                                 $s=~s/\.$//;
548                                 $subject.=" -- $s";
549                             }
550                         } else {
551                             $gensubdivision=~s/\.$//;
552                             $subject.=" -- $gensubdivision";
553                         }
554                     }
555                     if ($chronsubdivision=$field->{'subfields'}->{'y'}) {
556                         my @sub=@$chronsubdivision;
557                         if ($#sub>=0) {
558                             foreach $s (@sub) {
559                                 $s=~s/\.$//;
560                                 $subject.=" -- $s";
561                             }
562                         } else {
563                             $chronsubdivision=~s/\.$//;
564                             $subject.=" -- $chronsubdivision";
565                         }
566                     }
567                     if ($geosubdivision=$field->{'subfields'}->{'z'}) {
568                         my @sub=@$geosubdivision;
569                         if ($#sub>=0) {
570                             foreach $s (@sub) {
571                                 $s=~s/\.$//;
572                                 $subject.=" -- $s";
573                             }
574                         } else {
575                             $geosubdivision=~s/\.$//;
576                             $subject.=" -- $geosubdivision";
577                         }
578                     }
579                     push @subjects, $subject;
580                 }
581             }
582             $titleinput=$input->textfield(-name=>'title', -default=>$title, -size=>40);
583             $marcinput=$input->hidden(-name=>'marc', -default=>$marc);
584             $subtitleinput=$input->textfield(-name=>'subtitle', -default=>$subtitle, -size=>40);
585             $authorinput=$input->textfield(-name=>'author', -default=>$author);
586             $illustratorinput=$input->textfield(-name=>'illustrator', -default=>$illustrator);
587             $additionalauthorsinput=$input->textarea(-name=>'additionalauthors', -default=>$additionalauthors, -rows=>4, -cols=>20);
588             my $subject='';
589             foreach (@subjects) {
590                 $subject.="$_\n";
591             }
592             $subjectinput=$input->textarea(-name=>'subject', -default=>$subject, -rows=>4, -cols=>40);
593             $noteinput=$input->textarea(-name=>'note', -default=>$note, -rows=>4, -cols=>40, -wrap=>'physical');
594             $copyrightinput=$input->textfield(-name=>'copyrightdate', -default=>$copyrightdate);
595             $seriestitleinput=$input->textfield(-name=>'seriestitle', -default=>$seriestitle);
596             $volumeinput=$input->textfield(-name=>'volume', -default=>$volume);
597             $volumedateinput=$input->textfield(-name=>'volumedate', -default=>$volumedate);
598             $volumeddescinput=$input->textfield(-name=>'volumeddesc', -default=>$volumeddesc);
599             $numberinput=$input->textfield(-name=>'number', -default=>$number);
600             $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn);
601             $issninput=$input->textfield(-name=>'issn', -default=>$issn);
602             $lccninput=$input->textfield(-name=>'lccn', -default=>$lccn);
603             $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn);
604             $deweyinput=$input->textfield(-name=>'dewey', -default=>$dewey);
605             $cleanauthor=$author;
606             $cleanauthor=~s/[^A-Za-z]//g;
607             $subclassinput=$input->textfield(-name=>'subclass', -default=>uc(substr($cleanauthor,0,3)));
608             $publisherinput=$input->textfield(-name=>'publishercode', -default=>$publisher);
609             $pubyearinput=$input->textfield(-name=>'publicationyear', -default=>$publicationyear);
610             $placeinput=$input->textfield(-name=>'place', -default=>$place);
611             $pagesinput=$input->textfield(-name=>'pages', -default=>$pages);
612             $sizeinput=$input->textfield(-name=>'size', -default=>$size);
613             $fileinput=$input->hidden(-name=>'file', -default=>$file);
614             $origisbn=$input->hidden(-name=>'origisbn', -default=>$isbn);
615             $origissn=$input->hidden(-name=>'origissn', -default=>$issn);
616             $origlccn=$input->hidden(-name=>'origlccn', -default=>$lccn);
617             $origcontrolnumber=$input->hidden(-name=>'origcontrolnumber', -default=>$controlnumber);
618
619             my $itemtypeselect='';
620             $sth=$dbh->prepare("select itemtype,description from itemtypes");
621             $sth->execute;
622             while (my ($itemtype, $description) = $sth->fetchrow) {
623                 $itemtypeselect.="<option value=$itemtype>$itemtype - $description\n";
624             }
625             ($qissn) || ($qissn='NIL');
626             ($qlccn) || ($qlccn='NIL');
627             ($qisbn) || ($qisbn='NIL');
628             ($qcontrolnumber) || ($qcontrolnumber='NIL');
629             $controlnumber=~s/\s+//g;
630             unless (($isbn eq $qisbn) || ($issn eq $qissn) || ($lccn eq $qlccn) || ($controlnumber eq $qcontrolnumber)) {
631                 next RECORD;
632             }
633
634             print << "EOF";
635             <center>
636             <h1>New Record</h1>
637             Full MARC Record available at bottom
638             <form method=post>
639             <table border=1>
640             <tr><td>Title</td><td>$titleinput</td></tr>
641             <tr><td>Subtitle</td><td>$subtitleinput</td></tr>
642             <tr><td>Author</td><td>$authorinput</td></tr>
643             <tr><td>Additional Authors</td><td>$additionalauthorsinput</td></tr>
644             <tr><td>Illustrator</td><td>$illustratorinput</td></tr>
645             <tr><td>Copyright</td><td>$copyrightinput</td></tr>
646             <tr><td>Series Title</td><td>$seriestitleinput</td></tr>
647             <tr><td>Volume</td><td>$volumeinput</td></tr>
648             <tr><td>Number</td><td>$numberinput</td></tr>
649             <tr><td>Volume Date</td><td>$volumedateinput</td></tr>
650             <tr><td>Volume Description</td><td>$volumeddescinput</td></tr>
651             <tr><td>Subject</td><td>$subjectinput</td></tr>
652             <tr><td>Notes</td><td>$noteinput</td></tr>
653             <tr><td>Item Type</td><td><select name=itemtype>$itemtypeselect</select></td></tr>
654             <tr><td>ISBN</td><td>$isbninput</td></tr>
655             <tr><td>ISSN</td><td>$issninput</td></tr>
656             <tr><td>LCCN</td><td>$lccninput</td></tr>
657             <tr><td>Dewey</td><td>$deweyinput</td></tr>
658             <tr><td>Subclass</td><td>$subclassinput</td></tr>
659             <tr><td>Publication Year</td><td>$pubyearinput</td></tr>
660             <tr><td>Publisher</td><td>$publisherinput</td></tr>
661             <tr><td>Place</td><td>$placeinput</td></tr>
662             <tr><td>Pages</td><td>$pagesinput</td></tr>
663             <tr><td>Size</td><td>$sizeinput</td></tr>
664             </table>
665             <input type=submit>
666             <input type=hidden name=insertnewrecord value=1>
667             $fileinput
668             $marcinput
669             $origisbn
670             $origissn
671             $origlccn
672             $origcontrolnumber
673             </form>
674             $marctext
675 EOF
676         }
677     } else {
678         #open (F, "$file");
679         #my $data=<F>;
680         my $data;
681         my $name;
682         my $z3950=0;
683         if ($file=~/Z-(\d+)/) {
684             print << "EOF";
685 <center>
686 <p>
687 <a href=$ENV{'SCRIPT_NAME'}?menu=$menu>Select a New File</a>
688 <p>
689 <table border=0 cellpadding=10 cellspacing=0>
690 <tr><th bgcolor=black><font color=white>Select a Record to Import</font></th></tr>
691 <tr><td bgcolor=#dddddd>
692 EOF
693             my $id=$1;
694             my $sth=$dbh->prepare("select servers from z3950queue where id=$id");
695             $sth->execute;
696             my ($servers) = $sth->fetchrow;
697             my $serverstring;
698             foreach $serverstring (split(/\s+/, $servers)) {
699                 my ($name, $server, $database, $auth) = split(/\//, $serverstring, 4);
700                 if ($name eq 'MAN') {
701                     print "$server/$database<br>\n";
702                 } elsif ($name eq 'LOC') {
703                     print "Library of Congress<br>\n";
704                 } elsif ($name eq 'NLC') {
705                     print "National Library of Canada<br>\n";
706                 } else {
707                     my $sti=$dbh->prepare("select name from
708                     z3950servers where id=$name");
709                     $sti->execute;
710                     my ($longname)=$sti->fetchrow;
711                     print "$longname<br>\n";
712                 }
713                 print "<ul>\n";
714                 my $q_server=$dbh->quote($serverstring);
715                 my $sti=$dbh->prepare("select numrecords,id,results,startdate,enddate from z3950results where queryid=$id and server=$q_server");
716                 $sti->execute;
717                 ($numrecords,$resultsid,$data,$startdate,$enddate) = $sti->fetchrow;
718                 if ($sti->rows == 0) {
719                     print "pending...";
720                 } elsif ($enddate == 0) {
721                     my $now=time();
722                     my $elapsed=$now-$startdate;
723                     my $elapsedtime='';
724                     if ($elapsed>60) {
725                         $elapsedtime=sprintf "%d minutes",($elapsed/60);
726                     } else {
727                         $elapsedtime=sprintf "%d seconds",$elapsed;
728                     }
729                     print "<font color=red>processing... ($elapsedtime)</font>";
730                 } elsif ($numrecords) {
731                     my @records=parsemarcdata($data);
732                     foreach $record (@records) {
733                         my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $controlnumber);
734                         foreach $field (@$record) {
735                             if ($field->{'tag'} eq '001') {
736                                 $controlnumber=$field->{'indicator'};
737                             }
738                             if ($field->{'tag'} eq '010') {
739                                 $lccn=$field->{'subfields'}->{'a'};
740                                 $lccn=~s/^\s*//;
741                                 ($lccn) = (split(/\s+/, $lccn))[0];
742                             }
743                             if ($field->{'tag'} eq '015') {
744                                 $lccn=$field->{'subfields'}->{'a'};
745                                 $lccn=~s/^\s*//;
746                                 $lccn=~s/^C//;
747                                 ($lccn) = (split(/\s+/, $lccn))[0];
748                             }
749                             if ($field->{'tag'} eq '020') {
750                                 $isbn=$field->{'subfields'}->{'a'};
751                                 ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]);
752                                 $isbn=~s/[^\d]*//g;
753                             }
754                             if ($field->{'tag'} eq '022') {
755                                 $issn=$field->{'subfields'}->{'a'};
756                                 $issn=~s/^\s*//;
757                                 ($issn) = (split(/\s+/, $issn))[0];
758                             }
759                             if ($field->{'tag'} eq '100') {
760                                 $author=$field->{'subfields'}->{'a'};
761                             }
762                             if ($field->{'tag'} eq '245') {
763                                 $title=$field->{'subfields'}->{'a'};
764                                 $title=~s/ \/$//;
765                                 $subtitle=$field->{'subfields'}->{'b'};
766                                 $subtitle=~s/ \/$//;
767                             }
768                         }
769                         my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
770                         my $q_issn=$dbh->quote((($issn) || ('NIL')));
771                         my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
772                         my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL')));
773                         my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber");
774                         $sth->execute;
775                         my $donetext='';
776                         if ($sth->rows) {
777                             $donetext="DONE";
778                         }
779                         $sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
780                         $sth->execute;
781                         if ($sth->rows) {
782                             $donetext="DONE";
783                         }
784                         ($author) && ($author="by $author");
785                         if ($isbn) {
786                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&isbn=$isbn>$title$subtitle $author</a> $donetext<br>\n";
787                         } elsif ($lccn) {
788                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&lccn=$lccn>$title$subtitle $author</a> $donetext<br>\n";
789                         } elsif ($issn) {
790                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&issn=$issn>$title$subtitle $author</a><br> $donetext\n";
791                         } elsif ($controlnumber) {
792                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&controlnumber=$controlnumber>$title $author</a><br> $donetext\n";
793                         } else {
794                             print "Error: Contact steve regarding $title by $author<br>\n";
795                         }
796                     }
797                     print "<p>\n";
798                 } else {
799                     print "No records returned.<p>\n";
800                 }
801                 print "</ul>\n";
802             }
803         } else {
804             my $sth=$dbh->prepare("select marc,name from uploadedmarc where id=$file");
805             $sth->execute;
806             ($data, $name) = $sth->fetchrow;
807             print << "EOF";
808 <center>
809 <p>
810 <a href=$ENV{'SCRIPT_NAME'}?menu=$menu>Select a New File</a>
811 <p>
812 <table border=0 cellpadding=10 cellspacing=0>
813 <tr><th bgcolor=black><font color=white>Select a Record to Import<br>from $name</font></th></tr>
814 <tr><td bgcolor=#dddddd>
815 EOF
816             
817             my @records=parsemarcdata($data);
818             foreach $record (@records) {
819                 my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $controlnumber);
820                 foreach $field (@$record) {
821                     if ($field->{'tag'} eq '001') {
822                         $controlnumber=$field->{'indicator'};
823                     }
824                     if ($field->{'tag'} eq '010') {
825                         $lccn=$field->{'subfields'}->{'a'};
826                         $lccn=~s/^\s*//;
827                         ($lccn) = (split(/\s+/, $lccn))[0];
828                     }
829                     if ($field->{'tag'} eq '015') {
830                         $lccn=$field->{'subfields'}->{'a'};
831                         $lccn=~s/^\s*//;
832                         $lccn=~s/^C//;
833                         ($lccn) = (split(/\s+/, $lccn))[0];
834                     }
835                     if ($field->{'tag'} eq '020') {
836                         $isbn=$field->{'subfields'}->{'a'};
837                         ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]);
838                         $isbn=~s/[^\d]*//g;
839                     }
840                     if ($field->{'tag'} eq '022') {
841                         $issn=$field->{'subfields'}->{'a'};
842                         $issn=~s/^\s*//;
843                         ($issn) = (split(/\s+/, $issn))[0];
844                     }
845                     if ($field->{'tag'} eq '100') {
846                         $author=$field->{'subfields'}->{'a'};
847                     }
848                     if ($field->{'tag'} eq '245') {
849                         $title=$field->{'subfields'}->{'a'};
850                         $title=~s/ \/$//;
851                         $subtitle=$field->{'subfields'}->{'b'};
852                         $subtitle=~s/ \/$//;
853                     }
854                 }
855                 my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
856                 my $q_issn=$dbh->quote((($issn) || ('NIL')));
857                 my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
858                 my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL')));
859                 my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber");
860                 $sth->execute;
861                 my $donetext='';
862                 if ($sth->rows) {
863                     $donetext="DONE";
864                 }
865                 $sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
866                 $sth->execute;
867                 if ($sth->rows) {
868                     $donetext="DONE";
869                 }
870                 ($author) && ($author="by $author");
871                 if ($isbn) {
872                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&isbn=$isbn>$title$subtitle $author</a> $donetext<br>\n";
873                 } elsif ($lccn) {
874                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&lccn=$lccn>$title$subtitle $author</a> $donetext<br>\n";
875                 } elsif ($issn) {
876                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&issn=$issn>$title$subtitle $author</a><br> $donetext\n";
877                 } elsif ($controlnumber) {
878                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&controlnumber=$controlnumber>$title by $author</a><br> $donetext\n";
879                 } else {
880                     print "Error: Contact steve regarding $title by $author<br>\n";
881                 }
882             }
883         }
884         print "</td></tr></table>\n";
885     }
886 } else {
887
888 SWITCH:
889     {
890         if ($menu eq 'z3950') { z3950(); last SWITCH; }
891         if ($menu eq 'uploadmarc') { uploadmarc(); last SWITCH; }
892         if ($menu eq 'manual') { manual(); last SWITCH; }
893         mainmenu();
894     }
895
896 }
897
898
899 sub z3950 {
900     $sth=$dbh->prepare("select id,term,type,done,numrecords,length(results),startdate,enddate,servers from z3950queue order by id desc limit 20");
901     $sth->execute;
902     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
903     print "<table border=0><tr><td valign=top>\n";
904     print "<h2>Results of Z39.50 searches</h2>\n";
905     print "<a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Refresh</a><br>\n<ul>\n";
906     while (my ($id, $term, $type, $done, $numrecords, $length, $startdate, $enddate, $servers) = $sth->fetchrow) {
907         $type=uc($type);
908         $term=~s/</&lt;/g;
909         $term=~s/>/&gt;/g;
910         if ($done == 1) {
911             my $elapsed=$enddate-$startdate;
912             my $elapsedtime='';
913             if ($elapsed>60) {
914                 $elapsedtime=sprintf "%d minutes",($elapsed/60);
915             } else {
916                 $elapsedtime=sprintf "%d seconds",$elapsed;
917             }
918             if ($numrecords) {
919                 print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1>Done. $numrecords records found in $elapsedtime.</font><br>\n";
920             } else {
921                 print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1>Done.  No records found.  Search took $elapsedtime.</font><br>\n";
922             }
923         } elsif ($done == -1) {
924             my $elapsed=time()-$startdate;
925             my $elapsedtime='';
926             if ($elapsed>60) {
927                 $elapsedtime=sprintf "%d minutes",($elapsed/60);
928             } else {
929                 $elapsedtime=sprintf "%d seconds",$elapsed;
930             }
931             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font color=red size=-1>Processing ($elapsedtime)</font><br>\n";
932         } else {
933             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> $done <font size=-1>Pending</font><br>\n";
934         }
935     }
936     print "</ul>\n";
937     print "</td><td valign=top width=30%>\n";
938     my $sth=$dbh->prepare("select id,name,checked from z3950servers order by rank");
939     $sth->execute;
940     my $serverlist='';
941     while (my ($id, $name, $checked) = $sth->fetchrow) {
942         ($checked) ? ($checked='checked') : ($checked='');
943         $serverlist.="<input type=checkbox name=S-$id $checked> $name<br>\n";
944     }
945     $serverlist.="<input type=checkbox name=S-MAN> <input name=manualz3950server size=25 value=otherserver:210/DATABASE>\n";
946     
947 print << "EOF";
948     <form action=$ENV{'SCRIPT_NAME'} method=GET>
949     <input type=hidden name=z3950queue value=1>
950     <input type=hidden name=menu value=$menu>
951     <p>
952     <input type=hidden name=test value=testvalue>
953     <table border=1 bgcolor=#dddddd><tr><th bgcolor=#bbbbbb colspan=2>Search for MARC records</th></tr>
954     <tr><td>Query Term</td><td><input name=query></td></tr>
955     <tr><td colspan=2 align=center><input type=radio name=type value=isbn checked> ISBN <input type=radio name=type value=lccn> LCCN <input type=radio name=type value=title> Title</td></tr>
956     <tr><td colspan=2>
957     $serverlist
958     </td></tr>
959     <tr><td colspan=2 align=center>
960     <input type=submit>
961     </td></tr>
962     </table>
963
964     </form>
965 EOF
966 print "</td></tr></table>\n";
967 }
968
969 sub uploadmarc {
970     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
971     my $sth=$dbh->prepare("select id,name from uploadedmarc");
972     $sth->execute;
973     print "<h2>Select a set of MARC records</h2>\n<ul>";
974     while (my ($id, $name) = $sth->fetchrow) {
975         print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$id&menu=$menu>$name</a><br>\n";
976     }
977     print "</ul>\n";
978     print "<p>\n";
979     print "<table border=1 bgcolor=#dddddd><tr><th bgcolor=#bbbbbb
980     colspan=2>Upload a set of MARC records</th></tr>\n";
981     print "<tr><td>Upload a set of MARC records:</td><td>";
982     print $input->start_multipart_form();
983     print $input->filefield('uploadmarc');
984     print << "EOF";
985     </td></tr>
986     <tr><td>
987     <input type=hidden name=menu value=$menu>
988     Name this set of MARC records:</td><td><input type=text
989     name=name></td></tr>
990     <tr><td colspan=2 align=center>
991     <input type=submit>
992     </td></tr>
993     </table>
994     </form>
995 EOF
996 }
997
998 sub manual {
999 }
1000
1001
1002 sub mainmenu {
1003     print << "EOF";
1004 <h1>Main Menu</h1>
1005 <ul>
1006 <li><a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Z39.50 Search</a>
1007 <li><a href=$ENV{'SCRIPT_NAME'}?menu=uploadmarc>Upload MARC Records</a>
1008 </ul>
1009 EOF
1010 }
1011
1012 sub skip {
1013
1014     #opendir(D, "/home/$userid/");
1015     #my @dirlist=readdir D;
1016     #foreach $file (@dirlist) {
1017 #       (next) if ($file=~/^\./);
1018 #       (next) if ($file=~/^nsmail$/);
1019 #       (next) if ($file=~/^public_html$/);
1020 #       ($file=~/\.mrc/) || ($filelist.="$file<br>\n");
1021 #       (next) unless ($file=~/\.mrc$/);
1022 #       $file=~s/ /\%20/g;
1023 #       print "<a href=$ENV{'SCRIPT_NAME'}?file=/home/$userid/$file>$file</a><br>\n";
1024 #    }
1025
1026
1027     #<form action=$ENV{'SCRIPT_NAME'} method=POST enctype=multipart/form-data>
1028
1029 }
1030 print endmenu();
1031 print endpage();
1032
1033 sub parsemarcdata {
1034     my $data=shift;
1035     my $splitchar=chr(29);
1036     my @records;
1037     my $record;
1038     foreach $record (split(/$splitchar/, $data)) {
1039         my $leader=substr($record,0,24);
1040         #print "<tr><td>Leader:</td><td>$leader</td></tr>\n";
1041         $record=substr($record,24);
1042         my $splitchar2=chr(30);
1043         my $directory=0;
1044         my $tagcounter=0;
1045         my %tag;
1046         my @record;
1047         my $field;
1048         foreach $field (split(/$splitchar2/, $record)) {
1049             my %field;
1050             ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
1051             unless ($directory) {
1052                 $directory=$field;
1053                 my $itemcounter=1;
1054                 $counter=0;
1055                 while ($item=substr($directory,0,12)) {
1056                     $tag=substr($directory,0,3);
1057                     $length=substr($directory,3,4);
1058                     $start=substr($directory,7,6);
1059                     $directory=substr($directory,12);
1060                     $tag{$counter}=$tag;
1061                     $counter++;
1062                 }
1063                 $directory=1;
1064                 next;
1065             }
1066             $tag=$tag{$tagcounter};
1067             $tagcounter++;
1068             $field{'tag'}=$tag;
1069             $splitchar3=chr(31);
1070             my @subfields=split(/$splitchar3/, $field);
1071             $indicator=$subfields[0];
1072             $field{'indicator'}=$indicator;
1073             my $firstline=1;
1074             unless ($#subfields==0) {
1075                 my %subfields;
1076                 for ($i=1; $i<=$#subfields; $i++) {
1077                     my $text=$subfields[$i];
1078                     my $subfieldcode=substr($text,0,1);
1079                     my $subfield=substr($text,1);
1080                     if ($subfields{$subfieldcode}) {
1081                         my $subfieldlist=$subfields{$subfieldcode};
1082                         my @subfieldlist=@$subfieldlist;
1083                         if ($#subfieldlist>=0) {
1084 #                       print "$tag Adding to array $subfieldcode -- $subfield<br>\n";
1085                             push (@subfieldlist, $subfield);
1086                         } else {
1087 #                       print "$tag Arraying $subfieldcode -- $subfield<br>\n";
1088                             @subfieldlist=($subfields{$subfieldcode}, $subfield);
1089                         }
1090                         $subfields{$subfieldcode}=\@subfieldlist;
1091                     } else {
1092                         $subfields{$subfieldcode}=$subfield;
1093                     }
1094                 }
1095                 $field{'subfields'}=\%subfields;
1096             }
1097             push (@record, \%field);
1098         }
1099         push (@records, \@record);
1100         $counter++;
1101     }
1102     return @records;
1103 }