2 # $Id: z39util.tcl,v 1.9 1995/11/13 18:17:48 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 {sno} {
32 set status [z39.$sno responseStatus]
33 if {[lindex $status 0] == "NSD"} {
34 z39.$sno 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} {
387 set host $hist($setNo,$tno,host)
388 set idAuth $hist($setNo,$tno,idAuthentication)
389 set database $hist($setNo,$tno,database)
390 set query $hist($setNo,$tno,query)
393 set host $hist($setNo,host)
394 set idAuth $hist($setNo,idAuthentication)
395 set database $hist($setNo,database)
396 set query $hist($setNo,query)
398 if {[catch [list $zz failback fail-response]]} {
401 if {[catch [list set oldHost [$zz connect]]]} {
404 $zz callback ok-response
405 $zz failback fail-response
406 if {$oldHost != $host} {
407 catch [list $zz disconnect]
410 if {[catch [list $zz connect $host]]} {
411 displayError "Cannot connect to target" $host
413 } elseif {$sessionWait == 0} {
415 if {$sessionWait != 1} {
416 displayError "Cannot connect to target" $host
420 $zz idAuthentication $idAuth
422 if {[catch {$zz init}]} {
423 displayError "Cannot initialize target" $host
426 if {[catch {zwait sessionWait 60}]} {
427 displayError "Cannot initialize target" $host
431 if {$sessionWait != "1"} {
432 displayError "Cannot initialize target" $host
436 if {![$zz initResult]} {
437 set u [$zz userInformationField]
439 displayError "Cannot initialize target $host" $u
443 if {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
446 ir-set $zz.$setNo $zz
447 $zz.$setNo smallSetElementSetNames $elements
448 $zz.$setNo mediumSetElementSetNames $elements
449 $zz.$setNo recordElements $elements
450 eval $zz.$setNo databaseNames $database
452 $zz.$setNo preferredRecordSyntax USMARC
454 $zz callback search-response $setNo
456 $zz.$setNo largeSetLowerBound 999999
457 $zz.$setNo smallSetUpperBound 0
458 $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
460 $zz.$setNo largeSetLowerBound 2
461 $zz.$setNo smallSetUpperBound 0
462 $zz.$setNo mediumSetPresentNumber 0
465 $zz.$setNo search $query
467 if {[catch {zwait sessionWait 600}]} {
468 displayError "Timeout in search" {}
469 html "</body></html>\n"
474 if {$sessionWait != 1} {
475 displayError "Search fail" "Connection closed"
476 html "</body></html>\n"
480 set status [$zz.$setNo responseStatus]
481 if {[lindex $status 0] == "NSD"} {
482 set code [lindex $status 1]
483 set msg [lindex $status 2]
484 set addinfo [lindex $status 3]
485 displayError "Diagnostic message" \
486 "$msg: $addinfo\n<br>(error code $code)"
489 set hist($setNo,hits) [$zz.$setNo resultCount]
493 proc init-m-response {i} {
497 wlog debug "init-m-response"
503 proc connect-m-response {i} {
507 wlog debug "connect-m-response"
508 z39$i callback [list init-m-response $i]
509 if {[catch {z39$i init}]} {
515 proc fail-m-response {i} {
519 wlog debug "fail-m-response"
524 proc search-m-response {setNo i} {
532 proc z39msearch {setNo piggy elements} {
537 set not $hist($setNo,0,host)
539 for {set i 1} {$i <= $not} {incr i} {
540 set host $hist($setNo,$i,host)
541 if {[catch {z39 failback fail-response}]} {
544 if {[catch {set oldHost [z39$i connect]}]} {
547 if {$oldHost != $host} {
548 catch {z39$i disconnect}
550 z39$i callback [list connect-m-response $i]
551 z39$i failback [list fail-m-response $i]
554 for {set i 1} {$i <= $not} {incr i} {
555 set oldHost [z39$i connect]
556 set host $hist($setNo,$i,host)
557 if {$oldHost == $host} {
561 z39$i idAuthentication $hist($setNo,$i,idAuthentication)
562 html "Connecting to target " $host " <br>\n"
564 if {![catch {z39$i connect $host}]} {
569 wlog debug "Waiting for init response"
570 if {[catch {zwait zleft 10}]} {
575 for {set i 1} {$i <= $not} {incr i} {
576 html "host " $hist($setNo,$i,host) ": "
577 if {$zstatus($i) >= 1} {
579 ir-set z39$i.$setNo z39$i
580 set hist($setNo,$i,offset) 0
581 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
583 z39$i.$setNo smallSetElementSetNames $elements
584 z39$i.$setNo mediumSetElementSetNames $elements
585 z39$i.$setNo recordElements $elements
587 z39$i.$setNo preferredRecordSyntax USMARC
588 z39$i callback [list search-m-response $setNo $i]
591 z39$i.$setNo largeSetLowerBound 999999
592 z39$i.$setNo smallSetUpperBound 0
593 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
595 z39$i.$setNo largeSetLowerBound 2
596 z39$i.$setNo smallSetUpperBound 0
597 z39$i.$setNo mediumSetPresentNumber 0
600 wlog debug "search " $hist($setNo,$i,query)
601 z39$i.$setNo search $hist($setNo,$i,query)
608 wlog debug "Waiting for search response"
609 if {[catch {zwait zleft 30}]} {
613 for {set i 1} {$i <= $not} {incr i} {
614 if {$zstatus($i) != 2} continue
615 set status [z39$i.$setNo responseStatus]
616 if {[lindex $status 0] != "NSD"} {
617 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
622 proc z39present {setNo tno setOffset setMax dfunc elements} {
632 $zz.$setNo elementSetNames $elements
633 $zz.$setNo recordElements $elements
634 set toGet [expr 1 + $setMax - $setOffset]
635 while {$setMax > 0 && $toGet > 0} {
636 for {set got 0} {$got < $toGet} {incr got} {
637 if {[$zz.$setNo type [expr $setOffset + $got]] == ""} {
643 $zz.$setNo present $setOffset $toGet
644 if {[catch {zwait sessionWait 300}]} {
648 if {$sessionWait == "0"} {
651 if {$sessionWait != "1"} {
654 set got [$zz.$setNo numberOfRecordsReturned]
659 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $tno
660 set setOffset [expr $got + $setOffset]
661 set toGet [expr 1 + $setMax - $setOffset]
673 if {![info exists nextSetNo]} {
676 html "<hr><h3>History</h3><dl>\n"
677 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
678 html {<dt> <a href="http:} $env(SCRIPT_NAME)
679 html / $sessionId {/search.egw/} $setNo + 1
680 html + $hist($setNo,maxPresent)
681 html {"> } [lindex $targets($hist($setNo,host)) 0]
682 if {[llength $hist($setNo,database)] > 1} {
684 foreach b $hist($setNo,database) {
690 if {[info exists hist($setNo,hits)]} {
691 html $hist($setNo,hits) " hits"
700 proc displayError {msga msgb} {
702 html {<img src="/gif/noway.gif">}
703 html "<h2>" $msga "</h2>\n"
705 html "<h3>" $msgb "</h3>\n"
707 html "</center><p>\n"