2 package ZOOM::IRSpy::Utils;
10 use Exporter 'import';
11 our @EXPORT_OK = qw(utf8param
20 irspy_record2identifier
21 irspy_identifier2target
25 calc_reliability_string
26 calc_reliability_stats);
29 use XML::LibXML::XPathContext;
31 use Encode qw(is_utf8);
34 our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
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.
45 my %_paramsbyrequest; # Used for Apache2 only
47 my($r, $key, $value) = @_;
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};
55 require Apache2::Request;
56 #warn "making new Apache2::RequestReq for '$r'";
57 $r = $_apache2request{$r} = new Apache2::Request($r);
62 return map { decode_utf8($_) } $r->param();
66 $raw = $_paramsbyrequest{$r}->{$key} if $r->isa('Apache2::Request');
67 $raw = $r->param($key) if !defined $raw;
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);
77 $r->param($key, encode_utf8($value));
81 return undef if !defined $raw;
82 my $cooked = decode_utf8($raw);
83 warn "converted '$raw' to '", $cooked, "'\n" if $cooked ne $raw;
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;
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;
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);
108 # strips whitespaces at start and ends of a field
118 # utf8param() with trim
120 my $result = utf8param(@_);
122 if (defined $result) {
123 $result = trimField($result);
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!
135 my($text, $fallback, $opts) = @_;
136 if (!defined $opts && ref $fallback) {
137 # The second and third arguments are both optional
141 $opts = {} if !defined $opts;
143 $text = $fallback if !defined $text;
145 confess "xml_encode(): text and fallback both undefined"
148 $text =~ s/&/&/g;
151 # Internet Explorer can't display ' (!) so don't create it
152 #$text =~ s/['']/'/g;
153 $text =~ s/[""]/"/g;
154 $text =~ s/ / /g if $opts->{nbsp};
160 # Quotes a term for use in a CQL query
164 $term =~ s/([""\\*?])/\\$1/g;
165 $term = qq["$term"] if $term =~ /[\s""\/]/;
170 # Makes a CQL query that finds a specified target. Arguments may be
171 # either an ID alone, or a (host, port, db) triple.
173 my($protocol, $host, $port, $db) = @_;
177 $id = irspy_make_identifier($protocol, $host, $port, $db);
182 return "rec.id=" . cql_quote($id);
186 # PRIVATE to irspy_namespace() and irspy_xpath_context()
188 e => 'http://explain.z3950.org/dtd/2.0/',
193 sub irspy_namespace {
197 confess "irspy_namespace(undef)" if !defined $prefix;
198 my $uri = $_namespaces{$prefix};
199 die "irspy_namespace(): no URI for namespace prefix '$prefix'"
206 sub irspy_xpath_context {
209 if (ref $record && $record->isa("ZOOM::Record")) {
210 $record = $record->render();
217 my $parser = new XML::LibXML();
218 my $doc = $parser->parse_string($record);
219 $root = $doc->getDocumentElement();
222 my $xc = XML::LibXML::XPathContext->new($root);
223 foreach my $prefix (keys %_namespaces) {
224 $xc->registerNs($prefix, $_namespaces{$prefix});
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
234 # This is the converse of _parse_target_string() in IRSpy.pm, which
235 # should be renamed and moved into this package.
237 sub irspy_make_identifier {
238 my($protocol, $host, $port, $dbname) = @_;
240 die "irspy_make_identifier(" . join(", ", map { "'$_'" } @_).
241 "): wrong number of arguments" if @_ != 4;
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;
248 return "$protocol:$host:$port/$dbname";
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.
257 sub irspy_record2identifier {
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)");
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.
274 sub irspy_identifier2target {
275 my $res = _irspy_identifier2target(@_);
276 #carp "converted ID '@_' to target '$res'";
280 sub _irspy_identifier2target {
283 confess "_irspy_identifier2target(): id is undefined"
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";
295 warn "_irspy_identifier2target($id): unrecognised protocol '$protocol'";
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.
305 sub modify_xml_document {
306 my($xc, $fieldsByKey, $data) = @_;
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);
316 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
317 my $node = $nodes[0];
319 if ($node->isa("XML::LibXML::Attr")) {
320 if ($value ne $node->getValue()) {
321 $node->setValue($value);
323 #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
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
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";
343 next if $value eq $old;
345 $node->removeChildNodes();
346 my $child = new XML::LibXML::Text($value);
347 $node->appendChild($child);
349 #print STDERR "Elem $key ($xpath): ", _renderchars($old), " -> '", _renderchars($value), "\n";
351 warn "unexpected node type $node";
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";
370 return "'" . $text . "'", " (", join(" ", map {ord($_)} split //, $text), "), is_utf8=" , is_utf8($text);
375 my($xc, $ppath, $selector, $value, @addAfter) = @_;
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;
381 my $is_attr = ($selector =~ s/^@//);
382 my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
383 #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
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
389 $node->setAttributeNS(irspy_namespace($prefix),
392 $node->setAttribute($simpleSel, $value);
397 my $new = new XML::LibXML::Element($simpleSel);
398 $new->setNamespace(irspy_namespace($prefix), $prefix)
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'";
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();
417 $node->insertBefore($new, $children[0]);
418 #warn "Added new first child";
420 $node->appendChild($new);
421 #warn "Added new only child";
425 my $text = xml_encode(inheritance_tree($xc));
426 $text =~ s/\n/<br\/>$&/sg;
427 print "<pre>$text</pre>\n";
432 sub find_or_make_node {
433 my($xc, $path, $recursion_level) = @_;
435 die "deep recursion in find_or_make_node($path)"
436 if $recursion_level == 10;
437 $path = "." if $path eq "";
439 my @nodes = $xc->findnodes($path);
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);
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);
456 $parent->appendChild($new);
459 warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
464 sub inheritance_tree {
465 my($type, $level) = @_;
466 $level = 0 if !defined $level;
467 return "Woah! Too deep, man!\n" if $level > 20;
469 $type = ref $type if ref $type;
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);
482 # This function is made available in xslt using the register_function call
484 my ($arg1, $arg2) = @_;
485 return "$arg1" cmp "$arg2";
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",
496 6 => "Title uniform",
499 9 => "LC card number",
500 10 => "BNB card no.",
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",
517 27 => "LC subject heading",
518 28 => "RVM subject heading",
519 29 => "Local subject index",
521 31 => "Date of publication",
522 32 => "Date of acquisition",
524 34 => "Title collective",
525 35 => "Title parallel",
527 37 => "Title added title page",
528 38 => "Title caption",
529 39 => "Title running",
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",
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",
551 61 => "Microform generation",
554 1000 => "Author-title",
555 1001 => "Record type",
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",
571 1017 => "Server-choice",
573 1019 => "Record-source",
576 1022 => "Geographic-class",
577 1023 => "Indexed-by",
580 1026 => "Related-periodical",
581 1027 => "Report-number",
582 1028 => "Stock-number",
583 1030 => "Thematic-number",
584 1031 => "Material-type",
587 1034 => "Content-type",
589 1036 => "Author-Title-Subject",
590 1032 => "Doc-id (semantic definition change)",
592 1038 => "Abstract-language",
593 1039 => "Application-kind",
594 1040 => "Classification",
595 1041 => "Classification-basic",
596 1042 => "Classification-local-record",
598 1044 => "Possessing-institution",
599 1045 => "Record-linking",
600 1046 => "Record-status",
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",
652 1098 => "DC-Creator",
653 1099 => "DC-Subject",
654 1100 => "DC-Description",
655 1101 => "DC-Publisher",
657 1103 => "DC-ResourceType",
658 1104 => "DC-ResourceIdentifier",
659 1105 => "DC-Language",
660 1106 => "DC-OtherContributor",
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",
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",
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",
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",
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",
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",
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",
771 1217 => "Nationality",
773 1219 => "Compression",
775 1221 => "Subject - occupation",
776 1222 => "Subject - function",
780 sub bib1_access_point {
783 return $_bib1_access_point{$ap} ||
784 "unknown BIB-1 attribute '$ap'";
789 my($rs, $which, $elementSetName) = @_;
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);
798 return $rec->render();
802 sub calc_reliability_string {
805 my($nok, $nall, $percent) = calc_reliability_stats($xc);
806 return "[untested]" if $nall == 0;
807 return "$nok/$nall = " . $percent . "%";
811 sub calc_reliability_stats {
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"]');
819 my $percent = int(100*$nok/$nall + 0.5);
820 return ($nok, $nall, $percent);