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