2 # $Id: z39util.tcl,v 1.4 1995/11/08 16:14:36 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\}"
25 proc search-response {sno} {
28 set status [z39.$sno responseStatus]
29 if {[lindex $status 0] == "NSD"} {
30 z39.$sno nextResultSetPosition 0
31 set code [lindex $status 1]
32 set msg [lindex $status 2]
33 set addinfo [lindex $status 3]
34 html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
46 proc fail-response {} {
51 proc display-brief {zset no} {
56 set type [$zset type $no]
58 set err [lindex [$zset diag $no] 1]
59 set add [lindex [$zset diag $no] 2]
63 html "${no} Error ${err}${add} <br>\n"
70 set rtype [$zset recordType $no]
71 if {$rtype == "SUTRS"} {
72 html [join [$zset getSutrs $no]]
77 set title [lindex [$zset getMarc $no field 245 * a] 0]
78 set year [lindex [$zset getMarc $no field 260 * c] 0]
80 html {<a href="http:} $env(SCRIPT_NAME) /
81 html $sessionId {/showfull.egw/} $setNo + $no {"> } $title {</a>}
82 html " <i> ${year} </i>"
87 proc display-full {zset no} {
88 set type [$zset type $no]
90 set err [lindex [$zset diag $no] 1]
91 set add [lindex [$zset diag $no] 2]
95 html "<h3>${no}</h3>\n"
96 html "Error ${err}${add} <br>\n"
102 html "<h3>${no}</h3>\n"
103 set rtype [$zset recordType $no]
104 if {$rtype == "SUTRS"} {
105 html [join [$zset getSutrs $no]] "<br>\n"
108 if {[catch {set r [$zset getMarc $no line * * *]}]} {
109 html "Unknown record type: $rtype <br>\n"
113 set tag [lindex $line 0]
114 set indicator [lindex $line 1]
115 set fields [lindex $line 2]
116 set l [string length $indicator]
119 for {set i 0} {$i < $l} {incr i} {
120 if {[string index $indicator $i] == " "} {
123 html [string index $tag $i]
127 foreach field $fields {
128 set id [lindex $field 0]
129 set data [lindex $field 1]
131 html " <b>\$$id</b> "
139 proc display-rec {from to dfunc zz} {
142 while {$from <= $to} {
143 eval "$dfunc $zz.$setNo $from"
148 proc build-query {t} {
153 for {set i 1} {$i < 4} {incr i} {
154 set term1 [wform entry$i]
155 regsub {\+} $term1 " " term
157 set field [wform menu$i]
158 foreach x [lindex $targets($t) 2] {
159 if {[lindex $x 0] == $field} {
160 set attr [lindex $x 1]
165 { set q "@and $q ${attr} \{${term}\}" }
167 { set q "@or $q ${attr} \{${term}\}" }
169 { set q "@not $q ${attr} \{${term}\}" }
171 { set q "${attr} \{${term}\}" }
173 set op [wform logic$i]
179 proc z39search {setNo piggy} {
183 set host $hist($setNo,host)
184 if {[catch {z39 failback fail-response}]} {
187 if {[catch {set oldHost [z39 connect]}]} {
190 z39 callback ok-response
191 z39 failback fail-response
192 if {$oldHost != $host} {
193 catch {z39 disconnect}
195 html "Connecting to target " $host " <br>\n"
197 if {[catch {z39 connect $host}]} {
198 html "Cannot connect to target ${host} <br>\n"
200 } elseif {$sessionWait == 0} {
202 if {$sessionWait != 1} {
203 html "Cannot connect to target ${host} <br>\n"
207 z39 idAuthentication $hist($setNo,idAuthentication)
209 if {[catch {z39 init}]} {
210 html "Cannot initialize with target ${host} <br>\n"
213 if {[catch {zwait sessionWait 60}]} {
214 html "Cannot initialize with target ${host} <br>\n"
218 if {$sessionWait != "1"} {
219 html "Cannot initialize with target ${host} <br>\n"
224 if {![catch {z39.$setNo smallSetUpperBound 0}]} {
227 ir-set z39.$setNo z39
228 eval z39.$setNo databaseNames $hist($setNo,database)
230 z39.$setNo preferredRecordSyntax USMARC
232 z39 callback search-response $setNo
234 z39.$setNo largeSetLowerBound 999999
235 z39.$setNo smallSetUpperBound 0
236 z39.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
238 z39.$setNo largeSetLowerBound 2
239 z39.$setNo smallSetUpperBound 0
240 z39.$setNo mediumSetPresentNumber 0
243 z39.$setNo search $hist($setNo,query)
245 if {[catch {zwait sessionWait 600}]} {
246 html "</body></html>\n"
251 if {$sessionWait != 1} {
252 html "</body></html>\n"
256 set status [z39.$setNo responseStatus]
257 if {[lindex $status 0] == "NSD"} {
258 set code [lindex $status 1]
259 set msg [lindex $status 2]
260 set addinfo [lindex $status 3]
261 html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
264 set hist($setNo,hits) [z39.$setNo resultCount]
268 proc init-m-response {i} {
272 wlog debug "init-m-response"
278 proc connect-m-response {i} {
282 wlog debug "connect-m-response"
283 z39$i callback [list init-m-response $i]
284 if {[catch {z39$i init}]} {
290 proc fail-m-response {i} {
294 wlog debug "fail-m-response"
299 proc search-m-response {setNo i} {
307 proc z39msearch {setNo piggy} {
312 set not $hist($setNo,0,host)
314 for {set i 1} {$i <= $not} {incr i} {
315 set host $hist($setNo,$i,host)
316 if {[catch {z39 failback fail-response}]} {
319 if {[catch {set oldHost [z39$i connect]}]} {
322 if {$oldHost != $host} {
323 catch {z39$i disconnect}
325 z39$i callback [list connect-m-response $i]
326 z39$i failback [list fail-m-response $i]
329 for {set i 1} {$i <= $not} {incr i} {
330 set oldHost [z39$i connect]
331 set host $hist($setNo,$i,host)
332 if {$oldHost == $host} {
336 html "Connecting to target " $host " <br>\n"
338 if {![catch {z39$i connect $host}]} {
343 wlog debug "Waiting for init response"
344 if {[catch {zwait zleft 10}]} {
349 for {set i 1} {$i <= $not} {incr i} {
350 html "host " $hist($setNo,$i,host) ": "
351 if {$zstatus($i) >= 1} {
353 ir-set z39$i.$setNo z39$i
354 set hist($setNo,$i,offset) 0
355 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
356 z39$i.$setNo preferredRecordSyntax USMARC
357 z39$i callback [list search-m-response $setNo $i]
360 z39$i.$setNo largeSetLowerBound 999999
361 z39$i.$setNo smallSetUpperBound 0
362 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
364 z39$i.$setNo largeSetLowerBound 2
365 z39$i.$setNo smallSetUpperBound 0
366 z39$i.$setNo mediumSetPresentNumber 0
369 wlog debug "search " $hist($setNo,$i,query)
370 z39$i.$setNo search $hist($setNo,$i,query)
377 wlog debug "Waiting for search response"
378 if {[catch {zwait zleft 30}]} {
382 for {set i 1} {$i <= $not} {incr i} {
383 if {$zstatus($i) != 2} continue
384 set status [z39$i.$setNo responseStatus]
385 if {[lindex $status 0] != "NSD"} {
386 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
391 proc z39present {setNo setOffset setMax dfunc} {
395 set toGet [expr 1 + $setMax - $setOffset]
396 while {$setMax > 0 && $toGet > 0} {
397 for {set got 0} {$got < $toGet} {incr got} {
398 if {[z39.$setNo type [expr $setOffset + $got]] == ""} {
404 z39.$setNo present $setOffset $toGet
405 if {[catch {zwait sessionWait 300}]} {
409 if {$sessionWait != "1"} {
412 set got [z39.$setNo numberOfRecordsReturned]
414 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc z39
415 set setOffset [expr $got + $setOffset]
416 set toGet [expr 1 + $setMax - $setOffset]
428 if {![info exists nextSetNo]} {
431 html "<hr><h3>History</h3><dl>\n"
432 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
433 html {<dt> <a href="http:} $env(SCRIPT_NAME)
434 html / $sessionId {/search.egw/} $setNo + 1
435 html + [expr $hist($setNo,maxPresent) - 1]
436 html {"> } [lindex $targets($hist($setNo,host)) 0]
437 if {[llength $hist($setNo,database)] > 1} {
439 foreach b $hist($setNo,database) {
445 if {[info exists hist($setNo,hits)]} {
446 html $hist($setNo,hits) " hits"