1 package OpenILS::WWW::SuperCat;
2 use strict; use warnings;
6 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
7 use APR::Const -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use Apache2::RequestUtil;
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::Utils qw/:datetime/;
16 use OpenSRF::Utils::Cache;
18 use OpenSRF::AppSession;
22 use Unicode::Normalize;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::WWW::SuperCat::Feed;
27 # set the bootstrap config when this module is loaded
28 my ($bootstrap, $supercat, $actor, $parser, $search);
37 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
38 $supercat = OpenSRF::AppSession->create('open-ils.supercat');
39 $actor = OpenSRF::AppSession->create('open-ils.actor');
40 $search = OpenSRF::AppSession->create('open-ils.search');
41 $parser = new XML::LibXML;
47 return Apache2::Const::DECLINED if (-e $apache->filename);
49 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
52 ->request("open-ils.supercat.oisbn", $isbn)
55 print "Content-type: application/xml; charset=utf-8\n\n";
56 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
58 unless (exists $$list{metarecord}) {
60 return Apache2::Const::OK;
63 print "<idlist metarecord='$$list{metarecord}'>\n";
65 for ( keys %{ $$list{record_list} } ) {
66 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
67 print " <isbn record='$_'>$o</isbn>\n"
72 return Apache2::Const::OK;
78 return Apache2::Const::DECLINED if (-e $apache->filename);
81 my $rel_name = quotemeta($cgi->url(-relative=>1));
84 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
87 my $url = $cgi->url(-path_info=>$add_path);
88 my $root = (split 'unapi', $url)[0];
89 my $base = (split 'unapi', $url)[0] . 'unapi';
92 my $uri = $cgi->param('id') || '';
93 my $host = $cgi->virtual_host || $cgi->server_name;
95 my $format = $cgi->param('format');
96 my ($id,$type,$command) = ('','','');
99 print "Content-type: application/xml; charset=utf-8\n";
101 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
104 $type = 'metarecord' if ($1 =~ /^m/o);
107 ->request("open-ils.supercat.$type.formats")
112 my $body = "<formats id='$uri'><format name='opac' type='text/html'/>";
115 my ($type) = keys %$h;
116 $body .= "<format name='$type' type='application/xml'";
118 for my $part ( qw/namespace_uri docs schema_location/ ) {
119 $body .= " $part='$$h{$type}{$part}'"
120 if ($$h{$type}{$part});
126 $body .= "</formats>\n";
128 $apache->custom_response( 300, $body);
132 ->request("open-ils.supercat.record.formats")
137 ->request("open-ils.supercat.metarecord.formats")
141 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
142 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
144 print "<formats><format name='opac' type='text/html'/>";
147 my ($type) = keys %$h;
148 print "<format name='$type' type='application/xml'";
150 for my $part ( qw/namespace_uri docs schema_location/ ) {
151 print " $part='$$h{$type}{$part}'"
152 if ($$h{$type}{$part});
158 print "</formats>\n";
161 return Apache2::Const::OK;
166 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
169 $type = 'metarecord' if ($1 =~ /^m/o);
170 $command = 'retrieve';
173 if ($format eq 'opac') {
174 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
175 if ($type eq 'metarecord');
176 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
177 if ($type eq 'record');
179 } elsif ($format =~ /^html/o) {
180 my $feed = create_record_feed(
186 $feed->creator($host);
187 $feed->update_ts(gmtime_ISO8601());
189 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
190 print entityize($feed->toString) . "\n";
192 return Apache2::Const::OK;
195 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
199 print "Content-type: text/html; charset=utf-8\n\n";
200 $apache->custom_response( 404, <<" HTML");
203 <title>$type $id not found!</title>
207 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
214 print "Content-type: application/xml; charset=utf-8\n\n";
215 print $req->gather(1);
217 return Apache2::Const::OK;
223 return Apache2::Const::DECLINED if (-e $apache->filename);
227 my $rel_name = quotemeta($cgi->url(-relative=>1));
230 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
233 my $url = $cgi->url(-path_info=>$add_path);
234 my $root = (split 'supercat', $url)[0];
235 my $base = (split 'supercat', $url)[0] . 'supercat';
236 my $path = (split 'supercat', $url)[1];
237 my $unapi = (split 'supercat', $url)[0] . 'unapi';
239 my $host = $cgi->virtual_host || $cgi->server_name;
241 my ($id,$type,$format,$command) = reverse split '/', $path;
244 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
245 print "Content-type: application/xml; charset=utf-8\n";
248 ->request("open-ils.supercat.$1.formats")
256 <type>text/html</type>
260 my ($type) = keys %$h;
261 print "<format><name>$type</name><type>application/xml</type>";
263 for my $part ( qw/namespace_uri docs schema_location/ ) {
264 print "<$part>$$h{$type}{$part}</$part>"
265 if ($$h{$type}{$part});
271 print "</formats>\n";
273 return Apache2::Const::OK;
277 ->request("open-ils.supercat.record.formats")
282 ->request("open-ils.supercat.metarecord.formats")
286 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
287 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
292 <type>text/html</type>
296 my ($type) = keys %$h;
297 print "<format><name>$type</name><type>application/xml</type>";
299 for my $part ( qw/namespace_uri docs schema_location/ ) {
300 print "<$part>$$h{$type}{$part}</$part>"
301 if ($$h{$type}{$part});
307 print "</formats>\n";
310 return Apache2::Const::OK;
313 if ($format eq 'opac') {
314 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
315 if ($type eq 'metarecord');
316 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
317 if ($type eq 'record');
319 } elsif ($format =~ /^html/o) {
320 my $feed = create_record_feed( $format => [ $id ], $unapi,);
323 $feed->creator($host);
324 $feed->update_ts(gmtime_ISO8601());
326 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
327 print entityize($feed->toString) . "\n";
329 return Apache2::Const::OK;
332 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
336 print "Content-type: text/html; charset=utf-8\n\n";
337 $apache->custom_response( 404, <<" HTML");
340 <title>$type $id not found!</title>
344 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
351 print "Content-type: application/xml; charset=utf-8\n\n";
352 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
354 return Apache2::Const::OK;
360 return Apache2::Const::DECLINED if (-e $apache->filename);
364 my $year = (gmtime())[5] + 1900;
365 my $host = $cgi->virtual_host || $cgi->server_name;
367 my $rel_name = quotemeta($cgi->url(-relative=>1));
370 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
372 my $url = $cgi->url(-path_info=>$add_path);
373 my $root = (split 'feed', $url)[0];
374 my $base = (split 'bookbag', $url)[0] . 'bookbag';
375 my $path = (split 'bookbag', $url)[1];
376 my $unapi = (split 'feed', $url)[0] . 'unapi';
379 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
381 my ($id,$type) = reverse split '/', $path;
383 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
384 return Apache2::Const::NOT_FOUND unless($bucket);
386 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
387 if ($type eq 'opac') {
388 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
389 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
394 my $feed = create_record_feed(
396 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
401 $feed->title("Items in Book Bag [".$bucket->name."]");
402 $feed->creator($host);
403 $feed->update_ts(gmtime_ISO8601());
405 $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
406 $feed->link(rss2 => $base . "/rss2/$id");
407 $feed->link(html => $base . "/html/$id" => 'text/html');
408 $feed->link(unapi => $unapi);
412 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
413 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
418 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
419 print entityize($feed->toString) . "\n";
421 return Apache2::Const::OK;
426 return Apache2::Const::DECLINED if (-e $apache->filename);
430 my $year = (gmtime())[5] + 1900;
431 my $host = $cgi->virtual_host || $cgi->server_name;
433 my $rel_name = quotemeta($cgi->url(-relative=>1));
436 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
438 my $url = $cgi->url(-path_info=>$add_path);
439 my $root = (split 'feed', $url)[0];
440 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
441 my $path = (split 'freshmeat', $url)[1];
442 my $unapi = (split 'feed', $url)[0] . 'unapi';
445 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
449 my ($type,$rtype,$axis,$date,$limit) = split '/', $path;
453 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
455 if ($type eq 'opac') {
456 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
457 join('&', map { "rl=" . $_ } @$list) .
462 my $feed = create_record_feed( $type, $list, $unapi);
465 $feed->title("$limit most recent $rtype changes from $date forward");
466 $feed->creator($host);
467 $feed->update_ts(gmtime_ISO8601());
469 $feed->link(atom => $base . "/atom/$rtype/$axis/$date/$limit" => 'application/atom+xml');
470 $feed->link(rss2 => $base . "/rss2/$rtype/$axis/$date/$limit");
471 $feed->link(html => $base . "/html/$rtype/$axis/$date/$limit" => 'text/html');
472 $feed->link(unapi => $unapi);
476 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
477 join('&', map { 'rl=' . $_} @$list ),
482 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
483 print entityize($feed->toString) . "\n";
485 return Apache2::Const::OK;
494 if ($version eq '1.0') {
496 Content-type: application/opensearchdescription+xml; charset=utf-8
498 <?xml version="1.0" encoding="UTF-8"?>
499 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
500 <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&startIndex={startIndex}&count={count}</Url>
501 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
502 <ShortName>$lib</ShortName>
503 <LongName>Search $lib</LongName>
504 <Description>Search the $lib OPAC by $class.</Description>
505 <Tags>$lib book library</Tags>
506 <SampleSearch>harry+potter</SampleSearch>
507 <Developer>Mike Rylander for GPLS/PINES</Developer>
508 <Contact>feedback\@open-ils.org</Contact>
509 <SyndicationRight>open</SyndicationRight>
510 <AdultContent>false</AdultContent>
511 </OpenSearchDescription>
515 Content-type: application/opensearchdescription+xml; charset=utf-8
517 <?xml version="1.0" encoding="UTF-8"?>
518 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
519 <ShortName>$lib</ShortName>
520 <Description>Search the $lib OPAC by $class.</Description>
521 <Tags>$lib book library</Tags>
522 <Url type="application/atom+xml"
523 template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
524 <Url type="application/x-rss+xml"
525 template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
526 <Url type="application/x-mods3+xml"
527 template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
528 <Url type="application/x-mods+xml"
529 template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
530 <Url type="application/x-marcxml+xml"
531 template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
532 <LongName>Search $lib</LongName>
533 <Query role="example" searchTerms="harry+potter" />
534 <Developer>Mike Rylander for GPLS/PINES</Developer>
535 <SyndicationRight>open</SyndicationRight>
536 <AdultContent>false</AdultContent>
537 <Language>en-US</Language>
538 <OutputEncoding>UTF-8</OutputEncoding>
539 <InputEncoding>UTF-8</InputEncoding>
540 </OpenSearchDescription>
544 return Apache2::Const::OK;
547 sub opensearch_feed {
549 return Apache2::Const::DECLINED if (-e $apache->filename);
552 my $year = (gmtime())[5] + 1900;
554 my $host = $cgi->virtual_host || $cgi->server_name;
556 my $rel_name = quotemeta($cgi->url(-relative=>1));
559 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
561 my $url = $cgi->url(-path_info=>$add_path);
562 my $root = (split 'opensearch', $url)[0];
563 my $base = (split 'opensearch', $url)[0] . 'opensearch';
564 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
567 my $path = (split 'opensearch', $url)[1];
569 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
571 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
578 $lib = $actor->request(
579 'open-ils.actor.org_unit_list.search' => parent_ou => undef
580 )->gather(1)->[0]->shortname;
587 return opensearch_osd($version, $lib, $class, $base);
591 my $page = $cgi->param('startPage') || 1;
592 my $offset = $cgi->param('startIndex') || 1;
593 my $limit = $cgi->param('count') || 10;
594 my $lang = $cgi->param('language') || 'en-US';
596 $page = 1 if ($page !~ /^\d+$/);
597 $offset = 1 if ($offset !~ /^\d+$/);
598 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
599 $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
602 $offset = ($page - 1) * $limit;
607 my (undef,$version,$org,$type,$class,$terms) = split '/', $path;
609 $terms ||= $cgi->param('searchTerms');
610 $class ||= $cgi->param('searchClass') || '-';
611 $type ||= $cgi->param('responseType') || '-';
612 $org ||= $cgi->param('searchOrg') || '-';
614 if ($version eq '1.0') {
616 } elsif ($type eq '-') {
621 $class = 'keyword' if ($class eq '-');
622 $terms = decode_utf8($terms);
626 #warn "searching for $class -> [$terms] via OS $version, response type $type";
630 $org_unit = $actor->request(
631 'open-ils.actor.org_unit_list.search' => parent_ou => undef
634 $org_unit = $actor->request(
635 'open-ils.actor.org_unit_list.search' => shortname => $org
639 my $rs_name = $cgi->cookie('os_session');
640 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
643 if (!($recs = $$cached_res{os_results}{$class}{$terms}{$org})) {
644 warn "NOT pulling results from cache";
645 $rs_name = $cgi->remote_host . '::' . rand(time);
646 $recs = $search->request(
647 'open-ils.search.biblio.multiclass' => {
648 searches => { $class => { term => $terms, }, },
649 org_unit => $org_unit->[0]->id,
655 $$cached_res{os_results}{$class}{$terms}{$org} = $recs;
656 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
662 my $feed = create_record_feed(
664 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
669 $feed->search($terms);
671 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
672 $feed->creator($host);
673 $feed->update_ts(gmtime_ISO8601());
677 'http://a9.com/-/spec/opensearch/1.1/',
684 'http://a9.com/-/spec/opensearch/1.1/',
691 'http://a9.com/-/spec/opensearch/1.1/',
698 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
699 'application/opensearch+xml'
700 ) if ($offset + $limit < $recs->{count});
704 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
705 'application/opensearch+xml'
710 $base . "/$version/$org/$type/$class?searchTerms=$terms" =>
711 'application/opensearch+xml'
714 $feed->link( unapi => $unapi);
718 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
719 # join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
725 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
726 join('&', map { 'rl=' . $_->[0] } grep { defined $_ } @{$recs->{ids}} ),
731 -type => $feed->type,
733 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
736 print entityize($feed->toString) . "\n";
738 return Apache2::Const::OK;
741 sub create_record_feed {
747 my $base = $cgi->url;
748 my $host = $cgi->virtual_host || $cgi->server_name;
750 my $year = (gmtime())[5] + 1900;
752 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
754 $feed->unapi($unapi);
756 $type = 'atom' if ($type eq 'html');
757 $type = 'marcxml' if ($type eq 'htmlcard');
759 for my $rec (@$records) {
762 my $item_tag = "tag:$host,$year:biblio-record_entry/" . $rec;
765 my $xml = $supercat->request(
766 "open-ils.supercat.record.$type.retrieve",
770 my $node = $feed->add_item($xml);
772 $node->id($item_tag);
773 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=opac" => 'text/html');
774 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac");
775 $node->link(unapi => $feed->unapi . "?id=$item_tag");
776 $node->link('unapi-id' => $item_tag);
783 my $stuff = NFC(shift());
784 $stuff =~ s/&(?!\S+;)/&/gso;
785 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;