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