2 # $Id: z39util.tcl,v 1.12 1995/12/20 16:31:34 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]
132 foreach field $fields {
133 set id [lindex $field 0]
134 set data [lindex $field 1]
136 html " <b>\$$id</b> "
144 proc put-marc-contents {cc} {
146 if {[string first :// $cc] > 0} {
147 foreach urltype {gopher http ftp mailto} {
148 if {[string first ${urltype}:// $cc] == 0} {
163 proc dl-marc-field {zset no tag id la lb sep} {
165 set c [$zset getMarc $no field $tag * $id]
170 if {$len > 1 && "x$lb" != "x"} {
171 html "<dt><b>$lb</b>\n<dd>"
173 html "<dt><b>$la</b>\n<dd>"
179 put-marc-contents $cc
185 proc dd-marc-field {zset no tag id start stop} {
187 set c [$zset getMarc $no field $tag * $id]
194 put-marc-contents $cc
201 proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
203 set lines [$zset getMarc $no line $tag * *]
204 foreach line $lines {
205 foreach field [lindex $line 2] {
207 html "<dt><b>$lead</b>"
210 set id [lindex $field 0]
211 if {$id == $startid} {
217 html [lindex $field 1]
220 html [lindex $field 1]
229 proc display-full {zset no tno} {
230 set type [$zset type $no]
232 set err [lindex [$zset diag $no] 1]
233 set add [lindex [$zset diag $no] 2]
237 html "Error ${err}${add} <br>\n"
243 set rtype [$zset recordType $no]
244 if {$rtype == "SUTRS"} {
245 html [join [$zset getSutrs $no]] "<br>\n"
248 if {[catch {set r [$zset getMarc $no line * * *]}]} {
249 html "Unknown record type: $rtype <br>\n"
253 set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
255 set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
257 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
259 set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
261 set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
263 dd-marc-field $zset $no 245 b "<em>" "</em>"
264 dd-marc-field $zset $no 245 c " " ""
266 dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
268 dl-marc-field $zset $no 520 a "Abstract" {} ", "
269 dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
270 dl-marc-field $zset $no 650 * "Subject" {} ", "
271 dl-marc-field $zset $no 260 * "Publisher" {} " "
272 dl-marc-field $zset $no 300 * "Physical Description" {} " "
274 dl-marc-field-rec $zset $no 500 "Notes" "" "<br>\n" "a" ", "
276 dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
278 dl-marc-field-rec $zset $no 511 "Participant note" "" "<br>\n" "a" ", "
280 dl-marc-field $zset $no 513 a "Report type" {} ", "
281 dl-marc-field $zset $no 513 b "Period covered" {} ", "
282 dl-marc-field-rec $zset $no 515 "Numbering notes" "" "<br>\n" "a" ", "
283 dl-marc-field-rec $zset $no 516 "Data notes" "" "<br>\n" "a" ", "
284 dl-marc-field-rec $zset $no 518 "Date/time notes" "" "<br>\n" "a" ", "
286 dl-marc-field $zset $no 350 a "Price" {} ", "
287 dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
288 dl-marc-field $zset $no 850 a "Holdings" {} ", "
290 dl-marc-field-rec $zset $no 270 "Contact name" "" "<br>\n" p ", "
292 set n [dl-marc-field $zset $no 270 p "Contact name" {} ", "]
297 dl-marc-field $zset $no 270 a "Street" {} ", "
298 dl-marc-field $zset $no 270 b "City" {} ", "
299 dl-marc-field $zset $no 270 c "State" {} ", "
300 dl-marc-field $zset $no 270 e "Zip code" {} ", "
301 dl-marc-field $zset $no 270 d "Country" {} ", "
302 dl-marc-field $zset $no 270 m "Network address" {} ", "
303 dl-marc-field $zset $no 301 a "Service hours" {} ", "
304 dl-marc-field $zset $no 270 k "Phone" {} ", "
305 dl-marc-field $zset $no 270 l "Fax" {} ", "
307 dl-marc-field $zset $no 270 {[abcedmakl]} "Address" {} "<br>\n"
313 dl-marc-field $zset $no 010 a "LC control number" {} ", "
314 dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
315 dl-marc-field $zset $no 020 a "ISBN" {} ", "
316 dl-marc-field $zset $no 022 a "ISSN" {} ", "
317 set url [$zset getMarc $no field 856 * u]
318 set sp [$zset getMarc $no field 856 * 3]
319 if {"x$url" != "x"} {
320 html "<dt><b>URL</b>\n"
324 html {<dd><a href="} $url {">} [join $sp] "</a>\n"
326 dl-marc-field $zset $no 037 {[abc]} "Acquisition" {} "<br>\n"
327 dl-marc-field $zset $no 037 {[f6]} "Form of issue" {} "<br>\n"
328 dl-marc-field $zset $no 537 * "Source of data" {} "<br>\n"
329 dl-marc-field $zset $no 538 * "System details" {} "<br>\n"
330 dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
331 dl-marc-field $zset $no 001 * "Local control number" {} ", "
336 proc display-rec {from to dfunc tno} {
340 while {$from <= $to} {
341 eval "$dfunc z39${tno}.${setNo} $from $tno"
345 while {$from <= $to} {
346 eval "$dfunc z39.${setNo} $from 0"
352 proc build-query {t} {
357 for {set i 1} {$i < 4} {incr i} {
358 set term [wform entry$i]
360 set field [wform menu$i]
361 foreach x [lindex $targets($t) 2] {
362 if {[lindex $x 0] == $field} {
363 set attr [lindex $x 1]
368 { set q "@and $q ${attr} ${term}" }
370 { set q "@or $q ${attr} ${term}" }
372 { set q "@not $q ${attr} ${term}" }
374 { set q "${attr} ${term}" }
376 set op [wform logic$i]
382 proc z39search {setNo piggy tno elements} {
389 set host $hist($setNo,$tno,host)
390 set idAuth $hist($setNo,$tno,idAuthentication)
391 set database $hist($setNo,$tno,database)
392 set query $hist($setNo,$tno,query)
395 set host $hist($setNo,host)
396 set idAuth $hist($setNo,idAuthentication)
397 set database $hist($setNo,database)
398 set query $hist($setNo,query)
400 if {[catch [list $zz failback fail-response]]} {
403 if {[catch [list set oldHost [$zz connect]]]} {
406 $zz callback ok-response
407 $zz failback fail-response
408 if {$oldHost != $host} {
409 catch [list $zz disconnect]
412 if {[catch [list $zz connect $host]]} {
413 displayError "Cannot connect to target" $host
415 } elseif {$sessionWait == 0} {
416 if {[catch {zwait sessionWait 300}]} {
418 displayError "Cannot connect to target" $host
421 if {$sessionWait != 1} {
422 displayError "Cannot connect to target" $host
426 $zz idAuthentication $idAuth
428 if {[catch {$zz init}]} {
429 displayError "Cannot initialize target" $host
433 if {[catch {zwait sessionWait 60}]} {
434 displayError "Cannot initialize target" $host
438 if {$sessionWait != "1"} {
439 displayError "Cannot initialize target" $host
443 if {![$zz initResult]} {
444 set u [$zz userInformationField]
446 displayError "Cannot initialize target $host" $u
450 if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
454 ir-set $zz.$setNo $zz
456 if {![lindex $targets($host) 5]} {
459 $zz.$setNo smallSetElementSetNames $elements
460 $zz.$setNo mediumSetElementSetNames $elements
461 $zz.$setNo recordElements $elements
463 wlog debug "database=$database"
464 eval $zz.$setNo databaseNames $database
466 $zz.$setNo preferredRecordSyntax USMARC
468 $zz callback [list search-response $zz.$setNo]
470 $zz.$setNo largeSetLowerBound 999999
471 $zz.$setNo smallSetUpperBound 0
472 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
474 $zz.$setNo largeSetLowerBound 2
475 $zz.$setNo smallSetUpperBound 0
476 $zz.$setNo mediumSetPresentNumber 0
479 $zz.$setNo search $query
481 if {[catch {zwait sessionWait 600}]} {
482 wlog debug "timeout/cancel in search"
483 displayError "Timeout in search" {}
484 html "</body></html>\n"
489 if {$sessionWait == -1} {
490 displayError "Search fail" "Connection closed"
491 html "</body></html>\n"
494 if {$sessionWait != 1} {
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"