Readable detailed presentation format as well as raw format.
[egate.git] / www / z39util.tcl
index 20fcec2..82380f1 100644 (file)
@@ -1,5 +1,5 @@
 #
-# $Id: z39util.tcl,v 1.3 1995/11/08 12:42:18 adam Exp $
+# $Id: z39util.tcl,v 1.6 1995/11/09 15:24:37 adam Exp $
 #
 proc saveState {} {
     uplevel #0 {
@@ -8,15 +8,18 @@ proc saveState {} {
         if {$var == "f"} continue
         if {$var == "sessionId"} continue
         if {$var == "errorInfo"} continue
-       set names [array names $var]
-        if {$names != ""} {
+       if {[catch {set names [array names $var]}]} {
+            eval "set v \$${var}"
+            puts $f "set ${var} \{$v\}"
+        } else {
            foreach n $names {
                 eval "set v \$${var}(\$n)"
                puts $f "set ${var}($n) \{$v\}"
            }
-        } else {
-            eval "set v \$${var}"
-            puts $f "set ${var} \{$v\}"
+            catch {
+                eval "set v \$${var}"
+                puts $f "set ${var} \{$v\}"
+            }
         }
     }
     close $f
@@ -78,14 +81,14 @@ proc display-brief {zset no} {
         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>"
+        html {<a href="http:} $env(SCRIPT_NAME) /
+        html $sessionId {/showfull.egw/} $setNo + $no + full 
+        html {"> } $title {</a>} " <i> ${year} </i>"
     }
     html "<br>\n"
 }
 
-proc display-full {zset no} {
+proc display-raw {zset no} {
     set type [$zset type $no]
     if {$type == "SD"} {
         set err [lindex [$zset diag $no] 1]
@@ -100,7 +103,6 @@ proc display-full {zset no} {
     if {$type != "DB"} {
         return
     }
-    html "<h3>${no}</h3>\n"
     set rtype [$zset recordType $no]
     if {$rtype == "SUTRS"} {
         html [join [$zset getSutrs $no]] "<br>\n"
@@ -137,6 +139,164 @@ proc display-full {zset no} {
     }
 }
 
+proc put-marc-contents {cc} {
+    set ref ""
+    if {[string first :// $cc] > 0} {
+        foreach urltype {gopher http ftp mailto} {
+            if {[string first ${urltype}:// $cc] == 0} {
+                set ref $urltype
+                break
+            }
+        }
+    } 
+    if {$ref != ""} {
+        html {<a href="}
+    }
+    html $cc
+    if {$ref != ""} {
+        html {">} $urltype { reference</a>}
+    }
+}
+
+proc dl-marc-field {zset no tag id la lb sep} {
+    set n 0
+    set c [$zset getMarc $no field $tag * $id]
+    set len [llength $c]
+    if {$len == 0} {
+        return 0
+    }
+    if {$len > 1 && "x$lb" != "x"} {
+        html "<dt><b>$lb</b>\n<dd>"
+    } else {
+        html "<dt><b>$la</b>\n<dd>"
+    }
+    foreach cc $c {
+        if {$n > 0} {
+            html $sep
+        }
+        put-marc-contents $cc
+        incr n
+    }
+    return $n
+}
+
+proc dd-marc-field {zset no tag id start stop} {
+    set n 0
+    set c [$zset getMarc $no field $tag * $id]
+    set len [llength $c]
+    if {$len == 0} {
+        return 0
+    }
+    foreach cc $c {
+        html $start
+        put-marc-contents $cc
+        html $stop
+        incr n
+    }
+    return $n
+}
+
+proc dl-marc-field-rec {zset no tag lead start stop startid sep} {
+    set n 0
+    set lines [$zset getMarc $no line $tag * *]
+    foreach line $lines {
+        foreach field [lindex $line 2] {
+            if {$n == 0} {
+                html "<dt><b>$lead</b>"
+                html "\n<dd>"
+            }
+            set id [lindex $field 0]
+            if {$id == $startid} {
+                if {$n > 0} {
+                    html $stop
+                }
+                html $start
+                incr n
+                html [lindex $field 1]
+            } else {
+                html $sep
+                html [lindex $field 1]
+            }
+        }
+    }
+    if {$n > 0} {
+        html $stop
+    }
+}
+
+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}"
+        }
+        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
+    } 
+    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} {
+        set n [dl-marc-field $zset $no 100 a "Author" "Authors" "<br>\n"]
+    }
+    set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
+    if {$n == 0} {
+        set n [dl-marc-field $zset $no 710 a "Corporate Name" {} ", "]
+    }
+    set n [dl-marc-field $zset $no 245 {a} "Title" {} " "]
+    if {$n > 0} {
+        dd-marc-field $zset $no 245 b "<em>" "</em>"
+        dd-marc-field $zset $no 245 c " " ""
+    } else {
+        dl-marc-field $zset $no 245 {[ab]} "Title" {} " "
+    }
+    dl-marc-field $zset $no 520 a "Abstract" {} ", "
+    dl-marc-field $zset $no 072 * "Subject code" "Subject codes" ", "
+    dl-marc-field $zset $no 650 * "Subject" {} ", "
+    dl-marc-field $zset $no 260 * "Publisher" {} " "
+    dl-marc-field $zset $no 300 * "Physical Description" {} " "
+
+    dl-marc-field $zset $no 500 a "Notes" {} ", "
+
+    dl-marc-field-rec $zset $no 510 "References" "" "<br>\n" "a" ", "
+
+    dl-marc-field $zset $no 350 a "Price" {} ", "
+    dl-marc-field $zset $no 362 a "Dates of publication" {} ", "
+    dl-marc-field $zset $no 850 a "Holdings" {} ", "
+
+    dl-marc-field $zset $no 010 a "LC control number" {} ", "
+    dl-marc-field $zset $no 010 b "NUCMC control number" {} ", "
+    dl-marc-field $zset $no 020 a "ISBN" {} ", "
+    dl-marc-field $zset $no 022 a "ISSN" {} ", "
+    set url [$zset getMarc $no field 856 * u]
+    set sp [$zset getMarc $no field 856 * 3]
+    if {"x$url" != "x"} {
+        html "<dt><b>URL</b>\n"
+        if {"x$sp" == "x"} {
+            set sp reference
+        }
+        html {<dd><a href="} $url {">} [join $sp] "</a>\n"
+    }
+    dl-marc-field $zset $no 037 * "Acquisition" {} "<br>\n"
+    dl-marc-field $zset $no 787 {[rstw6]} "Related information" {} "<br>\n"
+    dl-marc-field $zset $no 001 * "Local control number" {} ", "
+    html "</dl>\n"
+}
+
+
 proc display-rec {from to dfunc zz} {
     global setNo
 
@@ -211,9 +371,14 @@ proc z39search {setNo piggy} {
             html "Cannot initialize with target ${host} <br>\n"
             return 0
         }
-        zwait sessionWait
+        if {[catch {zwait sessionWait 60}]} {
+            html "Cannot initialize with target ${host} <br>\n"
+           z39 disconnect
+            return 0
+        }
         if {$sessionWait != "1"} {
             html "Cannot initialize with target ${host} <br>\n"
+           z39 disconnect
             return 0
         }
     }
@@ -238,9 +403,15 @@ proc z39search {setNo piggy} {
     set sessionWait 0
     z39.$setNo search $hist($setNo,query)
 
-    zwait sessionWait
+    if {[catch {zwait sessionWait 600}]} {
+        html "</body></html>\n"
+        z39 disconnect
+        return 0
+    }
+        
     if {$sessionWait != 1} {
         html "</body></html>\n"
+        z39 disconnect
         return 0
     }
     set status [z39.$setNo responseStatus]
@@ -392,11 +563,17 @@ proc z39present {setNo setOffset setMax dfunc} {
         if {$got < $toGet} {
             set sessionWait 0
             z39.$setNo present $setOffset $toGet
-            zwait sessionWait
+            if {[catch {zwait sessionWait 300}]} {
+               z39 disconnect
+                break
+           }
             if {$sessionWait != "1"} {
                 break
             }
             set got [z39.$setNo numberOfRecordsReturned]
+            if {$got <= 0} {
+                break
+            }
         }
         display-rec $setOffset [expr $got + $setOffset - 1] $dfunc z39
         set setOffset [expr $got + $setOffset]
@@ -417,7 +594,7 @@ proc z39history {} {
     }
     html "<hr><h3>History</h3><dl>\n"
     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
-        html {<dt> <a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
+        html {<dt> <a href="http:} $env(SCRIPT_NAME)
         html / $sessionId {/search.egw/} $setNo + 1
         html + [expr $hist($setNo,maxPresent) - 1]
         html {"> } [lindex $targets($hist($setNo,host)) 0]
@@ -437,4 +614,4 @@ proc z39history {} {
         html "\n"
     }
     html "</dl>\n"
-}
\ No newline at end of file
+}