Fix
[irspy-moved-to-github.git] / zebra / ezeerex2pqfproperties.pl
1 #! /usr/bin/perl -w
2
3 # $Id: ezeerex2pqfproperties.pl,v 1.6 2006-06-20 11:05:43 mike Exp $
4 #
5 # Run like this:
6 #       ./ezeerex2pqfproperties.pl zeerex.xml
7
8 use strict;
9 use warnings;
10 use XML::LibXML;
11 use XML::LibXML::XPathContext;
12
13 my $text = join('', <>);
14 my $parser = new XML::LibXML();
15 my $doc = $parser->parse_string($text);
16 my $root = $doc->getDocumentElement();
17 my $xc = XML::LibXML::XPathContext->new($root);
18 $xc->registerNs(z => 'http://explain.z3950.org/dtd/2.0/');
19
20 my %setmap = print_sets($xc);
21 print_default_set($xc, \%setmap);
22 print_indexes($xc);
23 print_relations($xc);
24 print_relation_modifiers($xc);
25 print_positions($xc);
26 print_structures($xc);
27 print_truncations($xc);
28
29 # We could limit the sets output to those that are actually used by an
30 # SRU index: that way we could avoid defining
31 #       set.bib1 = 1.2.840.10003.3.1
32 # which is a Z39.50 attribute set that we don't need for CQL.  But
33 # doing that would be a lot of work for marginal gain.
34 #
35 sub print_sets {
36     my($xc) = @_;
37
38     my %setmap;
39     my(@nodes) = $xc->findnodes('z:indexInfo/z:set');
40     foreach my $node (@nodes) {
41         my $name = $node->findvalue('@name');
42         my $identifier = $node->findvalue('@identifier');
43         print "set.$name = $identifier\n";
44         $setmap{$name} = $identifier;
45     }
46
47     return %setmap;
48 }
49
50 sub print_default_set {
51     my($xc, $setmap) = @_;
52
53     my (@nodes) = $xc->findnodes('z:configInfo/' .
54                                  'z:default[@type="contextSet"]');
55     foreach my $node (@nodes) {
56         my $name = $node->findvalue('.');
57         my $identifier = $setmap->{$name}
58             or die "no identifier for default context-set name '$name'";
59
60         print "# default context-set name '$name'\n";
61         print "set = $identifier\n";
62     }
63 }
64
65 sub print_indexes {
66     my($xc) = @_;
67
68     foreach my $node ($xc->findnodes('z:indexInfo/' .
69                                      'z:index[@search="true"]')) {
70         my @pqf = $xc->findnodes("z:map/z:attr", $node);
71         die("no PQF mapping for index '" .
72             $xc->findvalue("z:title", $node) . "'")
73             if @pqf == 0;
74         my $ptype = $xc->findvalue('@type', $pqf[0]);
75         my $pval = $xc->findvalue(".", $pqf[0]);
76
77         foreach my $map ($xc->findnodes("z:map", $node)) {
78             my $setname = $xc->findvalue('z:name/@set', $map);
79             my $indexname = $xc->findvalue('z:name', $map);
80             ### We need a way for the ZeeRex record to specify other
81             #   attributes to be specified along with the access-point,
82             #   e.g. @attr 4=3 for whole-field indexes.
83             print "index.$setname.$indexname = $ptype=$pval\n"
84                 if $indexname ne "";
85         }
86     }
87 }
88
89 # I don't think these are affected by the ZeeRex record
90 sub print_relations {
91     my($xc) = @_;
92
93     print <<__EOT__;
94 relation.< = 2=1
95 relation.le = 2=2
96 relation.eq = 2=3
97 relation.exact = 2=3
98 relation.ge = 2=4
99 relation.> = 2=5
100 relation.<> = 2=6
101 relation.scr = 2=3
102 __EOT__
103 }
104
105 # I don't think these are affected by the ZeeRex record
106 sub print_relation_modifiers {
107     my($xc) = @_;
108
109     print <<__EOT__;
110 relationModifier.relevant = 2=102
111 relationModifier.fuzzy = 5=103
112 relationModifier.stem = 2=101
113 relationModifier.phonetic = 2=100
114 relationModifier.regexp = 5=102
115 __EOT__
116 }
117
118 # I don't think these are affected by the ZeeRex record
119 sub print_positions {
120     my($xc) = @_;
121
122     print <<__EOT__;
123 position.first = 3=1 6=1
124 position.any = 3=3 6=1
125 position.last = 3=4 6=1
126 position.firstAndLast = 3=3 6=3
127 __EOT__
128 }
129
130 # I don't think these are affected by the ZeeRex record
131 sub print_structures {
132     my($xc) = @_;
133
134     print <<__EOT__;
135 structure.exact = 4=108
136 structure.all = 4=2
137 structure.any = 4=2
138 structure.* = 4=1
139 __EOT__
140 }
141
142 # I don't think these are affected by the ZeeRex record
143 sub print_truncations {
144     my($xc) = @_;
145
146     print <<__EOT__;
147 truncation.right = 5=1
148 truncation.left = 5=2
149 truncation.both = 5=3
150 truncation.none = 5=100
151 truncation.regexp = 5=102
152 truncation.z3958 = 5=104
153 __EOT__
154 }