443181e62818b6214cb49b64a8a317c069f7a9ca
[irspy-moved-to-github.git] / web / htdocs / details / found.mc
1 %# $Id: found.mc,v 1.33 2009-04-15 18:16:46 wosch 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 # Just make this once; forge the connection on first use
23 our $conn = undef;
24 </%once>
25 <%perl>
26 my %params = map { ( $_, utf8param($r, $_)) } grep { &utf8param($r, $_) } &utf8param($r);
27 my $query;
28 if ($params{_query}) {
29     $query = $params{_query};
30 } else {
31     $query = "";
32     foreach my $key (keys %params) {
33         next if $key =~ /^_/;
34         my $val = $params{$key};
35         next if $val eq "";
36         $query .= " and " if $query ne "";
37         $query .= "$key = ($val)";
38     }
39 }
40 $query = 'cql.allRecords=1' if $query eq "";
41
42 my $sort = $params{"_sort"};
43 if ($sort) {
44     my $modifiers = "";
45     if ($sort =~ s/(\/.*)//) {
46         $modifiers = $1;
47     }
48     $query .= " or $sort=/sort";
49     $query .= "-desc" if $params{_desc};
50     $query .= $modifiers;
51     $query .= " 0";
52 }
53
54 my $tried_to_open = 0;
55 if (!defined $conn) {
56   OPEN:
57     my $db = ZOOM::IRSpy::connect_to_registry();
58     $conn = new ZOOM::Connection($db);
59     $conn->option(elementSetName => "zeerex");
60     $conn->option(count => 20);
61 }
62
63 my $rs;
64 eval { $rs = $conn->search(new ZOOM::Query::CQL($query)) };
65 if ($@ && ref $@ && $@->isa('ZOOM::Exception') &&
66     $@->code() == ZOOM::Error::CONNECTION_LOST && !$tried_to_open) {
67     $tried_to_open = 1;
68     goto OPEN;
69 } elsif ($@) {
70     die $@;
71 }
72
73 my $n = $rs->size();
74
75 my $skip = $params{"_skip"} || 0;
76 my $count = $params{"_count"} || 10;
77
78 my $first = $skip+1;
79 my $last = $first+$count-1;
80 $last = $n if $last > $n;
81 </%perl>
82      <form method="get" action=""><p>
83       <input type="text" name="_query" size="60" value="<% xml_encode($query) %>"/>
84       <input type="submit" name="_search" value="Search"/>
85      </p></form>
86      <p>
87 % if ($n == 0) {
88       No matches
89 % } elsif ($first > $n) {
90 %# "Can't happen"
91       Past end of <% $n %> records
92 % } else {
93       Records <% $first %> to <% $last %> of <% $n %><br/>
94 <%perl>
95 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
96 print_navlink(\%params, $last < $n, "Next", $skip+$count);
97 </%perl>
98 % }
99      </p>
100 % if ($n > 0 && $first <= $n) {
101      <table width="100%">
102       <tr class="thleft">
103        <th>#</th>
104        <th>Title</th>
105        <th>Host Connection Reliability <& /help/link.mc, help => "info/reliability" &>
106        </th>
107        <th>Host</th>
108        <th>Port</th>
109        <th>DB</th>
110        <th></th>
111        <th></th>
112       </tr>
113 % my @ids;
114 % $rs->records($first-1, $last-$first+1); ## Force pre-cache
115 % foreach my $i ($first .. $last) {
116 <%perl>
117 my $xc = irspy_xpath_context($rs->record($i-1));
118 my $title = $xc->find("e:databaseInfo/e:title") || "[UNTITLED]";
119 my $reliability = calc_reliability_string($xc);
120 my $host = $xc->find("e:serverInfo/e:host");
121 my $port = $xc->find("e:serverInfo/e:port");
122 my $db = $xc->find("e:serverInfo/e:database");
123 my $id = irspy_record2identifier($xc);
124 push @ids, $id;
125 </%perl>
126       <tr style="background: <% ($i % 2) ? '#ffffc0' : 'white' %>">
127        <td><% $i %></td>
128        <td><a href="<% xml_encode("/full.html?id=" . uri_escape($id))
129                 %>"><% xml_encode($title) %></a></td>
130        <td><% xml_encode($reliability, "", { nbsp => 1 }) %></td>
131        <td><% xml_encode($host, "") %></td>
132        <td><% xml_encode($port, "") %></td>
133        <td><% xml_encode($db, "") %></td>
134        <td>
135         <a href="<% xml_encode("/admin/check.html?id=" . uri_escape($id))
136                 %>" title="Test this target">Test</a
137         >&nbsp;<a href="<% xml_encode("/admin/edit.html?op=edit&id=" .
138                 uri_escape($id))
139                 %>" title="Edit this target's record">Edit</a
140         >&nbsp;<a href="<% xml_encode("/raw.html?id=" . uri_escape($id))
141                 %>" title="Raw XML record">XML</a>
142        </td>
143       </tr>
144 % }
145      </table>
146 <%perl>
147 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
148 print_navlink(\%params, $last < $n, "Next", $skip+$count);
149 </%perl>
150      <p>
151       <a href="<% "/admin/check.html?" .
152         xml_encode(join("&", map { "id=" . uri_escape($_) } @ids))
153         %>">[Test all targets on this list]</a>
154      </p>
155      <p>
156       <a href="<% "/stats.html?query=" . xml_encode(uri_escape($query))
157         %>">[Statistics for targets on this list]</a>
158      </p>
159 % }