-# $Id: Utils.pm,v 1.14 2006-11-14 16:21:49 mike Exp $
+# $Id: Utils.pm,v 1.17 2006-11-29 18:15:10 mike Exp $
package ZOOM::IRSpy::Utils;
use warnings;
use Exporter 'import';
-our @EXPORT_OK = qw(xml_encode
+our @EXPORT_OK = qw(isodate
+ xml_encode
+ cql_quote
+ cql_target
irspy_xpath_context
- modify_xml_document
- inheritance_tree);
+ modify_xml_document);
use XML::LibXML;
use XML::LibXML::XPathContext;
# Utility functions follow, exported for use of web UI
+sub isodate {
+ my($time) = @_;
+
+ my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
+ return sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
+ $year+1900, $mon+1, $mday, $hour, $min, $sec);
+}
+
# I can't -- just can't, can't, can't -- believe that this function
# isn't provided by one of the core XML modules. But the evidence all
# Template::Plugin both roll their own. So I will do likewise. D'oh!
#
sub xml_encode {
- my ($text, $fallback) = @_;
+ my($text, $fallback) = @_;
$text = $fallback if !defined $text;
use Carp;
}
+# Quotes a term for use in a CQL query
+sub cql_quote {
+ my($term) = @_;
+
+ $term =~ s/([""\\])/\\$1/g;
+ $term = qq["$term"] if $term =~ /\s/;
+ return $term;
+}
+
+
+# Makes a CQL query that finds a specified target
+sub cql_target {
+ my($host, $port, $db) = @_;
+
+ return ("host=" . cql_quote($host) . " and " .
+ "port=" . cql_quote($port) . " and " .
+ "path=" . cql_quote($db));
+}
+
+
# PRIVATE to irspy_namespace() and irspy_xpath_context()
my %_namespaces = (
e => 'http://explain.z3950.org/dtd/2.0/',
sub modify_xml_document {
my($xc, $fieldsByKey, $data) = @_;
- my $nchanges = 0;
+ my @changes = ();
foreach my $key (keys %$data) {
my $value = $data->{$key};
my $ref = $fieldsByKey->{$key} or die "no field '$key'";
if ($node->isa("XML::LibXML::Attr")) {
if ($value ne $node->getValue()) {
$node->setValue($value);
- $nchanges++;
+ push @changes, $ref;
#print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
}
} elsif ($node->isa("XML::LibXML::Element")) {
$node->removeChildNodes();
my $child = new XML::LibXML::Text($value);
$node->appendChild($child);
- $nchanges++;
+ push @changes, $ref;
#print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
} else {
warn "unexpected node type $node";
my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/;
dom_add_node($xc, $ppath, $selector, $value, @addAfter);
#print "New $key ($xpath) = '$value'<br/>\n";
- $nchanges++;
+ push @changes, $ref;
}
}
- return $nchanges;
+ return @changes;
}