Use utf8param($r, $key) instead of $r->param($key) throughout.
[irspy-moved-to-github.git] / web / htdocs / details / found.mc
1 %# $Id: found.mc,v 1.30 2007-05-03 14:15:56 mike Exp $
2 <%once>
3 sub print_navlink {
4     my($params, $cond, $caption, $skip) = @_;
5
6     if ($cond) {
7         print('     <a href="', navlink($params, $caption, $skip),
8               '"', ">$caption</a>\n");
9     } else {
10         print qq[     <span class="disabled">$caption</span>\n];
11     }
12 }
13
14 sub navlink {
15     my($params, $caption, $skip) = @_;
16     local $params->{_skip} = $skip;
17     my $url = "?" . join("&", map { "$_=" . $params->{$_}  } sort keys %$params);
18     $url = xml_encode($url);
19     return $url;
20 }
21
22 # Identical to the same-named function in full.mc
23 # So maybe this should go into IRSpy::Utils.pm?
24 #
25 sub calc_reliability {
26     my($xc) = @_;
27
28     my @allpings = $xc->findnodes("i:status/i:probe");
29     my $nall = @allpings;
30     return "[untested]" if $nall == 0;
31     my @okpings = $xc->findnodes('i:status/i:probe[@ok = "1"]');
32     my $nok = @okpings;
33     return "$nok/$nall = " . int(100*$nok/$nall) . "%";
34 }
35
36
37 # Just make this once; forge the connection on first use
38 our $conn = undef;
39 </%once>
40 <%perl>
41 my %params = map { ( $_, utf8param($r, $_)) } grep { $r->param($_) } $r->param();
42 my $query;
43 if ($params{_query}) {
44     $query = $params{_query};
45 } else {
46     $query = "";
47     foreach my $key (keys %params) {
48         next if $key =~ /^_/;
49         my $val = $params{$key};
50         next if $val eq "";
51         $query .= " and " if $query ne "";
52         $query .= "$key = ($val)";
53     }
54 }
55 $query = 'cql.allRecords=1' if $query eq "";
56
57 my $sort = $params{"_sort"};
58 if ($sort) {
59     my $modifiers = "";
60     if ($sort =~ s/(\/.*)//) {
61         $modifiers = $1;
62     }
63     $query .= " or $sort=/sort";
64     $query .= "-desc" if $params{_desc};
65     $query .= $modifiers;
66     $query .= " 0";
67 }
68
69 my $tried_to_open = 0;
70 if (!defined $conn) {
71   OPEN:
72     $conn = new ZOOM::Connection("localhost:8018/IR-Explain---1");
73     $conn->option(elementSetName => "zeerex");
74 }
75
76 my $rs;
77 eval { $rs = $conn->search(new ZOOM::Query::CQL($query)) };
78 if ($@ && ref $@ && $@->isa('ZOOM::Exception') &&
79     $@->code() == ZOOM::Error::CONNECTION_LOST && !$tried_to_open) {
80     $tried_to_open = 1;
81     goto OPEN;
82 } elsif ($@) {
83     die $@;
84 }
85
86 my $n = $rs->size();
87
88 my $skip = $params{"_skip"} || 0;
89 my $count = $params{"_count"} || 10;
90
91 my $first = $skip+1;
92 my $last = $first+$count-1;
93 $last = $n if $last > $n;
94 </%perl>
95      <form method="get" action=""><p>
96       <input type="text" name="_query" size="60" value="<% xml_encode($query) %>"/>
97       <input type="submit" name="_search" value="Search"/>
98      </p></form>
99      <p>
100 % if ($n == 0) {
101       No matches
102 % } elsif ($first > $n) {
103 %# "Can't happen"
104       Past end of <% $n %> records
105 % } else {
106       Records <% $first %> to <% $last %> of <% $n %><br/>
107 <%perl>
108 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
109 print_navlink(\%params, $last < $n, "Next", $skip+$count);
110 </%perl>
111 % }
112      </p>
113 % if ($n > 0 && $first <= $n) {
114      <table width="100%">
115       <tr class="thleft">
116        <th>#</th>
117        <th>Title</th>
118        <th>Reliability <& /help/link.mc, help => "info/reliability" &>
119        </th>
120        <th>Host</th>
121        <th>Port</th>
122        <th>DB</th>
123        <th></th>
124        <th></th>
125       </tr>
126 % my @ids;
127 % foreach my $i ($first .. $last) {
128 <%perl>
129 my $xc = irspy_xpath_context($rs->record($i-1));
130 my $title = $xc->find("e:databaseInfo/e:title") || "[UNTITLED]";
131 my $reliability = calc_reliability($xc);
132 my $host = $xc->find("e:serverInfo/e:host");
133 my $port = $xc->find("e:serverInfo/e:port");
134 my $db = $xc->find("e:serverInfo/e:database");
135 my $id = irspy_record2identifier($xc);
136 push @ids, $id;
137 </%perl>
138       <tr style="background: <% ($i % 2) ? '#ffffc0' : 'white' %>">
139        <td><% $i %></td>
140        <td><a href="<% xml_encode("/full.html?id=" . uri_escape($id))
141                 %>"><% xml_encode($title) %></a></td>
142        <td><% xml_encode($reliability, "", { nbsp => 1 }) %></td>
143        <td><% xml_encode($host, "") %></td>
144        <td><% xml_encode($port, "") %></td>
145        <td><% xml_encode($db, "") %></td>
146        <td>
147         <a href="<% xml_encode("/admin/check.html?id=" . uri_escape($id))
148                 %>" title="Test this target">Test</a
149         >&nbsp;<a href="<% xml_encode("/admin/edit.html?op=edit&id=" .
150                 uri_escape($id))
151                 %>" title="Edit this target's record">Edit</a
152         >&nbsp;<a href="<% xml_encode("/raw.html?id=" . uri_escape($id))
153                 %>" title="Raw XML record">XML</a>
154        </td>
155       </tr>
156 % }
157      </table>
158 <%perl>
159 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
160 print_navlink(\%params, $last < $n, "Next", $skip+$count);
161 </%perl>
162      <p>
163       <a href="<% "/admin/check.html?" .
164         xml_encode(join("&", map { "id=" . uri_escape($_) } @ids))
165         %>">[Test all targets on this list]</a>
166      </p>
167      <p>
168       <a href="<% "/stats.html?query=" . xml_encode(uri_escape($query))
169         %>">[Statistics for targets on this list]</a>
170      </p>
171 % }