7df2dff5a1518242c5d946c62f38ba1b94f328b6
[irspy-moved-to-github.git] / web / htdocs / details / found.mc
1 %# $Id: found.mc,v 1.20 2006-11-16 17:10:30 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 # Just make this once; forge the connection on first use
23 our $conn = undef;
24 </%once>
25 <%perl>
26 my %params = map { ( $_, $r->param($_)) } grep { $r->param($_) } $r->param();
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=x' 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     $conn = new ZOOM::Connection("localhost:3313/IR-Explain---1");
58     $conn->option(elementSetName => "zeerex");
59 }
60
61 my $rs;
62 eval { $rs = $conn->search(new ZOOM::Query::CQL($query)) };
63 if ($@ && ref $@ && $@->isa('ZOOM::Exception') &&
64     $@->code() == ZOOM::Error::CONNECTION_LOST && !$tried_to_open) {
65     $tried_to_open = 1;
66     goto OPEN;
67 } elsif ($@) {
68     die $@;
69 }
70
71 my $n = $rs->size();
72
73 my $skip = $params{"_skip"} || 0;
74 my $count = $params{"_count"} || 10;
75
76 my $first = $skip+1;
77 my $last = $first+$count-1;
78 $last = $n if $last > $n;
79 </%perl>
80      <form method="get" action=""><p>
81       <input type="text" name="_query" size="60" value="<% xml_encode($query) %>"/>
82       <input type="submit" name="_search" value="Search"/>
83      </p></form>
84      <p>
85 % if ($n == 0) {
86       No matches
87 % } elsif ($first > $n) {
88 %# "Can't happen"
89       Past end of <% $n %> records
90 % } else {
91       Records <% $first %> to <% $last %> of <% $n %><br/>
92 <%perl>
93 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
94 print_navlink(\%params, $last < $n, "Next", $skip+$count);
95 </%perl>
96 % }
97      </p>
98 % if ($n > 0 && $first <= $n) {
99      <table width="100%">
100       <tr class="thleft">
101        <th>#</th>
102        <th>Title</th>
103        <th>Author</th>
104        <th>Host</th>
105        <th>Port</th>
106        <th>DB</th>
107        <th></th>
108        <th></th>
109       </tr>
110 % my @ids;
111 % foreach my $i ($first .. $last) {
112 <%perl>
113 my $xc = irspy_xpath_context($rs->record($i-1));
114 my $title = $xc->find("e:databaseInfo/e:title");
115 my $author = $xc->find("e:databaseInfo/e:author");
116 my $host = $xc->find("e:serverInfo/e:host");
117 my $port = $xc->find("e:serverInfo/e:port");
118 my $db = $xc->find("e:serverInfo/e:database");
119 my $id = $xc->find("concat(e:serverInfo/e:host, ':',
120                            e:serverInfo/e:port, '/',
121                            e:serverInfo/e:database)");
122 push @ids, $id;
123 </%perl>
124       <tr style="background: <% ($i % 2) ? '#ffffc0' : 'white' %>">
125        <td><% $i %></td>
126        <td><a href="<% xml_encode("/full.html?id=" . uri_escape($id))
127                 %>"><% xml_encode($title, "[untitled]") %></a></td>
128        <td><% xml_encode($author, "") %></td>
129        <td><% xml_encode($host, "") %></td>
130        <td><% xml_encode($port, "") %></td>
131        <td><% xml_encode($db, "") %></td>
132        <td>
133         <a href="<% xml_encode("/check.html?id=" . uri_escape($id))
134                 %>" title="Test this target">Test</a
135         >&nbsp;<a href="<% xml_encode("/edit.html?id=" . uri_escape($id))
136                 %>" title="Edit this target's record">Edit</a
137         >&nbsp;<a href="<% xml_encode("/raw.html?id=" . uri_escape($id))
138                 %>" title="Raw XML record">XML</a>
139        </td>
140       </tr>
141 % }
142      </table>
143 <%perl>
144 print_navlink(\%params, $skip > 0, "Prev", $count < $skip ? $skip-$count : 0);
145 print_navlink(\%params, $last < $n, "Next", $skip+$count);
146 </%perl>
147      <p>
148       <a href="<% "/check.html?" .
149         xml_encode(join("&", map { "id=" . uri_escape($_) } @ids))
150         %>">[Test all targets on this list]</a>
151      </p>
152 % }