+ if {$sessionWait == 0 && [catch {egw_wait sessionWait 60}]} {
+ displayError "Cannot initialize target" $thisHost
+ $zz disconnect
+ return 0
+ }
+ if {$sessionWait != "1"} {
+ displayError "Cannot initialize target" $thisHost
+ $zz disconnect
+ return 0
+ }
+ if {![$zz initResult]} {
+ set u [$zz userInformationField]
+ $zz disconnect
+ displayError "Cannot initialize target $thisHost" $u
+ return 0
+ }
+ } elseif {![catch [list $zz.$setNo smallSetUpperBound 0]]} {
+ if {[info exists hist($setNo,$tno,hits)]} {
+ return 1
+ }
+ }
+
+ if {![string compare [lindex $targets($host) 1] WAIS]} {
+ wais-set $zz.$setNo $zz
+ } else {
+ ir-set $zz.$setNo $zz
+ $zz.$setNo preferredRecordSyntax [lindex $targets($host) 1]
+ egw_log debug "set syntax to [lindex $targets($host) 1]"
+ }
+ if {![lindex $targets($host) 5]} {
+ set elements {}
+ }
+ $zz.$setNo smallSetElementSetNames $elements
+ $zz.$setNo mediumSetElementSetNames $elements
+ $zz.$setNo recordElements $elements
+
+ egw_log debug "database=$database"
+ eval $zz.$setNo databaseNames $database
+
+ $zz callback [list search-response $zz.$setNo]
+ if {$piggy} {
+ $zz.$setNo largeSetLowerBound 999999
+ $zz.$setNo smallSetUpperBound 0
+ $zz.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
+ } else {
+ $zz.$setNo largeSetLowerBound 2
+ $zz.$setNo smallSetUpperBound 0
+ $zz.$setNo mediumSetPresentNumber 0
+ }
+ set sessionWait 0
+ egw_log debug "search: $query"
+
+ if {[info exists docId]} {
+ $zz.$setNo search $query $docId
+ } else {
+ $zz.$setNo search $query
+ }
+
+ if {!$sessionWait && [catch {egw_wait sessionWait 60}]} {
+ egw_log debug "timeout/cancel in search"
+ displayError "Timeout in search" {}
+ html "</body></html>\n"
+ $zz disconnect
+ return 0
+ }
+
+ if {$sessionWait == -1} {
+ displayError "Search fail" "Connection closed"
+ html "</body></html>\n"
+ $zz disconnect
+ }
+ if {$sessionWait != 1} {
+ return 0
+ }
+ set hist($setNo,$tno,hits) [$zz.$setNo resultCount]
+ return 1
+}
+
+proc init-m-response {i} {
+ global zstatus
+ global zleft
+
+ egw_log debug "init-m-response"
+
+ incr zleft -1
+ if {![z39$i initResult]} {
+ set zstatus($i) -1
+ z39$i disconnect
+ return
+ }
+ set zstatus($i) 1
+}
+
+proc connect-m-response {i} {
+ global zstatus
+ global zleft
+
+ egw_log debug "connect-m-response"
+ z39$i callback [list init-m-response $i]
+ if {[catch {z39$i init}]} {
+ set zstatus($i) -1
+ incr zleft -1
+ }
+}
+
+proc fail-m-response {i} {
+ global zstatus
+ global zleft
+
+ egw_log debug "fail-m-response"
+ set zstatus($i) -1
+ incr zleft -1
+}
+
+proc search-m-response {setNo i start number} {
+ global zleft
+ global zstatus
+ global hist
+
+ egw_log debug "search-m-response"
+ set status [z39$i.$setNo responseStatus]
+ egw_log debug "search-m-response1"
+ if {[lindex $status 0] == "OK"} {
+ set nor 0
+ } elseif {[lindex $status 0] == "DBOSD"} {
+ set nor [z39$i.$setNo numberOfRecordsReturned]
+ } else {
+ egw_log debug "search-m-response2"
+ incr zleft -1
+ set zstatus($i) 2
+ return
+ }
+ set hist($setNo,$i,hits) [z39$i.$setNo resultCount]
+ egw_log debug "search-m-response3"
+ set hist($setNo,$i,offset) [expr $start + $nor -1]
+ if {[expr $nor + $start] > [z39$i.$setNo resultCount]} {
+ egw_log debug "search-m-response4"
+ incr zleft -1
+ set zstatus($i) 2
+ return
+ }
+ egw_log debug "search-m-response5"
+ if {$nor >= $number} {
+ egw_log debug "search-m-response6 nor=$nor number=$number"
+ incr zleft -1
+ set zstatus($i) 2
+ return
+ }
+ egw_log debug "search-m-response7"
+ set start [expr $start + $nor]
+ set number [expr $number - $nor]
+ if {[expr $start + $number - 1] > [z39$i.$setNo resultCount]} {
+ set number [expr [z39$i.$setNo resultCount] - $start + 1]
+ }
+ z39$i callback [list search-m-response $setNo $i $start $number]
+ egw_log debug "mpresent start=$number number=$number"
+ z39$i.$setNo present $start $number
+}
+
+proc z39msearch {setNo elements start number cache} {
+ global zleft
+ global zstatus
+ global hist
+ global targets
+ global debug
+
+ set not $hist($setNo,0,host)
+
+ egw_log debug "z39msearch start=$start number=$number elements=$elements"
+ for {set i 1} {$i <= $not} {incr i} {
+ set host $hist($setNo,$i,host)
+ mkAssoc z39$i $host
+ set oldHost [z39$i connect]
+ set thisHost [splitHostSpec $host]
+ if {[string compare $oldHost $thisHost]} {
+ catch {z39$i disconnect}
+ }
+ z39$i callback [list connect-m-response $i]
+ z39$i failback [list fail-m-response $i]
+ }
+ set zleft 0
+ for {set i 1} {$i <= $not} {incr i} {
+ set oldHost [z39$i connect]
+ set host $hist($setNo,$i,host)
+ set thisHost [splitHostSpec $host]
+ if {![string compare $oldHost $thisHost]} {
+ continue
+ }
+ egw_log debug "old=$oldHost this=$thisHost"
+ z39$i idAuthentication $hist($setNo,$i,idAuthentication)
+ html "Connecting to target " $thisHost " <br>\n"
+ set zstatus($i) -1
+ if {![catch {z39$i connect $thisHost}]} {
+ incr zleft
+ }
+ }
+ while {$zleft > 0} {
+ egw_log debug "Waiting for init response"
+ if {[catch {egw_wait zleft 20}]} {
+ break
+ }
+ }
+ set zleft 0
+ for {set i 1} {$i <= $not} {incr i} {
+ set host $hist($setNo,$i,host)
+ if {$debug} {
+ html "host " [splitHostSpec $host] ": "
+ }
+ egw_log debug "i=$i zstatus=$zstatus($i)"
+ if {$zstatus($i) < 1} {
+ if {$debug} {
+ html "fail<br>\n"
+ }
+ continue
+ }
+ if {[catch [list z39$i.$setNo preferredRecordSyntax]]} {
+ if {$debug} {
+ html "ok<br>\n"
+ }
+
+ if {![string compare [lindex $targets($host) 1] WAIS]} {
+ wais-set z39$i.$setNo z39$i
+ } else {
+ ir-set z39$i.$setNo z39$i
+ z39$i.$setNo preferredRecordSyntax [lindex $targets($host) 1]
+ egw_log debug "set syntax to [lindex $targets($host) 1]"
+ }
+ set hist($setNo,$i,offset) 0
+ eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
+
+ if {![lindex $targets($hist($setNo,$i,host)) 5]} {
+ set thisElements {}
+ } else {
+ set thisElements $elements
+ }
+ z39$i.$setNo smallSetElementSetNames $thisElements
+ z39$i.$setNo mediumSetElementSetNames $thisElements
+ z39$i.$setNo elementSetNames $thisElements
+ z39$i.$setNo recordElements $thisElements
+
+ z39$i callback [list search-m-response $setNo $i $start $number]
+
+ if {$start == 1} {
+ z39$i.$setNo largeSetLowerBound 999999
+ z39$i.$setNo smallSetUpperBound 0
+ z39$i.$setNo mediumSetPresentNumber $number
+ } else {
+ z39$i.$setNo largeSetLowerBound 2
+ z39$i.$setNo smallSetUpperBound 0
+ z39$i.$setNo mediumSetPresentNumber 0
+ }
+ set zstatus($i) 1
+ incr zleft
+ egw_log debug "msearch host=" $hist($setNo,$i,host)
+ egw_log debug "setNo=$setNo query=" $hist($setNo,$i,query) "="
+ if {[catch {z39$i.$setNo search $hist($setNo,$i,query)}]} {
+ set zstatus($i) -1
+ incr zleft -1
+ }
+ } elseif {[z39$i.$setNo resultCount] >= $start} {
+ if {[expr $start + $number - 1] > [z39$i.$setNo resultCount]} {
+ set tnumber [expr [z39$i.$setNo resultCount] - $start + 1]
+ } else {
+ set tnumber $number
+ }
+ if {![lindex $targets($hist($setNo,$i,host)) 5]} {
+ set thisElements {}
+ } else {
+ set thisElements $elements
+ }
+ z39$i.$setNo smallSetElementSetNames $thisElements
+ z39$i.$setNo mediumSetElementSetNames $thisElements
+ z39$i.$setNo elementSetNames $thisElements
+ z39$i.$setNo recordElements $thisElements
+
+ for {set n 0} {$n < $tnumber} {incr n} {
+ if {[z39$i.$setNo recordType [expr $start + $n]] == ""} {
+ if {$n > 0} {
+ egw_log debug "failed on $n"
+ }
+ if {$debug} {
+ html "no record at #" [expr $start + $n]
+ html " el=-" $thisElements "-"
+ }
+ break
+ }
+ }
+ if {$n == $tnumber} {
+ if {$debug} {
+ html "cached<br>\n"
+ }
+ continue
+ }
+
+ html "present<br>\n"
+ z39$i callback [list search-m-response $setNo $i $start $tnumber]
+ incr zleft
+ egw_log debug "mpresent start=$start number=$tnumber"
+ z39$i.$setNo present $start $tnumber
+ } else {
+ if {$debug} {
+ html "ok<br>\n"
+ }
+ }
+ }
+ while {$zleft > 0} {
+ egw_log debug "Waiting for search/present response"
+ if {[catch {egw_wait zleft 60}]} {
+ break
+ }
+ }
+ for {set i 1} {$i <= $not} {incr i} {
+ if {$zstatus($i) != 2} continue
+ set status [z39$i.$setNo responseStatus]
+ if {0 && [lindex $status 0] != "NSD"} {
+ set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
+ }
+ }
+}
+
+proc z39present {setNo tno setOffset setMax dfunc elements} {
+ global hist
+ global sessionWait
+ global targets
+
+ set zz z39$tno
+ set host $hist($setNo,$tno,host)
+
+ if {![lindex $targets($host) 5]} {
+ set elements {}
+ }
+
+ $zz.$setNo elementSetNames $elements
+ $zz.$setNo recordElements $elements
+ set toGet [expr 1 + $setMax - $setOffset]
+
+ $zz callback [list search-response $zz.$setNo]
+
+ while {$setMax > 0 && $toGet > 0} {
+ for {set got 0} {$got < $toGet} {incr got} {
+ if {[$zz.$setNo recordType [expr $setOffset + $got]] == ""} {
+ break
+ }
+ }
+ if {$got < $toGet} {
+ set sessionWait 0
+ $zz.$setNo present $setOffset $toGet
+ if {[catch {egw_wait sessionWait 300}]} {
+ egw_log debug "timeout/cancel in present"
+ $zz disconnect
+ break
+ }
+ if {$sessionWait == "0"} {
+ $zz disconnect
+ }
+ if {$sessionWait != "1"} {
+ break
+ }
+ set got [$zz.$setNo numberOfRecordsReturned]
+ if {$got <= 0} {
+ break
+ }
+ }
+ display-rec $setOffset [expr $got + $setOffset - 1] $dfunc $setNo $tno
+ set setOffset [expr $got + $setOffset]
+ set toGet [expr 1 + $setMax - $setOffset]
+ egw_flush
+ }
+}
+
+proc buttons-result-set-s {setNo targetNo setMax startPos after} {
+ global sessionId
+ global useIcons
+ global env
+ global hist
+
+ set zz z39$targetNo
+ html "<p>\n"
+ button-main
+ if {$setMax > 0 && $setMax < [$zz.$setNo resultCount]} {
+ if {!$useIcons} {
+ html "\n | "
+ }
+ html {<a href="http:} $env(SCRIPT_NAME)
+ html / $sessionId {/search.egw/} $setNo + $targetNo +
+ html [expr $setMax + 1] + [expr $setMax + $hist($setNo,maxPresent)]
+ if {$useIcons} {
+ html {"><img src="/egwgif/button-next-records.gif" }
+ html {alt="Next Records" border=0></a>}
+ } else {
+ html {">Next Records</a>}
+ }
+ }
+ if {$setMax > 0 && $startPos != "" && $startPos != "1"} {
+ if {!$useIcons} {
+ html "\n | "
+ }
+ html {<a href="http:} $env(SCRIPT_NAME)
+ html / $sessionId {/search.egw/} $setNo + $targetNo
+ html + [expr $startPos - $hist($setNo,maxPresent)]
+ html + [expr $startPos - 1]
+ if {$useIcons} {
+ html {"><img src="/egwgif/button-previous-records.gif" }
+ html {alt="Previous Records" border=0></a>}
+ } else {
+ html {">Previous Records</a>}
+ }
+ }
+ if {$targetNo > 0} {
+ button-result-set $setNo $targetNo
+ }
+ button-new-query $setNo
+ button-new-target
+ button-view-history
+
+ html "<p>\n"
+}
+
+proc score-sort {l r} {
+ return [expr [lindex $r 0] - [lindex $l 0]]
+}
+
+proc display-result-set-m-score {setNo} {
+ global hist
+ global useIcons
+ global zstatus
+ global targets
+
+ set not $hist($setNo,0,host)
+ for {set i 1} {$i <= $not} {incr i} {
+ if {$zstatus($i) != 2} continue
+ set status [z39$i.$setNo responseStatus]
+ if {[lindex $status 0] != "DBOSD"} continue
+ set nor $hist($setNo,$i,offset)
+ for {set j 1} {$j <= $nor} {incr j} {
+ if {![string compare [z39$i.$setNo recordType $j] WAIS]} {
+ set score [z39$i.$setNo getWAIS $j score]
+ } elseif {![string compare [z39$i.$setNo recordType $j] USmarc]} {
+ set score [z39$i.$setNo getMarc $j field 999 * r]
+ if {[scan $score %d score] != 1} {
+ set score 10
+ }
+ } else {
+ set score 10
+ }
+ if {$score > 0} {
+ lappend scoreArray [list $score $i $j]
+ }
+ }
+ }
+ if {![info exists scoreArray]} {
+ html "<br><h3>Search produced no result</h3><br>\n"
+ } else {
+ html "<ul>\n"
+ set scoreSorted [lsort -command score-sort $scoreArray]
+ foreach r $scoreSorted {
+ set i [lindex $r 1]
+ set j [lindex $r 2]
+ display-$hist($setNo,format) z39$i.$setNo $j $setNo $i
+ }
+ html "<br></ul>\n"
+ }
+ for {set i 1} {$i <= $not} {incr i} {
+ if {$zstatus($i) != 2} continue
+ set status [z39$i.$setNo responseStatus]
+ if {[lindex $status 0] == "NSD"} {
+ z39$i.$setNo nextResultSetPosition 0
+ set code [lindex $status 1]
+ set msg [lindex $status 2]
+ set addinfo [lindex $status 3]
+ html {<dt>} [lindex $targets($hist($setNo,$i,host)) 0]
+ html "<dd>Error: $msg: $addinfo (code $code)<br>\n"
+ }
+ }
+ html "\n<br>"
+}
+
+proc display-result-set-m-server {setNo} {
+ global hist
+ global useIcons
+ global zstatus
+ global targets
+ global env
+ global sessionId
+
+ set not $hist($setNo,0,host)
+ html "<dl>\n"
+ for {set i 1} {$i <= $not} {incr i} {
+ if {$zstatus($i) != 2} continue
+ set status [z39$i.$setNo responseStatus]
+ if {[lindex $status 0] == "NSD"} {
+ html "<dt><h3>" [lindex $targets($hist($setNo,$i,host)) 0] ": "
+ z39$i.$setNo nextResultSetPosition 0
+ set code [lindex $status 1]
+ set msg [lindex $status 2]
+ set addinfo [lindex $status 3]
+ html "Error</h3>\n<dd>NSD$code: $msg: $addinfo"
+ } else {
+ html {<dt><a href="http:} $env(SCRIPT_NAME)
+ html / $sessionId {/search.egw/} $setNo + $i + 1
+ html + $hist($setNo,maxPresent) {">}
+ html "<h3>" [lindex $targets($hist($setNo,$i,host)) 0] "</a>: "
+ set r [z39$i.$setNo resultCount]
+ html "$r hits</h3>\n<dd>\n"
+
+ if {$hist($setNo,$i,offset) > $hist($setNo,maxPresent)} {
+ set nor $hist($setNo,maxPresent)
+ } else {
+ set nor $hist($setNo,$i,offset)
+ }
+ display-rec 1 $nor display-$hist($setNo,format) $setNo $i
+ }
+ html "\n"
+ }