2 # $Id: z39util.tcl,v 1.10 1995/11/14 09:53:16 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)"
51 proc fail-response {} {
56 proc display-brief {zset no tno} {
61 set type [$zset type $no]
63 set err [lindex [$zset diag $no] 1]
64 set add [lindex [$zset diag $no] 2]
68 html "${no} Error ${err}${add} <br>\n"
75 set rtype [$zset recordType $no]
76 if {$rtype == "SUTRS"} {
77 html [join [$zset getSutrs $no]]
82 set title [lindex [$zset getMarc $no field 245 * a] 0]
83 set year [lindex [$zset getMarc $no field 260 * c] 0]
85 html {<a href="http:} $env(SCRIPT_NAME) /
86 html $sessionId {/showfull.egw/} $setNo + $tno + $no + full
87 html {"> } $title {</a>} " <i> ${year} </i>"
92 proc display-raw {zset no tno} {
93 set type [$zset type $no]
95 set err [lindex [$zset diag $no] 1]
96 set add [lindex [$zset diag $no] 2]
100 html "<h3>${no}</h3>\n"
101 html "Error ${err}${add} <br>\n"
107 set rtype [$zset recordType $no]
108 if {$rtype == "SUTRS"} {
109 html [join [$zset getSutrs $no]] "<br>\n"
112 if {[catch {set r [$zset getMarc $no line * * *]}]} {
113 html "Unknown record type: $rtype <br>\n"
117 set tag [lindex $line 0]
118 set indicator [lindex $line 1]
119 set fields [lindex $line 2]
120 set l [string length $indicator]
123 for {set i 0} {$i < $l} {incr i} {
124 if {[string index $indicator $i] == " "} {
127 html [string index $tag $i]
131 foreach field $fields {
132 set id [lindex $field 0]
133 set data [lindex $field 1]
135 html " <b>\$$id</b> "
143 proc put-marc-contents {cc} {
145 if {[string first :// $cc] > 0} {
146 foreach urltype {gopher http ftp mailto} {
147 if {[string first ${urltype}:// $cc] == 0} {
158 html {">} $urltype { reference</a>}
162 proc dl-marc-field {zset no tag id la lb sep} {
164 set c [$zset getMarc $no field $tag * $id]
169 if {$len > 1 && "x$lb" != "x"} {
170 html "<dt><b>$lb</b>\n<dd>"
172 html "<dt><b>$la</b>\n<dd>"
178 put-marc-contents $cc
184 proc dd-marc-field {zset no tag id start stop} {
186 set c [$zset getMarc $no field $tag * $id]
193 put-marc-contents $cc
200 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
202 set lines [$zset getMarc $no line $tag * *]
203 foreach line $lines {
204 foreach field [lindex $line 2] {
206 html "<dt><b>$lead</b>"
209 set id [lindex $field 0]
210 if {$id == $startid} {
216 html [lindex $field 1]
219 html [lindex $field 1]
228 proc display-full {zset no tno} {
229 set type [$zset type $no]
231 set err [lindex [$zset diag $no] 1]
232 set add [lindex [$zset diag $no] 2]
236 html "Error ${err}${add} <br>\n"
242 set rtype [$zset recordType $no]
243 if {$rtype == "SUTRS"} {
244 html [join [$zset getSutrs $no]] "<br>\n"
247 if {[catch {set r [$zset getMarc $no line * * *]}]} {
248 html "Unknown record type: $rtype <br>\n"
252 set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
254 set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
256 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
258 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
260 set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
262 dd-marc-field $zset $no 245 b "<em>" "</em>"
263 dd-marc-field $zset $no 245 c " " ""
265 dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
267 dl-marc-field $zset $no 520 a "Abstract" {} ", "
268 dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
269 dl-marc-field $zset $no 650 * "Subject" {} ", "
270 dl-marc-field $zset $no 260 * "Publisher" {} " "
271 dl-marc-field $zset $no 300 * "Physical Description" {} " "
273 dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
275 dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
277 dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
279 dl-marc-field $zset $no 513 a "Report type" {} ", "
280 dl-marc-field $zset $no 513 b "Period covered" {} ", "
281 dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
282 dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
283 dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
285 dl-marc-field $zset $no 350 a "Price" {} ", "
286 dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
287 dl-marc-field $zset $no 850 a "Holdings" {} ", "
289 dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
291 set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
296 dl-marc-field $zset $no 270 a "Street" {} ", "
297 dl-marc-field $zset $no 270 b "City" {} ", "
298 dl-marc-field $zset $no 270 c "State" {} ", "
299 dl-marc-field $zset $no 270 e "Zip code" {} ", "
300 dl-marc-field $zset $no 270 d "Country" {} ", "
301 dl-marc-field $zset $no 270 m "Network address" {} ", "
302 dl-marc-field $zset $no 301 a "Service hours" {} ", "
303 dl-marc-field $zset $no 270 k "Phone" {} ", "
304 dl-marc-field $zset $no 270 l "Fax" {} ", "
306 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
312 dl-marc-field $zset $no 010 a "LC control number" {} ", "
313 dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
314 dl-marc-field $zset $no 020 a "ISBN" {} ", "
315 dl-marc-field $zset $no 022 a "ISSN" {} ", "
316 set url [$zset getMarc $no field 856 * u]
317 set sp [$zset getMarc $no field 856 * 3]
318 if {"x$url" != "x"} {
319 html "<dt><b>URL</b>\n"
323 html {<dd><a href="} $url {">} [join $sp] "</a>\n"
325 dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
326 dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
327 dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
328 dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
329 dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
330 dl-marc-field $zset $no 001 * "Local control number" {} ", "
335 proc display-rec {from to dfunc tno} {
339 while {$from <= $to} {
340 eval "$dfunc z39${tno}.${setNo} $from $tno"
344 while {$from <= $to} {
345 eval "$dfunc z39.${setNo} $from 0"
351 proc build-query {t} {
356 for {set i 1} {$i < 4} {incr i} {
357 set term [wform entry$i]
359 set field [wform menu$i]
360 foreach x [lindex $targets($t) 2] {
361 if {[lindex $x 0] == $field} {
362 set attr [lindex $x 1]
367 { set q "@and $q ${attr} ${term}" }
369 { set q "@or $q ${attr} ${term}" }
371 { set q "@not $q ${attr} ${term}" }
373 { set q "${attr} ${term}" }
375 set op [wform logic$i]
381 proc z39search {setNo piggy tno elements} {
388 set host $hist($setNo,$tno,host)
389 set idAuth $hist($setNo,$tno,idAuthentication)
390 set database $hist($setNo,$tno,database)
391 set query $hist($setNo,$tno,query)
394 set host $hist($setNo,host)
395 set idAuth $hist($setNo,idAuthentication)
396 set database $hist($setNo,database)
397 set query $hist($setNo,query)
399 if {[catch [list $zz failback fail-response]]} {
402 if {[catch [list set oldHost [$zz connect]]]} {
405 $zz callback ok-response
406 $zz failback fail-response
407 if {$oldHost != $host} {
408 catch [list $zz disconnect]
411 if {[catch [list $zz connect $host]]} {
412 displayError "Cannot connect to target" $host
414 } elseif {$sessionWait == 0} {
416 if {$sessionWait != 1} {
417 displayError "Cannot connect to target" $host
421 $zz idAuthentication $idAuth
423 if {[catch {$zz init}]} {
424 displayError "Cannot initialize target" $host
427 if {[catch {zwait sessionWait 60}]} {
428 displayError "Cannot initialize target" $host
432 if {$sessionWait != "1"} {
433 displayError "Cannot initialize target" $host
437 if {![$zz initResult]} {
438 set u [$zz userInformationField]
440 displayError "Cannot initialize target $host" $u
444 if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
448 ir-set $zz.$setNo $zz
450 if {![lindex $targets($host) 5]} {
453 $zz.$setNo smallSetElementSetNames $elements
454 $zz.$setNo mediumSetElementSetNames $elements
455 $zz.$setNo recordElements $elements
457 eval $zz.$setNo databaseNames $database
459 $zz.$setNo preferredRecordSyntax USMARC
461 $zz callback [list search-response $zz.$setNo]
463 $zz.$setNo largeSetLowerBound 999999
464 $zz.$setNo smallSetUpperBound 0
465 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
467 $zz.$setNo largeSetLowerBound 2
468 $zz.$setNo smallSetUpperBound 0
469 $zz.$setNo mediumSetPresentNumber 0
472 $zz.$setNo search $query
474 if {[catch {zwait sessionWait 600}]} {
475 wlog debug "timeout/cancel in present"
476 displayError "Timeout in search" {}
477 html "</body></html>\n"
482 if {$sessionWait != 1} {
483 displayError "Search fail" "Connection closed"
484 html "</body></html>\n"
488 set status [$zz.$setNo responseStatus]
489 if {[lindex $status 0] == "NSD"} {
490 set code [lindex $status 1]
491 set msg [lindex $status 2]
492 set addinfo [lindex $status 3]
493 displayError "Diagnostic message" \
494 "$msg: $addinfo\n<br>(error code $code)"
497 set hist($setNo,hits) [$zz.$setNo resultCount]
501 proc init-m-response {i} {
505 wlog debug "init-m-response"
511 proc connect-m-response {i} {
515 wlog debug "connect-m-response"
516 z39$i callback [list init-m-response $i]
517 if {[catch {z39$i init}]} {
523 proc fail-m-response {i} {
527 wlog debug "fail-m-response"
532 proc search-m-response {setNo i} {
540 proc z39msearch {setNo piggy elements} {
546 set not $hist($setNo,0,host)
548 for {set i 1} {$i <= $not} {incr i} {
549 set host $hist($setNo,$i,host)
550 if {[catch {z39 failback fail-response}]} {
553 if {[catch {set oldHost [z39$i connect]}]} {
556 if {$oldHost != $host} {
557 catch {z39$i disconnect}
559 z39$i callback [list connect-m-response $i]
560 z39$i failback [list fail-m-response $i]
563 for {set i 1} {$i <= $not} {incr i} {
564 set oldHost [z39$i connect]
565 set host $hist($setNo,$i,host)
566 if {$oldHost == $host} {
570 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
571 html "Connecting to target " $host " <br>\n"
573 if {![catch {z39$i connect $host}]} {
578 wlog debug "Waiting for init response"
579 if {[catch {zwait zleft 10}]} {
584 for {set i 1} {$i <= $not} {incr i} {
585 html "host " $hist($setNo,$i,host) ": "
586 if {$zstatus($i) >= 1} {
588 ir-set z39$i.$setNo z39$i
589 set hist($setNo,$i,offset) 0
590 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
592 if {![lindex $targets($hist($setNo,$i,host)) 5]} {
595 set thisElements $elements
597 z39$i.$setNo smallSetElementSetNames $thisElements
598 z39$i.$setNo mediumSetElementSetNames $thisElements
599 z39$i.$setNo recordElements $thisElements
601 z39$i.$setNo preferredRecordSyntax USMARC
602 z39$i callback [list search-m-response $setNo $i]
605 z39$i.$setNo largeSetLowerBound 999999
606 z39$i.$setNo smallSetUpperBound 0
607 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
609 z39$i.$setNo largeSetLowerBound 2
610 z39$i.$setNo smallSetUpperBound 0
611 z39$i.$setNo mediumSetPresentNumber 0
614 wlog debug "search " $hist($setNo,$i,query)
615 z39$i.$setNo search $hist($setNo,$i,query)
622 wlog debug "Waiting for search response"
623 if {[catch {zwait zleft 30}]} {
627 for {set i 1} {$i <= $not} {incr i} {
628 if {$zstatus($i) != 2} continue
629 set status [z39$i.$setNo responseStatus]
630 if {[lindex $status 0] != "NSD"} {
631 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
636 proc z39present {setNo tno setOffset setMax dfunc elements} {
643 set host $hist($setNo,$tno,host)
646 set host $hist($setNo,host)
649 if {![lindex $targets($host) 5]} {
653 $zz.$setNo elementSetNames $elements
654 $zz.$setNo recordElements $elements
655 set toGet [expr 1 + $setMax - $setOffset]
657 $zz callback [list search-response $zz.$setNo]
659 while {$setMax > 0 && $toGet > 0} {
660 for {set got 0} {$got < $toGet} {incr got} {
661 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
667 $zz.$setNo present $setOffset $toGet
668 if {[catch {zwait sessionWait 300}]} {
669 wlog debug "timeout/cancel in present"
673 if {$sessionWait == "0"} {
676 if {$sessionWait != "1"} {
679 set got [$zz.$setNo numberOfRecordsReturned]
684 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
685 set setOffset [expr $got + $setOffset]
686 set toGet [expr 1 + $setMax - $setOffset]
698 if {![info exists nextSetNo]} {
701 html "<hr><h3>History</h3><dl>\n"
702 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
703 html {<dt> <a href="http:} $env(SCRIPT_NAME)
704 html / $sessionId {/search.egw/} $setNo + 1
705 html + $hist($setNo,maxPresent)
706 html {"> } [lindex $targets($hist($setNo,host)) 0]
707 if {[llength $hist($setNo,database)] > 1} {
709 foreach b $hist($setNo,database) {
715 if {[info exists hist($setNo,hits)]} {
716 html $hist($setNo,hits) " hits"
725 proc displayError {msga msgb} {
727 html {<img src="/gif/noway.gif">}
728 html "<h2>" $msga "</h2>\n"
730 html "<h3>" $msgb "</h3>\n"
732 html "</center><p>\n"