2 # $Id: z39util.tcl,v 1.16 1996/01/03 08:59:45 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 i} {
367 set term [wform entry$i]
369 set field [wform menu$i]
370 foreach x [lindex $targets($t) 2] {
371 if {[lindex $x 0] == $field} {
372 set attr [lindex $x 1]
375 return [list $term $attr]
380 proc build-query {t ilines} {
385 for {set i 1} {$i <= $ilines} {incr i} {
386 set term [wform entry$i]
388 set field [wform menu$i]
389 foreach x [lindex $targets($t) 2] {
390 if {[lindex $x 0] == $field} {
391 set attr [lindex $x 1]
396 { set q "@and $q ${attr} ${term}" }
398 { set q "@or $q ${attr} ${term}" }
400 { set q "@not $q ${attr} ${term}" }
402 { set q "${attr} ${term}" }
404 set op [wform logic$i]
410 proc z39scan {setNo scanNo tno scanLines scanPos cache} {
417 set host $hist($setNo,$tno,host)
418 set idAuth $hist($setNo,$tno,idAuthentication)
419 set database $hist($setNo,$tno,database)
420 set scanAttr $hist($setNo,$tno,scanAttr)
421 set scanTerm $hist($setNo,$tno,$scanNo,scanTerm)
424 set host $hist($setNo,host)
425 set idAuth $hist($setNo,idAuthentication)
426 set database $hist($setNo,database)
427 set scanAttr $hist($setNo,scanAttr)
428 set scanTerm $hist($setNo,$scanNo,scanTerm)
430 if {[catch [list $zz failback fail-response]]} {
433 if {[catch [list set oldHost [$zz connect]]]} {
436 set zs $zz.s$scanNo.$setNo
437 $zz callback ok-response
438 $zz failback fail-response
439 if {$oldHost != $host} {
440 catch [list $zz disconnect]
443 if {[catch [list $zz connect $host]]} {
444 displayError "Cannot connect to target" $host
446 } elseif {$sessionWait == 0} {
447 if {[catch {zwait sessionWait 300}]} {
449 displayError "Cannot connect to target" $host
452 if {$sessionWait != 1} {
453 displayError "Cannot connect to target" $host
457 $zz idAuthentication $idAuth
459 if {[catch {$zz init}]} {
460 displayError "Cannot initialize target" $host
464 if {[catch {zwait sessionWait 60}]} {
465 displayError "Cannot initialize target" $host
469 if {$sessionWait != "1"} {
470 displayError "Cannot initialize target" $host
474 if {![$zz initResult]} {
475 set u [$zz userInformationField]
477 displayError "Cannot initialize target $host" $u
481 if {$cache && ![catch [list $zs numberOfTermsRequested 5]]} {
485 eval $zz databaseNames $database
489 $zs numberOfTermsRequested $scanLines
490 $zs preferredPositionInResponse $scanPos
492 $zz callback [list scan-response $zs]
495 $zs scan "${scanAttr} ${scanTerm}"
497 if {[catch {zwait sessionWait 600}]} {
498 wlog debug "timeout/cancel in scan"
499 displayError "Timeout in scan" {}
500 html "</body></html>\n"
504 if {$sessionWait == -1} {
505 displayError "Scan fail" "Connection closed"
506 html "</body></html>\n"
509 if {$sessionWait != 1} {
515 proc display-scan {setNo scanNo tno} {
526 set zs $zz.s$scanNo.$setNo
527 set m [$zs numberOfEntriesReturned]
530 set t [lindex [$zs scanLine 0] 1]
532 set hist($setNo,$tno,[expr $scanNo - 1],scanTerm) $t
534 set hist($setNo,[expr $scanNo - 1],scanTerm) $t
536 set t [lindex [$zs scanLine [expr $m - 1]] 1]
538 set hist($setNo,$tno,[expr $scanNo + 1],scanTerm) $t
540 set hist($setNo,[expr $scanNo + 1],scanTerm) $t
543 for {set i 0} {$i < $m} {incr i} {
544 html {<a href="http:} $env(SCRIPT_NAME)
545 html / $sessionId {/query.egw/} $hist($setNo,host) + $setNo +
546 html $hist($setNo,scan) + [lindex [$zs scanLine $i] 1] {">}
547 html [lindex [$zs scanLine $i] 1]
549 html [lindex [$zs scanLine $i] 2]
554 proc z39search {setNo piggy tno elements} {
561 set host $hist($setNo,$tno,host)
562 set idAuth $hist($setNo,$tno,idAuthentication)
563 set database $hist($setNo,$tno,database)
564 set query $hist($setNo,$tno,query)
567 set host $hist($setNo,host)
568 set idAuth $hist($setNo,idAuthentication)
569 set database $hist($setNo,database)
570 set query $hist($setNo,query)
572 if {[catch [list $zz failback fail-response]]} {
575 if {[catch [list set oldHost [$zz connect]]]} {
578 $zz callback ok-response
579 $zz failback fail-response
580 if {$oldHost != $host} {
581 catch [list $zz disconnect]
584 if {[catch [list $zz connect $host]]} {
585 displayError "Cannot connect to target" $host
587 } elseif {$sessionWait == 0} {
588 if {[catch {zwait sessionWait 300}]} {
590 displayError "Cannot connect to target" $host
593 if {$sessionWait != 1} {
594 displayError "Cannot connect to target" $host
598 $zz idAuthentication $idAuth
600 if {[catch {$zz init}]} {
601 displayError "Cannot initialize target" $host
605 if {[catch {zwait sessionWait 60}]} {
606 displayError "Cannot initialize target" $host
610 if {$sessionWait != "1"} {
611 displayError "Cannot initialize target" $host
615 if {![$zz initResult]} {
616 set u [$zz userInformationField]
618 displayError "Cannot initialize target $host" $u
622 if {[info exists hist($setNo,hits)] && \
623 ![catch [list $zz.$setNo smallSetUpperBound 0]]} {
628 ir-set $zz.$setNo $zz
630 if {![lindex $targets($host) 5]} {
633 $zz.$setNo smallSetElementSetNames $elements
634 $zz.$setNo mediumSetElementSetNames $elements
635 $zz.$setNo recordElements $elements
637 wlog debug "database=$database"
638 eval $zz.$setNo databaseNames $database
640 $zz.$setNo preferredRecordSyntax USMARC
642 $zz callback [list search-response $zz.$setNo]
644 $zz.$setNo largeSetLowerBound 999999
645 $zz.$setNo smallSetUpperBound 0
646 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
648 $zz.$setNo largeSetLowerBound 2
649 $zz.$setNo smallSetUpperBound 0
650 $zz.$setNo mediumSetPresentNumber 0
653 $zz.$setNo search $query
655 if {[catch {zwait sessionWait 600}]} {
656 wlog debug "timeout/cancel in search"
657 displayError "Timeout in search" {}
658 html "</body></html>\n"
663 if {$sessionWait == -1} {
664 displayError "Search fail" "Connection closed"
665 html "</body></html>\n"
668 if {$sessionWait != 1} {
671 set hist($setNo,hits) [$zz.$setNo resultCount]
675 proc init-m-response {i} {
679 wlog debug "init-m-response"
685 proc connect-m-response {i} {
689 wlog debug "connect-m-response"
690 z39$i callback [list init-m-response $i]
691 if {[catch {z39$i init}]} {
697 proc fail-m-response {i} {
701 wlog debug "fail-m-response"
706 proc search-m-response {setNo i} {
714 proc z39msearch {setNo piggy elements} {
720 set not $hist($setNo,0,host)
722 for {set i 1} {$i <= $not} {incr i} {
723 set host $hist($setNo,$i,host)
724 if {[catch {z39 failback fail-response}]} {
727 if {[catch {set oldHost [z39$i connect]}]} {
730 if {$oldHost != $host} {
731 catch {z39$i disconnect}
733 z39$i callback [list connect-m-response $i]
734 z39$i failback [list fail-m-response $i]
737 for {set i 1} {$i <= $not} {incr i} {
738 set oldHost [z39$i connect]
739 set host $hist($setNo,$i,host)
740 if {$oldHost == $host} {
744 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
745 html "Connecting to target " $host " <br>\n"
747 if {![catch {z39$i connect $host}]} {
752 wlog debug "Waiting for init response"
753 if {[catch {zwait zleft 10}]} {
758 for {set i 1} {$i <= $not} {incr i} {
759 html "host " $hist($setNo,$i,host) ": "
760 if {$zstatus($i) >= 1} {
762 ir-set z39$i.$setNo z39$i
763 set hist($setNo,$i,offset) 0
764 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
766 if {![lindex $targets($hist($setNo,$i,host)) 5]} {
769 set thisElements $elements
771 z39$i.$setNo smallSetElementSetNames $thisElements
772 z39$i.$setNo mediumSetElementSetNames $thisElements
773 z39$i.$setNo recordElements $thisElements
775 z39$i.$setNo preferredRecordSyntax USMARC
776 z39$i callback [list search-m-response $setNo $i]
779 z39$i.$setNo largeSetLowerBound 999999
780 z39$i.$setNo smallSetUpperBound 0
781 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
783 z39$i.$setNo largeSetLowerBound 2
784 z39$i.$setNo smallSetUpperBound 0
785 z39$i.$setNo mediumSetPresentNumber 0
788 wlog debug "search " $hist($setNo,$i,query)
789 z39$i.$setNo search $hist($setNo,$i,query)
796 wlog debug "Waiting for search response"
797 if {[catch {zwait zleft 30}]} {
801 for {set i 1} {$i <= $not} {incr i} {
802 if {$zstatus($i) != 2} continue
803 set status [z39$i.$setNo responseStatus]
804 if {[lindex $status 0] != "NSD"} {
805 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
810 proc z39present {setNo tno setOffset setMax dfunc elements} {
817 set host $hist($setNo,$tno,host)
820 set host $hist($setNo,host)
823 if {![lindex $targets($host) 5]} {
827 $zz.$setNo elementSetNames $elements
828 $zz.$setNo recordElements $elements
829 set toGet [expr 1 + $setMax - $setOffset]
831 $zz callback [list search-response $zz.$setNo]
833 while {$setMax > 0 && $toGet > 0} {
834 for {set got 0} {$got < $toGet} {incr got} {
835 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
841 $zz.$setNo present $setOffset $toGet
842 if {[catch {zwait sessionWait 300}]} {
843 wlog debug "timeout/cancel in present"
847 if {$sessionWait == "0"} {
850 if {$sessionWait != "1"} {
853 set got [$zz.$setNo numberOfRecordsReturned]
858 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
859 set setOffset [expr $got + $setOffset]
860 set toGet [expr 1 + $setMax - $setOffset]
872 if {![info exists nextSetNo]} {
875 html "<hr><h2>History</h2><dl><br>\n"
876 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
877 html {<dt> <a href="http:} $env(SCRIPT_NAME)
878 html / $sessionId {/search.egw/} $setNo + 1
879 html + $hist($setNo,maxPresent)
880 html {"> } [lindex $targets($hist($setNo,host)) 0]
881 if {[llength $hist($setNo,database)] > 1} {
883 foreach b $hist($setNo,database) {
889 if {[info exists hist($setNo,hits)]} {
890 html $hist($setNo,hits) " hits"
899 proc displayError {msga msgb} {
901 html {<img src="/gif/noway.gif" alt="Error">}
902 html "<h2>" $msga "</h2>\n"
904 html "<h3>" $msgb "</h3>\n"
906 html "</center><p>\n"