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