2 # $Id: z39util.tcl,v 1.13 1995/12/21 15:49:53 adam Exp $
6 set f [open "tcl.state.${sessionId}" w]
7 foreach var [info globals] {
8 if {$var == "f"} continue
9 if {$var == "sessionId"} continue
10 if {$var == "errorInfo"} continue
11 if {[catch {set names [array names $var]}]} {
13 puts $f "set ${var} \{$v\}"
16 eval "set v \$${var}(\$n)"
17 puts $f "set ${var}($n) \{$v\}"
21 puts $f "set ${var} \{$v\}"
29 proc search-response {zz} {
32 set status [$zz responseStatus]
33 if {[lindex $status 0] == "NSD"} {
34 $zz nextResultSetPosition 0
35 set code [lindex $status 1]
36 set msg [lindex $status 2]
37 set addinfo [lindex $status 3]
38 displayError "Diagnostic message" \
39 "$msg: $addinfo<br>\n(error code $code)"
46 proc scan-response {zz} {
49 set status [$zz scanStatus]
51 displayError "Scan fail" ""
63 proc fail-response {} {
68 proc display-brief {zset no tno} {
73 set type [$zset type $no]
75 set err [lindex [$zset diag $no] 1]
76 set add [lindex [$zset diag $no] 2]
80 html "${no} Error ${err}${add} <br>\n"
87 set rtype [$zset recordType $no]
88 if {$rtype == "SUTRS"} {
89 html [join [$zset getSutrs $no]]
94 set title [lindex [$zset getMarc $no field 245 * a] 0]
95 set year [lindex [$zset getMarc $no field 260 * c] 0]
97 html {<a href="http:} $env(SCRIPT_NAME) /
98 html $sessionId {/showfull.egw/} $setNo + $tno + $no + full
99 html {"> } $title {</a>} " <i> ${year} </i>"
104 proc display-raw {zset no tno} {
105 set type [$zset type $no]
107 set err [lindex [$zset diag $no] 1]
108 set add [lindex [$zset diag $no] 2]
112 html "<h3>${no}</h3>\n"
113 html "Error ${err}${add} <br>\n"
119 set rtype [$zset recordType $no]
120 if {$rtype == "SUTRS"} {
121 html [join [$zset getSutrs $no]] "<br>\n"
124 if {[catch {set r [$zset getMarc $no line * * *]}]} {
125 html "Unknown record type: $rtype <br>\n"
129 set tag [lindex $line 0]
130 set indicator [lindex $line 1]
131 set fields [lindex $line 2]
132 set l [string length $indicator]
135 for {set i 0} {$i < $l} {incr i} {
136 if {[string index $indicator $i] == " "} {
139 html [string index $tag $i]
144 foreach field $fields {
145 set id [lindex $field 0]
146 set data [lindex $field 1]
148 html " <b>\$$id</b> "
156 proc put-marc-contents {cc} {
158 if {[string first :// $cc] > 0} {
159 foreach urltype {gopher http ftp mailto} {
160 if {[string first ${urltype}:// $cc] == 0} {
175 proc dl-marc-field {zset no tag id la lb sep} {
177 set c [$zset getMarc $no field $tag * $id]
182 if {$len > 1 && "x$lb" != "x"} {
183 html "<dt><b>$lb</b>\n<dd>"
185 html "<dt><b>$la</b>\n<dd>"
191 put-marc-contents $cc
197 proc dd-marc-field {zset no tag id start stop} {
199 set c [$zset getMarc $no field $tag * $id]
206 put-marc-contents $cc
213 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
215 set lines [$zset getMarc $no line $tag * *]
216 foreach line $lines {
217 foreach field [lindex $line 2] {
219 html "<dt><b>$lead</b>"
222 set id [lindex $field 0]
223 if {$id == $startid} {
229 html [lindex $field 1]
232 html [lindex $field 1]
241 proc display-full {zset no tno} {
242 set type [$zset type $no]
244 set err [lindex [$zset diag $no] 1]
245 set add [lindex [$zset diag $no] 2]
249 html "Error ${err}${add} <br>\n"
255 set rtype [$zset recordType $no]
256 if {$rtype == "SUTRS"} {
257 html [join [$zset getSutrs $no]] "<br>\n"
260 if {[catch {set r [$zset getMarc $no line * * *]}]} {
261 html "Unknown record type: $rtype <br>\n"
265 set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
267 set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
269 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
271 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
273 set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
275 dd-marc-field $zset $no 245 b "<em>" "</em>"
276 dd-marc-field $zset $no 245 c " " ""
278 dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
280 dl-marc-field $zset $no 520 a "Abstract" {} ", "
281 dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
282 dl-marc-field $zset $no 650 * "Subject" {} ", "
283 dl-marc-field $zset $no 260 * "Publisher" {} " "
284 dl-marc-field $zset $no 300 * "Physical Description" {} " "
286 dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
288 dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
290 dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
292 dl-marc-field $zset $no 513 a "Report type" {} ", "
293 dl-marc-field $zset $no 513 b "Period covered" {} ", "
294 dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
295 dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
296 dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
298 dl-marc-field $zset $no 350 a "Price" {} ", "
299 dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
300 dl-marc-field $zset $no 850 a "Holdings" {} ", "
302 dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
304 set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
309 dl-marc-field $zset $no 270 a "Street" {} ", "
310 dl-marc-field $zset $no 270 b "City" {} ", "
311 dl-marc-field $zset $no 270 c "State" {} ", "
312 dl-marc-field $zset $no 270 e "Zip code" {} ", "
313 dl-marc-field $zset $no 270 d "Country" {} ", "
314 dl-marc-field $zset $no 270 m "Network address" {} ", "
315 dl-marc-field $zset $no 301 a "Service hours" {} ", "
316 dl-marc-field $zset $no 270 k "Phone" {} ", "
317 dl-marc-field $zset $no 270 l "Fax" {} ", "
319 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
325 dl-marc-field $zset $no 010 a "LC control number" {} ", "
326 dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
327 dl-marc-field $zset $no 020 a "ISBN" {} ", "
328 dl-marc-field $zset $no 022 a "ISSN" {} ", "
329 set url [$zset getMarc $no field 856 * u]
330 set sp [$zset getMarc $no field 856 * 3]
331 if {"x$url" != "x"} {
332 html "<dt><b>URL</b>\n"
336 html {<dd><a href="} $url {">} [join $sp] "</a>\n"
338 dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
339 dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
340 dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
341 dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
342 dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
343 dl-marc-field $zset $no 001 * "Local control number" {} ", "
348 proc display-rec {from to dfunc tno} {
352 while {$from <= $to} {
353 eval "$dfunc z39${tno}.${setNo} $from $tno"
357 while {$from <= $to} {
358 eval "$dfunc z39.${setNo} $from 0"
364 proc build-scan {t ilines} {
367 for {set i 1} {$i <= $ilines} {incr i} {
368 set term [wform entry$i]
370 set field [wform menu$i]
371 foreach x [lindex $targets($t) 2] {
372 if {[lindex $x 0] == $field} {
373 set attr [lindex $x 1]
376 return [list $term $attr]
382 proc build-query {t ilines} {
387 for {set i 1} {$i <= $ilines} {incr i} {
388 set term [wform entry$i]
390 set field [wform menu$i]
391 foreach x [lindex $targets($t) 2] {
392 if {[lindex $x 0] == $field} {
393 set attr [lindex $x 1]
398 { set q "@and $q ${attr} ${term}" }
400 { set q "@or $q ${attr} ${term}" }
402 { set q "@not $q ${attr} ${term}" }
404 { set q "${attr} ${term}" }
406 set op [wform logic$i]
412 proc z39scan {setNo scanNo tno scanLines scanPos} {
419 set host $hist($setNo,$tno,host)
420 set idAuth $hist($setNo,$tno,idAuthentication)
421 set database $hist($setNo,$tno,database)
422 set scanAttr $hist($setNo,$tno,scanAttr)
423 set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
426 set host $hist($setNo,host)
427 set idAuth $hist($setNo,idAuthentication)
428 set database $hist($setNo,database)
429 set scanAttr $hist($setNo,scanAttr)
430 set scanTerm $hist($setNo,$scanNo,scanTerm)
432 if {[catch [list $zz failback fail-response]]} {
435 if {[catch [list set oldHost [$zz connect]]]} {
438 set zs $zz.s$scanNo.$setNo
439 $zz callback ok-response
440 $zz failback fail-response
441 if {$oldHost != $host} {
442 catch [list $zz disconnect]
445 if {[catch [list $zz connect $host]]} {
446 displayError "Cannot connect to target" $host
448 } elseif {$sessionWait == 0} {
449 if {[catch {zwait sessionWait 300}]} {
451 displayError "Cannot connect to target" $host
454 if {$sessionWait != 1} {
455 displayError "Cannot connect to target" $host
459 $zz idAuthentication $idAuth
461 if {[catch {$zz init}]} {
462 displayError "Cannot initialize target" $host
466 if {[catch {zwait sessionWait 60}]} {
467 displayError "Cannot initialize target" $host
471 if {$sessionWait != "1"} {
472 displayError "Cannot initialize target" $host
476 if {![$zz initResult]} {
477 set u [$zz userInformationField]
479 displayError "Cannot initialize target $host" $u
483 if {![catch [list $zs numberOfTermsRequested 5]]} {
487 eval $zz databaseNames $database
491 $zs numberOfTermsRequested $scanLines
492 $zs preferredPositionInResponse $scanPos
494 $zz callback [list scan-response $zs]
497 $zs scan "${scanAttr} ${scanTerm}"
499 if {[catch {zwait sessionWait 600}]} {
500 wlog debug "timeout/cancel in scan"
501 displayError "Timeout in scan" {}
502 html "</body></html>\n"
506 if {$sessionWait == -1} {
507 displayError "Scan fail" "Connection closed"
508 html "</body></html>\n"
511 if {$sessionWait != 1} {
517 proc display-scan {setNo scanNo tno} {
526 set zs $zz.s$scanNo.$setNo
527 set m [$zs numberOfEntriesReturned]
530 for {set i 0} {$i < $m} {incr i} {
532 html [lindex [$zs scanLine $i] 1]
534 html [lindex [$zs scanLine $i] 2]
540 proc z39search {setNo piggy tno elements} {
547 set host $hist($setNo,$tno,host)
548 set idAuth $hist($setNo,$tno,idAuthentication)
549 set database $hist($setNo,$tno,database)
550 set query $hist($setNo,$tno,query)
553 set host $hist($setNo,host)
554 set idAuth $hist($setNo,idAuthentication)
555 set database $hist($setNo,database)
556 set query $hist($setNo,query)
558 if {[catch [list $zz failback fail-response]]} {
561 if {[catch [list set oldHost [$zz connect]]]} {
564 $zz callback ok-response
565 $zz failback fail-response
566 if {$oldHost != $host} {
567 catch [list $zz disconnect]
570 if {[catch [list $zz connect $host]]} {
571 displayError "Cannot connect to target" $host
573 } elseif {$sessionWait == 0} {
574 if {[catch {zwait sessionWait 300}]} {
576 displayError "Cannot connect to target" $host
579 if {$sessionWait != 1} {
580 displayError "Cannot connect to target" $host
584 $zz idAuthentication $idAuth
586 if {[catch {$zz init}]} {
587 displayError "Cannot initialize target" $host
591 if {[catch {zwait sessionWait 60}]} {
592 displayError "Cannot initialize target" $host
596 if {$sessionWait != "1"} {
597 displayError "Cannot initialize target" $host
601 if {![$zz initResult]} {
602 set u [$zz userInformationField]
604 displayError "Cannot initialize target $host" $u
608 if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
612 ir-set $zz.$setNo $zz
614 if {![lindex $targets($host) 5]} {
617 $zz.$setNo smallSetElementSetNames $elements
618 $zz.$setNo mediumSetElementSetNames $elements
619 $zz.$setNo recordElements $elements
621 wlog debug "database=$database"
622 eval $zz.$setNo databaseNames $database
624 $zz.$setNo preferredRecordSyntax USMARC
626 $zz callback [list search-response $zz.$setNo]
628 $zz.$setNo largeSetLowerBound 999999
629 $zz.$setNo smallSetUpperBound 0
630 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
632 $zz.$setNo largeSetLowerBound 2
633 $zz.$setNo smallSetUpperBound 0
634 $zz.$setNo mediumSetPresentNumber 0
637 $zz.$setNo search $query
639 if {[catch {zwait sessionWait 600}]} {
640 wlog debug "timeout/cancel in search"
641 displayError "Timeout in search" {}
642 html "</body></html>\n"
647 if {$sessionWait == -1} {
648 displayError "Search fail" "Connection closed"
649 html "</body></html>\n"
652 if {$sessionWait != 1} {
655 set hist($setNo,hits) [$zz.$setNo resultCount]
659 proc init-m-response {i} {
663 wlog debug "init-m-response"
669 proc connect-m-response {i} {
673 wlog debug "connect-m-response"
674 z39$i callback [list init-m-response $i]
675 if {[catch {z39$i init}]} {
681 proc fail-m-response {i} {
685 wlog debug "fail-m-response"
690 proc search-m-response {setNo i} {
698 proc z39msearch {setNo piggy elements} {
704 set not $hist($setNo,0,host)
706 for {set i 1} {$i <= $not} {incr i} {
707 set host $hist($setNo,$i,host)
708 if {[catch {z39 failback fail-response}]} {
711 if {[catch {set oldHost [z39$i connect]}]} {
714 if {$oldHost != $host} {
715 catch {z39$i disconnect}
717 z39$i callback [list connect-m-response $i]
718 z39$i failback [list fail-m-response $i]
721 for {set i 1} {$i <= $not} {incr i} {
722 set oldHost [z39$i connect]
723 set host $hist($setNo,$i,host)
724 if {$oldHost == $host} {
728 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
729 html "Connecting to target " $host " <br>\n"
731 if {![catch {z39$i connect $host}]} {
736 wlog debug "Waiting for init response"
737 if {[catch {zwait zleft 10}]} {
742 for {set i 1} {$i <= $not} {incr i} {
743 html "host " $hist($setNo,$i,host) ": "
744 if {$zstatus($i) >= 1} {
746 ir-set z39$i.$setNo z39$i
747 set hist($setNo,$i,offset) 0
748 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
750 if {![lindex $targets($hist($setNo,$i,host)) 5]} {
753 set thisElements $elements
755 z39$i.$setNo smallSetElementSetNames $thisElements
756 z39$i.$setNo mediumSetElementSetNames $thisElements
757 z39$i.$setNo recordElements $thisElements
759 z39$i.$setNo preferredRecordSyntax USMARC
760 z39$i callback [list search-m-response $setNo $i]
763 z39$i.$setNo largeSetLowerBound 999999
764 z39$i.$setNo smallSetUpperBound 0
765 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
767 z39$i.$setNo largeSetLowerBound 2
768 z39$i.$setNo smallSetUpperBound 0
769 z39$i.$setNo mediumSetPresentNumber 0
772 wlog debug "search " $hist($setNo,$i,query)
773 z39$i.$setNo search $hist($setNo,$i,query)
780 wlog debug "Waiting for search response"
781 if {[catch {zwait zleft 30}]} {
785 for {set i 1} {$i <= $not} {incr i} {
786 if {$zstatus($i) != 2} continue
787 set status [z39$i.$setNo responseStatus]
788 if {[lindex $status 0] != "NSD"} {
789 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
794 proc z39present {setNo tno setOffset setMax dfunc elements} {
801 set host $hist($setNo,$tno,host)
804 set host $hist($setNo,host)
807 if {![lindex $targets($host) 5]} {
811 $zz.$setNo elementSetNames $elements
812 $zz.$setNo recordElements $elements
813 set toGet [expr 1 + $setMax - $setOffset]
815 $zz callback [list search-response $zz.$setNo]
817 while {$setMax > 0 && $toGet > 0} {
818 for {set got 0} {$got < $toGet} {incr got} {
819 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
825 $zz.$setNo present $setOffset $toGet
826 if {[catch {zwait sessionWait 300}]} {
827 wlog debug "timeout/cancel in present"
831 if {$sessionWait == "0"} {
834 if {$sessionWait != "1"} {
837 set got [$zz.$setNo numberOfRecordsReturned]
842 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
843 set setOffset [expr $got + $setOffset]
844 set toGet [expr 1 + $setMax - $setOffset]
856 if {![info exists nextSetNo]} {
859 html "<hr><h3>History</h3><dl>\n"
860 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
861 html {<dt> <a href="http:} $env(SCRIPT_NAME)
862 html / $sessionId {/search.egw/} $setNo + 1
863 html + $hist($setNo,maxPresent)
864 html {"> } [lindex $targets($hist($setNo,host)) 0]
865 if {[llength $hist($setNo,database)] > 1} {
867 foreach b $hist($setNo,database) {
873 if {[info exists hist($setNo,hits)]} {
874 html $hist($setNo,hits) " hits"
883 proc displayError {msga msgb} {
885 html {<img src="/gif/noway.gif">}
886 html "<h2>" $msga "</h2>\n"
888 html "<h3>" $msgb "</h3>\n"
890 html "</center><p>\n"