Result set merge.
[egate.git] / www / z39util.tcl
index bfa5608..9efda0b 100644 (file)
@@ -1,5 +1,5 @@
 #
-# $Id: z39util.tcl,v 1.37 1996/03/08 16:47:08 adam Exp $
+# $Id: z39util.tcl,v 1.38 1996/03/11 17:40:49 adam Exp $
 #
 proc saveState {} {
     uplevel #0 {
@@ -65,7 +65,7 @@ proc fail-response {} {
     set sessionWait -1
 }
 
-proc display-brief {zset no setNo targetNo} {
+proc display-medium {zset no setNo targetNo} {
     global env
     global sessionId
 
@@ -84,21 +84,131 @@ proc display-brief {zset no setNo targetNo} {
         return
     }
     set rtype [$zset recordType $no]
-    if {$rtype == "SUTRS"} {
+    switch $rtype {
+        SUTRS {
+            html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
+            html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
+            html [join [$zset getSutrs $no]]
+            html "</a><br>\n"
+            return
+        }
+        WAIS {
+            html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
+            html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
+            html [join [$zset getWAIS $no headline]]
+            html {</a>}
+            html "<br>\n"
+            html {Score: } [$zset getWAIS $no score]
+            set lines [$zset getWAIS $no lines]
+            if {$lines > 0} {
+                html {, } $lines { lines}
+            }
+            html "<br>\n"
+            return
+        }
+    }
+    if {![catch {
+        set author [$zset getMarc $no field 100 * a]
+        set corp [$zset getMarc $no field 110 * a]
+        set meet [$zset getMarc $no field 111 * a]
+        set title [$zset getMarc $no field 245 * a]
+       if {[llength $author] == 0} {
+            set cover [$zset getMarc $no field 245 * {[bc]}]
+       } else {
+            set cover [$zset getMarc $no field 245 * b]
+       }
+       set location [$zset getMarc $no field 260 * a] 
+       set publisher [$zset getMarc $no field 260 * b]
+        set year [$zset getMarc $no field 260 * c]
+    } dispError ] } {
         html { <a href="http:} $env(SCRIPT_NAME) /
         html $sessionId {/showfull.egw/} $setNo + $targetNo + $no + full {">}
-        html [join [$zset getSutrs $no]]
-        html "</a><br>\n"
-        return
-    }
-    if {$rtype == "WAIS"} {
+        set p 0
+        foreach a $author {
+            if {$p} {
+                html ", "
+            }
+            html $a
+            set p 1
+        }
+        foreach a $corp {
+            if {$p} {
+                html ", "
+            }
+            html $a
+            set p 1
+        }
+        foreach a $meet {
+            if {$p} {
+                html ", "
+            }
+            html $a
+            set p 1
+        }
+        if {$p} {
+            html ": "
+        }
+       set nope 1
+        foreach v $title {
+            html $v
+            set nope 0
+        }
+        set v [join $cover ""]
+        if {[string length $v] > 0} {
+            set nope 0
+            html $v
+        } elseif {$nope} {
+            html "No Title"
+        }
+        html {</a> }
+    } else {
         html { <a href="http:} $env(SCRIPT_NAME) /
         html $sessionId {/showfull.egw/} $setNo + $targetNo + $no + full {">}
-        html [join [$zset getWAIS $no headline]]
-        html {</a>}
-        html "<br>\n"
+        html {No Title}
+        html {</a> }
+        html "Error: " $dispError "\n"
+    }
+    html "<br>\n"
+}
+
+proc display-brief {zset no setNo targetNo} {
+    global env
+    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}"
+        }
+        html "${no} Error ${err}${add} <br>\n"
+        return
+    }
+    if {$type != "DB"} {
         return
     }
+    set rtype [$zset recordType $no]
+    switch $rtype {
+        SUTRS {
+            html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
+            html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
+            html [string range [join [$zset getSutrs $no]] 0 70]
+            html "</a><br>\n"
+            return
+        }
+        WAIS {
+            html { <a href="http:} $env(SCRIPT_NAME) / $sessionId
+            html {/showfull.egw/} $setNo + $targetNo + $no + full {">}
+            html [string range [join [$zset getWAIS $no headline]] 0 70]
+            
+            html {</a>}
+            set score [$zset getWAIS $no score]
+            html { Score } $score
+            html "<br>\n"
+            return
+        }
+    }
     if {![catch {
         set author [$zset getMarc $no field 100 * a]
         set corp [$zset getMarc $no field 110 * a]
@@ -140,11 +250,13 @@ proc display-brief {zset no setNo targetNo} {
         if {$p} {
             html ": "
         }
+        html {<it>}
        set nope 1
         foreach v $title {
             html $v
             set nope 0
         }
+        html {</it>}
         if {$nope} {
             set v [join $cover ""]
             if {[string length $v] > 40} {
@@ -168,26 +280,36 @@ proc display-brief {zset no setNo targetNo} {
     html "<br>\n"
 }
 
-proc display-raw {zset no tno} {
+proc display-raw {zset no setNo targetNo} {
     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}"
+    switch $type {
+        SD {
+            set err [lindex [$zset diag $no] 1]
+            set add [lindex [$zset diag $no] 2]
+            if {$add != {}} {
+                set add " :${add}"
+            }
+            html "<h3>${no}</h3>\n"
+            html "Error ${err}${add} <br>\n"
+            return
+        }
+        DB {
+        }
+        default {
+            return
         }
-        html "<h3>${no}</h3>\n"
-        html "Error ${err}${add} <br>\n"
-        return
-    }
-    if {$type != "DB"} {
-        return
     }
     set rtype [$zset recordType $no]
-    if {$rtype == "SUTRS"} {
-        html [join [$zset getSutrs $no]] "<br>\n"
-        return
-    } 
+    switch $rtype {
+        SUTRS {
+            html "<xmp>\n" [join [$zset getSutrs $no]] "\n</xmp>\n"
+            return
+        } 
+        WAIS {
+            html "<xmp>\n" [join [$zset getWAIS $no text]] "\n</xmp>\n"
+            return
+        }
+    }
     if {[catch {set r [$zset getMarc $no line * * *]}]} {
         html "Unknown record type: $rtype <br>\n"
         return
@@ -305,110 +427,11 @@ proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
     }
 }
 
-proc display-full-wais {zset no setNo targetNo} {
+proc display-full-marc {zset no setNo targetNo} {
     global env
     global hist
     global sessionId
 
-    set i 0    
-    set element junk
-    htmlToken l [join [$zset getWAIS $no text]] {
-        if {[string compare [string index $l 0] {<}]} {
-            set data($element) $l
-            continue
-        }
-        switch -exact $l {
-            <ti> {
-                set element title 
-            }
-            <dm> {
-                set element dateOfLastModification
-            }
-            <ci> {
-                set element controlIdentifier
-            }
-            <lc> {
-                set element lastChecked
-            }
-            <by> {
-                set element bytes
-            }
-            <avli> {
-                set element linkage
-            }
-            <cr> {
-                incr i
-            }
-            <li> {
-                set element "$i,linkage"
-            }
-            <cp> {
-                set element "$i,title"
-            }
-            default {
-                set element junk
-            }
-        }
-    }
-    if {![info exists data(title)] || ![info exists data(linkage)]} {
-        html "<pre>" [join [$zset getWAIS $no text]] "\n</pre>\n"
-        return
-    }
-    html {Title: } {<a href="} $data(linkage) {">} $data(title) "</a><br>\n"
-    html {URL: } $data(linkage) "<br>\n"
-    html {Score: } [$zset getWAIS $no score] "<br>\n"
-    html {Lines: } [$zset getWAIS $no lines] "<br>\n"
-    if {[info exists data(bytes)]} {
-        html {Bytes: } $data(bytes) "<br>\n"
-    }
-    if {[info exists data(dateOfLastModification)]} {
-        html {Last modified: } $data(dateOfLastModification) "<br>\n"
-    }
-    if {[info exists data(lastChecked)]} {
-        html {Last checked: } $data(lastChecked) "<br>\n"
-    }
-    html {<a href="} $env(SCRIPT_NAME) / $sessionId {/sameas.egw/}
-    html $setNo + $targetNo + 1 + $hist($setNo,maxPresent) +
-    html [egw_enc [$zset getWAIS $no documentID]] {">}
-    html {Similar WAIS record</a><br>}
-    html "<ul>\n"
-    for {set i 1} {[info exists data($i,linkage)]} {incr i} {
-        html {<li><a href="} $data($i,linkage) {">}
-        html $data($i,title) "</a><br>\n"
-        html "URL: " $data($i,linkage)
-    }
-    html "</ul>\n"
-}
-
-proc display-full {zset no setNo targetNo} {
-    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}"
-        }
-        html "Error ${err}${add} <br>\n"
-        return
-    }
-    if {$type != "DB"} {
-        return
-    }
-    set rtype [$zset recordType $no]
-    if {$rtype == "SUTRS"} {
-        html "<pre>"
-        html [join [$zset getSutrs $no]]
-        html "</pre><br>\n"
-        return
-    }
-    if {$rtype == "WAIS"} {
-        display-full-wais $zset $no $setNo $targetNo
-        return
-    }
-    if {[catch {set r [$zset getMarc $no line * * *]}]} {
-        html "Unknown record type: $rtype <br>\n"
-        return
-    }
     html "<dl>\n"
     set n [dl-marc-field $zset $no 700 a "Author" "Authors" "<br>\n"]
     if {$n == 0} {
@@ -501,6 +524,144 @@ proc display-full {zset no setNo targetNo} {
     html "</dl>\n"
 }
 
+proc display-full-wais {zset no setNo targetNo} {
+    global env
+    global hist
+    global sessionId
+
+    set i 0    
+    set element junk
+    htmlToken l [join [$zset getWAIS $no text]] {
+        if {[string compare [string index $l 0] {<}]} {
+            if {[info exist data($element)]} {
+                set data($element) $data($element)$l
+            } else {
+                set data($element) $l
+            }
+            continue
+        }
+        switch -- $l {
+            <ti> {
+                set element title 
+            }
+            <dm> {
+                set element dateOfLastModification
+            }
+            <ci> {
+                set element controlIdentifier
+            }
+            <lc> {
+                set element lastChecked
+            }
+            <by> {
+                set element bytes
+            }
+            <avli> {
+                set element linkage
+            }
+            <cr> {
+                incr i
+            }
+            <li> {
+                set element "$i,linkage"
+            }
+            <cp> {
+                set element "$i,title"
+            }
+            <ip> {
+                set element ip
+            }
+            default {
+                set element junk
+            }
+        }
+    }
+    if {![info exists data(title)] || ![info exists data(linkage)]} {
+        set nwi 0
+    } else {
+        set nwi 1
+    }
+    if {$nwi} {
+        html {Title: <a href="} $data(linkage) {">} $data(title) "</a><br>\n"
+        html {URL: } $data(linkage) "<br>\n"
+    } else {
+        html {Title: } [join [$zset getWAIS $no headline]] "<br>\n"
+    }
+    html {Score: } [$zset getWAIS $no score] "<br>\n"
+    set lines [$zset getWAIS $no lines]
+    if {$lines > 0} {
+        html {Lines: } $lines "<br>\n"
+    }
+    if {!$nwi} {
+        html "<pre>\n" [join [$zset getWAIS $no text]] "\n</pre>\n"
+        return
+    }
+    if {[info exists data(bytes)]} {
+        html {Bytes: } $data(bytes) "<br>\n"
+    }
+    if {[info exists data(dateOfLastModification)]} {
+        html {Last modified: } $data(dateOfLastModification) "<br>\n"
+    }
+    if {[info exists data(lastChecked)]} {
+        html {Last checked: } $data(lastChecked) "<br>\n"
+    }
+    if {[info exists data(ip)]} {
+        html {<dl><dt>Initial text<dd>} $data(ip) "</dl><br>\n"
+    }
+
+    html {<a href="} $env(SCRIPT_NAME) / $sessionId {/sameas.egw/}
+    html $setNo + $targetNo + 1 + $hist($setNo,maxPresent) +
+    html [egw_enc [$zset getWAIS $no documentID]] {">}
+    html {Similar WAIS record</a><br>}
+    html "<dl><dt>References\n<dd>\n"
+    for {set i 1} {[info exists data($i,linkage)]} {incr i} {
+        html {<a href="} $data($i,linkage) {">}
+        if {[info exists data($i,title)]} {
+            html $data($i,title)
+        } else {
+            html Untitled
+        }
+        html "</a><br>\n"
+    }
+    html "</dl>\n"
+}
+
+proc display-full {zset no setNo targetNo} {
+    set type [$zset type $no]
+    switch $type {
+        SD {
+            set err [lindex [$zset diag $no] 1]
+            set add [lindex [$zset diag $no] 2]
+            if {$add != {}} {
+                set add " :${add}"
+            }
+            html "Error ${err}${add} <br>\n"
+            return
+        }
+        DB {
+        }
+        default {
+            return
+        }
+    }
+    set rtype [$zset recordType $no]
+    switch $rtype {
+        SUTRS {
+            html "<pre>" [join [$zset getSutrs $no]] "</pre><br>\n"
+            return
+        }
+        WAIS {
+            display-full-wais $zset $no $setNo $targetNo
+            return
+        }
+    }
+    if {[catch {set r [$zset getMarc $no line * * *]}]} {
+        html "Unknown record type: $rtype <br>\n"
+        return
+    }
+    display-full-marc $zset $no $setNo $targetNo
+}
+
 
 proc display-rec {from to dfunc setNo targetNo} {
     while {$from <= $to} { 
@@ -1021,8 +1182,12 @@ proc z39msearch {setNo elements start number cache} {
             }
             set zstatus($i) 1
             incr zleft
-            egw_log debug "setNo=$setNo msearch " $hist($setNo,$i,query)
-            z39$i.$setNo search $hist($setNo,$i,query)
+            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]
@@ -1173,6 +1338,10 @@ proc buttons-result-set-s {setNo targetNo setMax startPos after} {
     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
@@ -1181,23 +1350,38 @@ proc display-result-set-m-score {setNo} {
 
     set not $hist($setNo,0,host)
     for {set i 1} {$i <= $not} {incr i} {
-        if {[lindex $status 0] != "DBOSD"} continue
-        
+        if {$zstatus($i) != 2} continue
+        set status [z39$i.$setNo responseStatus]
+        if {[lindex $status 0] != "DBOSD"} continue        
         if {$hist($setNo,$i,offset) > $hist($setNo,maxPresent)} {
-            set nor $hist($setNo,$i,maxPresent)
+            set nor $hist($setNo,maxPresent)
         } else {
             set nor $hist($setNo,$i,offset)
         }
-        for {set j 1} {$j < $not} {incr j} {
-            if {![string compare [z39$i.$setNo recordType] WAIS]} {
-                lappend scoreArray [list \
-                        [z39$i.$setNo getWAIS $j score] $i $setNo $j]
+        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]
+                if {$score > 0} {
+                    lappend scoreArray [list $score $i $j]
+                }
             } else {
-                lappend scoreArray [list 0 $i $setNo $j]
+                lappend scoreArray [list 10 $i $j]
             }
         }
     }
-    html "score merge<br>"
+    if {![info exists scoreArray]} {
+        html "<br><h3>Search produced no result</h3><br>\n"
+        return
+    }
+    set scoreSorted [lsort -command score-sort $scoreArray]
+
+    html "<ul>\n"
+    foreach r $scoreSorted {
+        set i [lindex $r 1]
+        set j [lindex $r 2]
+        display-$hist($setNo,format) z39$i.$setNo $j $setNo $i
+    }
+    html "</ul><br>\n"
 }
 
 proc display-result-set-m-server {setNo} {
@@ -1220,16 +1404,14 @@ proc display-result-set-m-server {setNo} {
             html "Error</h3>\n<dd>NSD$code: $msg: $addinfo"
         } else {
             set r [z39$i.$setNo resultCount]
-            html "$r hits</h3>\n<dd>\n<ul>\n"
+            html "$r hits</h3>\n<dd>\n"
             
             if {$hist($setNo,$i,offset) > $hist($setNo,maxPresent)} {
-                display-rec 1 $hist($setNo,maxPresent) \
-                        display-brief $setNo $i
+                set nor $hist($setNo,maxPresent)
             } else {
-                display-rec 1 $hist($setNo,$i,offset) \
-                        display-brief $setNo $i
+                set nor $hist($setNo,$i,offset)
             }
-            html "</ul>"
+            display-rec 1 $nor display-$hist($setNo,format) $setNo $i
         }
         html "\n"
     }