From ac230f070e785770f5c36681c4871ac0dedc1871 Mon Sep 17 00:00:00 2001 From: tipaul Date: Thu, 20 Jun 2002 15:56:51 +0000 Subject: [PATCH] trying to merge rel-1-2 into main branch... test with this script, choosen randomly ;-) --- acqui.simple/marcimport.pl | 2226 ++++++++++++++++++++---------------- 1 file changed, 1259 insertions(+), 967 deletions(-) diff --git a/acqui.simple/marcimport.pl b/acqui.simple/marcimport.pl index 2b4d2e1d8e..f94397505c 100755 --- a/acqui.simple/marcimport.pl +++ b/acqui.simple/marcimport.pl @@ -6,7 +6,7 @@ # Koha library project www.koha.org # Licensed under the GPL - +use strict; #use strict; # standard or CPAN modules used @@ -18,6 +18,9 @@ use C4::Database; use C4::Acquisitions; use C4::Output; +use C4::Input; +use C4::Biblio; + #------------------ # Constants @@ -83,6 +86,32 @@ my %tagtext = ( '856' => 'Electronic location and access', ); +# tag, subfield, field name, repeats, striptrailingchars +my %tagmap=( + '010'=>{'a'=>{name=> 'lccn', rpt=>0 }}, + '015'=>{'a'=>{name=> 'lccn', rpt=>0 }}, + '020'=>{'a'=>{name=> 'isbn', rpt=>0 }}, + '022'=>{'a'=>{name=> 'issn', rpt=>0 }}, + '082'=>{'a'=>{name=> 'dewey', rpt=>0 }}, + '100'=>{'a'=>{name=> 'author', rpt=>0, striptrail=>',:;/-' }}, + '245'=>{'a'=>{name=> 'title', rpt=>0, striptrail=>',:;/' }, + 'b'=>{name=> 'subtitle', rpt=>0, striptrail=>',:;/' }}, + '260'=>{'a'=>{name=> 'place', rpt=>0, striptrail=>',:;/-' }, + 'b'=>{name=> 'publisher', rpt=>0, striptrail=>',:;/-' }, + 'c'=>{name=> 'year' , rpt=>0, striptrail=>'.,:;/-' }}, + '300'=>{'a'=>{name=> 'pages', rpt=>0, striptrail=>',:;/-' }, + 'c'=>{name=> 'size', rpt=>0, striptrail=>',:;/-' }}, + '362'=>{'a'=>{name=> 'volume-number', rpt=>0 }}, + '440'=>{'a'=>{name=> 'seriestitle', rpt=>0, striptrail=>',:;/' }, + 'v'=>{name=> 'volume-number',rpt=>0 }}, + '490'=>{'a'=>{name=> 'seriestitle', rpt=>0, striptrail=>',:;/' }, + 'v'=>{name=> 'volume-number',rpt=>0 }}, + '700'=>{'a'=>{name=> 'addtional-author-illus',rpt=>1, striptrail=>',:;/' }}, + '5xx'=>{'a'=>{name=> 'notes', rpt=>1 }}, + '65x'=>{'a'=>{name=> 'subject', rpt=>1, striptrail=>'.,:;/-' }}, +); + +#------------- #------------- # Initialize @@ -96,840 +125,355 @@ my $dbh=C4Connect; print $input->header; print startpage(); print startmenu('acquisitions'); - #------------- # Process input parameters my $file=$input->param('file'); +my $menu = $input->param('menu'); if ($input->param('z3950queue')) { - my $query=$input->param('query'); - - my @serverlist; + AcceptZ3950Queue($dbh,$input); +} - my $isbngood=1; - if ($input->param('type') eq 'isbn') { - $isbngood=CheckIsbn($query); - } - if ($isbngood) { - foreach ($input->param) { - if (/S-(.*)/) { - my $server=$1; - if ($server eq 'MAN') { - push @serverlist, "MAN/".$input->param('manualz3950server')."//" -; - } else { - push @serverlist, $server; - } - } - } +if ($input->param('uploadmarc')) { + AcceptMarcUpload($dbh,$input) +} - Addz3950queue($input->param('query'), $input->param('type'), - $input->param('rand'), @serverlist); - } else { - print "$query is not a valid ISBN - Number

\n"; +if ($input->param('insertnewrecord')) { + # Add biblio item, and set up menu for adding item copies + my ($biblionumber,$biblioitemnumber)=AcceptBiblioitem($dbh,$input); + ItemCopyForm($dbh,$input,$biblionumber,$biblioitemnumber); + print endmenu(); + print endpage(); + exit; +} + + +if ($input->param('newitem')) { + # Add item copy + &AcceptItemCopy($dbh,$input); +} # if newitem + +if ($file) { + ProcessFile($dbh,$input); +} else { +SWITCH: + { + if ($menu eq 'z3950') { z3950menu($dbh,$input); last SWITCH; } + if ($menu eq 'uploadmarc') { uploadmarc(); last SWITCH; } + if ($menu eq 'manual') { manual(); last SWITCH; } + mainmenu(); } + } +print endmenu(); +print endpage(); + -sub Addz3950queue { +sub ProcessFile { + # A MARC file has been specified; process it for review form use strict; + + # Input params my ( - $query, # value to look up - $type, # type of value ("isbn", "lccn", etc). - $requestid, - @z3950list, # list of z3950 servers to query + $dbh, + $input, )=@_; + # local vars my ( - @serverlist, - $server, - $failed, + $sth, + $record, ); - # list of servers: entry can be a fully qualified URL-type entry - # or simply just a server ID number. + my $debug=0; + my $splitchar=chr(29); - my $sth=$dbh->prepare("select host,port,db,userid,password - from z3950servers - where id=? "); - foreach $server (@z3950list) { - if ($server =~ /:/ ) { - push @serverlist, $server; - } else { - $sth->execute($server); - my ($host, $port, $db, $userid, $password) = $sth->fetchrow; - push @serverlist, "$server/$host\:$port/$db/$userid/$password"; - } - } + requireDBI($dbh,"ProcessFile"); - my $serverlist=''; - foreach (@serverlist) { - $serverlist.="$_ "; - } - chop $serverlist; + print "Main Menu


\n"; + my $qisbn=$input->param('isbn'); + my $qissn=$input->param('issn'); + my $qlccn=$input->param('lccn'); + my $qcontrolnumber=$input->param('controlnumber'); - # Don't allow reinsertion of the same request number. - my $sth=$dbh->prepare("select identifier from z3950queue - where identifier=?"); - $sth->execute($requestid); - unless ($sth->rows) { - $sth=$dbh->prepare("insert into z3950queue - (term,type,servers, identifier) - values (?, ?, ?, ?)"); - $sth->execute($query, $type, $serverlist, $requestid); - } -} # sub + # See if a particular result item was specified + if ($qisbn || $qissn || $qlccn || $qcontrolnumber) { + print "New File
\n"; + #open (F, "$file"); + #my $data=; + my $data; -#-------------------------------------- -sub CheckIsbn { - my ($q)=@_ ; - - my $isbngood = 0; - - $q=~s/[^X\d]//g; - $q=~s/X.//g; - if (length($q)==10) { - my $checksum=substr($q,9,1); - my $isbn=substr($q,0,9); - my $i; - my $c=0; - for ($i=0; $i<9; $i++) { - my $digit=substr($q,$i,1); - $c+=$digit*(10-$i); - } - $c=int(11-($c/11-int($c/11))*11+.1); - ($c==10) && ($c='X'); - if ($c eq $checksum) { - $isbngood=1; - } else { - $isbngood=0; - } + if ($file=~/Z-(\d+)/) { + my $id=$1; + my $resultsid=$input->param('resultsid'); + my $sth=$dbh->prepare("select results from z3950results where id=$resultsid"); + $sth->execute; + ($data) = $sth->fetchrow; } else { - $isbngood=0; - } - - return $isbngood; - -} # sub CheckIsbn - - - -if (my $data=$input->param('uploadmarc')) { - my $name=$input->param('name'); - ($name) || ($name=$data); - my $marcrecord=''; - if (length($data)>0) { - while (<$data>) { - $marcrecord.=$_; + my $sth=$dbh->prepare("select marc from uploadedmarc where id=$file"); + $sth->execute; + ($data) = $sth->fetchrow; } - } - my $q_marcrecord=$dbh->quote($marcrecord); - my $q_name=$dbh->quote($name); - my $sth=$dbh->prepare("insert into uploadedmarc (marc,name) values ($q_marcrecord, $q_name)"); - $sth->execute; -} + my @records; -if ($input->param('insertnewrecord')) { - my $isbn=$input->param('isbn'); - my $issn=$input->param('issn'); - my $lccn=$input->param('lccn'); - my $q_origisbn=$dbh->quote($input->param('origisbn')); - my $q_origissn=$dbh->quote($input->param('origissn')); - my $q_origlccn=$dbh->quote($input->param('origlccn')); - my $q_origcontrolnumber=$dbh->quote($input->param('origcontrolnumber')); - my $q_isbn=$dbh->quote((($isbn) || ('NIL'))); - my $q_issn=$dbh->quote((($issn) || ('NIL'))); - my $q_lccn=$dbh->quote((($lccn) || ('NIL'))); - my $sth=$dbh->prepare("insert into marcrecorddone values ($q_origisbn, $q_origissn, $q_origlccn, $q_origcontrolnumber)"); - $sth->execute; - my $sth=$dbh->prepare("select biblionumber,biblioitemnumber from biblioitems where issn=$q_issn or isbn=$q_isbn or lccn=$q_lccn"); - $sth->execute; - my $biblionumber=0; - my $biblioitemnumber=0; - print "
\n"; - print "New Record | New File
\n"; - if ($sth->rows) { - ($biblionumber, $biblioitemnumber) = $sth->fetchrow; - my $title=$input->param('title'); - print << "EOF"; - - - -
Record already in database
$title is already in the database with biblionumber $biblionumber and biblioitemnumber $biblioitemnumber
-

-EOF - } else { - use strict; - my $error; - my %biblio; - my %biblioitem; - - # convert to upper case and split on lines - my $subjectheadings=$input->param('subject'); - my @subjectheadings=split(/[\r\n]+/,$subjectheadings); - - my $additionalauthors=$input->param('additionalauthors'); - my @additionalauthors=split(/[\r\n]+/,uc($additionalauthors)); - - # Use individual assignments to hash buckets, in case - # any of the input parameters are empty or don't exist - $biblio{title} =$input->param('title'); - $biblio{author} =$input->param('author'); - $biblio{copyright} =$input->param('copyrightdate'); - $biblio{seriestitle} =$input->param('seriestitle'); - $biblio{notes} =$input->param('notes'); - $biblio{abstract} =$input->param('abstract'); - $biblio{subtitle} =$input->param('subtitle'); - - $biblioitem{volume} =$input->param('volume'); - $biblioitem{number} =$input->param('number'); - $biblioitem{itemtype} =$input->param('itemtype'); - $biblioitem{isbn} =$input->param('isbn'); - $biblioitem{issn} =$input->param('issn'); - $biblioitem{dewey} =$input->param('dewey'); - $biblioitem{subclass} =$input->param('subclass'); - $biblioitem{publicationyear} =$input->param('publicationyear'); - $biblioitem{publishercode} =$input->param('publishercode'); - $biblioitem{volumedate} =$input->param('volumedate'); - $biblioitem{volumeddesc} =$input->param('volumeddesc'); - $biblioitem{illus} =$input->param('illustrator'); - $biblioitem{pages} =$input->param('pages'); - $biblioitem{notes} =$input->param('notes'); - $biblioitem{size} =$input->param('size'); - $biblioitem{place} =$input->param('place'); - $biblioitem{lccn} =$input->param('lccn'); - $biblioitem{marc} =$input->param('marc'); +RECORD: + foreach $record (split(/$splitchar/, $data)) { - print "

subjects=@subjectheadings
\n"; - print "
auth=@additionalauthors
\n"; + my ( + $bib, # hash ref to named fields + $fieldlist, # list ref + $lccn, $isbn, $issn, $dewey, + $publisher, $publicationyear, $volume, + $number, @subjects, $notes, $additionalauthors, + $copyrightdate, $seriestitle, + $origisbn, $origissn, $origlccn, $origcontrolnumber, + $subtitle, + $controlnumber, + $cleanauthor, + $subject, + $volumedate, + $volumeddesc, + $itemtypeselect, + ); + my ($lccninput, $isbninput, $issninput, $deweyinput, $authorinput, $titleinput, + $placeinput, $publisherinput, $publicationyearinput, $volumeinput, + $numberinput, $notesinput, $additionalauthorsinput, + $illustratorinput, $copyrightdateinput, $seriestitleinput, + $subtitleinput, + $copyrightinput, + $volumedateinput, + $volumeddescinput, + $subjectinput, + $noteinput, + $subclassinput, + $pubyearinput, + $pagesinput, + $sizeinput, + $marcinput, + $fileinput, + ); + + + my $marctext; + + my $marc=$record; + + ($fieldlist)=parsemarcfileformat($record ); + + $bib=extractmarcfields($fieldlist ); + + print "Title=$bib->{title}\n" if $debug; + + $marctext=FormatMarcText($fieldlist); + + $controlnumber =$bib->{controlnumber}; + $lccn =$bib->{lccn}; + $isbn =$bib->{isbn}; + $issn =$bib->{issn}; + $publisher =$bib->{publisher}; + $publicationyear =$bib->{publicationyear}; + $copyrightdate =$bib->{copyrightdate}; - ($biblionumber, $biblioitemnumber, $error)= - NewBiblioItem($dbh, - \%biblio, - \%biblioitem, - \@subjectheadings, - \@additionalauthors - ); + $volume =$bib->{volume}; + $number =$bib->{number}; + $seriestitle =$bib->{seriestitle}; + $additionalauthors =$bib->{additionalauthors}; + $notes =$bib->{notes}; + $titleinput=$input->textfield(-name=>'title', -default=>$bib->{title}, -size=>40); + $marcinput=$input->hidden(-name=>'marc', -default=>$marc); + $subtitleinput=$input->textfield(-name=>'subtitle', -default=>$bib->{subtitle}, -size=>40); + $authorinput=$input->textfield(-name=>'author', -default=>$bib->{author}); + $illustratorinput=$input->textfield(-name=>'illustrator', + -default=>$bib->{illustrator}); + $additionalauthorsinput=$input->textarea(-name=>'additionalauthors', -default=>$additionalauthors, -rows=>4, -cols=>20); - my $title=$input->param('title'); - print << "EOF"; - - - -
Record entered into database
$title has been entered into the database with biblionumber - $biblionumber and biblioitemnumber $biblioitemnumber
-EOF - } # if new record + my $subject=''; + foreach ( @{$bib->{subject} } ) { + $subject.="$_\n"; + print "
form subject=$subject
\n" if $debug; + } + $subjectinput=$input->textarea(-name=>'subject', + -default=>$subject, -rows=>4, -cols=>40); - my $title=$input->param('title'); + $noteinput=$input->textarea(-name=>'notes', + -default=>$notes, -rows=>4, -cols=>40, -wrap=>'physical'); + $copyrightinput=$input->textfield(-name=>'copyrightdate', -default=>$copyrightdate); + $seriestitleinput=$input->textfield(-name=>'seriestitle', -default=>$seriestitle); + $volumeinput=$input->textfield(-name=>'volume', -default=>$volume); + $volumedateinput=$input->textfield(-name=>'volumedate', -default=>$volumedate); + $volumeddescinput=$input->textfield(-name=>'volumeddesc', -default=>$volumeddesc); + $numberinput=$input->textfield(-name=>'number', -default=>$number); + $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn); + $issninput=$input->textfield(-name=>'issn', -default=>$issn); + $lccninput=$input->textfield(-name=>'lccn', -default=>$lccn); + $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn); + $deweyinput=$input->textfield(-name=>'dewey', -default=>$bib->{dewey}); + $cleanauthor=$bib->{author}; + $cleanauthor=~s/[^A-Za-z]//g; + $subclassinput=$input->textfield(-name=>'subclass', -default=>uc(substr($cleanauthor,0,3))); + $publisherinput=$input->textfield(-name=>'publishercode', -default=>$publisher); + $pubyearinput=$input->textfield(-name=>'publicationyear', -default=>$publicationyear); + $placeinput=$input->textfield(-name=>'place', -default=>$bib->{place}); + $pagesinput=$input->textfield(-name=>'pages', -default=>$bib->{pages}); + $sizeinput=$input->textfield(-name=>'size', -default=>$bib->{size}); + $fileinput=$input->hidden(-name=>'file', -default=>$file); + $origisbn=$input->hidden(-name=>'origisbn', -default=>$isbn); + $origissn=$input->hidden(-name=>'origissn', -default=>$issn); + $origlccn=$input->hidden(-name=>'origlccn', -default=>$lccn); + $origcontrolnumber=$input->hidden(-name=>'origcontrolnumber', -default=>$controlnumber); - # Get next barcode, or pick random one if none exist yet - $sth=$dbh->prepare("select max(barcode) from items"); - $sth->execute; - my ($barcode) = $sth->fetchrow; - $barcode++; - if ($barcode==1) { - $barcode=int(rand()*1000000); - } + #print "
getting itemtypeselect
\n"; + $itemtypeselect=&getkeytableselectoptions( + $dbh, 'itemtypes', 'itemtype', 'description', 1); + #print "
it=$itemtypeselect
\n"; - my $branchselect=GetKeyTableSelectOptions( - $dbh, 'branches', 'branchcode', 'branchname', 0); + ($qissn) || ($qissn='NIL'); + ($qlccn) || ($qlccn='NIL'); + ($qisbn) || ($qisbn='NIL'); + ($qcontrolnumber) || ($qcontrolnumber='NIL'); + $controlnumber=~s/\s+//g; - print << "EOF"; - - - -
-Add a New Item for $title - -
-
- - - - - - - - - -
BARCODE - -Home Branch:
Replacement Price:
Notes
-
-

- - + unless (($isbn eq $qisbn) || ($issn eq $qissn) || ($lccn eq $qlccn) || ($controlnumber eq $qcontrolnumber)) { + #print "

Skip record $isbn $issn $lccn 
\n"; + next RECORD; + } + + print << "EOF"; +
+

New Record

+ Full MARC Record available at bottom +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Title$titleinput
Subtitle$subtitleinput
Author$authorinput
Additional Authors$additionalauthorsinput
Illustrator$illustratorinput
Copyright$copyrightinput
Series Title$seriestitleinput
Volume$volumeinput
Number$numberinput
Volume Date$volumedateinput
Volume Description$volumeddescinput
Subject$subjectinput
Notes$noteinput
Item Type
ISBN$isbninput
ISSN$issninput
LCCN$lccninput
Dewey$deweyinput
Subclass$subclassinput
Publication Year$pubyearinput
Publisher$publisherinput
Place$placeinput
Pages$pagesinput
Size$sizeinput
+ + + $fileinput + $marcinput + $origisbn + $origissn + $origlccn + $origcontrolnumber +
+ $marctext EOF -print endmenu(); -print endpage(); + } # foreach record + } else { + # No result item specified, list results + ListFileRecords($dbh,$input); + } # if +} # sub ProcessFile -exit; -} +sub ListFileRecords { + use strict; -sub NewBiblioItem { - use strict; + # Input parameters + my ( + $dbh, + $input, + )=@_; - my ( $dbh, # DBI handle - $biblio, # hash ref to biblio record - $biblioitem, # hash ref to biblioitem record - $subjects, # list ref of subjects - $addlauthors, # list ref of additional authors - )=@_ ; + my ( + $sth, $sti, + $field, + $data, # records in MARC file format + $name, + $srvid, + %servernames, + $serverdb, + ); - my ( $biblionumber, $biblioitemnumber, $error); # return values + my $z3950=0; + my $recordsource; + my $record; + my ($numrecords,$resultsid,$data,$startdate,$enddate); - my $debug=1; - my $sth; - my $subjectheading; - my $additionalauthor; + requireDBI($dbh,"ListFileRecords"); - #-------- + # File can be z3950 search query or uploaded MARC data - print "
Trying to add biblio item Title=$biblio->{title} " .
-		"ISBN=$biblioitem->{isbn} 
\n" if $debug; + # if z3950 results + if ($file=~/Z-(\d+)/) { + # This is a z3950 search + $recordsource=''; + } else { + # This is a Marc upload + $sth=$dbh->prepare("select marc,name from uploadedmarc where id=$file"); + $sth->execute; + ($data, $name) = $sth->fetchrow; + $recordsource="from $name"; + } - # Make sure master biblio entry exists - $biblionumber=GetOrAddBiblio($dbh, $biblio); + print << "EOF"; +
+

+ Select a New File +

+ + +
+ Select a Record to Import $recordsource +
+EOF - # Get next biblioitemnumber - $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems"); - $sth->execute; - ($biblioitemnumber) = $sth->fetchrow; - $biblioitemnumber++; - - print "
Next biblio item is $biblioitemnumber
\n" if $debug; - - $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" ); + if ($file=~/Z-(\d+)/) { + # This is a z3950 search - $sth->execute( - $biblioitemnumber, - $biblionumber, - $biblioitem->{volume}, - $biblioitem->{number}, - $biblioitem->{itemtype}, - $biblioitem->{isbn}, - $biblioitem->{issn}, - $biblioitem->{dewey}, - $biblioitem->{subclass}, - $biblioitem->{publicationyear}, - $biblioitem->{publishercode}, - $biblioitem->{volumedate}, - $biblioitem->{volumeddesc}, - $biblioitem->{illus}, - $biblioitem->{pages}, - $biblioitem->{notes}, - $biblioitem->{size}, - $biblioitem->{place}, - $biblioitem->{lccn}, - $biblioitem->{marc} ); - - $sth=$dbh->prepare("insert into bibliosubject - (biblionumber,subject) - values (?, ? )" ); - foreach $subjectheading (@{$subjects} ) { - $sth->execute($biblionumber, $subjectheading); - } + my $id=$1; # search query id number + my $serverstring; + my $starttimer=time(); - $sth=$dbh->prepare("insert into additionalauthors - (biblionumber,author) - values (?, ? )"); - foreach $additionalauthor (@{$addlauthors} ) { - $sth->execute($biblionumber, $additionalauthor); - } - - return ( $biblionumber, $biblioitemnumber, $error); - -} # sub NewBiblioItem - -#--------------------------------------- -# Find a biblio entry, or create a new one if it doesn't exist. -sub GetOrAddBiblio { - use strict; # in here until rest cleaned up - # input params - my ( - $dbh, # db handle - $biblio, # hash ref to fields - )=@_; - - # return - my $biblionumber; - - my $debug=1; - my $sth; - - #----- - print "
Looking for biblio 
\n" if $debug; - $sth=$dbh->prepare("select biblionumber - from biblio - where title=? and author=? - and copyrightdate=? and seriestitle=?"); - $sth->execute( - $biblio->{title}, $biblio->{author}, - $biblio->{copyright}, $biblio->{seriestitle} ); - if ($sth->rows) { - ($biblionumber) = $sth->fetchrow; - print "
Biblio exists with number $biblionumber
\n" if $debug; - } else { - # Doesn't exist. Add new one. - print "
Adding biblio
\n" if $debug; - $biblionumber=&newbiblio($biblio); - print "
Added with biblio number $biblionumber
\n" if $debug; - &newsubtitle($biblionumber,$biblio->{subtitle} ); - } - - return $biblionumber; - -} # sub GetOrAddBiblio -#--------------------------------------- - -if ($input->param('newitem')) { - use strict; - my $error; - my $barcode=$input->param('barcode'); - my $replacementprice=($input->param('replacementprice') || 0); - - my $sth=$dbh->prepare("select barcode - from items - where barcode=?"); - $sth->execute($barcode); - if ($sth->rows) { - print "Barcode '$barcode' has already been assigned.

\n"; - } else { - # Insert new item into database - $error=&newitems( - { biblionumber=> $input->param('biblionumber'), - biblioitemnumber=> $input->param('biblioitemnumber'), - itemnotes=> $input->param('notes'), - homebranch=> $input->param('homebranch'), - replacementprice=> $replacementprice, - }, - $barcode - ); - if ( $error ) { - print "Error: $error

\n"; - } else { - - print "Item added with barcode $barcode -

\n"; - } # if error - } # if barcode exists -} - - -my $menu = $input->param('menu'); -if ($file) { - print "Main Menu


\n"; - my $qisbn=$input->param('isbn'); - my $qissn=$input->param('issn'); - my $qlccn=$input->param('lccn'); - my $qcontrolnumber=$input->param('controlnumber'); - if ($qisbn || $qissn || $qlccn || $qcontrolnumber) { - print "New File
\n"; - #open (F, "$file"); - #my $data=; - my $data; - if ($file=~/Z-(\d+)/) { - my $id=$1; - my $resultsid=$input->param('resultsid'); - my $sth=$dbh->prepare("select results from z3950results where id=$resultsid"); - $sth->execute; - ($data) = $sth->fetchrow; - } else { - my $sth=$dbh->prepare("select marc from uploadedmarc where id=$file"); - $sth->execute; - ($data) = $sth->fetchrow; - } - - $splitchar=chr(29); - my @records; - foreach $record (split(/$splitchar/, $data)) { - my $marctext="\n"; - $marctext.="\n"; - $leader=substr($record,0,24); - $marctext.="\n"; - $record=substr($record,24); - $splitchar2=chr(30); - my $directory=0; - my $tagcounter=0; - my %tag; - my @record; - foreach $field (split(/$splitchar2/, $record)) { - my %field; - ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1); - unless ($directory) { - $directory=$field; - my $itemcounter=1; - $counter=0; - while ($item=substr($directory,0,12)) { - $tag=substr($directory,0,3); - $length=substr($directory,3,4); - $start=substr($directory,7,6); - $directory=substr($directory,12); - $tag{$counter}=$tag; - $counter++; - } - $directory=1; - next; - } - $tag=$tag{$tagcounter}; - $tagcounter++; - $field{'tag'}=$tag; - $marctext.=""; - $splitchar3=chr(31); - my @subfields=split(/$splitchar3/, $field); - $indicator=$subfields[0]; - $field{'indicator'}=$indicator; - my $firstline=1; - if ($#subfields==0) { - $marctext.=""; - } else { - my %subfields; - $marctext.="\n"; - $field{'subfields'}=\%subfields; - } - push (@record, \%field); - } - $marctext.="
MARC RECORD
Leader:$leader
$tagtext{$tag}$tag$indicator
\n"; - my $color2=$color; - for ($i=1; $i<=$#subfields; $i++) { - ($color2 eq $lc1) ? ($color2=$lc2) : ($color2=$lc1); - my $text=$subfields[$i]; - my $subfieldcode=substr($text,0,1); - my $subfield=substr($text,1); - $marctext.="\n"; - if ($subfields{$subfieldcode}) { - my $subfieldlist=$subfields{$subfieldcode}; - my @subfieldlist=@$subfieldlist; - if ($#subfieldlist>=0) { - push (@subfieldlist, $subfield); - } else { - @subfieldlist=($subfields{$subfieldcode}, $subfield); - } - $subfields{$subfieldcode}=\@subfieldlist; - } else { - $subfields{$subfieldcode}=$subfield; - } - } - $marctext.="
$subfieldcode
$subfield
\n"; - $marctext{\@record}=$marctext; - $marc{\@record}=$record; - push (@records, \@record); - $counter++; - } -RECORD: - foreach $record (@records) { - my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $notes, $additionalauthors, $illustrator, $copyrightdate, $seriestitle); - my $marctext=$marctext{$record}; - my $marc=$marc{$record}; - foreach $field (@$record) { - if ($field->{'tag'} eq '001') { - $controlnumber=$field->{'indicator'}; - } - if ($field->{'tag'} eq '010') { - $lccn=$field->{'subfields'}->{'a'}; - $lccn=~s/^\s*//; - ($lccn) = (split(/\s+/, $lccn))[0]; - } - if ($field->{'tag'} eq '015') { - $lccn=$field->{'subfields'}->{'a'}; - $lccn=~s/^\s*//; - $lccn=~s/^C//; - ($lccn) = (split(/\s+/, $lccn))[0]; - } - if ($field->{'tag'} eq '020') { - $isbn=$field->{'subfields'}->{'a'}; - ($isbn=~/^ARRAY/) && ($isbn=$$isbn[0]); - $isbn=~s/[^\d]*//g; - } - if ($field->{'tag'} eq '022') { - $issn=$field->{'subfields'}->{'a'}; - $issn=~s/^\s*//; - ($issn) = (split(/\s+/, $issn))[0]; - } - if ($field->{'tag'} eq '082') { - $dewey=$field->{'subfields'}->{'a'}; - $dewey=~s/\///g; - if (@$dewey) { - $dewey=$$dewey[0]; - } - #$dewey=~s/\///g; - } - if ($field->{'tag'} eq '100') { - $author=$field->{'subfields'}->{'a'}; - } - if ($field->{'tag'} eq '245') { - $title=$field->{'subfields'}->{'a'}; - $title=~s/ \/$//; - $subtitle=$field->{'subfields'}->{'b'}; - $subtitle=~s/ \/$//; - } - if ($field->{'tag'} eq '260') { - $place=$field->{'subfields'}->{'a'}; - if (@$place) { - $place=$$place[0]; - } - $place=~s/\s*:$//g; - $publisher=$field->{'subfields'}->{'b'}; - if (@$publisher) { - $publisher=$$publisher[0]; - } - $publisher=~s/\s*:$//g; - $publicationyear=$field->{'subfields'}->{'c'}; - if ($publicationyear=~/c(\d\d\d\d)/) { - $copyrightdate=$1; - } - if ($publicationyear=~/[^c](\d\d\d\d)/) { - $publicationyear=$1; - } elsif ($copyrightdate) { - $publicationyear=$copyrightdate; - } else { - $publicationyear=~/(\d\d\d\d)/; - $publicationyear=$1; - } - } - if ($field->{'tag'} eq '300') { - $pages=$field->{'subfields'}->{'a'}; - $pages=~s/ \;$//; - $size=$field->{'subfields'}->{'c'}; - $pages=~s/\s*:$//g; - $size=~s/\s*:$//g; - } - if ($field->{'tag'} eq '362') { - if ($field->{'subfields'}->{'a'}=~/(\d+).*(\d+)/) { - $volume=$1; - $number=$2; - } - } - if ($field->{'tag'} eq '440') { - $seriestitle=$field->{'subfields'}->{'a'}; - if ($field->{'subfields'}->{'v'}=~/(\d+).*(\d+)/) { - $volume=$1; - $number=$2; - } - } - if ($field->{'tag'} eq '700') { - my $name=$field->{'subfields'}->{'a'}; - if ($field->{'subfields'}->{'c'}=~/ill/) { - $additionalauthors.="$name\n"; - } else { - $illustrator=$name; - } - } - if ($field->{'tag'} =~/^5/) { - $notes.="$field->{'subfields'}->{'a'}\n"; - } - if ($field->{'tag'} =~/65\d/) { - my $subject=$field->{'subfields'}->{'a'}; - $subject=~s/\.$//; - if ($gensubdivision=$field->{'subfields'}->{'x'}) { - my @sub=@$gensubdivision; - if ($#sub>=0) { - foreach $s (@sub) { - $s=~s/\.$//; - $subject.=" -- $s"; - } - } else { - $gensubdivision=~s/\.$//; - $subject.=" -- $gensubdivision"; - } - } - if ($chronsubdivision=$field->{'subfields'}->{'y'}) { - my @sub=@$chronsubdivision; - if ($#sub>=0) { - foreach $s (@sub) { - $s=~s/\.$//; - $subject.=" -- $s"; - } - } else { - $chronsubdivision=~s/\.$//; - $subject.=" -- $chronsubdivision"; - } - } - if ($geosubdivision=$field->{'subfields'}->{'z'}) { - my @sub=@$geosubdivision; - if ($#sub>=0) { - foreach $s (@sub) { - $s=~s/\.$//; - $subject.=" -- $s"; - } - } else { - $geosubdivision=~s/\.$//; - $subject.=" -- $geosubdivision"; - } - } - push @subjects, $subject; - } - } - $titleinput=$input->textfield(-name=>'title', -default=>$title, -size=>40); - $marcinput=$input->hidden(-name=>'marc', -default=>$marc); - $subtitleinput=$input->textfield(-name=>'subtitle', -default=>$subtitle, -size=>40); - $authorinput=$input->textfield(-name=>'author', -default=>$author); - $illustratorinput=$input->textfield(-name=>'illustrator', -default=>$illustrator); - $additionalauthorsinput=$input->textarea(-name=>'additionalauthors', -default=>$additionalauthors, -rows=>4, -cols=>20); - my $subject=''; - foreach (@subjects) { - $subject.="$_\n"; - } - $subjectinput=$input->textarea(-name=>'subject', -default=>$subject, -rows=>4, -cols=>40); - $noteinput=$input->textarea(-name=>'notes', -default=>$notes, -rows=>4, -cols=>40, -wrap=>'physical'); - $copyrightinput=$input->textfield(-name=>'copyrightdate', -default=>$copyrightdate); - $seriestitleinput=$input->textfield(-name=>'seriestitle', -default=>$seriestitle); - $volumeinput=$input->textfield(-name=>'volume', -default=>$volume); - $volumedateinput=$input->textfield(-name=>'volumedate', -default=>$volumedate); - $volumeddescinput=$input->textfield(-name=>'volumeddesc', -default=>$volumeddesc); - $numberinput=$input->textfield(-name=>'number', -default=>$number); - $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn); - $issninput=$input->textfield(-name=>'issn', -default=>$issn); - $lccninput=$input->textfield(-name=>'lccn', -default=>$lccn); - $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn); - $deweyinput=$input->textfield(-name=>'dewey', -default=>$dewey); - $cleanauthor=$author; - $cleanauthor=~s/[^A-Za-z]//g; - $subclassinput=$input->textfield(-name=>'subclass', -default=>uc(substr($cleanauthor,0,3))); - $publisherinput=$input->textfield(-name=>'publishercode', -default=>$publisher); - $pubyearinput=$input->textfield(-name=>'publicationyear', -default=>$publicationyear); - $placeinput=$input->textfield(-name=>'place', -default=>$place); - $pagesinput=$input->textfield(-name=>'pages', -default=>$pages); - $sizeinput=$input->textfield(-name=>'size', -default=>$size); - $fileinput=$input->hidden(-name=>'file', -default=>$file); - $origisbn=$input->hidden(-name=>'origisbn', -default=>$isbn); - $origissn=$input->hidden(-name=>'origissn', -default=>$issn); - $origlccn=$input->hidden(-name=>'origlccn', -default=>$lccn); - $origcontrolnumber=$input->hidden(-name=>'origcontrolnumber', -default=>$controlnumber); - - #print "
getting itemtypeselect
\n"; - $itemtypeselect=&GetKeyTableSelectOptions( - $dbh, 'itemtypes', 'itemtype', 'description', 1); - #print "
it=$itemtypeselect
\n"; - - ($qissn) || ($qissn='NIL'); - ($qlccn) || ($qlccn='NIL'); - ($qisbn) || ($qisbn='NIL'); - ($qcontrolnumber) || ($qcontrolnumber='NIL'); - $controlnumber=~s/\s+//g; - - unless (($isbn eq $qisbn) || ($issn eq $qissn) || ($lccn eq $qlccn) || ($controlnumber eq $qcontrolnumber)) { - #print "
Skip record $isbn $issn $lccn 
\n"; - next RECORD; - } - - print << "EOF"; -
-

New Record

- Full MARC Record available at bottom -
- - - - - - - - - - - - - - - - - - - - - - - - - -
Title$titleinput
Subtitle$subtitleinput
Author$authorinput
Additional Authors$additionalauthorsinput
Illustrator$illustratorinput
Copyright$copyrightinput
Series Title$seriestitleinput
Volume$volumeinput
Number$numberinput
Volume Date$volumedateinput
Volume Description$volumeddescinput
Subject$subjectinput
Notes$noteinput
Item Type
ISBN$isbninput
ISSN$issninput
LCCN$lccninput
Dewey$deweyinput
Subclass$subclassinput
Publication Year$pubyearinput
Publisher$publisherinput
Place$placeinput
Pages$pagesinput
Size$sizeinput
- - - $fileinput - $marcinput - $origisbn - $origissn - $origlccn - $origcontrolnumber -
- $marctext -EOF - } - } else { - #open (F, "$file"); - #my $data=; - my $data; - my $name; - my $z3950=0; - if ($file=~/Z-(\d+)/) { - print << "EOF"; -
-

-Select a New File -

- - -
Select a Record to Import
-EOF - my $id=$1; - my $sth=$dbh->prepare("select servers from z3950queue where id=$id"); - $sth->execute; - my ($servers) = $sth->fetchrow; - my $serverstring; - my $starttimer=time(); - foreach $serverstring (split(/\s+/, $servers)) { - my ($name, $server, $database, $auth) = split(/\//, $serverstring, 4); - if ($name eq 'MAN') { - print "$server/$database
\n"; - } else { - my $sti=$dbh->prepare("select name from - z3950servers where id=$name"); - $sti->execute; - my ($longname)=$sti->fetchrow; - print "\n"; - if ($longname) { - print "$longname \n"; - } else { - print "$server/$database \n"; - } - } - my $q_server=$dbh->quote($serverstring); - my $startrecord=$input->param("ST-$name"); + $sth=$dbh->prepare(" + select z3950results.numrecords,z3950results.id,z3950results.results, + z3950results.startdate,z3950results.enddate,server + from z3950queue left outer join z3950results + on z3950queue.id=z3950results.queryid + where z3950queue.id=? + order by server + "); + $sth->execute($id); + if ( $sth->rows ) { + # loop through all servers in search results + while ( ($numrecords,$resultsid,$data, + $startdate,$enddate,$serverstring) = $sth->fetchrow ) { + my ($srvid, $server, $database, $auth) = split(/\//, $serverstring, 4); + #print "server=$serverstring\n"; + if ( $server ) { + print " " . + &z3950servername($dbh,$srvid,"$server/$database") . "\n"; + } # if $server + my $startrecord=$input->param("ST-$srvid"); ($startrecord) || ($startrecord='0'); - my $sti=$dbh->prepare("select numrecords,id,results,startdate,enddate from z3950results where queryid=$id and server=$q_server"); - $sti->execute; - ($numrecords,$resultsid,$data,$startdate,$enddate) = $sti->fetchrow; my $serverplaceholder=''; foreach ($input->param) { (next) unless (/ST-(.+)/); my $serverid=$1; - (next) if ($serverid eq $name); + (next) if ($serverid eq $srvid); my $place=$input->param("ST-$serverid"); $serverplaceholder.="\&ST-$serverid=$place"; } @@ -937,23 +481,25 @@ EOF my $previous=''; my $next=''; if ($startrecord>0) { - $previous="Previous"; + $previous="Previous"; } my $highest; $highest=$startrecord+10; ($highest>$numrecords) && ($highest=$numrecords); if ($numrecords>$startrecord+10) { - $next="Next"; + $next="Next"; } print "[Viewing ".($startrecord+1)." to ".$highest." of $numrecords records] $previous | $next
\n"; + my $stj=$dbh->prepare("update z3950results + set highestseen=? where id=?"); + $stj->execute($startrecord+10,$resultsid); } else { print "
\n"; } print "
    \n"; - my $stj=$dbh->prepare("update z3950results set highestseen=".($startrecord+10)." where id=$resultsid"); - $stj->execute; - if ($sti->rows == 0) { - print "pending..."; + + if (! $server ) { + print "Search still pending..."; } elsif ($enddate == 0) { my $now=time(); my $elapsed=$now-$startdate; @@ -965,9 +511,8 @@ EOF } print "processing... ($elapsedtime)"; } elsif ($numrecords) { - my $splitchar=chr(29); - my @records=split(/$splitchar/, $data); - $data=''; + my @records=parsemarcfileformat($data); + my $i; for ($i=$startrecord; $i<$startrecord+10; $i++) { $data.=$records[$i].$splitchar; } @@ -1041,154 +586,350 @@ EOF } } print "

    \n"; + } else { + if ( $records[$i] ) { + &PrintResultRecordLink($dbh,$records[$i],$resultsid); + } # if record + } # for records + print "

    \n"; } else { print "No records returned.

    \n"; } print "

\n"; - } + } # foreach server my $elapsed=time()-$starttimer; print "
It took $elapsed seconds to process this page.\n"; + } else { + print "No results found for query $id\n"; + } # if rows } else { - my $sth=$dbh->prepare("select marc,name from uploadedmarc where id=$file"); - $sth->execute; - ($data, $name) = $sth->fetchrow; - print << "EOF"; -
-

-Select a New File -

- - -
Select a Record to Import
from $name
-EOF - - my @records=parsemarcdata($data); + # This is an uploaded Marc record + + my @records=parsemarcfileformat($data); foreach $record (@records) { - my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $notes, $controlnumber); - foreach $field (@$record) { - if ($field->{'tag'} eq '001') { - $controlnumber=$field->{'indicator'}; - } - if ($field->{'tag'} eq '010') { - $lccn=$field->{'subfields'}->{'a'}; - $lccn=~s/^\s*//; - ($lccn) = (split(/\s+/, $lccn))[0]; - } - if ($field->{'tag'} eq '015') { - $lccn=$field->{'subfields'}->{'a'}; - $lccn=~s/^\s*//; - $lccn=~s/^C//; - ($lccn) = (split(/\s+/, $lccn))[0]; - } - if ($field->{'tag'} eq '020') { - $isbn=$field->{'subfields'}->{'a'}; - ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]); - $isbn=~s/[^\d]*//g; - } - if ($field->{'tag'} eq '022') { - $issn=$field->{'subfields'}->{'a'}; - $issn=~s/^\s*//; - ($issn) = (split(/\s+/, $issn))[0]; - } - if ($field->{'tag'} eq '100') { - $author=$field->{'subfields'}->{'a'}; - } - if ($field->{'tag'} eq '245') { - $title=$field->{'subfields'}->{'a'}; - $title=~s/ \/$//; - $subtitle=$field->{'subfields'}->{'b'}; - $subtitle=~s/ \/$//; - } - } - my $q_isbn=$dbh->quote((($isbn) || ('NIL'))); - my $q_issn=$dbh->quote((($issn) || ('NIL'))); - my $q_lccn=$dbh->quote((($lccn) || ('NIL'))); - my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL'))); - my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber"); - $sth->execute; - my $donetext=''; - if ($sth->rows) { - $donetext="DONE"; + &PrintResultRecordLink($dbh,$record,''); + } # foreach record + } # if z3950 or marc upload + print "
\n"; +} # sub ListFileRecords + +#-------------- +sub z3950servername { + # inputs + my ( + $dbh, + $srvid, # server id number + $default, + )=@_; + # return + my $longname; + #---- + + requireDBI($dbh,"z3950servername"); + + my $sti=$dbh->prepare("select name + from z3950servers + where id=?"); + $sti->execute($srvid); + if ( ! $sti->err ) { + ($longname)=$sti->fetchrow; + } + if (! $longname) { + $longname="$default"; + } + return $longname; +} # sub z3950servername + +sub PrintResultRecordLink { + use strict; + my ($dbh,$record,$resultsid)=@_; # input + + my ( + $sth, + $bib, # hash ref to named fields + $searchfield, $searchvalue, + $donetext, + $fieldname, + ); + + requireDBI($dbh,"PrintResultRecordLink"); + + $bib=extractmarcfields($record); + + $sth=$dbh->prepare("select * + from biblioitems + where isbn=? or issn=? or lccn=? "); + $sth->execute($bib->{isbn},$bib->{issn},$bib->{lccn}); + if ($sth->rows) { + $donetext="DONE"; + } else { + $donetext=""; + } + ($bib->{author}) && ($bib->{author}="by $bib->{author}"); + + $searchfield=""; + foreach $fieldname ( "controlnumber", "lccn", "issn", "isbn") { + if ( defined $bib->{$fieldname} ) { + $searchfield=$fieldname; + $searchvalue=$bib->{$fieldname}; + } # if defined fieldname + } # foreach + + if ( $searchfield ) { + print "$bib->{title} $bib->{author}" . + " $donetext
\n"; + } else { + print "Error: Problem with $bib->{title} $bib->{author}
\n"; + } # if searchfield +} # sub PrintResultRecordLink + +#------------------ +sub extractmarcfields { + use strict; + # input + my ( + $record, # pointer to list of MARC field hashes. + # Example: $record->[0]->{'tag'} = '100' # Author + # $record->[0]->{'subfields'}->{'a'} = subfieldvalue + )=@_; + + # return + my $bib; # pointer to hash of named output fields + # Example: $bib->{'author'} = "Twain, Mark"; + + my $debug=0; + + my ( + $field, # hash ref + $value, + $subfield, # Marc subfield [a-z] + $fieldname, # name of field "author", "title", etc. + $strip, # chars to remove from end of field + $stripregex, # reg exp pattern + ); + my ($lccn, $isbn, $issn, + $publicationyear, @subjects, $subject, + $controlnumber, + $notes, $additionalauthors, $illustrator, $copyrightdate, + $s, $subdivision, $subjectsubfield, + ); + + print "

\n" if $debug;
+
+    if ( ref($record) eq "ARRAY" ) {
+        foreach $field (@$record) {
+
+	    # Check each subfield in field
+	    foreach $subfield ( keys %{$field->{subfields}} ) {
+		# see if it is defined in our Marc to koha mapping table
+	    	if ( $fieldname=$tagmap{ $field->{'tag'} }->{$subfield}->{name} ) {
+		    # Yes, so keep the value
+		    if ( ref($field->{'subfields'}->{$subfield} ) eq 'ARRAY' ) {
+		        # if it was an array, just keep first element.
+		        $bib->{$fieldname}=$field->{'subfields'}->{$subfield}[0];
+		    } else {
+		        $bib->{$fieldname}=$field->{'subfields'}->{$subfield};
+		    } # if array
+		    print "$field->{'tag'} $subfield $fieldname=$bib->{$fieldname}\n" if $debug;
+		    # see if this field should have trailing chars dropped
+	    	    if ($strip=$tagmap{ $field->{'tag'} }->{$subfield}->{striptrail} ) {
+			$strip=~s//\\/; # backquote each char
+			$stripregex='[ ' . $strip . ']+$';  # remove trailing spaces also
+			$bib->{$fieldname}=~s/$stripregex//;
+		    } # if strip
+		    print "Found subfield $field->{'tag'} $subfield " .
+			"$fieldname = $bib->{$fieldname}\n" if $debug;
+		} # if tagmap exists
+
+	    } # foreach subfield
+
+
+	    if ($field->{'tag'} eq '001') {
+		$bib->{controlnumber}=$field->{'indicator'};
+	    }
+	    if ($field->{'tag'} eq '015') {
+		$bib->{lccn}=$field->{'subfields'}->{'a'};
+		$bib->{lccn}=~s/^\s*//;
+		$bib->{lccn}=~s/^C//;
+		($bib->{lccn}) = (split(/\s+/, $bib->{lccn}))[0];
+	    }
+
+
+		if ($field->{'tag'} eq '260') {
+
+		    $publicationyear=$field->{'subfields'}->{'c'};
+		    if ($publicationyear=~/c(\d\d\d\d)/) {
+			$copyrightdate=$1;
+		    }
+		    if ($publicationyear=~/[^c](\d\d\d\d)/) {
+			$publicationyear=$1;
+		    } elsif ($copyrightdate) {
+			$publicationyear=$copyrightdate;
+		    } else {
+			$publicationyear=~/(\d\d\d\d)/;
+			$publicationyear=$1;
+		    }
 		}
-		$sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
-		$sth->execute;
-		if ($sth->rows) {
-		    $donetext="DONE";
+		if ($field->{'tag'} eq '700') {
+		    my $name=$field->{'subfields'}->{'a'};
+		    if ($field->{'subfields'}->{'e'}!~/ill/) {
+			$additionalauthors.="$name\n";
+		    } else {
+			$illustrator=$name;
+		    }
 		}
-		($author) && ($author="by $author");
-		if ($isbn) {
-		    print "$title$subtitle $author $donetext
\n"; - } elsif ($lccn) { - print "$title$subtitle $author $donetext
\n"; - } elsif ($issn) { - print "$title$subtitle $author
$donetext\n"; - } elsif ($controlnumber) { - print "$title by $author
$donetext\n"; - } else { - print "Error: Contact steve regarding $title by $author
\n"; + if ($field->{'tag'} =~/^5/) { + $notes.="$field->{'subfields'}->{'a'}\n"; } + if ($field->{'tag'} =~/65\d/) { + my $sub; + my $subject=$field->{'subfields'}->{'a'}; + $subject=~s/\.$//; + print "Subject=$subject\n" if $debug; + foreach $subjectsubfield ( 'x','y','z' ) { + if ($subdivision=$field->{'subfields'}->{$subjectsubfield}) { + if ( ref($subdivision) eq 'ARRAY' ) { + foreach $s (@$subdivision) { + $s=~s/\.$//; + $subject.=" -- $s"; + } # foreach subdivision + } else { + $subdivision=~s/\.$//; + $subject.=" -- $subdivision"; + } # if array + } # if subfield exists + } # foreach subfield + print "Subject=$subject\n" if $debug; + push @subjects, $subject; + } # if tag 65x + + + } # foreach field + ($publicationyear ) && ($bib->{publicationyear}=$publicationyear ); + ($copyrightdate ) && ($bib->{copyrightdate}=$copyrightdate ); + ($additionalauthors ) && ($bib->{additionalauthors}=$additionalauthors ); + ($illustrator ) && ($bib->{illustrator}=$illustrator ); + ($notes ) && ($bib->{notes}=$notes ); + ($#subjects ) && ($bib->{subject}=\@subjects ); + + # Misc cleanup + $bib->{dewey}=~s/\///g; # drop any slashes + + ($bib->{lccn}) = (split(/\s+/, $bib->{lccn}))[0]; # only keep first word + + $bib->{isbn}=~s/[^\d]*//g; # drop non-digits + + $bib->{issn}=~s/^\s*//; + ($bib->{issn}) = (split(/\s+/, $bib->{issn}))[0]; + + if ( $bib->{'volume-number'} ) { + if ($bib->{'volume-number'}=~/(\d+).*(\d+)/ ) { + $bib->{'volume'}=$1; + $bib->{'number'}=$2; + } else { + $bib->{volume}=$bib->{'volume-number'}; } - } - print "
\n"; - } -} else { + delete $bib->{'volume-number'}; + } # if volume-number -SWITCH: - { - if ($menu eq 'z3950') { z3950(); last SWITCH; } - if ($menu eq 'uploadmarc') { uploadmarc(); last SWITCH; } - if ($menu eq 'manual') { manual(); last SWITCH; } - mainmenu(); + } else { + print "Error: extractmarcfields: input ref $record is " . + ref($record) . " not ARRAY. Contact sysadmin.\n"; } + print "\n" if $debug; -} + return $bib; +} # sub extractmarcfields +#--------------------------------- + +sub z3950menu { + use strict; + my ( + $dbh, + $input, + )=@_; + + my ( + $sth, $sti, + $processing, + $realenddate, + $totalrecords, + $elapsed, + $elapsedtime, + $resultstatus, $statuscolor, + $id, $term, $type, $done, + $startdate, $enddate, $servers, + $record,$bib,$title, + ); + + requireDBI($dbh,"z3950menu"); -sub z3950 { - my $sth=$dbh->prepare("select id,term,type,done,numrecords,length(results),startdate,enddate,servers from z3950queue order by id desc limit 20"); - $sth->execute; print "Main Menu


\n"; print "\n"; + # End of query listing + + #------------------------------ + # Search input form + print "
\n"; print "

Results of Z39.50 searches

\n"; - print "Refresh
\n
    \n"; - while (my ($id, $term, $type, $done, $numrecords, $length, $startdate, $enddate, $servers) = $sth->fetchrow) { + print "Refresh
    \n" . + "
      \n"; + + # Check queued queries + $sth=$dbh->prepare("select id,term,type,done, + startdate,enddate,servers + from z3950queue + order by id desc + limit 20 "); + $sth->execute; + while ( ($id, $term, $type, $done, + $startdate, $enddate, $servers) = $sth->fetchrow) { $type=uc($type); $term=~s//>/g; - my $sti=$dbh->prepare("select id,server,startdate,enddate,numrecords from z3950results where queryid=$id"); - $sti->execute; + + $title=""; + # See if query produced results + $sti=$dbh->prepare("select id,server,startdate,enddate,numrecords,results + from z3950results + where queryid=?"); + $sti->execute($id); if ($sti->rows) { - my $processing=0; - my $realenddate=0; - my $totalrecords=0; - while (my ($r_id,$r_server,$r_startdate,$r_enddate,$r_numrecords) = $sti->fetchrow) { + $processing=0; + $realenddate=0; + $totalrecords=0; + while (my ($r_id,$r_server,$r_startdate,$r_enddate,$r_numrecords,$r_marcdata) + = $sti->fetchrow) { if ($r_enddate==0) { + # It hasn't finished yet $processing=1; } else { + # It finished, see how long it took. if ($r_enddate>$realenddate) { $realenddate=$r_enddate; } - } + # Snag any title from the results if there were any + if ( ! $title && $r_marcdata ) { + ($record)=parsemarcfileformat($r_marcdata); + $bib=extractmarcfields($record); + if ( $bib->{title} ) { $title=$bib->{title} }; + } # if no title yet + } # if finished $totalrecords+=$r_numrecords; - } + } # while results + if ($processing) { - my $elapsed=time()-$startdate; - my $elapsedtime=''; - if ($elapsed>60) { - $elapsedtime=sprintf "%d minutes",($elapsed/60); - } else { - $elapsedtime=sprintf "%d seconds",$elapsed; - } - if ($totalrecords) { - $totalrecords="$totalrecords found."; - } else { - $totalrecords=''; - } - print "
    • $type=$term Processing... $totalrecords ($elapsedtime)
      \n"; + $elapsed=time()-$startdate; + $resultstatus="Processing..."; + $statuscolor="red"; } else { - my $elapsed=$realenddate-$startdate; - my $elapsedtime=''; + $elapsed=$realenddate-$startdate; + $resultstatus="Done."; + $statuscolor="black"; + } + if ($elapsed>60) { $elapsedtime=sprintf "%d minutes",($elapsed/60); } else { @@ -1199,15 +940,25 @@ sub z3950 { } else { $totalrecords=''; } - print "
    • $type=$term Done. $totalrecords ($elapsedtime)
      \n"; - } + print "
    • ". + "$type=$term" . + "$resultstatus $totalrecords " . + "($elapsedtime) $title
      \n"; } else { - print "
    • $type=$term Pending
      \n"; - } - } - print "
    \n"; - print "
\n"; - my $sth=$dbh->prepare("select id,name,checked from z3950servers order by rank"); + print "
  • + $type=$term Pending
    \n"; + } # if results done + } # while queries + print "
  • \n"; + + my $sth=$dbh->prepare("select id,name,checked + from z3950servers + order by rank"); $sth->execute; my $serverlist=''; while (my ($id, $name, $checked) = $sth->fetchrow) { @@ -1224,23 +975,31 @@ print << "EOF";

    - +
    Search for MARC records
    + - - +  ISBN +  LCCN
    +  Author +  Title +  Keyword + +
    Search for MARC records
    Query Term
     ISBN  LCCN
     Author  Title  Keyword
    - $serverlist -
    - -
    $serverlist
    EOF -print "

    \n"; -} + print "
    \n"; +} # sub z3950menu +#--------------------------------- sub uploadmarc { + use strict; + my ($dbh)=@_; + + requireDBI($dbh,"uploadmarc"); + print "Main Menu


    \n"; my $sth=$dbh->prepare("select id,name from uploadedmarc"); $sth->execute; @@ -1281,58 +1040,57 @@ sub mainmenu {
  • Upload MARC Records EOF -} - -sub skip { +} # sub mainmenu - #opendir(D, "/home/$userid/"); - #my @dirlist=readdir D; - #foreach $file (@dirlist) { -# (next) if ($file=~/^\./); -# (next) if ($file=~/^nsmail$/); -# (next) if ($file=~/^public_html$/); -# ($file=~/\.mrc/) || ($filelist.="$file
    \n"); -# (next) unless ($file=~/\.mrc$/); -# $file=~s/ /\%20/g; -# print "$file
    \n"; -# } - - #
    - -} -print endmenu(); -print endpage(); - -sub parsemarcdata { +#-------------------------- +# Parse MARC data in file format with control-character separators +# May be multiple records. +sub parsemarcfileformat { + use strict; + # Input is one big text string my $data=shift; - my $splitchar=chr(29); + # Output is list of records. Each record is list of field hashes my @records; + + my $splitchar=chr(29); + my $splitchar2=chr(30); + my $splitchar3=chr(31); + my $debug=0; my $record; foreach $record (split(/$splitchar/, $data)) { - my $leader=substr($record,0,24); - #print "Leader:$leader\n"; - $record=substr($record,24); - my $splitchar2=chr(30); + my @record; my $directory=0; my $tagcounter=0; my %tag; - my @record; my $field; + + my $leader=substr($record,0,24); + print "Leader:$leader\n" if $debug; + push (@record, { + 'tag' => 'Leader', + 'indicator' => $leader , + } ); + + $record=substr($record,24); foreach $field (split(/$splitchar2/, $record)) { my %field; - ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1); + my $tag; + my $indicator; unless ($directory) { $directory=$field; my $itemcounter=1; - $counter=0; + my $counter2=0; + my $item; + my $length; + my $start; while ($item=substr($directory,0,12)) { $tag=substr($directory,0,3); $length=substr($directory,3,4); $start=substr($directory,7,6); $directory=substr($directory,12); - $tag{$counter}=$tag; - $counter++; + $tag{$counter2}=$tag; + $counter2++; } $directory=1; next; @@ -1340,46 +1098,348 @@ sub parsemarcdata { $tag=$tag{$tagcounter}; $tagcounter++; $field{'tag'}=$tag; - $splitchar3=chr(31); my @subfields=split(/$splitchar3/, $field); $indicator=$subfields[0]; $field{'indicator'}=$indicator; my $firstline=1; unless ($#subfields==0) { my %subfields; + my @subfieldlist; + my $i; for ($i=1; $i<=$#subfields; $i++) { my $text=$subfields[$i]; my $subfieldcode=substr($text,0,1); my $subfield=substr($text,1); + # if this subfield already exists, do array if ($subfields{$subfieldcode}) { my $subfieldlist=$subfields{$subfieldcode}; - my @subfieldlist=@$subfieldlist; - if ($#subfieldlist>=0) { -# print "$tag Adding to array $subfieldcode -- $subfield
    \n"; + if ( ref($subfieldlist) eq 'ARRAY' ) { + # Already an array, add on to it + print "$tag Adding to array $subfieldcode -- $subfield
    \n" if $debug; + @subfieldlist=@$subfieldlist; push (@subfieldlist, $subfield); } else { -# print "$tag Arraying $subfieldcode -- $subfield
    \n"; + # Change simple value to array + print "$tag Arraying $subfieldcode -- $subfield
    \n" if $debug; @subfieldlist=($subfields{$subfieldcode}, $subfield); } + # keep new array $subfields{$subfieldcode}=\@subfieldlist; } else { + # subfield doesn't exist yet, keep simple value $subfields{$subfieldcode}=$subfield; } } $field{'subfields'}=\%subfields; } push (@record, \%field); - } + } # foreach field in record push (@records, \@record); - $counter++; + # $counter++; } + print "" if $debug; return @records; -} +} # sub parsemarcfileformat + +#---------------------------- +# Accept form results to add query to z3950 queue +sub AcceptZ3950Queue { + use strict; + + # input parameters + my ( + $dbh, # DBI handle + $input, # CGI parms + )=@_; + + my @serverlist; + + requireDBI($dbh,"AcceptZ3950Queue"); + + my $query=$input->param('query'); + + my $isbngood=1; + if ($input->param('type') eq 'isbn') { + $isbngood=checkvalidisbn($query); + } + if ($isbngood) { + foreach ($input->param) { + if (/S-(.*)/) { + my $server=$1; + if ($server eq 'MAN') { + push @serverlist, "MAN/".$input->param('manualz3950server')."//" +; + } else { + push @serverlist, $server; + } + } + } + + addz3950queue($dbh,$input->param('query'), $input->param('type'), + $input->param('rand'), @serverlist); + } else { + print "$query is not a valid ISBN + Number

    \n"; + } +} # sub AcceptZ3950Queue + +#--------------------------------------------- +sub AcceptMarcUpload { + use strict; + my ( + $dbh, # DBI handle + $input, # CGI parms + )=@_; + + requireDBI($dbh,"AcceptMarcUpload"); + + my $name=$input->param('name'); + my $data=$input->param('uploadmarc'); + my $marcrecord=''; + + ($name) || ($name=$data); + if (length($data)>0) { + while (<$data>) { + $marcrecord.=$_; + } + } + my $q_marcrecord=$dbh->quote($marcrecord); + my $q_name=$dbh->quote($name); + my $sth=$dbh->prepare("insert into uploadedmarc + (marc,name) + values ($q_marcrecord, $q_name)"); + $sth->execute; +} # sub AcceptMarcUpload + +#------------------------------------------- +sub AcceptBiblioitem { + use strict; + my ( + $dbh, + $input, + )=@_; + + my $biblionumber=0; + my $biblioitemnumber=0; + my $sth; + + requireDBI($dbh,"AcceptBiblioitem"); + + my $isbn=$input->param('isbn'); + my $issn=$input->param('issn'); + my $lccn=$input->param('lccn'); + my $q_origisbn=$dbh->quote($input->param('origisbn')); + my $q_origissn=$dbh->quote($input->param('origissn')); + my $q_origlccn=$dbh->quote($input->param('origlccn')); + my $q_origcontrolnumber=$dbh->quote($input->param('origcontrolnumber')); + my $q_isbn=$dbh->quote((($isbn) || ('NIL'))); + my $q_issn=$dbh->quote((($issn) || ('NIL'))); + my $q_lccn=$dbh->quote((($lccn) || ('NIL'))); + my $file=$input->param('file'); + + #my $sth=$dbh->prepare("insert into marcrecorddone values ($q_origisbn, $q_origissn, $q_origlccn, $q_origcontrolnumber)"); + #$sth->execute; + + print "

    \n"; + print "New Record | New File
    \n"; + + # See if it already exists + my $sth=$dbh->prepare("select biblionumber,biblioitemnumber + from biblioitems + where issn=$q_issn or isbn=$q_isbn or lccn=$q_lccn"); + $sth->execute; + if ($sth->rows) { + # Already exists + ($biblionumber, $biblioitemnumber) = $sth->fetchrow; + my $title=$input->param('title'); + print << "EOF"; + + + +
    Record already in database +
    $title is already in the database with + biblionumber $biblionumber and biblioitemnumber $biblioitemnumber +
    +

    +EOF + } else { + + # It doesn't exist; add it. + + my $error; + my %biblio; + my %biblioitem; + + # convert to upper case and split on lines + my $subjectheadings=$input->param('subject'); + my @subjectheadings=split(/[\r\n]+/,$subjectheadings); + + my $additionalauthors=$input->param('additionalauthors'); + my @additionalauthors=split(/[\r\n]+/,uc($additionalauthors)); + + # Use individual assignments to hash buckets, in case + # any of the input parameters are empty or don't exist + $biblio{title} =$input->param('title'); + $biblio{author} =$input->param('author'); + $biblio{copyright} =$input->param('copyrightdate'); + $biblio{seriestitle} =$input->param('seriestitle'); + $biblio{notes} =$input->param('notes'); + $biblio{abstract} =$input->param('abstract'); + $biblio{subtitle} =$input->param('subtitle'); + + $biblioitem{volume} =$input->param('volume'); + $biblioitem{number} =$input->param('number'); + $biblioitem{itemtype} =$input->param('itemtype'); + $biblioitem{isbn} =$input->param('isbn'); + $biblioitem{issn} =$input->param('issn'); + $biblioitem{dewey} =$input->param('dewey'); + $biblioitem{subclass} =$input->param('subclass'); + $biblioitem{publicationyear} =$input->param('publicationyear'); + $biblioitem{publishercode} =$input->param('publishercode'); + $biblioitem{volumedate} =$input->param('volumedate'); + $biblioitem{volumeddesc} =$input->param('volumeddesc'); + $biblioitem{illus} =$input->param('illustrator'); + $biblioitem{pages} =$input->param('pages'); + $biblioitem{notes} =$input->param('notes'); + $biblioitem{size} =$input->param('size'); + $biblioitem{place} =$input->param('place'); + $biblioitem{lccn} =$input->param('lccn'); + $biblioitem{marc} =$input->param('marc'); + + #print "

    subjects=@subjectheadings
    \n"; + #print "
    auth=@additionalauthors
    \n"; + + ($biblionumber, $biblioitemnumber, $error)= + newcompletebiblioitem($dbh, + \%biblio, + \%biblioitem, + \@subjectheadings, + \@additionalauthors + ); + + if ( $error ) { + print "

    Error adding biblio item

    $error\n"; + } else { + + my $title=$input->param('title'); + print << "EOF"; + + + +
    Record entered into database
    $title has been entered into the database with biblionumber + $biblionumber and biblioitemnumber $biblioitemnumber
    +EOF + } # if error + } # if new record + + return $biblionumber,$biblioitemnumber; +} # sub AcceptBiblioitem + +sub ItemCopyForm { + use strict; + my ( + $dbh, + $input, # CGI input object + $biblionumber, + $biblioitemnumber, + )=@_; + + my $sth; + my $barcode; + requireDBI($dbh,"ItemCopyForm"); + + my $title=$input->param('title'); + my $file=$input->param('file'); + + # Get next barcode, or pick random one if none exist yet + $sth=$dbh->prepare("select max(barcode) from items"); + $sth->execute; + ($barcode) = $sth->fetchrow; + $barcode++; + if ($barcode==1) { + $barcode=int(rand()*1000000); + } + + my $branchselect=getkeytableselectoptions( + $dbh, 'branches', 'branchcode', 'branchname', 0); + + print << "EOF"; + + + +
    + Add a New Item for $title +
    + + + + + + + + + + + +
    BARCODE + Home Branch: +
    Replacement Price:
    Notes +
    +

    + + +

    +EOF + +} # sub ItemCopyForm + +#--------------------------------------- +# Accept form data to add an item copy +sub AcceptItemCopy { + use strict; + my ( $dbh, $input )=@_; + + my $error; + + requireDBI($dbh,"AcceptItemCopy"); + + my $barcode=$input->param('barcode'); + my $replacementprice=($input->param('replacementprice') || 0); + + my $sth=$dbh->prepare("select barcode + from items + where barcode=?"); + $sth->execute($barcode); + if ($sth->rows) { + print "Barcode '$barcode' has already been assigned.

    \n"; + } else { + # Insert new item into database + $error=&newitems( + { biblionumber=> $input->param('biblionumber'), + biblioitemnumber=> $input->param('biblioitemnumber'), + itemnotes=> $input->param('notes'), + homebranch=> $input->param('homebranch'), + replacementprice=> $replacementprice, + }, + $barcode + ); + if ( $error ) { + print "Error: $error

    \n"; + } else { + + print " +
    + Item added with barcode $barcode +
    \n"; + } # if error + } # if barcode exists +} # sub AcceptItemCopy #--------------- # Create an HTML option list for a