dfe4e9979121a6a526dc77f628d8b3447e34d1f9
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Utils.pm
1 # $Id: Utils.pm,v 1.38 2009-04-15 18:16:45 wosch Exp $
2
3 package ZOOM::IRSpy::Utils;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use Scalar::Util;
10
11 use Exporter 'import';
12 our @EXPORT_OK = qw(utf8param
13                     isodate
14                     xml_encode 
15                     cql_quote
16                     cql_target
17                     irspy_xpath_context
18                     irspy_make_identifier
19                     irspy_record2identifier
20                     irspy_identifier2target
21                     modify_xml_document
22                     bib1_access_point
23                     render_record
24                     calc_reliability_string
25                     calc_reliability_stats);
26
27 use XML::LibXML;
28 use XML::LibXML::XPathContext;
29 use Encode;
30 use Encode qw(is_utf8);
31
32
33 our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
34
35 # Under Apache 2/mod_perl 2, the ubiquitous $r is no longer and
36 # Apache::Request object, nor even an Apache2::Request, but an
37 # Apache2::RequestReq ... which, astonishingly, doesn't have the
38 # param() method.  So if we're given one of these things, we need to
39 # make an Apache::Request out of, which at least isn't too hard.
40 # However *sigh* this may not be a cheap operation, so we keep a cache
41 # of already-made Request objects.
42 #
43 my %_apache2request;
44 my %_paramsbyrequest;           # Used for Apache2 only
45 sub utf8param {
46     my($r, $key, $value) = @_;
47
48     if ($r->isa('Apache2::RequestRec')) {
49         # Running under Apache2
50         if (defined $_apache2request{$r}) {
51             #warn "using existing Apache2::RequestReq for '$r'";
52             $r = $_apache2request{$r};
53         } else {
54             require Apache2::Request;
55             #warn "making new Apache2::RequestReq for '$r'";
56             $r = $_apache2request{$r} = new Apache2::Request($r);
57         }
58     }
59
60     if (!defined $key) {
61         return map { decode_utf8($_) } $r->param();
62     }
63
64     my $raw = undef;
65     $raw = $_paramsbyrequest{$r}->{$key} if $r->isa('Apache2::Request');
66     $raw = $r->param($key) if !defined $raw;
67
68     if (defined $value) {
69         # Argh!  Simply writing through to the underlying method
70         # param() won't work in Apache2, where param() is readonly.
71         # So we have to keep a hash of additional values, which we
72         # consult (above) before the actual parameters.  Ouch ouch.
73         if ($r->isa('Apache2::Request')) {
74             $_paramsbyrequest{$r}->{$key} = encode_utf8($value);
75         } else {
76             $r->param($key, encode_utf8($value));
77         }
78     }
79
80     return undef if !defined $raw;
81     my $cooked = decode_utf8($raw);
82     warn "converted '$raw' to '", $cooked, "'\n" if $cooked ne $raw;
83     return $cooked;
84 }
85
86
87 # Utility functions follow, exported for use of web UI
88 sub utf8param_apache1 {
89     my($r, $key, $value) = @_;
90     die "utf8param() called with value '$value'" if defined $value;
91
92     my $raw = $r->param($key);
93     return undef if !defined $raw;
94     my $cooked = decode_utf8($raw);
95     warn "converted '$raw' to '", $cooked, "'\n" if $cooked ne $raw;
96     return $cooked;
97 }
98
99
100 sub isodate {
101     my($time) = @_;
102
103     my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
104     return sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
105                    $year+1900, $mon+1, $mday, $hour, $min, $sec);
106 }
107
108
109 # I can't -- just can't, can't, can't -- believe that this function
110 # isn't provided by one of the core XML modules.  But the evidence all
111 # says that it's not: among other things, XML::Generator and
112 # Template::Plugin both roll their own.  So I will do likewise.  D'oh!
113 #
114 sub xml_encode {
115     my($text, $fallback, $opts) = @_;
116     if (!defined $opts && ref $fallback) {
117         # The second and third arguments are both optional
118         $opts = $fallback;
119         $fallback = undef;
120     }
121     $opts = {} if !defined $opts;
122
123     $text = $fallback if !defined $text;
124     use Carp;
125     confess "xml_encode(): text and fallback both undefined"
126         if !defined $text;
127
128     $text =~ s/&/&/g;
129     $text =~ s/</&lt;/g;
130     $text =~ s/>/&gt;/g;
131     # Internet Explorer can't display &apos; (!) so don't create it
132     #$text =~ s/['']/&apos;/g;
133     $text =~ s/[""]/&quot;/g;
134     $text =~ s/ /&nbsp;/g if $opts->{nbsp};
135
136     return $text;
137 }
138
139
140 # Quotes a term for use in a CQL query
141 sub cql_quote {
142     my($term) = @_;
143
144     $term =~ s/([""\\*?])/\\$1/g;
145     $term = qq["$term"] if $term =~ /[\s""\/]/;
146     return $term;
147 }
148
149
150 # Makes a CQL query that finds a specified target.  Arguments may be
151 # either an ID alone, or a (host, port, db) triple.
152 sub cql_target {
153     my($protocol, $host, $port, $db) = @_;
154
155     my $id;
156     if (defined $host) {
157         $id = irspy_make_identifier($protocol, $host, $port, $db);
158     } else {
159         $id = $protocol;
160     }
161
162     return "rec.id=" . cql_quote($id);
163 }
164
165
166 # PRIVATE to irspy_namespace() and irspy_xpath_context()
167 my %_namespaces = (
168                    e => 'http://explain.z3950.org/dtd/2.0/',
169                    i => $IRSPY_NS,
170                    );
171
172
173 sub irspy_namespace {
174     my($prefix) = @_;
175
176     use Carp;
177     confess "irspy_namespace(undef)" if !defined $prefix;
178     my $uri = $_namespaces{$prefix};
179     die "irspy_namespace(): no URI for namespace prefix '$prefix'"
180         if !defined $uri;
181
182     return $uri;
183 }
184
185
186 sub irspy_xpath_context {
187     my($record) = @_;
188
189     if (ref $record && $record->isa("ZOOM::Record")) {
190         $record = $record->render();
191     }
192
193     my $root;
194     if (ref $record) {
195         $root = $record;
196     } else {
197         my $parser = new XML::LibXML();
198         my $doc = $parser->parse_string($record);
199         $root = $doc->getDocumentElement();
200     }
201
202     my $xc = XML::LibXML::XPathContext->new($root);
203     foreach my $prefix (keys %_namespaces) {
204         $xc->registerNs($prefix, $_namespaces{$prefix});
205     }
206     return $xc;
207 }
208
209
210 # Construct an opaque identifier from its components.  Although it's
211 # trivial, this is needed in so many places that it really needs to be
212 # factored out.
213 #
214 # This is the converse of _parse_target_string() in IRSpy.pm, which
215 # should be renamed and moved into this package.
216 #
217 sub irspy_make_identifier {
218     my($protocol, $host, $port, $dbname) = @_;
219
220     die "irspy_make_identifier(" . join(", ", map { "'$_'" } @_).
221         "): wrong number of arguments" if @_ != 4;
222
223     die "irspy_make_identifier(): protocol undefined" if !defined $protocol;
224     die "irspy_make_identifier(): host undefined" if !defined $host;
225     die "irspy_make_identifier(): port undefined" if !defined $port;
226     die "irspy_make_identifier(): dbname undefined" if !defined $dbname;
227
228     return "$protocol:$host:$port/$dbname";
229 }
230
231
232 # Returns the opaque identifier of an IRSpy record based on the
233 # XPathContext'ed DOM object, as returned by irspy_xpath_context().
234 # This is doing the same thing as irspy_make_identifier() but from a
235 # record rather than a set of parameters.
236 #
237 sub irspy_record2identifier {
238     my($xc) = @_;
239
240     ### Must be kept the same as is used in ../../../zebra/*.xsl
241     return $xc->find("concat(e:serverInfo/\@protocol, ':',
242                              e:serverInfo/e:host, ':',
243                              e:serverInfo/e:port, '/',
244                              e:serverInfo/e:database)");
245 }
246
247
248 # Transforms an IRSpy opqaue identifier, as returned from
249 # irspy_make_identifier() or irspy_record2identifier(), into a YAZ
250 # target-string suitable for feeding to ZOOM.  Before we introduced
251 # the protocol element at the start of the identifier string, this was
252 # a null transform; now we have to be a bit cleverer.
253 #
254 sub irspy_identifier2target {
255     my $res = _irspy_identifier2target(@_);
256     #carp "converted ID '@_' to target '$res'";
257     return $res;
258 }
259
260 sub _irspy_identifier2target {
261     my($id) = @_;
262
263     confess "_irspy_identifier2target(): id is undefined"
264         if !defined $id;
265
266     my($protocol, $target) = ($id =~ /(.*?):(.*)/);
267     if (uc($protocol) eq "Z39.50" || uc($protocol) eq "TCP") {
268         return "tcp:$target";
269     } elsif (uc($protocol) eq "SRU") {
270         return "sru=get,http:$target";
271     } elsif (uc($protocol) eq "SRW") {
272         return "sru=srw,http:$target";
273     }
274
275     warn "_irspy_identifier2target($id): unrecognised protocol '$protocol'";
276     return $target;
277 }
278
279
280 # Modifies the XML document for which $xc is an XPath context by
281 # inserting or replacing the values specified in the hash %$data.  The
282 # keys are fieldnames, which are looked up in the register
283 # $fieldsByKey to determine, among other things, what their XPath is.
284
285 sub modify_xml_document {
286     my($xc, $fieldsByKey, $data) = @_;
287
288     my @changes = ();
289     foreach my $key (keys %$data) {
290         my $value = $data->{$key};
291         my $ref = $fieldsByKey->{$key} or die "no field '$key'";
292         my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
293         #print "Considering $key='$value' ($xpath)<br/>\n";
294         my @nodes = $xc->findnodes($xpath);
295         if (@nodes) {
296             warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
297             my $node = $nodes[0];
298
299             if ($node->isa("XML::LibXML::Attr")) {
300                 if ($value ne $node->getValue()) {
301                     $node->setValue($value);
302                     push @changes, $ref;
303                     #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
304                 }
305             } elsif ($node->isa("XML::LibXML::Element")) {
306                 # The contents could be any mixture of text and
307                 # comments and maybe even other crud such as processing
308                 # instructions.  The simplest thing is just to throw it all
309                 # away and start again, making a single Text node the
310                 # canonical representation.  But before we do that,
311                 # we'll check whether the element is already
312                 # canonical, to determine whether our change is a
313                 # no-op.
314                 my $old = "";
315                 my @children = $node->childNodes();
316                 if (@children == 1) {
317                     my $child = $node->firstChild();
318                     if (ref $child && ref $child eq "XML::LibXML::Text") {
319                         $old = $child->getData();
320                         #print STDERR "child='$child', old=", _renderchars($old), "\n" if $key eq "title";
321                     }
322                 }
323                 next if $value eq $old;
324
325                 $node->removeChildNodes();
326                 my $child = new XML::LibXML::Text($value);
327                 $node->appendChild($child);
328                 push @changes, $ref;
329                 #print STDERR "Elem $key ($xpath): ", _renderchars($old), " -> '", _renderchars($value), "\n";
330             } else {
331                 warn "unexpected node type $node";
332             }
333
334         } else {
335             next if !$value; # No need to create a new empty node
336             my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/;
337             dom_add_node($xc, $ppath, $selector, $value, @addAfter);
338             #print "New $key ($xpath) = '$value'<br/>\n";
339             push @changes, $ref;
340         }
341     }
342
343     return @changes;
344 }
345
346
347 sub _renderchars {
348     my($text) = @_;
349
350     return "'" . $text . "'", " (", join(" ", map {ord($_)} split //, $text), "), is_utf8=" , is_utf8($text);
351 }
352
353
354 sub dom_add_node {
355     my($xc, $ppath, $selector, $value, @addAfter) = @_;
356
357     #print "Adding $selector='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
358     my $node = find_or_make_node($xc, $ppath, 0);
359     die "couldn't find or make node '$node'" if !defined $node;
360
361     my $is_attr = ($selector =~ s/^@//);
362     my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
363     #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
364     if ($is_attr) {
365         if (defined $prefix) {
366             ### This seems to no-op (thank, DOM!) but I have have no
367             # idea, and it's not needed for IRSpy, so I am not going
368             # to debug it now.
369             $node->setAttributeNS(irspy_namespace($prefix),
370                                   $simpleSel, $value);
371         } else {
372             $node->setAttribute($simpleSel, $value);
373         }
374         return;
375     }
376
377     my $new = new XML::LibXML::Element($simpleSel);
378     $new->setNamespace(irspy_namespace($prefix), $prefix)
379         if defined $prefix;
380
381     $new->appendText($value);
382     foreach my $predecessor (reverse @addAfter) {
383         my($child) = $xc->findnodes($predecessor, $node);
384         if (defined $child) {
385             $node->insertAfter($new, $child);
386             #warn "Added after '$predecessor'";
387             return;
388         }
389     }
390
391     # Didn't find any of the nodes that are supposed to precede the
392     # new one, so we need to insert the new node as the first of the
393     # parent's children.  However *sigh* there is no prependChild()
394     # analogous to appendChild(), so we have to go the long way round.
395     my @children = $node->childNodes();
396     if (@children) {
397         $node->insertBefore($new, $children[0]);
398         #warn "Added new first child";
399     } else {
400         $node->appendChild($new);
401         #warn "Added new only child";
402     }
403
404     if (0) {
405         my $text = xml_encode(inheritance_tree($xc));
406         $text =~ s/\n/<br\/>$&/sg;
407         print "<pre>$text</pre>\n";
408     }
409 }
410
411
412 sub find_or_make_node {
413     my($xc, $path, $recursion_level) = @_;
414
415     die "deep recursion in find_or_make_node($path)"
416         if $recursion_level == 10;
417     $path = "." if $path eq "";
418
419     my @nodes = $xc->findnodes($path);
420     if (@nodes == 0) {
421         # Oh dear, the parent node doesn't exist.  We could make it,
422         my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
423         $ppath = "" if !defined $ppath;
424         #warn "path='$path', ppath='$ppath', element='$element'";
425         #warn "no node '$path': making it";
426         my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
427
428         my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
429         #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
430         my $new = new XML::LibXML::Element($nsElem);
431         if (defined $prefix) {
432             #warn "setNamespace($prefix)";
433             $new->setNamespace(irspy_namespace($prefix), $prefix);
434         }
435
436         $parent->appendChild($new);
437         return $new;
438     }
439     warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
440     return $nodes[0];
441 }
442
443
444 sub inheritance_tree {
445     my($type, $level) = @_;
446     $level = 0 if !defined $level;
447     return "Woah!  Too deep, man!\n" if $level > 20;
448
449     $type = ref $type if ref $type;
450     my $text = "";
451     $text = "--> " if $level == 0;
452     $text .= ("\t" x $level) . "$type\n";
453     my @ISA = eval "\@${type}::ISA";
454     foreach my $superclass (@ISA) {
455         $text .= inheritance_tree($superclass, $level+1);
456     }
457
458     return $text;
459 }
460
461
462 # This function is made available in xslt using the register_function call
463 sub xslt_strcmp {
464     my ($arg1, $arg2) = @_;
465     return "$arg1" cmp "$arg2";
466 }
467
468
469 ### It feels like this should be in YAZ, exported via ZOOM-Perl.
470 my %_bib1_access_point = (
471         1 =>    "Personal name",
472         2 =>    "Corporate name",
473         3 =>    "Conference name",
474         4 =>    "Title",
475         5 =>    "Title series",
476         6 =>    "Title uniform",
477         7 =>    "ISBN",
478         8 =>    "ISSN",
479         9 =>    "LC card number",
480         10 =>   "BNB card no.",
481         11 =>   "BGF number",
482         12 =>   "Local number",
483         13 =>   "Dewey classification",
484         14 =>   "UDC classification",
485         15 =>   "Bliss classification",
486         16 =>   "LC call number",
487         17 =>   "NLM call number",
488         18 =>   "NAL call number",
489         19 =>   "MOS call number",
490         20 =>   "Local classification",
491         21 =>   "Subject heading",
492         22 =>   "Subject Rameau",
493         23 =>   "BDI index subject",
494         24 =>   "INSPEC subject",
495         25 =>   "MESH subject",
496         26 =>   "PA subject",
497         27 =>   "LC subject heading",
498         28 =>   "RVM subject heading",
499         29 =>   "Local subject index",
500         30 =>   "Date",
501         31 =>   "Date of publication",
502         32 =>   "Date of acquisition",
503         33 =>   "Title key",
504         34 =>   "Title collective",
505         35 =>   "Title parallel",
506         36 =>   "Title cover",
507         37 =>   "Title added title page",
508         38 =>   "Title caption",
509         39 =>   "Title running",
510         40 =>   "Title spine",
511         41 =>   "Title other variant",
512         42 =>   "Title former",
513         43 =>   "Title abbreviated",
514         44 =>   "Title expanded",
515         45 =>   "Subject precis",
516         46 =>   "Subject rswk",
517         47 =>   "Subject subdivision",
518         48 =>   "No. nat'l biblio.",
519         49 =>   "No. legal deposit",
520         50 =>   "No. govt pub.",
521         51 =>   "No. music publisher",
522         52 =>   "Number db",
523         53 =>   "Number local call",
524         54 =>   "Code--language",
525         55 =>   "Code--geographic area",
526         56 =>   "Code--institution",
527         57 =>   "Name and title *",
528         58 =>   "Name geographic",
529         59 =>   "Place publication",
530         60 =>   "CODEN",
531         61 =>   "Microform generation",
532         62 =>   "Abstract",
533         63 =>   "Note",
534         1000 => "Author-title",
535         1001 => "Record type",
536         1002 => "Name",
537         1003 => "Author",
538         1004 => "Author-name personal",
539         1005 => "Author-name corporate",
540         1006 => "Author-name conference",
541         1007 => "Identifier--standard",
542         1008 => "Subject--LC children's",
543         1009 => "Subject name -- personal",
544         1010 => "Body of text",
545         1011 => "Date/time added to db",
546         1012 => "Date/time last modified",
547         1013 => "Authority/format id",
548         1014 => "Concept-text",
549         1015 => "Concept-reference",
550         1016 => "Any",
551         1017 => "Server-choice",
552         1018 => "Publisher",
553         1019 => "Record-source",
554         1020 => "Editor",
555         1021 => "Bib-level",
556         1022 => "Geographic-class",
557         1023 => "Indexed-by",
558         1024 => "Map-scale",
559         1025 => "Music-key",
560         1026 => "Related-periodical",
561         1027 => "Report-number",
562         1028 => "Stock-number",
563         1030 => "Thematic-number",
564         1031 => "Material-type",
565         1032 => "Doc-id",
566         1033 => "Host-item",
567         1034 => "Content-type",
568         1035 => "Anywhere",
569         1036 => "Author-Title-Subject",
570         1032 => "Doc-id (semantic definition change)",
571         1037 => "SICI",
572         1038 => "Abstract-language",
573         1039 => "Application-kind",
574         1040 => "Classification",
575         1041 => "Classification-basic",
576         1042 => "Classification-local-record",
577         1043 => "Enzyme",
578         1044 => "Possessing-institution",
579         1045 => "Record-linking",
580         1046 => "Record-status",
581         1047 => "Treatment",
582         1048 => "Control-number-GKD",
583         1049 => "Control-number-linking",
584         1050 => "Control-number-PND",
585         1051 => "Control-number-SWD",
586         1052 => "Control-number-ZDB",
587         1053 => "Country-publication (country of Publication)",
588         1054 => "Date-conference (meeting date)",
589         1055 => "Date-record-status",
590         1056 => "Dissertation-information",
591         1057 => "Meeting-organizer",
592         1058 => "Note-availability",
593         1059 => "Number-CAS-registry (CAS registry number)",
594         1060 => "Number-document (document number)",
595         1061 => "Number-local-accounting",
596         1062 => "Number-local-acquisition",
597         1063 => "Number-local-call-copy-specific",
598         1064 => "Number-of-reference (reference count)",
599         1065 => "Number-norm",
600         1066 => "Number-volume",
601         1067 => "Place-conference (meeting location)",
602         1068 => "Reference (references and footnotes)",
603         1069 => "Referenced-journal (reference work)",
604         1070 => "Section-code",
605         1071 => "Section-heading",
606         1072 => "Subject-GOO",
607         1073 => "Subject-name-conference",
608         1074 => "Subject-name-corporate",
609         1075 => "Subject-genre/form",
610         1076 => "Subject-name-geographical",
611         1077 => "Subject--chronological",
612         1078 => "Subject--title",
613         1079 => "Subject--topical",
614         1080 => "Subject-uncontrolled",
615         1081 => "Terminology-chemical (chemical name)",
616         1082 => "Title-translated",
617         1083 => "Year-of-beginning",
618         1084 => "Year-of-ending",
619         1085 => "Subject-AGROVOC",
620         1086 => "Subject-COMPASS",
621         1087 => "Subject-EPT",
622         1088 => "Subject-NAL",
623         1089 => "Classification-BCM",
624         1090 => "Classification-DB",
625         1091 => "Identifier-ISRC",
626         1092 => "Identifier-ISMN",
627         1093 => "Identifier-ISRN",
628         1094 => "Identifier-DOI",
629         1095 => "Code-language-original",
630         1096 => "Title-later",
631         1097 => "DC-Title",
632         1098 => "DC-Creator",
633         1099 => "DC-Subject",
634         1100 => "DC-Description",
635         1101 => "DC-Publisher",
636         1102 => "DC-Date",
637         1103 => "DC-ResourceType",
638         1104 => "DC-ResourceIdentifier",
639         1105 => "DC-Language",
640         1106 => "DC-OtherContributor",
641         1107 => "DC-Format",
642         1108 => "DC-Source",
643         1109 => "DC-Relation",
644         1110 => "DC-Coverage",
645         1111 => "DC-RightsManagement",
646         1112 => "Controlled Subject Index",
647         1113 => "Subject Thesaurus",
648         1114 => "Index Terms -- Controlled",
649         1115 => "Controlled Term",
650         1116 => "Spatial Domain",
651         1117 => "Bounding Coordinates",
652         1118 => "West Bounding Coordinate",
653         1119 => "East Bounding Coordinate",
654         1120 => "North Bounding Coordinate",
655         1121 => "South Bounding Coordinate",
656         1122 => "Place",
657         1123 => "Place Keyword Thesaurus",
658         1124 => "Place Keyword",
659         1125 => "Time Period",
660         1126 => "Time Period Textual",
661         1127 => "Time Period Structured",
662         1128 => "Beginning Date",
663         1129 => "Ending Date",
664         1130 => "Availability",
665         1131 => "Distributor",
666         1132 => "Distributor Name",
667         1133 => "Distributor Organization",
668         1134 => "Distributor Street Address",
669         1135 => "Distributor City",
670         1136 => "Distributor State or Province",
671         1137 => "Distributor Zip or Postal Code",
672         1138 => "Distributor Country",
673         1139 => "Distributor Network Address",
674         1140 => "Distributor Hours of Service",
675         1141 => "Distributor Telephone",
676         1142 => "Distributor Fax",
677         1143 => "Resource Description",
678         1144 => "Order Process",
679         1145 => "Order Information",
680         1146 => "Cost",
681         1147 => "Cost Information",
682         1148 => "Technical Prerequisites",
683         1149 => "Available Time Period",
684         1150 => "Available Time Textual",
685         1151 => "Available Time Structured",
686         1152 => "Available Linkage",
687         1153 => "Linkage Type",
688         1154 => "Linkage",
689         1155 => "Sources of Data",
690         1156 => "Methodology",
691         1157 => "Access Constraints",
692         1158 => "General Access Constraints",
693         1159 => "Originator Dissemination Control",
694         1160 => "Security Classification Control",
695         1161 => "Use Constraints",
696         1162 => "Point of Contact",
697         1163 => "Contact Name",
698         1164 => "Contact Organization",
699         1165 => "Contact Street Address",
700         1166 => "Contact City",
701         1167 => "Contact State or Province",
702         1168 => "Contact Zip or Postal Code",
703         1169 => "Contact Country",
704         1170 => "Contact Network Address",
705         1171 => "Contact Hours of Service",
706         1172 => "Contact Telephone",
707         1173 => "Contact Fax",
708         1174 => "Supplemental Information",
709         1175 => "Purpose",
710         1176 => "Agency Program",
711         1177 => "Cross Reference",
712         1178 => "Cross Reference Title",
713         1179 => "Cross Reference Relationship",
714         1180 => "Cross Reference Linkage",
715         1181 => "Schedule Number",
716         1182 => "Original Control Identifier",
717         1183 => "Language of Record",
718         1184 => "Record Review Date",
719         1185 => "Performer",
720         1186 => "Performer-Individual",
721         1187 => "Performer-Group",
722         1188 => "Instrumentation",
723         1189 => "Instrumentation-Original",
724         1190 => "Instrumentation-Current",
725         1191 => "Arrangement",
726         1192 => "Arrangement-Original",
727         1193 => "Arrangement-Current",
728         1194 => "Musical Key-Original",
729         1195 => "Musical Key-Current",
730         1196 => "Date-Composition",
731         1197 => "Date-Recording",
732         1198 => "Place-Recording",
733         1199 => "Country-Recording",
734         1200 => "Number-ISWC",
735         1201 => "Number-Matrix",
736         1202 => "Number-Plate",
737         1203 => "Classification-McColvin",
738         1204 => "Duration",
739         1205 => "Number-Copies",
740         1206 => "Musical Theme",
741         1207 => "Instruments - total number",
742         1208 => "Instruments - distinct number",
743         1209 => "Identifier - URN",
744         1210 => "Sears Subject Heading",
745         1211 => "OCLC Number",
746         1212 => "Composition",
747         1213 => "Intellectual level",
748         1214 => "EAN",
749         1215 => "NLC",
750         1216 => "CRCS",
751         1217 => "Nationality",
752         1218 => "Equinox",
753         1219 => "Compression",
754         1220 => "Format",
755         1221 => "Subject - occupation",
756         1222 => "Subject - function",
757         1223 => "Edition",
758 );
759
760 sub bib1_access_point {
761     my($ap) = @_;
762
763     return $_bib1_access_point{$ap} ||
764         "unknown BIB-1 attribute '$ap'";
765 }
766
767
768 sub render_record {
769     my($rs, $which, $elementSetName) = @_;
770
771     # There is a slight race condition here on the element-set name,
772     # but it shouldn't be a problem as this is (currently) only called
773     # from parts of the program that run single-threaded.
774     my $old = $rs->option(elementSetName => $elementSetName);
775     my $rec = $rs->record($which);
776     $rs->option(elementSetName => $old);
777
778     return $rec->render();
779 }
780
781
782 sub calc_reliability_string {
783     my($xc) = @_;
784
785     my($nok, $nall, $percent) = calc_reliability_stats($xc);
786     return "[untested]" if $nall == 0;
787     return "$nok/$nall = " . $percent . "%";
788 }
789
790
791 sub calc_reliability_stats {
792     my($xc) = @_;
793
794     my @allpings = $xc->findnodes("i:status/i:probe");
795     my $nall = @allpings;
796     return (0, 0, 0) if $nall == 0;
797     my @okpings = $xc->findnodes('i:status/i:probe[@ok = "1"]');
798     my $nok = @okpings;
799     my $percent = int(100*$nok/$nall);
800     return ($nok, $nall, $percent);
801 }
802
803
804 1;