Use new utility function irspy_xpath_context()
[irspy-moved-to-github.git] / web / htdocs / details / found.mc
1 %# $Id: found.mc,v 1.15 2006-10-20 16:57:23 mike Exp $
2 <%once>
3 use URI::Escape;
4
5 sub print_navlink {
6     my($params, $cond, $caption, $skip) = @_;
7
8     if ($cond) {
9         print('     <a href="', navlink($params, $caption, $skip),
10               '"', ">$caption</a>\n");
11     } else {
12         print qq[     <span class="disabled">$caption</span>\n];
13     }
14 }
15
16 sub navlink {
17     my($params, $caption, $skip) = @_;
18     local $params->{_skip} = $skip;
19     my $url = "?" . join("&", map { "$_=" . $params->{$_}  } sort keys %$params);
20     $url = xml_encode($url);
21     return $url;
22 }
23
24 # Just make this once; forge the connection on first use
25 our $conn = undef;
26 </%once>
27 <%perl>
28 my %params = map { ( $_, $r->param($_)) } grep { $r->param($_) } $r->param();
29 my $query;
30 if ($params{_query}) {
31     $query = $params{_query};
32 } else {
33     $query = "";
34     foreach my $key (keys %params) {
35         next if $key =~ /^_/;
36         my $val = $params{$key};
37         next if $val eq "";
38         $query .= " and " if $query ne "";
39         $query .= "$key = ($val)";
40     }
41 }
42 $query = 'cql.allRecords=x' if $query eq "";
43
44 my $sort = $params{"_sort"};
45 if ($sort) {
46     my $modifiers = "";
47     if ($sort =~ s/(\/.*)//) {
48         $modifiers = $1;
49     }
50     $query .= " or $sort=/sort";
51     $query .= "-desc" if $params{_desc};
52     $query .= $modifiers;
53     $query .= " 0";
54 }
55
56 my $tried_to_open = 0;
57 if (!defined $conn) {
58   OPEN:
59     $conn = new ZOOM::Connection("localhost:3313/IR-Explain---1");
60     $conn->option(elementSetName => "zeerex");
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="">
83       <input type="text" name="_query" size="60" value="<% xml_encode($query) %>"/>
84       <input type="submit" name="_search" value="Search"/>
85      </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>Author</th>
106        <th>Host</th>
107        <th>Port</th>
108        <th>DB</th>
109        <th></th>
110        <th></th>
111       </tr>
112 % my @ids;
113 % foreach my $i ($first .. $last) {
114 <%perl>
115 my $rec = $rs->record($i-1);
116 my $xc = irspy_xpath_context($rec);
117 my $title = $xc->find("e:databaseInfo/e:title");
118 my $author = $xc->find("e:databaseInfo/e:author");
119 my $host = $xc->find("e:serverInfo/e:host");
120 my $port = $xc->find("e:serverInfo/e:port");
121 my $db = $xc->find("e:serverInfo/e:database");
122 my $id = $xc->find("concat(e:serverInfo/e:host, ':',
123                            e:serverInfo/e:port, '/',
124                            e:serverInfo/e:database)");
125 push @ids, $id;
126 </%perl>
127       <tr style="background: <% ($i % 2) ? '#ffffc0' : 'white' %>">
128        <td><% $i %></td>
129        <td><a href="<% xml_encode("/full.html?id=" . uri_escape($id))
130                 %>"><% xml_encode($title) %></a></td>
131        <td><% xml_encode($author) %></td>
132        <td><% xml_encode($host) %></td>
133        <td><% xml_encode($port) %></td>
134        <td><% xml_encode($db) %></td>
135        <td>
136         <a href="<% xml_encode("/check.html?id=" . uri_escape($id))
137                 %>" title="Test this target"><b>T</b></a
138         >&nbsp;<a href="<% xml_encode("/edit.html?id=" . uri_escape($id))
139                 %>" title="Edit this target's record"><b>E</b></a
140         >&nbsp;<a href="<% xml_encode("/raw.html?id=" . uri_escape($id))
141                 %>" title="Raw XML record"><b>X</b></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="<% "/check.html?" .
152         xml_encode(join("&", map { "id=" . uri_escape($_) } @ids))
153         %>">[Test all targets on this list]</a>
154      </p>
155 % }