« blib/lib/Keystone/Resolver/Utils.pm
↑ Summary
blib/lib/Keystone/Resolver/plugins/ServiceType/fulltext.pm »
| 1 | # $Id: ContextObject.pm,v 1.10 2008-04-10 00:27:02 mike Exp $ | ||
| 2 | |||
| 3 | package Keystone::Resolver::ContextObject; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use LWP; | ||
| 8 | use URI::Escape; | ||
| 9 | use Encode; | ||
| 10 | use Scalar::Util; | ||
| 11 | use Keystone::Resolver::Descriptor; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Keystone::Resolver::ContextObject - an OpenURL Framework (Z39.88) ContextObject | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | $co = new Keystone::Resolver::ContextObject(\%args); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Read the OpenURL v1.0 standard (Z39.88) to understand the details, but | ||
| 24 | a ContextObject is a group of six Descriptors, each of which describes | ||
| 25 | an Entity, plus a small amount of additional administrative | ||
| 26 | information. The six types of Entity include Referent (the thing | ||
| 27 | we're trying to resolve), Referer (the service that generated the | ||
| 28 | OpenURL), etc. The administrative information includes details such | ||
| 29 | as the character encoding used by the constituent entities (which is | ||
| 30 | UTF-8 by default, unless otherwise specified by a C<ctx_enc> argument, | ||
| 31 | in which case the data is all canonicalised into UTF-8 anyway.) | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | |||
| 38 | =head2 new() | ||
| 39 | |||
| 40 | $co = new Keystone::Resolver::ContextObject(\%args); | ||
| 41 | |||
| 42 | Constructs a new ContextObject from the specified set of arguments. | ||
| 43 | Although the class model of the code is based on v1.0 of the OpenURL | ||
| 44 | standard, v0.1 OpenURLs are also transparently handled, so that | ||
| 45 | equivalent v0.1 and v1.0 OpenURL parameter sets are built into | ||
| 46 | identical ContextObjects. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | # Distilled summary of Z39.88 Part 2, section 7 (ContextObject Format) | ||
| 51 | # -------------------------------------------------------------------- | ||
| 52 | # | ||
| 53 | # [Table 3, Table 5] Special keys describing the ContextObject as a | ||
| 54 | # whole: | ||
| 55 | # ctx_ver Version of the standard (always "Z39.88-2004") | ||
| 56 | # ctx_enc Character Encoding (expressed as an info URI) | ||
| 57 | # Default is info:ofi/enc:UTF-8 | ||
| 58 | # ctx_id Identifier of ContextObject | ||
| 59 | # ctx_tim ISO8601 date-time of ContextObject creation | ||
| 60 | # These are all optional and non-repeatable. | ||
| 61 | # | ||
| 62 | # According to the standard (Part 2, section 7.1.1, p10), each | ||
| 63 | # ContextObject must contain exactly one Referent and Referrer, and | ||
| 64 | # zero or one of each of the other entities. | ||
| 65 | # | ||
| 66 | # [Table 1] Each of the six entities is built based on control | ||
| 67 | # elements of the form <prefix>_<type>=<value> where prefix is one of: | ||
| 68 | # rft Referent | ||
| 69 | # rfe ReferringEntity ### Table 1 says to use "ref" | ||
| 70 | # req Requester | ||
| 71 | # svc ServiceType | ||
| 72 | # res Resolver | ||
| 73 | # rfr Referrer | ||
| 74 | # and <type> is one of: | ||
| 75 | # id Entity is uniquely identified by <value> | ||
| 76 | # val_* By-value metadata | ||
| 77 | # ref* By-reference metadata | ||
| 78 | # dat Private data | ||
| 79 | # Any number of "id"s may be specified for a given entity (e.g. both a | ||
| 80 | # DOI and a Pubmed ID), but no more than one metadata set (whether by | ||
| 81 | # value or by reference) or private data cookie. | ||
| 82 | # | ||
| 83 | # [Table 2] Meanings of specific control elements: | ||
| 84 | # *_val_fmt Metadata is in the format specified by <value> | ||
| 85 | # (and is in <prefix>.<field> elements of this CO) | ||
| 86 | # *_ref Metadata is found at the location <value> | ||
| 87 | # *_ref_fmt Metadata is in the format specified by <value> | ||
| 88 | # | ||
| 89 | # Actual metadata for a given entity is represented by keys of the | ||
| 90 | # form <prefix>.<field>, where prefix indicates one of the six | ||
| 91 | # entities using the three-letter codes above. This is only supplied | ||
| 92 | # for by-value metadata -- NOT for by-reference metadata (or IDs or | ||
| 93 | # private data). | ||
| 94 | # | ||
| 95 | # Since there must be a referent in a context object, and a referent | ||
| 96 | # must contain exactly one of rft_id, rft_val_fmt (with rft.* | ||
| 97 | # metadata), rft_ref_fmt (with rft_ref pointing to metadata) or | ||
| 98 | # rft_dat, the existence of one of these is the test for a valid | ||
| 99 | # ContextObject | ||
| 100 | # | ||
| 101 | # (Section 7.2 of the standard describes the XML ContextObject format. | ||
| 102 | # We do not plan to implement this in initial versions of Keystone.) | ||
| 103 | # | ||
| 104 | # Section 8 lists identifiers for five KEV-based metadata schemas | ||
| 105 | # (book, dissertation, journal, patent, sch_svc) and eight XML-based | ||
| 106 | # ones (the same five plus MARC21, oai_dc and pro); but only the | ||
| 107 | # journal metadata formats are actually described. The others are | ||
| 108 | # supposed to be in the registry at openurl.info, but since that site | ||
| 109 | # is perpetually down, it's hard to tell whether that's true. Anyway, | ||
| 110 | # we will only concern ourselves, for now, with journals. | ||
| 111 | # | ||
| 112 | # Section 8.1 (Key/Encoded-Value Metadata Format to Describe Journals) | ||
| 113 | # lists 29 fields, of which maybe half a dozen will actually get used: | ||
| 114 | # aulast, aufirst, auinit, auinit1, auinitm, ausuffix, au, | ||
| 115 | # aucorp, atitle, title, jtitle, stitle, date, chron, ssn, | ||
| 116 | # quarter, volume, part, issue, spage, epage, pages, artnum, | ||
| 117 | # isbn, issn, eissn, coden, sici, genre | ||
| 118 | # | ||
| 119 | # ContextObjects are delivered using one of three transports described | ||
| 120 | # in section 9 (by-reference, by-value and inline). Each of which | ||
| 121 | # comes in two flavours (http and https) which are of no interest to | ||
| 122 | # us. We can tell which of the transports is in use by checking for | ||
| 123 | # elements url_ctx_ref (the ContextObject is provided by reference) | ||
| 124 | # and url_ctx_val (it is provided as the value of that element); if | ||
| 125 | # both are missing, the ContextObject is inline. | ||
| 126 | # | ||
| 127 | # The standard says (sections 9.1.1, 9.2.1, 9.3.1) that elements not | ||
| 128 | # specified for the three transports may be supplied by clients and | ||
| 129 | # may be ignored by servers, so they are suitable for passing private | ||
| 130 | # information such as opt_loglevel=7 to the resolver. (Identifying | ||
| 131 | # such elements is handled by the driver program rather than by the | ||
| 132 | # Resolver library: by the time the library sees them, they've already | ||
| 133 | # been turned into options.) | ||
| 134 | |||
| 135 | sub new { | ||
| 136 | my $class = shift(); | ||
| 137 | my($resolver, $openurl, $argsref) = @_; | ||
| 138 | |||
| 139 | my %args = %$argsref; # local copy | ||
| 140 | |||
| 141 | if (!defined $args{url_ver}) { | ||
| 142 | # It's not a Z39.88 OpenURL: if it's a v0.1 OpenURL we can | ||
| 143 | # translate it and we'll be happy. | ||
| 144 | if (!_translate_0point1($openurl, \%args)) { | ||
| 145 | $openurl->die("can't translate v0.1 OpenURL to v1.0"); | ||
| 146 | } | ||
| 147 | } elsif (($openurl->arg1(\%args, "url_ver")) ne "Z39.88-2004") { | ||
| 148 | $openurl->die("not a v1.0 (Z39.88-2004) OpenURL"); | ||
| 149 | } | ||
| 150 | |||
| 151 | my $this = $class->_create($resolver, $openurl, \%args); | ||
| 152 | my $err; | ||
| 153 | $err = $this->_validate(\%args) | ||
| 154 | and $this->die("validate: $err"); | ||
| 155 | $err = $this->_dereference_contextObject(\%args) | ||
| 156 | and $this->die("dereference: $err"); | ||
| 157 | $err = $this->_canonicalise_charenc(\%args) | ||
| 158 | and $this->die("canonicalise_charenc: $err"); | ||
| 159 | $err = $this->_make_entities(\%args) | ||
| 160 | and $this->die("make_entities: $err"); | ||
| 161 | |||
| 162 | foreach my $key (sort keys %args) { | ||
| 163 | $this->warn("remaining key $key=", join(", ", @{ $args{$key} })); | ||
| 164 | } | ||
| 165 | |||
| 166 | $err = $this->_dereference_entities() | ||
| 167 | and $this->die("dereference_entities: $err"); | ||
| 168 | |||
| 169 | # Earlier versions used to try to resolve IDs here, but in fact | ||
| 170 | # that's premature, since they usually resolve not to metadata | ||
| 171 | # that we can go on to use, but directly to the URL of the | ||
| 172 | # resource. Accordingly, IDs are now handled in | ||
| 173 | # Keystone::Resolver::OpenURL::resolve_to_results(). However, if | ||
| 174 | # we later run into different ID schemes that do resolve to | ||
| 175 | # metadata rather than content URLs, we'll need to resurrect the | ||
| 176 | # code that used to be here for that purpose. | ||
| 177 | |||
| 178 | # This under-the-bonnet pre-test is only for efficiency | ||
| 179 | if ($this->option("loglevel") & | ||
| 180 | Keystone::Resolver::LogLevel::DUMPDESCRIPTORS) { | ||
| 181 | $this->log(Keystone::Resolver::LogLevel::DUMPDESCRIPTORS, | ||
| 182 | $this->_dump()); | ||
| 183 | } | ||
| 184 | |||
| 185 | return $this; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | # All the following tables are PRIVATE to _translate_0point1() | ||
| 190 | |||
| 191 | ### Why are isbn/sici (identifiers) considered v1.0 metadata? | ||
| 192 | my @_v01v10same = qw(artnum atitle aufirst auinit auinit1 auinitm aulast | ||
| 193 | coden date eissn epage isbn issn issue pages part | ||
| 194 | quarter sici spage ssn stitle title volume); | ||
| 195 | my %_v01v10map = ( map { ($_ => "rft.$_") } @_v01v10same); | ||
| 196 | |||
| 197 | # In v0.1 but not in v1.0: | ||
| 198 | # genre: (journal, book, conference, etc.) | ||
| 199 | # bici: "A BICI for a section of a book, to which an ISBN has | ||
| 200 | # been assigned. Compliant with http://www.niso.org/bici.html" | ||
| 201 | |||
| 202 | # "au", "aucorp", "ausuffix" and "chron" are in v1.0 but not in v0.1, | ||
| 203 | # so we have no way of generating them. "jtitle" is a synonym with | ||
| 204 | # "title", and is handled separately in the constructor since v1.0 | ||
| 205 | # requires it to be handled for all six entities. | ||
| 206 | |||
| 207 | # See Z39.88 part 2, section 8.1 for a rationale of the | ||
| 208 | # journal-related elements of this hash, and the on-line registry for | ||
| 209 | # the book-related parts. (There are no books in Z39.88 itself). | ||
| 210 | # Two of the v0.1 genres (conference, proceeding) are supported by | ||
| 211 | # both the journal and book metadata formats in v1.0. We arbitrarily | ||
| 212 | # choose the journal versions, in the absence of any other guidance. | ||
| 213 | my %_v01v10genre = ( | ||
| 214 | journal => [ "info:ofi/fmt:kev:mtx:journal", "journal" ], | ||
| 215 | book => [ "info:ofi/fmt:kev:mtx:book", "book" ], | ||
| 216 | conference => [ "info:ofi/fmt:kev:mtx:journal", "conference" ], | ||
| 217 | article => [ "info:ofi/fmt:kev:mtx:journal", "article" ], | ||
| 218 | preprint => [ "info:ofi/fmt:kev:mtx:journal", "preprint" ], | ||
| 219 | proceeding => [ "info:ofi/fmt:kev:mtx:journal", "proceeding" ], | ||
| 220 | bookitem => [ "info:ofi/fmt:kev:mtx:book", "bookitem" ], | ||
| 221 | ); | ||
| 222 | |||
| 223 | sub _translate_0point1 { | ||
| 224 | my($openurl, $args) = @_; | ||
| 225 | |||
| 226 | my %new = ( | ||
| 227 | url_ver => [ "Z39.88-2004" ], | ||
| 228 | url_ctx_fmt => [ "info:ofi/fmt:kev:mtx:ctx" ], | ||
| 229 | ); | ||
| 230 | |||
| 231 | foreach my $key (sort keys %new) { | ||
| 232 | $openurl->log(Keystone::Resolver::LogLevel::CONVERT01, | ||
| 233 | "added $key=", list($new{$key})); | ||
| 234 | } | ||
| 235 | |||
| 236 | foreach my $key (keys %$args) { | ||
| 237 | my $mapped = $_v01v10map{$key}; | ||
| 238 | if (defined $mapped) { | ||
| 239 | $new{$mapped} = delete $args->{$key}; | ||
| 240 | $openurl->log(Keystone::Resolver::LogLevel::CONVERT01, | ||
| 241 | "converted $key=", list($new{$mapped}), | ||
| 242 | " to $mapped"); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | my $id = delete $args->{id}; | ||
| 247 | if (defined $id) { | ||
| 248 | $new{rft_id} = [ map { s/:/\//; "info:$_"; } @$id ]; | ||
| 249 | $openurl->log(Keystone::Resolver::LogLevel::CONVERT01, | ||
| 250 | "converted id=", list($new{rft_id}), " to rft_id"); | ||
| 251 | } | ||
| 252 | |||
| 253 | my $sid = delete $args->{sid}; | ||
| 254 | if (defined $sid) { | ||
| 255 | $new{rfr_id} = [ map { "info:sid/$_"; } @$sid ]; | ||
| 256 | $openurl->log(Keystone::Resolver::LogLevel::CONVERT01, | ||
| 257 | "converted sid=", list($new{rfr_id}), " to rfr_id"); | ||
| 258 | } | ||
| 259 | |||
| 260 | # The OpenURL 0.1 standard says nothing about the mandatory or | ||
| 261 | # optional nature of the "genre" tag, but by inspection some | ||
| 262 | # OpenURLs, including one of page 5 of the standard itself, omit | ||
| 263 | # this, so we need to choose a sensible default. | ||
| 264 | my $genre = $openurl->arg1($args, "genre", 1, 1); | ||
| 265 | $genre = "article" if !defined $genre; | ||
| 266 | |||
| 267 | if (defined $genre) { | ||
| 268 | my $ref = $_v01v10genre{$genre}; | ||
| 269 | my($format, $v1point0genre) = @$ref; | ||
| 270 | $new{rft_val_fmt} = [ $format ]; | ||
| 271 | $new{"rft.genre"} = [ $v1point0genre ]; | ||
| 272 | $openurl->log(Keystone::Resolver::LogLevel::CONVERT01, | ||
| 273 | "converted genre=$genre to ", | ||
| 274 | "rft_val_fmt=$format, rft.genre=$v1point0genre"); | ||
| 275 | } | ||
| 276 | |||
| 277 | # Check for left-over, untranslated, elements in $args | ||
| 278 | foreach my $key (keys %$args) { | ||
| 279 | $openurl->warn("can't translate OpenURL 0.1 key $key=", | ||
| 280 | join(", ", @{ $args->{$key} })); | ||
| 281 | $new{"rft.$key"} = delete $args->{$key}; | ||
| 282 | } | ||
| 283 | |||
| 284 | # Move the new set of elements across into %$args | ||
| 285 | foreach my $key (keys %new) { | ||
| 286 | $args->{$key} = $new{$key}; | ||
| 287 | } | ||
| 288 | |||
| 289 | return 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | # Returns a rendering of an array-reference | ||
| 294 | sub list { | ||
| 295 | my($aref) = @_; | ||
| 296 | |||
| 297 | CORE::die "not an array reference" if CORE::ref($aref) ne "ARRAY"; | ||
| 298 | return "[]" if @$aref == 0; | ||
| 299 | return $aref->[0] if @$aref == 1; | ||
| 300 | return "[" . join(", ", @$aref), "]"; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | sub _create { | ||
| 305 | my $class = shift(); | ||
| 306 | my($resolver, $openurl, $args) = @_; | ||
| 307 | |||
| 308 | my $this = bless { | ||
| 309 | openurl => $openurl, | ||
| 310 | ### The next five parameters arguably belong in the OpenURL object. | ||
| 311 | url_ver => $openurl->arg1($args, "url_ver", 1, 1), # unused | ||
| 312 | url_tim => $openurl->arg1($args, "url_tim", 1, 1), # unused | ||
| 313 | format => $openurl->arg1($args, "url_ctx_fmt", 1, 1), | ||
| 314 | ref => $openurl->arg1($args, "url_ctx_ref", 1, 1), | ||
| 315 | val => $openurl->arg1($args, "url_ctx_val", 1, 1), | ||
| 316 | ### What are these parameters (apart from encoding) actually _for_? | ||
| 317 | version => $openurl->arg1($args, "ctx_ver", 1, 1), # unused | ||
| 318 | encoding => $openurl->arg1($args, "ctx_enc", 1, 1), | ||
| 319 | id => $openurl->arg1($args, "ctx_id", 1, 1), # unused | ||
| 320 | timestamp => $openurl->arg1($args, "ctx_tim", 1, 1), # unused | ||
| 321 | descriptors => {}, | ||
| 322 | }, $class; | ||
| 323 | |||
| 324 | Scalar::Util::weaken($this->{openurl}); | ||
| 325 | return $this; | ||
| 326 | } | ||
| 327 | |||
| 328 | |||
| 329 | sub resolver { return shift()->openurl()->resolver() } | ||
| 330 | sub openurl { return shift()->{openurl} } | ||
| 331 | sub format { return shift()->{format} } | ||
| 332 | sub ref { return shift()->{ref} } | ||
| 333 | sub val { return shift()->{val} } | ||
| 334 | sub encoding { return shift()->{encoding} } | ||
| 335 | |||
| 336 | sub descriptors { | ||
| 337 | my $this = shift(); | ||
| 338 | |||
| 339 | return sort { $a->name() lt $b->name() } | ||
| 340 | values %{ $this->{descriptors} }; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub descriptor { | ||
| 344 | my $this = shift(); | ||
| 345 | my($name) = @_; | ||
| 346 | |||
| 347 | return $this->{descriptors}->{$name}; | ||
| 348 | } | ||
| 349 | |||
| 350 | # Delegations | ||
| 351 | sub option { my $this = shift(); return $this->resolver()->option(@_) } | ||
| 352 | sub log { my $this = shift(); return $this->resolver()->log(@_) } | ||
| 353 | sub fail { my $this = shift(); return $this->openurl()->fail(@_) } | ||
| 354 | sub die { my $this = shift(); return $this->openurl()->die(@_) } | ||
| 355 | sub warn { my $this = shift(); return $this->openurl()->warn(@_) } | ||
| 356 | |||
| 357 | |||
| 358 | sub _validate { | ||
| 359 | my $this = shift(); | ||
| 360 | my($args) = @_; | ||
| 361 | |||
| 362 | my $isByRef = defined $this->ref(); | ||
| 363 | my $isByVal = defined $this->val(); | ||
| 364 | my $isInline = (defined $args->{rft_id} || | ||
| 365 | defined $args->{rft_ref_fmt} || | ||
| 366 | defined $args->{rft_val_fmt}); | ||
| 367 | |||
| 368 | if ($isByRef && $isByVal && $isInline) { | ||
| 369 | return "OpenURL is By-Reference, By-Value and Inline"; | ||
| 370 | } elsif ($isByRef && $isByVal) { | ||
| 371 | return "both By-Reference and By-Value"; | ||
| 372 | } elsif ($isByRef && $isInline) { | ||
| 373 | return "both By-Reference and Inline"; | ||
| 374 | } elsif ($isByVal && $isInline) { | ||
| 375 | return "both By-Value and Inline"; | ||
| 376 | } elsif (!$isByRef && !$isByVal && !$isInline) { | ||
| 377 | return "OpenURL is not By-Reference, By-Value or Inline"; | ||
| 378 | } | ||
| 379 | |||
| 380 | ### Apart from doing the following check, we actually ignore the | ||
| 381 | # ContextObject format if it's there. Should recognise: | ||
| 382 | # info:ofi/fmt:kev:mtx:ctx -- key/value pairs | ||
| 383 | # info:ofi/fmt:xml:xsd:ctx -- XML | ||
| 384 | |||
| 385 | if (!$isInline && !defined $this->format()) { | ||
| 386 | return "no ContextObject format specified " . | ||
| 387 | "for By-Reference or By-Value v1.0 OpenURL"; | ||
| 388 | } | ||
| 389 | |||
| 390 | if ($isInline) { | ||
| 391 | my $correct = "info:ofi/fmt:kev:mtx:ctx"; | ||
| 392 | my $format = $this->format(); | ||
| 393 | if (!defined $format) { | ||
| 394 | # format may be omitted for Inline v1.0 OpenURLs | ||
| 395 | $this->{format} = $correct; | ||
| 396 | } elsif ($format ne $correct) { | ||
| 397 | return "incorrect format '$format' for Inline v1.0 OpenURL"; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | # If we got this far, it has the correct signature, a defined | ||
| 402 | # ContextObject format and parameters for exactly one of the three | ||
| 403 | # standard transports. | ||
| 404 | return undef; | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | sub _dereference_contextObject { | ||
| 409 | my $this = shift(); | ||
| 410 | my($args) = @_; | ||
| 411 | |||
| 412 | my $ref = $this->ref(); | ||
| 413 | if (defined $ref) { | ||
| 414 | #return "no network for _dereference_contextObject()";#### | ||
| 415 | my ($val, $errmsg) = $this->fetch($ref, "ContextObject"); | ||
| 416 | if (!defined $val) { | ||
| 417 | return !defined $errmsg ? undef : | ||
| 418 | "_dereference_contextObject: $errmsg"; | ||
| 419 | } | ||
| 420 | $this->{val} = $val; | ||
| 421 | } | ||
| 422 | |||
| 423 | my $val = $this->val(); | ||
| 424 | if (defined $val) { | ||
| 425 | # Resolve a value to a set of elements | ||
| 426 | foreach my $nv (split /&/, $val) { | ||
| 427 | next if !$nv; | ||
| 428 | my($name, $value) = split /=/, $nv; | ||
| 429 | push @{ $args->{$name} }, uri_unescape($value); | ||
| 430 | } | ||
| 431 | $this->log(Keystone::Resolver::LogLevel::DISSECT, | ||
| 432 | "ContextObject value $val"); | ||
| 433 | } | ||
| 434 | |||
| 435 | return undef; | ||
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | # Fetch a reference to a value and canonicalise it. Returns two | ||
| 440 | # elements, being the dereferenced value or undef, and an error | ||
| 441 | # message. If the optional third argument is provided and true, then | ||
| 442 | # we do not follow redirections, and instead just return the URL to | ||
| 443 | # which we get redirected. | ||
| 444 | # | ||
| 445 | # ### Note that this method has security implementations. See | ||
| 446 | # KEV_Guidelines-20040211.pdf (annex to Z39.88), Appendix D. | ||
| 447 | # For example, we should notice and refuse to follow links that | ||
| 448 | # look like OpenURLs, to avoid recursion. | ||
| 449 | # | ||
| 450 | sub fetch { | ||
| 451 | my $this = shift(); | ||
| 452 | my($ref, $label, $returnRedirect) = @_; | ||
| 453 | |||
| 454 | my($scheme, $host, $tail) = $ref =~ m[^([^:]*)://([^/]*)/(.*)]; | ||
| 455 | my $domain = $this->openurl()->db()->domain_by_name($host); | ||
| 456 | my $status = defined $domain ? $domain->status() : 0; | ||
| 457 | |||
| 458 | if ($status == 1) { | ||
| 459 | # Never even try to fetch data from this domain | ||
| 460 | $this->log(Keystone::Resolver::LogLevel::DEREFERENCE, | ||
| 461 | "skipping bad domain ($host) for $label $ref"); | ||
| 462 | return (undef, undef); | ||
| 463 | } | ||
| 464 | |||
| 465 | my $ua = $this->resolver()->ua(); | ||
| 466 | my $oldRedirectable; | ||
| 467 | if ($returnRedirect) { | ||
| 468 | # How clumsy that this must be done modally | ||
| 469 | $oldRedirectable = $ua->requests_redirectable(); | ||
| 470 | $ua->requests_redirectable([]); | ||
| 471 | } | ||
| 472 | |||
| 473 | my $res = $ua->get($ref); | ||
| 474 | if ($returnRedirect) { | ||
| 475 | $ua->requests_redirectable($oldRedirectable); | ||
| 476 | } | ||
| 477 | |||
| 478 | if (($returnRedirect && !$res->is_redirect()) || | ||
| 479 | (!$returnRedirect && !$res->is_success())) { | ||
| 480 | return (undef, "ref($ref): " . $res->status_line()) | ||
| 481 | if $status == 0; | ||
| 482 | |||
| 483 | $this->die("impossible status '$status' for domain $host") | ||
| 484 | if $status != 2; | ||
| 485 | |||
| 486 | # This domain is "greylisted": failure to fetch from here is ignored | ||
| 487 | $this->log(Keystone::Resolver::LogLevel::DEREFERENCE, | ||
| 488 | "ignoring fetch failure ($host) for $label $ref"); | ||
| 489 | return (undef, undef); | ||
| 490 | } | ||
| 491 | |||
| 492 | $this->log(Keystone::Resolver::LogLevel::DEREFERENCE, "$label URI $ref"); | ||
| 493 | return $res->header("Location") | ||
| 494 | if $returnRedirect; | ||
| 495 | |||
| 496 | my $val = $res->content(); | ||
| 497 | # The next line discards all whitespace and is contentious. See | ||
| 498 | # http://isi.1cate.com/t/test/niso-suite-10.html | ||
| 499 | $val =~ s/\s+//g; | ||
| 500 | |||
| 501 | return $val; | ||
| 502 | } | ||
| 503 | |||
| 504 | |||
| 505 | # On entry to this function, each data value is a string of UTF-8 | ||
| 506 | # octets in the range 0 to 255; on exit, it has become a string of | ||
| 507 | # (generally fewer) Unicode characters in the range 0 to 2^32-1 | ||
| 508 | # | ||
| 509 | sub _canonicalise_charenc { | ||
| 510 | my $this = shift(); | ||
| 511 | my($args) = @_; | ||
| 512 | |||
| 513 | my $enc = $this->encoding(); | ||
| 514 | # Z39.88-2004, part 2, section 7.1.5 says this is optional and | ||
| 515 | # that in its absence we can assume UTF-8. | ||
| 516 | $enc = "info:ofi/enc:UTF-8" if !defined $enc; | ||
| 517 | |||
| 518 | if ($enc !~ s/^info:ofi\/enc://) { | ||
| 519 | return "character encoding '$enc' is not an info:ofi/enc URI"; | ||
| 520 | } | ||
| 521 | |||
| 522 | foreach my $key (keys %$args) { | ||
| 523 | my @translated; | ||
| 524 | foreach my $datum (@{ $args->{$key} }) { | ||
| 525 | my $old = $datum; # Needed since $datum seems to get cleared | ||
| 526 | my $new = decode($enc, $datum, Encode::FB_CROAK); | ||
| 527 | push @translated, $new; | ||
| 528 | #CORE::warn "translated ", _render($old), " to ", _render($new) if $new ne $old; | ||
| 529 | } | ||
| 530 | $args->{$key} = \@translated; | ||
| 531 | } | ||
| 532 | |||
| 533 | return undef; | ||
| 534 | } | ||
| 535 | |||
| 536 | |||
| 537 | # Play out a string by individual characters | ||
| 538 | sub _render { | ||
| 539 | my($s) = @_; | ||
| 540 | |||
| 541 | my $res = "'$s' ("; | ||
| 542 | $res .= join(" ", map { ord($_) } split //, $s); | ||
| 543 | return $res . ")"; | ||
| 544 | } | ||
| 545 | |||
| 546 | # Break into six descriptors (rft, rfe, etc.) each with relevant data | ||
| 547 | sub _make_entities { | ||
| 548 | my $this = shift(); | ||
| 549 | my($args) = @_; | ||
| 550 | |||
| 551 | foreach my $key (sort keys %$args) { | ||
| 552 | my $found_sep = ($key =~ /(.*?)([_.])(.*)/); | ||
| 553 | next if !$found_sep; | ||
| 554 | my($name, $sep, $field) = ($1, $2, $3); | ||
| 555 | next if $name eq 'url'; | ||
| 556 | next if $name eq 'ctx'; | ||
| 557 | |||
| 558 | my $d = $this->{descriptors}->{$name}; | ||
| 559 | if (!defined $d) { | ||
| 560 | $d = $this->{descriptors}->{$name} = | ||
| 561 | new Keystone::Resolver::Descriptor($name); | ||
| 562 | $this->warn("created non-standard entity '$name'") | ||
| 563 | if !grep { $name eq $_ } qw(rft rfe req svc res rfr); | ||
| 564 | } | ||
| 565 | |||
| 566 | if ($sep eq "_") { | ||
| 567 | $d->superdata($field, delete $args->{$key}); | ||
| 568 | } else { | ||
| 569 | my $val = delete $args->{$key}; | ||
| 570 | $d->metadata($field, $val); | ||
| 571 | # OpenURL 1.0 supports "title" as an obsolete "jtitle" synonym | ||
| 572 | $d->metadata("jtitle", $val) | ||
| 573 | if $field eq "title" && !defined $args->{"$name.jtitle"}; | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | return undef; | ||
| 578 | } | ||
| 579 | |||
| 580 | |||
| 581 | sub _dereference_entities { | ||
| 582 | my $this = shift(); | ||
| 583 | |||
| 584 | # Resolve parameters indicated by "ref" attribute in each descriptor | ||
| 585 | foreach my $d ($this->descriptors()) { | ||
| 586 | my $name = $d->name(); | ||
| 587 | next if $name eq "req"; # No need to dereference requestor | ||
| 588 | # ### and probably others | ||
| 589 | my $errmsg = $this->_dereference_entity($d); | ||
| 590 | return "_dereference_entity($name): $errmsg" | ||
| 591 | if defined $errmsg; | ||
| 592 | } | ||
| 593 | |||
| 594 | return undef; | ||
| 595 | } | ||
| 596 | |||
| 597 | |||
| 598 | sub _dereference_entity { | ||
| 599 | my $this = shift(); | ||
| 600 | my($d) = @_; | ||
| 601 | |||
| 602 | my $name = $d->name(); | ||
| 603 | my $ref = $d->superdata1("ref"); | ||
| 604 | my $fmt = $d->superdata1("ref_fmt"); | ||
| 605 | |||
| 606 | return undef | ||
| 607 | if !defined $ref && !defined $fmt; | ||
| 608 | return "reference provided but no reference-format" | ||
| 609 | if !defined $fmt; | ||
| 610 | return "reference-format provided but no reference" | ||
| 611 | if !defined $ref; | ||
| 612 | #return "no network for _dereference_entity()";#### | ||
| 613 | |||
| 614 | my($val, $errmsg) = $this->fetch($ref, "entity($name)"); | ||
| 615 | return $errmsg if !defined $val; | ||
| 616 | |||
| 617 | $d->superdata("val_fmt", $d->delete_superdata("ref_fmt")); | ||
| 618 | $d->delete_superdata("ref"); | ||
| 619 | |||
| 620 | foreach my $nv (split /&/, $val) { | ||
| 621 | next if !$nv; | ||
| 622 | my($name, $value) = split /=/, $nv; | ||
| 623 | $d->push_metadata($name => uri_unescape($value)); | ||
| 624 | } | ||
| 625 | |||
| 626 | $this->log(Keystone::Resolver::LogLevel::DISSECT, | ||
| 627 | "entity($name) value $val"); | ||
| 628 | return undef; | ||
| 629 | } | ||
| 630 | |||
| 631 | |||
| 632 | # Used only for generating logging output | ||
| 633 | sub _dump { | ||
| 634 | my($this) = shift(); | ||
| 635 | |||
| 636 | my $s = "\n"; | ||
| 637 | foreach my $des ($this->descriptors()) { | ||
| 638 | $s .= "\t" . $des->name() . " = {\n"; | ||
| 639 | foreach my $key ($des->superdata_keys()) { | ||
| 640 | $s .= "\t\t*$key -> " . | ||
| 641 | join(", ", @{ $des->superdata($key) }) . "\n"; | ||
| 642 | } | ||
| 643 | foreach my $key ($des->metadata_keys()) { | ||
| 644 | $s .= "\t\t $key -> " . | ||
| 645 | join(", ", @{ $des->metadata($key) }) . "\n"; | ||
| 646 | } | ||
| 647 | $s .= "\t}\n"; | ||
| 648 | } | ||
| 649 | |||
| 650 | return $s; | ||
| 651 | } | ||
| 652 | |||
| 653 | |||
| 654 | 1; |