Bug fixes and select on FIFOs in wcgi - doesn't really work!
[egate.git] / www / search.egw
1 <html>
2 {
3 # $Id: search.egw,v 1.7 1995/11/02 16:35:36 adam Exp $
4
5 proc search-response {sno} {
6     global sessionWait
7
8     set status [z39.$sno responseStatus]
9     if {[lindex $status 0] == "NSD"} {
10         z39.$sno nextResultSetPosition 0
11         set code [lindex $status 1]
12         set msg [lindex $status 2]
13         set addinfo [lindex $status 3]
14         htmlr {<strong>Error} $code {: } $msg {: } $addinfo { </strong><br>}
15         set sessionWait -2
16     } else {
17         set sessionWait 1
18     }
19 }
20
21 proc ok-response {} {
22     global sessionWait
23     set sessionWait 1
24 }
25
26 proc fail-response {} {
27     global sessionWait
28     set sessionWait -1
29 }
30
31 proc display-brief {zset no} {
32     global env
33     global setNo
34     global sessionId
35
36     set type [$zset type $no]
37     if {$type == "SD"} {
38         set err [lindex [$zset diag $no] 1]
39         set add [lindex [$zset diag $no] 2]
40         if {$add != {}} {
41             set add " :${add}"
42         }
43         htmlr "${no} Error ${err}${add} <br>"
44         return
45     }
46     if {$type != "DB"} {
47         return
48     }
49     html "${no} "
50     set rtype [$zset recordType $no]
51     if {$rtype == "SUTRS"} {
52         html [join [$zset getSutrs $no]]
53         htmlr {<br>}
54         return
55     } 
56     if {![catch {
57         set title [lindex [$zset getMarc $no field 245 * a] 0]
58         set year [lindex [$zset getMarc $no field 260 * c] 0]
59     } ] } {
60         html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME) /
61         html $sessionId {/showfull.egw/} $setNo + $no {"> } $title {</a>}
62         html " <i> ${year} </i>"
63     }
64     htmlr {<br>}
65 }
66
67 proc display-full {zset no} {
68     set type [$zset type $no]
69     if {$type == "SD"} {
70         set err [lindex [$zset diag $no] 1]
71         set add [lindex [$zset diag $no] 2]
72         if {$add != {}} {
73             set add " :${add}"
74         }
75         htmlr "<hr> ${no} <br>"
76         htmlr "Error ${err}${add} <br>"
77         return
78     }
79     if {$type != "DB"} {
80         return
81     }
82     htmlr "<hr> ${no} <br>"
83     set rtype [$zset recordType $no]
84     if {$rtype == "SUTRS"} {
85         htmlr [join [$zset getSutrs $no]]
86         return
87     } 
88     if {[catch {set r [$zset getMarc $no line * * *]}]} {
89         htmlr "Unknown record type: $rtype"
90         return
91     }
92     foreach line $r {
93         set tag [lindex $line 0]
94         set indicator [lindex $line 1]
95         set fields [lindex $line 2]
96         set l [string length $indicator]
97         html "$tag "
98         if {$l > 0} {
99             for {set i 0} {$i < $l} {incr i} {
100                 if {[string index $tag $i] == " "} {
101                     html "_"
102                 } else {
103                     html [string index $tag $i]
104                 }
105             }
106         }
107         foreach field $fields {
108             set id [lindex $field 0]
109             set data [lindex $field 1]
110             if {$id != ""} {
111                 html " <b>\$$id</b> "
112             }
113             html $data
114         }
115         htmlr {<br>}
116     }
117 }
118
119 proc display-rec {from to} {
120     global setNo
121
122     while {$from <= $to} { 
123         display-brief z39.$setNo $from
124         incr from
125     }
126 }
127
128 proc build-query {} {
129     global targets
130     global t
131
132     set op {}
133     set q {}
134     for {set i 1} {$i < 4} {incr i} {
135         set term [wform entry$i]
136         if {$term != ""} {
137             set field [wform menu$i]
138             foreach x [lindex $targets($t) 2] {
139                 if {[lindex $x 0] == $field} {
140                     set attr [lindex $x 1]
141                 }
142             }
143             switch $op {
144             And
145                 { set q "@and $q ${attr} ${term}" }
146             Or
147                 { set q "@or $q ${attr} ${term}" }
148             {And not}
149                 { set q "@not $q ${attr} ${term}" }
150             {}
151                 { set q "${attr} ${term}" }
152             }
153             set op [wform logic$i]
154         }
155     }
156     return $q
157 }
158
159 proc research {setNo oldHost piggy} {
160     global hist
161     global sessionWait
162
163     set host $hist($setNo,host)
164     if {[catch {z39 failback fail-response}]} {
165         ir z39
166     }
167     if {[catch {set oldHost [z39 connect]}]} {
168         set oldHost ""
169     }
170     z39 callback ok-response
171     z39 failback fail-response
172     if {$oldHost != $host} {
173         catch {z39 disconnect}
174
175         html "Connecting to target " $host " <br>\n"
176         set sessionWait 0
177         if {[catch {z39 connect $host}]} {
178             htmlr "Cannot connect to target ${host} <br>"
179             htmlr "</body></html>"
180             wabort
181         } elseif {$sessionWait == 0} {
182             zwait sessionWait
183             if {$sessionWait != 1} {
184                 htmlr "Cannot connect to target ${host} <br>"
185                 htmlr "</body></html>"
186                 wabort
187             }
188         }
189         set sessionWait 0
190         z39 init
191         zwait sessionWait
192         if {$sessionWait != "1"} {
193             htmlr "Cannot initialize with target ${host} <br>"
194             htmlr "</body></html>"
195             wabort
196         }
197     }
198     if {![catch {z39.$setNo smallSetUpperBound 0}]} {
199         return
200     }
201     ir-set z39.$setNo z39
202     eval z39.$setNo databaseNames $hist($setNo,database)
203
204     z39.$setNo preferredRecordSyntax USMARC
205
206     z39 callback search-response $setNo
207     if {$piggy} {
208         z39.$setNo largeSetLowerBound 999999
209         z39.$setNo smallSetUpperBound 0
210         z39.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
211     } else {
212         z39.$setNo largeSetLowerBound 2
213         z39.$setNo smallSetUpperBound 0
214         z39.$setNo mediumSetPresentNumber 0
215     }
216     set sessionWait 0
217     z39.$setNo search $hist($setNo,query)
218
219     zwait sessionWait
220     if {$sessionWait != 1} {
221         htmlr {</body></html>}
222         wabort
223     }
224 }
225
226     global sessionWait
227     global nextSetNo
228     global setNo
229     global hist
230
231     set oldHost $hist($setNo,host)
232
233     if {[wform menu1] == ""} {
234         html "state 1<br>\n"
235         set setNo [lindex $sessionParms 0]
236     } else {
237         html "state 2<br>\n"
238         if {![info exists hist($nextSetNo,host)]} {
239            set hist($nextSetNo,host) $oldHost
240         }
241         set setNo $nextSetNo
242         html "using host " $hist($setNo,host) " <br\n"
243         incr nextSetNo
244
245         set hist($setNo,query) [build-query]
246         set b [wform base]
247         if {$b == ""} {
248             set hist($setNo,database) $databases
249         } else {
250             set hist($setNo,database) $b
251         }
252         set hist($setNo,maxPresent) [wform hits]
253         if {$hist($setNo,maxPresent) == ""} {
254             set hist($setNo,maxPresent) 30
255         }
256     }
257     set host $hist($setNo,host)
258     set databases [lindex $targets($host) 1]
259
260     htmlr {<head><title> WWW/Z39.50 Gateway Search } $host { </title>}
261     htmlr {</head><body>}
262     html "<h2> Search result </h2>\n"
263     wflush
264
265
266     set startPos [lindex $sessionParms 1]
267     set endPos [lindex $sessionParms 2]
268     if {$startPos == ""} {
269         research $setNo $oldHost 1
270
271         set r [z39.$setNo resultCount]
272         html "<h3> $r hits</h3><br>\n"
273         set setOffset [z39.$setNo numberOfRecordsReturned]
274         display-rec 1 $setOffset
275         wflush
276         incr setOffset
277         set setMax [z39.$setNo resultCount]
278         if {$setMax > $hist($setNo,maxPresent)} {
279             set setMax $hist($setNo,maxPresent)
280         }
281     } else {
282         research $setNo $oldHost 0
283
284         set setOffset $startPos
285         set setMax [z39.$setNo resultCount]
286         if {$setMax > $endPos} {
287             set setMax $endPos
288         }
289     }
290     set toGet [expr 1 + $setMax - $setOffset]
291     while {$toGet > 0} {
292         for {set got 0} {$got < $toGet} {incr got} {
293             if {[z39.$setNo type [expr $setOffset + $got]] == ""} {
294                 break
295             }
296         }
297         if {$got < $toGet} {
298             set sessionWait 0
299             z39.$setNo present $setOffset $toGet
300             zwait sessionWait
301             if {$sessionWait != "1"} {
302                 break
303             }
304             set got [z39.$setNo numberOfRecordsReturned]
305         }
306         display-rec $setOffset [expr $got + $setOffset - 1]
307         set setOffset [expr $got + $setOffset]
308         set toGet [expr 1 + $setMax - $setOffset]
309         wflush
310     }
311 }
312
313 {
314     html "<hr>\n"
315     html "<strong>Debug information</strong><br>\n"
316     html "sessionId: $sessionId <br>\n"
317     html "sessionParms: $sessionParms <br>\n"
318     foreach e {SERVER_NAME PATH_INFO SCRIPT_NAME} {
319         html $e {: } $env($e) {<br>} \n
320     }
321     html "form: " [wform] " <br>\n"
322     html "target: " $host " <br>\n"
323     html "databases: " $hist($setNo,database) " <br>\n"
324     html "selected: " [wform base] " <br>\n"
325     html "query: ->" $hist($setNo,query) "<- <br>"
326     html "setNo: " $setNo " <br>\n"
327     html "nextSetNo: " $nextSetNo " <br>\n"
328 }
329
330 <hr>
331 {
332     if {$setMax < [z39.$setNo resultCount]} {
333         html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
334         html / $sessionId {/search.egw/} $setNo + [expr $setMax + 1]
335         html + [expr $setMax + $hist($setNo,maxPresent)] {"> Next </a>} "| \n"
336     }
337     if {$startPos != ""} {
338         html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
339         html / $sessionId {/search.egw/} $setNo 
340         if {[expr $startPos - $hist($setNo,maxPresent)] > 1} {
341             html + [expr $startPos - $hist($setNo,maxPresent)]
342             html + [expr $startPos - 1]
343         }
344         html {"> Prev </a>} "| \n"
345     }
346     html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
347     html / $sessionId {/targets.egw"> New target </a>} " | \n"
348     html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
349     html / $sessionId {/query.egw/} $host + $setNo {"> New query </a>}
350 }
351 </body>
352 </html>