2 # $Id: z39util.tcl,v 1.2 1995/11/07 14:57:00 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 set names [array names $var]
14 eval "set v \$${var}(\$n)"
15 puts $f "set ${var}($n) \{$v\}"
19 puts $f "set ${var} \{$v\}"
26 proc search-response {sno} {
29 set status [z39.$sno responseStatus]
30 if {[lindex $status 0] == "NSD"} {
31 z39.$sno nextResultSetPosition 0
32 set code [lindex $status 1]
33 set msg [lindex $status 2]
34 set addinfo [lindex $status 3]
35 html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
47 proc fail-response {} {
52 proc display-brief {zset no} {
57 set type [$zset type $no]
59 set err [lindex [$zset diag $no] 1]
60 set add [lindex [$zset diag $no] 2]
64 html "${no} Error ${err}${add} <br>\n"
71 set rtype [$zset recordType $no]
72 if {$rtype == "SUTRS"} {
73 html [join [$zset getSutrs $no]]
78 set title [lindex [$zset getMarc $no field 245 * a] 0]
79 set year [lindex [$zset getMarc $no field 260 * c] 0]
81 html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME) /
82 html $sessionId {/showfull.egw/} $setNo + $no {"> } $title {</a>}
83 html " <i> ${year} </i>"
88 proc display-full {zset no} {
89 set type [$zset type $no]
91 set err [lindex [$zset diag $no] 1]
92 set add [lindex [$zset diag $no] 2]
96 htmlr "<hr> ${no} <br>"
97 htmlr "Error ${err}${add} <br>"
103 htmlr "<hr> ${no} <br>"
104 set rtype [$zset recordType $no]
105 if {$rtype == "SUTRS"} {
106 htmlr [join [$zset getSutrs $no]]
109 if {[catch {set r [$zset getMarc $no line * * *]}]} {
110 htmlr "Unknown record type: $rtype"
114 set tag [lindex $line 0]
115 set indicator [lindex $line 1]
116 set fields [lindex $line 2]
117 set l [string length $indicator]
120 for {set i 0} {$i < $l} {incr i} {
121 if {[string index $tag $i] == " "} {
124 html [string index $tag $i]
128 foreach field $fields {
129 set id [lindex $field 0]
130 set data [lindex $field 1]
132 html " <b>\$$id</b> "
140 proc display-rec {from to dfunc zz} {
143 while {$from <= $to} {
144 eval "$dfunc $zz.$setNo $from"
149 proc build-query {t} {
154 for {set i 1} {$i < 4} {incr i} {
155 set term1 [wform entry$i]
156 regsub {\+} $term1 " " term
158 set field [wform menu$i]
159 foreach x [lindex $targets($t) 2] {
160 if {[lindex $x 0] == $field} {
161 set attr [lindex $x 1]
166 { set q "@and $q ${attr} \{${term}\}" }
168 { set q "@or $q ${attr} \{${term}\}" }
170 { set q "@not $q ${attr} \{${term}\}" }
172 { set q "${attr} \{${term}\}" }
174 set op [wform logic$i]
180 proc z39search {setNo piggy} {
184 set host $hist($setNo,host)
185 if {[catch {z39 failback fail-response}]} {
188 if {[catch {set oldHost [z39 connect]}]} {
191 z39 callback ok-response
192 z39 failback fail-response
193 if {$oldHost != $host} {
194 catch {z39 disconnect}
196 html "Connecting to target " $host " <br>\n"
198 if {[catch {z39 connect $host}]} {
199 html "Cannot connect to target ${host} <br>\n"
201 } elseif {$sessionWait == 0} {
203 if {$sessionWait != 1} {
204 html "Cannot connect to target ${host} <br>\n"
209 if {[catch {z39 init}]} {
210 html "Cannot initialize with target ${host} <br>\n"
214 if {$sessionWait != "1"} {
215 html "Cannot initialize with target ${host} <br>\n"
219 if {![catch {z39.$setNo smallSetUpperBound 0}]} {
222 ir-set z39.$setNo z39
223 eval z39.$setNo databaseNames $hist($setNo,database)
225 z39.$setNo preferredRecordSyntax USMARC
227 z39 callback search-response $setNo
229 z39.$setNo largeSetLowerBound 999999
230 z39.$setNo smallSetUpperBound 0
231 z39.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
233 z39.$setNo largeSetLowerBound 2
234 z39.$setNo smallSetUpperBound 0
235 z39.$setNo mediumSetPresentNumber 0
238 z39.$setNo search $hist($setNo,query)
241 if {$sessionWait != 1} {
242 html "</body></html>\n"
245 set status [z39.$setNo responseStatus]
246 if {[lindex $status 0] == "NSD"} {
247 set code [lindex $status 1]
248 set msg [lindex $status 2]
249 set addinfo [lindex $status 3]
250 html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
253 set hist($setNo,hits) [z39.$setNo resultCount]
257 proc init-m-response {i} {
261 wlog debug "init-m-response"
267 proc connect-m-response {i} {
271 wlog debug "connect-m-response"
272 z39$i callback [list init-m-response $i]
273 if {[catch {z39$i init}]} {
279 proc fail-m-response {i} {
283 wlog debug "fail-m-response"
288 proc search-m-response {setNo i} {
296 proc z39msearch {setNo piggy} {
301 set not $hist($setNo,0,host)
303 for {set i 1} {$i <= $not} {incr i} {
304 set host $hist($setNo,$i,host)
305 if {[catch {z39 failback fail-response}]} {
308 if {[catch {set oldHost [z39$i connect]}]} {
311 if {$oldHost != $host} {
312 catch {z39$i disconnect}
314 z39$i callback [list connect-m-response $i]
315 z39$i failback [list fail-m-response $i]
318 for {set i 1} {$i <= $not} {incr i} {
319 set oldHost [z39$i connect]
320 set host $hist($setNo,$i,host)
321 if {$oldHost == $host} {
325 html "Connecting to target " $host " <br>\n"
327 if {![catch {z39$i connect $host}]} {
332 wlog debug "Waiting for init response"
333 if {[catch {zwait zleft 10}]} {
338 for {set i 1} {$i <= $not} {incr i} {
339 html "host " $hist($setNo,$i,host) ": "
340 if {$zstatus($i) >= 1} {
342 ir-set z39$i.$setNo z39$i
343 set hist($setNo,$i,offset) 0
344 eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
345 z39$i.$setNo preferredRecordSyntax USMARC
346 z39$i callback [list search-m-response $setNo $i]
349 z39$i.$setNo largeSetLowerBound 999999
350 z39$i.$setNo smallSetUpperBound 0
351 z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
353 z39$i.$setNo largeSetLowerBound 2
354 z39$i.$setNo smallSetUpperBound 0
355 z39$i.$setNo mediumSetPresentNumber 0
358 wlog debug "search " $hist($setNo,$i,query)
359 z39$i.$setNo search $hist($setNo,$i,query)
366 wlog debug "Waiting for search response"
367 if {[catch {zwait zleft 30}]} {
371 for {set i 1} {$i <= $not} {incr i} {
372 if {$zstatus($i) != 2} continue
373 set status [z39$i.$setNo responseStatus]
374 if {[lindex $status 0] != "NSD"} {
375 set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
380 proc z39present {setNo setOffset setMax dfunc} {
384 set toGet [expr 1 + $setMax - $setOffset]
385 while {$setMax > 0 && $toGet > 0} {
386 for {set got 0} {$got < $toGet} {incr got} {
387 if {[z39.$setNo type [expr $setOffset + $got]] == ""} {
393 z39.$setNo present $setOffset $toGet
395 if {$sessionWait != "1"} {
398 set got [z39.$setNo numberOfRecordsReturned]
400 display-rec $setOffset [expr $got + $setOffset - 1] $dfunc
401 set setOffset [expr $got + $setOffset]
402 set toGet [expr 1 + $setMax - $setOffset]
413 if {![info exists nextSetNo]} {
416 html "<hr><h3>History</h3><dl>\n"
417 for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
418 html {<dt> <a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
419 html / $sessionId {/search.egw/} $setNo + 1
420 html + [expr $hist($setNo,maxPresent) - 1]
421 html {"> } $hist($setNo,host)
422 if {[llength $hist($setNo,database)] > 1} {
424 foreach b $hist($setNo,database) {
430 if {[info exists hist($setNo,hits)]} {
431 html $hist($setNo,hits) " hits"