Support new "irspy_data" log-level to register information written to
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
1
2 package ZOOM::IRSpy::Record;
3 ### I don't think there's any reason for this to be separate from
4 #   ZOOM::IRSpy::Connection, now that the correspondence is always 1:1
5
6 use 5.008;
7 use strict;
8 use warnings;
9
10 use Scalar::Util;
11 use XML::LibXML;
12 use XML::LibXML::XPathContext;
13 use ZOOM::IRSpy::Utils qw(xml_encode isodate irspy_xpath_context);
14
15 =head1 NAME
16
17 ZOOM::IRSpy::Record - record describing a target for IRSpy
18
19 =head1 SYNOPSIS
20
21  ## To follow
22
23 =head1 DESCRIPTION
24
25 I<## To follow>
26
27 =cut
28
29 sub new {
30     my $class = shift();
31     my($irspy, $target, $zeerex) = @_;
32
33     if (!defined $zeerex) {
34         $zeerex = _empty_zeerex_record($target);
35     }
36
37     ### Parser should be in the IRSpy object
38     my $parser = new XML::LibXML();
39     my $this = bless {
40         irspy => $irspy,
41         target => $target,
42         parser => $parser,
43         zeerex => $parser->parse_string($zeerex)->documentElement(),
44         zoom_error => { TIMEOUT => 0 },
45     }, $class;
46
47     #Scalar::Util::weaken($this->{irspy});
48     #Scalar::Util::weaken($this->{parser});
49
50     return $this;
51 }
52
53 sub zoom_error { return shift->{'zoom_error'} }
54
55 sub _empty_zeerex_record {
56     my($target) = @_;
57
58     my($protocol, $host, $port, $db) =
59         ZOOM::IRSpy::_parse_target_string($target);
60
61     my $xprotocol = xml_encode($protocol);
62     my $xhost = xml_encode($host);
63     my $xport = xml_encode($port);
64     my $xdb = xml_encode($db);
65     return <<__EOT__;
66 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
67  <serverInfo protocol="$xprotocol">
68   <host>$xhost</host>
69   <port>$xport</port>
70   <database>$xdb</database>
71  </serverInfo>
72 </explain>
73 __EOT__
74 }
75
76
77 sub append_entry {
78     my $this = shift();
79     my($xpath, $frag) = @_;
80
81     #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
82     my $xc = $this->xpath_context();
83     $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
84     $xc->registerNs(irspy => $ZOOM::IRSpy::Utils::IRSPY_NS);
85
86     my @nodes = $xc->findnodes($xpath);
87     if (@nodes == 0) {
88         # Make the node that we're inserting into, if possible.  A
89         # fully general version would work its way through each
90         # component of the XPath, but for now we just treat it as a
91         # single chunk to go inside the top-level node.
92         $this->_half_decent_appendWellBalancedChunk($xc->getContextNode(),
93                                                     "<$xpath></$xpath>");
94         @nodes = $xc->findnodes($xpath);
95         die("still no matches for '$xpath' after creating: can't append")
96             if @nodes == 0;
97     }
98
99     $this->{irspy}->log("warn",
100                         scalar(@nodes), " matches for '$xpath': using first")
101         if @nodes > 1;
102
103     $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
104 }
105
106 sub xpath_context {
107     my $this = shift();
108
109     return irspy_xpath_context($this->{zeerex});
110 }
111
112 sub store_result {
113     my ($this, $type, %info) = @_;
114     my $xml = "<irspy:$type";
115
116     foreach my $key (keys %info) {
117         $xml .= " $key=\"" . xml_encode($info{$key}) . "\"";
118     }
119
120     $xml .= ">" . isodate(time()) . "</irspy:$type>";
121
122     $this->{irspy}->log("irspy_data", $xml);
123     $this->append_entry('irspy:status', $xml . "\n");
124 }
125
126
127 # *sigh*
128 #
129 # _Clearly_ the right way to append a well-balanced chunk of XML to
130 # a node's children is to call appendWellBalancedChunk() from the
131 # XML::LibXML::Element class.  However, this fails in the common case
132 # where the ZeeRex record we're working with doesn't declare the
133 # "irspy" namespace that the inserted fragments use.
134 #
135 # To my utter astonishment it seems that XML::LibXML (as of version
136 # 1.58, 31st March 2004) doesn't provide ANY way to register a
137 # namespace for parsing, which makes the parse_balanced_chunk()
138 # function that appendWellBalancedChunk() uses effectively useless.
139 # It _is_ possible to use setNamespace() on a node, to register a new
140 # namespace mapping for that node -- but that only affects pre-parsed
141 # trees, and is no use for parsing.  Hence the following pair of lines
142 # DOES NOT WORK:
143 #       $node->setNamespace($ZOOM::IRSpy::Utils::IRSPY_NS, "irspy", 0);
144 #       $node->appendWellBalancedChunk($frag);
145 #
146 # Instead I have to go the long way round, hence this method.  I have
147 # two candidate re-implementations, of which the former is marginally
148 # less loathsome, but does require that the excess namespace
149 # declarations be factored out later -- as least, if you want neat
150 # output.
151 #
152 sub _half_decent_appendWellBalancedChunk {
153     my $this = shift();
154     my($node, $frag) = @_;
155
156     if (1) {
157         $frag =~ s,>, xmlns:irspy="$ZOOM::IRSpy::Utils::IRSPY_NS">,;
158         eval {
159             $node->appendWellBalancedChunk($frag);
160         }; if ($@) {
161             print STDERR "died while trying to appendWellBalancedChunk(), probably due to bad XML:\n$frag";
162             die $@;
163         }
164         return;
165     }
166
167     # Instead -- and to call this brain-damaged would be an insult
168     # to all those fine people out there with actual brain damage
169     # -- I have to "parse" the XML fragment myself and insert the
170     # resulting hand-build DOM tree.  Someone shoot me now.
171     my($open, $content, $close) = $frag =~ /^<(.*?)>(.*)<\/(.*?)>$/;
172     die "can't 'parse' XML fragment '$frag'"
173         if !defined $open;
174     my($tag, $attrs) = $open =~ /(.*?)\s(.*)/;
175     $tag = $open if !defined $tag;
176     die "mismatched XML start/end <$open>...<$close>"
177         if $close ne $tag;
178     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
179     die "## no code yet to make DOM node";
180 }
181
182
183 =head1 SEE ALSO
184
185 ZOOM::IRSpy
186
187 =head1 AUTHOR
188
189 Mike Taylor, E<lt>mike@indexdata.comE<gt>
190
191 =head1 COPYRIGHT AND LICENSE
192
193 Copyright (C) 2006 by Index Data ApS.
194
195 This library is free software; you can redistribute it and/or modify
196 it under the same terms as Perl itself, either Perl version 5.8.7 or,
197 at your option, any later version of Perl 5 you may have available.
198
199 =cut
200
201 1;