3 # $Id: test-xml-update.pl,v 1.9 2007-03-19 18:50:22 mike Exp $
6 # perl -I ../lib ./test-xml-update.pl bagel.indexdata.dk:210/gils title "Test Database" author "Adam" description "This is a nice database"
12 use ZOOM::IRSpy::Utils qw(irspy_xpath_context modify_xml_document);
13 use ZOOM::IRSpy; # For _really_rewrite_record()
15 # This array copied from ../web/htdocs/details/edit.mc
18 [ protocol => 0, "Protocol", "e:serverInfo/\@protocol" ],
19 [ host => 0, "Host", "e:serverInfo/e:host" ],
20 [ port => 0, "Port", "e:serverInfo/e:port" ],
21 [ dbname => 0, "Database Name", "e:serverInfo/e:database",
23 [ username => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
25 [ password => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
27 [ title => 0, "Title", "e:databaseInfo/e:title",
29 [ description => 5, "Description", "e:databaseInfo/e:description",
31 [ author => 0, "Author", "e:databaseInfo/e:author",
32 qw(e:title e:description) ],
33 [ contact => 0, "Contact", "e:databaseInfo/e:contact",
34 qw(e:title e:description) ],
35 [ extent => 3, "Extent", "e:databaseInfo/e:extent",
36 qw(e:title e:description) ],
37 [ history => 5, "History", "e:databaseInfo/e:history",
38 qw(e:title e:description) ],
39 [ language => 0, "Language of Records", "e:databaseInfo/e:langUsage",
40 qw(e:title e:description) ],
41 [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
42 qw(e:title e:description) ],
43 [ subjects => 2, "Subjects", "e:databaseInfo/e:subjects",
44 qw(e:title e:description) ],
48 if (!getopts('wnxd', \%opts) || @ARGV % 2 == 0) {
49 print STDERR "Usage: %0 [options] <id> [<key1> <value1> ...]\n";
50 print STDERR " -w Write modified record back to DB\n";
51 print STDERR " -n Show new values of fields using XPath\n";
52 print STDERR " -x Show new XML document with changes made\n";
53 print STDERR " -d Show differences between old and new XML\n";
56 my($id, %data) = @ARGV;
58 my $conn = new ZOOM::Connection("localhost:8018/IR-Explain---1", 0,
59 user => "admin", password => "fruitbat");
60 $conn->option(elementSetName => "zeerex");
63 my $query = qq[rec.id="$qid"];
64 my $rs = $conn->search(new ZOOM::Query::CQL($query));
67 print STDERR "$0: no record with ID '$id'";
71 my $rec = $rs->record(0);
72 my $xc = irspy_xpath_context($rec);
73 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
75 my $oldText = $xc->getContextNode()->toString();
76 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
77 my $nchanges = @changedFields;
78 my $newText = $xc->getContextNode()->toString();
79 print("Document modified with $nchanges change", $nchanges == 1 ? "" : "s",
80 ": ", join(", ", map { $_->[2] } @changedFields), "\n");
83 ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode(), $id);
84 print "Rewrote record '$id'\n";
88 foreach my $key (sort keys %data) {
89 my $ref = $fieldsByKey{$key};
90 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
91 print "New $caption ($xpath) = '", $xc->findvalue($xpath), "'\n";
100 my $oldFile = "/tmp/old.txu.$$";
101 my $newFile = "/tmp/new.txu.$$";
102 open OLD, ">$oldFile";
105 open NEW, ">/tmp/new.txu.$$";
108 system("diff $oldFile $newFile");