Remove leading "tcp:" from target string given to ZAP.
[irspy-moved-to-github.git] / web / htdocs / details / full.mc
1 %# $Id: full.mc,v 1.30 2007-07-03 13:10:50 mike Exp $
2 <%args>
3 $id
4 </%args>
5 <%perl>
6 my $conn = new ZOOM::Connection("localhost:8018/IR-Explain---1");
7 $conn->option(elementSetName => "zeerex");
8 my $query = cql_target($id);
9 my $rs = $conn->search(new ZOOM::Query::CQL($query));
10 my $n = $rs->size();
11 if ($n == 0) {
12     $m->comp("/details/error.mc",
13              title => "Error", message => "No such ID '$id'");
14 } else {
15     my $xc = irspy_xpath_context($rs->record(0));
16     my @fields = (
17                   [ Name => "e:databaseInfo/e:title",
18                     lang => "en", primary => "true" ],
19                   [ Country => "i:status/i:country" ],
20                   [ "Last Checked" => "i:status/i:probe[last()]" ],
21                   [ Protocol => "e:serverInfo/\@protocol" ],
22                   [ Host => "e:serverInfo/e:host" ],
23                   [ Port => "e:serverInfo/e:port" ],
24                   [ "Database Name" => "e:serverInfo/e:database" ],
25                   [ "Type of Library" => "i:status/i:libraryType" ],
26 #                 [ "Username (if needed)" => "e:serverInfo/e:authentication/e:user" ],
27 #                 [ "Password (if needed)" => "e:serverInfo/e:authentication/e:password" ],
28                   [ "Server ID" => 'i:status/i:serverImplementationId/@value' ],
29                   [ "Server Name" => 'i:status/i:serverImplementationName/@value' ],
30                   [ "Server Version" => 'i:status/i:serverImplementationVersion/@value' ],
31                   [ Description => "e:databaseInfo/e:description",
32                     lang => "en", primary => "true" ],
33                   [ Author => "e:databaseInfo/e:author" ],
34                   [ Contact => "e:databaseInfo/e:contact" ],
35                   [ "URL to Hosting Organisation" => "i:status/i:hostURL" ],
36                   [ Extent => "e:databaseInfo/e:extent" ],
37                   [ History => "e:databaseInfo/e:history" ],
38                   [ "Language of Records" => "e:databaseInfo/e:langUsage" ],
39                   [ Restrictions => "e:databaseInfo/e:restrictions" ],
40                   [ Subjects => "e:databaseInfo/e:subjects" ],
41                   [ "Implementation ID" => "i:status/i:implementationId" ],
42                   [ "Implementation Name" => "i:status/i:implementationName" ],
43                   [ "Implementation Version" => "i:status/i:implementationVersion" ],
44                   [ "Reliability/reliability" => \&calc_reliability, $xc ],
45                   [ "Services" => \&calc_init_options, $xc ],
46                   [ "Bib-1 Use attributes" => \&calc_ap, $xc, "bib-1" ],
47                   [ "Dan-1 Use attributes" => \&calc_ap, $xc, "dan-1" ],
48                   [ "Operators" => \&calc_boolean, $xc ],
49                   [ "Named Result Sets" => \&calc_nrs, $xc ],
50                   [ "Record syntaxes" => \&calc_recsyn, $xc ],
51                   [ "Explain" => \&calc_explain, $xc ],
52                   );
53     my $title = $xc->find("e:databaseInfo/e:title");
54 </%perl>
55      <h2><% xml_encode($title, "") %></h2>
56      <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
57 <%perl>
58     foreach my $ref (@fields) {
59         my($caption, $xpath, @args) = @$ref;
60         my($data, $linkURL);
61         if (ref $xpath && ref($xpath) eq "CODE") {
62             ($data, $linkURL) = &$xpath($id, @args);
63         } else {
64             $data = $xc->find($xpath);
65         }
66         if ($data) {
67             print "      <tr>\n";
68             $caption =~ s/\/(.*)//;
69             my $help = $1;
70             my($linkstart, $linkend) = ("", "");
71             if (defined $linkURL) {
72                 $linkstart = '<a href="' . xml_encode($linkURL) . '">';
73                 $linkend = "</a>";
74             }
75 </%perl>
76        <th><% xml_encode($caption) %><%
77         !defined $help ? "" : $m->comp("/help/link.mc", help =>"info/$help")
78         %></th>
79        <td><% $linkstart . xml_encode($data) . $linkend %></td>
80       </tr>
81 %       }
82 %   }
83      </table>
84      <p>
85 % my $target = irspy_identifier2target($id);
86 % $target =~ s/^tcp://; # Apparently ZAP can't handle the leading "tcp:"
87       <a href="<% xml_encode("http://targettest.indexdata.com/targettest/search/index.zap?" .
88         join("&",
89              "target=" . uri_escape_utf8($target),
90              "name=" . uri_escape_utf8($title),
91              "attr=" . join(" ", _list_ap($xc, "bib-1")),
92              "formats=" . calc_recsyn($id, $xc, " ")))
93         %>">Search this target.</a>
94      </p>
95 % }
96 <%perl>
97
98 sub calc_reliability {
99     my($id, $xc) = @_;
100
101     my @allpings = $xc->findnodes("i:status/i:probe");
102     my $nall = @allpings;
103     return "[untested]" if $nall == 0;
104     my @okpings = $xc->findnodes('i:status/i:probe[@ok = "1"]');
105     my $nok = @okpings;
106     return "$nok/$nall = " . int(100*$nok/$nall) . "%";
107 }
108
109 sub calc_init_options {
110     my($id, $xc) = @_;
111
112     my @ops;
113     my @nodes = $xc->findnodes('e:configInfo/e:supports/@type');
114     foreach my $node (@nodes) {
115         my $type = $node->value();
116         if ($type =~ s/^z3950_//) {
117             push @ops, $type;
118         }
119     }
120
121     return join(", ", @ops);
122 }
123
124 sub calc_ap {
125     my($id, $xc, $set) = @_;
126
127     my @aps = _list_ap($xc, $set);
128     my $n = @aps;
129     return "[none]" if $n == 0;
130
131     my $res = "";
132     my($first, $last);
133     foreach my $ap (@aps) {
134         if (!defined $first) {
135             $first = $last = $ap;
136         } elsif ($ap == $last+1) {
137             $last++;
138         } else {
139             # Got a complete range
140             $res .= ", " if $res ne "";
141             $res .= "$first";
142             $res .= "-$last" if $last > $first;
143             $first = $last = $ap;
144         }
145     }
146
147     # Leftovers
148     if (defined $first) {
149         $res .= ", " if $res ne "";
150         $res .= "$first";
151         $res .= "-$last" if $last > $first;
152     }
153
154     return ("$n access points: $res",
155             "/ap.html?id=$id&set=$set");
156 }
157
158 sub _list_ap {
159     my($xc, $set) = @_;
160
161     my $expr = 'e:indexInfo/e:index[@search = "true"]/e:map/e:attr[
162         @set = "'.$set.'" and @type = "1"]';
163     my @nodes = $xc->findnodes($expr);
164     return sort { $a <=> $b } map { $_->findvalue(".") } @nodes;
165 }
166
167 sub calc_boolean {
168     my($id, $xc) = @_;
169
170     ### Note that we are currently interrogating an IRSpy extension.
171     #   The standard ZeeRex record should be extended with a
172     #   "supports" type for this.
173     my @nodes = $xc->findnodes('i:status/i:boolean[@ok = "1"]');
174     my $res = join(", ", map { $_->findvalue('@operator') } @nodes);
175     $res = "[none]" if $res eq "";
176     return $res;
177 }
178
179 sub calc_nrs {
180     my($id, $xc) = @_;
181
182     my @nodes = $xc->findnodes('i:status/i:named_resultset[@ok = "1"]');
183     return @nodes ? "Yes" : "No";
184 }
185
186 sub calc_recsyn {
187     my($id, $xc, $sep) = @_;
188     $sep = ", " if !defined $sep;
189
190     my @nodes = $xc->findnodes('e:recordInfo/e:recordSyntax');
191     my $res = join($sep, map { $_->findvalue('@name') } @nodes);
192     $res = "[none]" if $res eq "";
193     return $res;
194 }
195
196 sub calc_explain {
197     my($id, $xc) = @_;
198
199     my @nodes = $xc->findnodes('i:status/i:explain[@ok = "1"]');
200     my $res = join(", ", map { $_->findvalue('@category') } @nodes);
201     $res = "[none]" if $res eq "";
202     return $res;
203 }
204 </%perl>