State reestablised when shell restarts. History of previous
[egate.git] / www / search.egw
index 0b2c02c..08e0aa1 100644 (file)
 <html>
 {
-# $Id: search.egw,v 1.6 1995/11/01 16:15:45 adam Exp $
+# $Id: search.egw,v 1.8 1995/11/06 17:44:21 adam Exp $
 
-proc search-response {sno} {
-    global sessionWait
-
-    set status [z39.$sno responseStatus]
-    if {[lindex $status 0] == "NSD"} {
-        z39.$sno nextResultSetPosition 0
-        set code [lindex $status 1]
-        set msg [lindex $status 2]
-        set addinfo [lindex $status 3]
-        htmlr {<strong>Error} $code {: } $msg {: } $addinfo { </strong><br>}
-        set sessionWait -2
-    } else {
-        set sessionWait 1
-    }
-}
-
-proc ok-response {} {
-    global sessionWait
-    set sessionWait 1
-}
-
-proc fail-response {} {
-    global sessionWait
-    set sessionWait -1
-}
-
-proc display-brief {zset no} {
-    global env
-    global setNo
-    global sessionId
-
-    set type [$zset type $no]
-    if {$type == "SD"} {
-        set err [lindex [$zset diag $no] 1]
-        set add [lindex [$zset diag $no] 2]
-        if {$add != {}} {
-            set add " :${add}"
-        }
-        htmlr "${no} Error ${err}${add} <br>"
-        return
-    }
-    if {$type != "DB"} {
-        return
-    }
-    html "${no} "
-    set rtype [$zset recordType $no]
-    if {$rtype == "SUTRS"} {
-        html [join [$zset getSutrs $no]]
-        htmlr {<br>}
-        return
-    } 
-    if {![catch {
-        set title [lindex [$zset getMarc $no field 245 * a] 0]
-        set year [lindex [$zset getMarc $no field 260 * c] 0]
-    } ] } {
-        html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME) /
-        html $sessionId {/showfull.egw/} $setNo + $no {"> } $title {</a>}
-        html " <i> ${year} </i>"
-    }
-    htmlr {<br>}
-}
-
-proc display-full {zset no} {
-    set type [$zset type $no]
-    if {$type == "SD"} {
-        set err [lindex [$zset diag $no] 1]
-        set add [lindex [$zset diag $no] 2]
-        if {$add != {}} {
-            set add " :${add}"
-        }
-        htmlr "<hr> ${no} <br>"
-        htmlr "Error ${err}${add} <br>"
-        return
-    }
-    if {$type != "DB"} {
-        return
-    }
-    htmlr "<hr> ${no} <br>"
-    set rtype [$zset recordType $no]
-    if {$rtype == "SUTRS"} {
-        htmlr [join [$zset getSutrs $no]]
-        return
-    } 
-    if {[catch {set r [$zset getMarc $no line * * *]}]} {
-        htmlr "Unknown record type: $rtype"
-        return
-    }
-    foreach line $r {
-        set tag [lindex $line 0]
-        set indicator [lindex $line 1]
-        set fields [lindex $line 2]
-        set l [string length $indicator]
-        html "$tag "
-        if {$l > 0} {
-            for {set i 0} {$i < $l} {incr i} {
-                if {[string index $tag $i] == " "} {
-                    html "_"
-                } else {
-                    html [string index $tag $i]
-                }
-            }
-        }
-        foreach field $fields {
-            set id [lindex $field 0]
-            set data [lindex $field 1]
-            if {$id != ""} {
-                html " <b>\$$id</b> "
-            }
-            html $data
-        }
-        htmlr {<br>}
+    if {[info commands saveState] == ""} {
+        source z39util.tcl
     }
-}
-
-proc display-rec {from to} {
-    global setNo
-
-    while {$from <= $to} { 
-        display-brief z39.$setNo $from
-        incr from
-    }
-}
-
-proc build-query {} {
-    global targets
-    global t
-
-    set op {}
-    set q {}
-    for {set i 1} {$i < 4} {incr i} {
-        set term [wform entry$i]
-        if {$term != ""} {
-            set field [wform menu$i]
-            foreach x [lindex $targets($t) 2] {
-                if {[lindex $x 0] == $field} {
-                    set attr [lindex $x 1]
-                }
-            }
-            switch $op {
-            And
-                { set q "@and $q ${attr} ${term}" }
-            Or
-                { set q "@or $q ${attr} ${term}" }
-            {And not}
-                { set q "@not $q ${attr} ${term}" }
-            {}
-                { set q "${attr} ${term}" }
-            }
-            set op [wform logic$i]
-        }
-    }
-    return $q
-}
-
-proc research {setNo oldHost piggy} {
-    global hist
-    global sessionWait
-
-    set host $hist($setNo,host)
-    if {[catch {z39 failback fail-response}]} {
-        ir z39
-    }
-    if {[catch {set oldHost [z39 connect]}]} {
-        set oldHost ""
-    }
-    z39 callback ok-response
-    z39 failback fail-response
-    if {$oldHost != $host} {
-        catch {z39 disconnect}
-
-        html "Connecting to target " $host " <br>\n"
-        set sessionWait 0
-        if {[catch {z39 connect $host}]} {
-            htmlr "Cannot connect to target ${host} <br>"
-            htmlr "</body></html>"
-            wabort
-        } elseif {$sessionWait == 0} {
-            zwait sessionWait
-            if {$sessionWait != 1} {
-                htmlr "Cannot connect to target ${host} <br>"
-                htmlr "</body></html>"
-                wabort
-            }
-        }
-        set sessionWait 0
-        z39 init
-        zwait sessionWait
-        if {$sessionWait != "1"} {
-            htmlr "Cannot initialize with target ${host} <br>"
-            htmlr "</body></html>"
-            wabort
-        }
-    }
-    if {![catch {z39.$setNo smallSetUpperBound 0}]} {
-        return
-    }
-    ir-set z39.$setNo z39
-    eval z39.$setNo databaseNames $hist($setNo,database)
-
-    z39.$setNo preferredRecordSyntax USMARC
-
-    z39 callback search-response $setNo
-    if {$piggy} {
-        z39.$setNo largeSetLowerBound 999999
-        z39.$setNo smallSetUpperBound 0
-        z39.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
-    } else {
-        z39.$setNo largeSetLowerBound 2
-        z39.$setNo smallSetUpperBound 0
-        z39.$setNo mediumSetPresentNumber 0
-    }
-    set sessionWait 0
-    z39.$setNo search $hist($setNo,query)
-
-    zwait sessionWait
-    if {$sessionWait != 1} {
-        htmlr {</body></html>}
-        wabort
-    }
-}
-
     global sessionWait
     global nextSetNo
     global setNo
@@ -237,6 +19,7 @@ proc research {setNo oldHost piggy} {
            set hist($nextSetNo,host) $oldHost
         }
         set setNo $nextSetNo
+        html "using host " $hist($setNo,host) " <br\n"
         incr nextSetNo
 
         set hist($setNo,query) [build-query]
@@ -254,75 +37,45 @@ proc research {setNo oldHost piggy} {
     set host $hist($setNo,host)
     set databases [lindex $targets($host) 1]
 
-    htmlr {<head><title> WWW/Z39.50 Gateway Search } $host { </title>}
-    htmlr {</head><body>}
-    html "<h2> Search result </h2>\n"
-    wflush
-
+    html "<head><title> WWW/Z39.50 Gateway Search " $host " </title>\n"
+    html "</head><body>\n"
 
     set startPos [lindex $sessionParms 1]
     set endPos [lindex $sessionParms 2]
+    set setMax 0
+    set setOffset 0
     if {$startPos == ""} {
-        research $setNo $oldHost 1
-
+        if {[z39search $setNo 1] != "1"} {
+            return
+        }
         set r [z39.$setNo resultCount]
-        html "<h3> $r hits</h3><br>\n"
-        set setOffset [z39.$setNo numberOfRecordsReturned]
-        display-rec 1 $setOffset
+        html "<h2> Search result $r hits</h2>\n"
         wflush
+        set setOffset [z39.$setNo numberOfRecordsReturned]
+        display-rec 1 $setOffset display-brief
         incr setOffset
         set setMax [z39.$setNo resultCount]
         if {$setMax > $hist($setNo,maxPresent)} {
             set setMax $hist($setNo,maxPresent)
         }
     } else {
-        research $setNo $oldHost 0
-
+        if {[z39search $setNo 0] != "1"} {
+            return 
+        }
+        set r [z39.$setNo resultCount]
+        html "<h2> Search result $r hits</h2>\n"
+        wflush
         set setOffset $startPos
         set setMax [z39.$setNo resultCount]
         if {$setMax > $endPos} {
             set setMax $endPos
         }
     }
-    set toGet [expr 1 + $setMax - $setOffset]
-    while {$toGet > 0} {
-        for {set got 0} {$got < $toGet} {incr got} {
-            if {[z39.$setNo type [expr $setOffset + $got]] == ""} {
-                break
-            }
-        }
-        if {$got < $toGet} {
-            set sessionWait 0
-            z39.$setNo present $setOffset $toGet
-            zwait sessionWait
-            if {$sessionWait != "1"} {
-                break
-            }
-            set got [z39.$setNo numberOfRecordsReturned]
-        }
-        display-rec $setOffset [expr $got + $setOffset - 1]
-        set setOffset [expr $got + $setOffset]
-        set toGet [expr 1 + $setMax - $setOffset]
-        wflush
+    if {$setMax > 0} {
+        z39present $setNo $setOffset $setMax display-brief
     }
 }
 
-{
-    html "<hr>\n"
-    html "<strong>Debug information</strong><br>\n"
-    html "sessionId: $sessionId <br>\n"
-    html "sessionParms: $sessionParms <br>\n"
-    foreach e {SERVER_NAME PATH_INFO SCRIPT_NAME} {
-        html $e {: } $env($e) {<br>} \n
-    }
-    html "form: " [wform] " <br>\n"
-    html "target: " $host " <br>\n"
-    html "databases: " $hist($setNo,database) " <br>\n"
-    html "selected: " [wform base] " <br>\n"
-    html "query: ->" $hist($setNo,query) "<- <br>"
-    html "setNo: " $setNo " <br>\n"
-    html "nextSetNo: " $nextSetNo " <br>\n"
-}
 
 <hr>
 {
@@ -331,7 +84,7 @@ proc research {setNo oldHost piggy} {
         html / $sessionId {/search.egw/} $setNo + [expr $setMax + 1]
         html + [expr $setMax + $hist($setNo,maxPresent)] {"> Next </a>} "| \n"
     }
-    if {$startPos != ""} {
+    if {$startPos != "" && $startPos != "1"} {
         html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
         html / $sessionId {/search.egw/} $setNo 
         if {[expr $startPos - $hist($setNo,maxPresent)] > 1} {
@@ -343,7 +96,25 @@ proc research {setNo oldHost piggy} {
     html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
     html / $sessionId {/targets.egw"> New target </a>} " | \n"
     html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
-    html / $sessionId {/query.egw/} $host {"> New query </a>}
+    html / $sessionId {/query.egw/} $host + $setNo {"> New query </a>}
+}
+
+{
+    html "<hr>\n"
+    html "<h3>Debug information</h3>\n"
+    html "sessionId: $sessionId <br>\n"
+    html "sessionParms: $sessionParms <br>\n"
+    foreach e {SERVER_NAME PATH_INFO SCRIPT_NAME} {
+        html $e {: } $env($e) {<br>} \n
+    }
+    html "form: " [wform] " <br>\n"
+    html "target: " $host " <br>\n"
+    html "databases: " $hist($setNo,database) " <br>\n"
+    html "selected: " [wform base] " <br>\n"
+    html "query: --" $hist($setNo,query) "-- <br>"
+    html "setNo: " $setNo " <br>\n"
+    html "nextSetNo: " $nextSetNo " <br>\n"
 }
+
 </body>
 </html>