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,$lib) = ('','','');
99 print "Content-type: application/xml; charset=utf-8\n";
101 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
105 $type = 'metarecord' if ($1 =~ /^m/o);
108 ->request("open-ils.supercat.$type.formats")
113 my $body = "<formats id='$uri'><format name='opac' type='text/html'/>";
116 my ($type) = keys %$h;
117 $body .= "<format name='$type' type='application/xml'";
119 for my $part ( qw/namespace_uri docs schema_location/ ) {
120 $body .= " $part='$$h{$type}{$part}'"
121 if ($$h{$type}{$part});
127 $body .= "</formats>\n";
129 $apache->custom_response( 300, $body);
133 ->request("open-ils.supercat.record.formats")
138 ->request("open-ils.supercat.metarecord.formats")
142 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
143 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
145 print "<formats><format name='opac' type='text/html'/>";
148 my ($type) = keys %$h;
149 print "<format name='$type' type='application/xml'";
151 for my $part ( qw/namespace_uri docs schema_location/ ) {
152 print " $part='$$h{$type}{$part}'"
153 if ($$h{$type}{$part});
159 print "</formats>\n";
162 return Apache2::Const::OK;
167 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)(?:/(.+))?}o) {
171 $type = 'metarecord' if ($1 =~ /^m/o);
172 $command = 'retrieve';
175 if ($format eq 'opac') {
176 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
177 if ($type eq 'metarecord');
178 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
179 if ($type eq 'record');
182 #} elsif ($format =~ /^html/o) {
183 my $feed = create_record_feed(
190 $feed->creator($host);
191 $feed->update_ts(gmtime_ISO8601());
193 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
194 print entityize($feed->toString) . "\n";
196 return Apache2::Const::OK;
199 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
203 print "Content-type: text/html; charset=utf-8\n\n";
204 $apache->custom_response( 404, <<" HTML");
207 <title>$type $id not found!</title>
211 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
218 print "Content-type: application/xml; charset=utf-8\n\n";
219 print $req->gather(1);
221 return Apache2::Const::OK;
227 return Apache2::Const::DECLINED if (-e $apache->filename);
231 my $rel_name = quotemeta($cgi->url(-relative=>1));
234 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
237 my $url = $cgi->url(-path_info=>$add_path);
238 my $root = (split 'supercat', $url)[0];
239 my $base = (split 'supercat', $url)[0] . 'supercat';
240 my $path = (split 'supercat', $url)[1];
241 my $unapi = (split 'supercat', $url)[0] . 'unapi';
243 my $host = $cgi->virtual_host || $cgi->server_name;
245 my ($id,$type,$format,$command) = reverse split '/', $path;
248 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
249 print "Content-type: application/xml; charset=utf-8\n";
252 ->request("open-ils.supercat.$1.formats")
260 <type>text/html</type>
264 my ($type) = keys %$h;
265 print "<format><name>$type</name><type>application/xml</type>";
267 for my $part ( qw/namespace_uri docs schema_location/ ) {
268 print "<$part>$$h{$type}{$part}</$part>"
269 if ($$h{$type}{$part});
275 print "</formats>\n";
277 return Apache2::Const::OK;
281 ->request("open-ils.supercat.record.formats")
286 ->request("open-ils.supercat.metarecord.formats")
290 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
291 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
296 <type>text/html</type>
300 my ($type) = keys %$h;
301 print "<format><name>$type</name><type>application/xml</type>";
303 for my $part ( qw/namespace_uri docs schema_location/ ) {
304 print "<$part>$$h{$type}{$part}</$part>"
305 if ($$h{$type}{$part});
311 print "</formats>\n";
314 return Apache2::Const::OK;
317 if ($format eq 'opac') {
318 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
319 if ($type eq 'metarecord');
320 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
321 if ($type eq 'record');
323 } elsif ($format =~ /^html/o) {
324 my $feed = create_record_feed( $format => [ $id ], $unapi,);
327 $feed->creator($host);
328 $feed->update_ts(gmtime_ISO8601());
330 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
331 print entityize($feed->toString) . "\n";
333 return Apache2::Const::OK;
336 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
340 print "Content-type: text/html; charset=utf-8\n\n";
341 $apache->custom_response( 404, <<" HTML");
344 <title>$type $id not found!</title>
348 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
355 print "Content-type: application/xml; charset=utf-8\n\n";
356 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
358 return Apache2::Const::OK;
364 return Apache2::Const::DECLINED if (-e $apache->filename);
368 my $year = (gmtime())[5] + 1900;
369 my $host = $cgi->virtual_host || $cgi->server_name;
371 my $rel_name = quotemeta($cgi->url(-relative=>1));
374 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
376 my $url = $cgi->url(-path_info=>$add_path);
377 my $root = (split 'feed', $url)[0];
378 my $base = (split 'bookbag', $url)[0] . 'bookbag';
379 my $path = (split 'bookbag', $url)[1];
380 my $unapi = (split 'feed', $url)[0] . 'unapi';
383 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
385 my ($id,$type) = reverse split '/', $path;
387 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
388 return Apache2::Const::NOT_FOUND unless($bucket);
390 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
391 if ($type eq 'opac') {
392 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
393 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
398 my $feed = create_record_feed(
400 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
405 $feed->title("Items in Book Bag [".$bucket->name."]");
406 $feed->creator($host);
407 $feed->update_ts(gmtime_ISO8601());
409 $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
410 $feed->link(rss2 => $base . "/rss2/$id");
411 $feed->link(html => $base . "/html/$id" => 'text/html');
412 $feed->link(unapi => $unapi);
416 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
417 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
422 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
423 print entityize($feed->toString) . "\n";
425 return Apache2::Const::OK;
430 return Apache2::Const::DECLINED if (-e $apache->filename);
434 my $year = (gmtime())[5] + 1900;
435 my $host = $cgi->virtual_host || $cgi->server_name;
437 my $rel_name = quotemeta($cgi->url(-relative=>1));
440 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
442 my $url = $cgi->url(-path_info=>$add_path);
443 my $root = (split 'feed', $url)[0];
444 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
445 my $path = (split 'freshmeat', $url)[1];
446 my $unapi = (split 'feed', $url)[0] . 'unapi';
449 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
453 my ($type,$rtype,$axis,$date,$limit) = split '/', $path;
457 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
459 if ($type eq 'opac') {
460 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
461 join('&', map { "rl=" . $_ } @$list) .
466 my $feed = create_record_feed( $type, $list, $unapi);
469 $feed->title("$limit most recent $rtype changes from $date forward");
470 $feed->creator($host);
471 $feed->update_ts(gmtime_ISO8601());
473 $feed->link(atom => $base . "/atom/$rtype/$axis/$date/$limit" => 'application/atom+xml');
474 $feed->link(rss2 => $base . "/rss2/$rtype/$axis/$date/$limit");
475 $feed->link(html => $base . "/html/$rtype/$axis/$date/$limit" => 'text/html');
476 $feed->link(unapi => $unapi);
480 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
481 join('&', map { 'rl=' . $_} @$list ),
486 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
487 print entityize($feed->toString) . "\n";
489 return Apache2::Const::OK;
498 if ($version eq '1.0') {
500 Content-type: application/opensearchdescription+xml; charset=utf-8
502 <?xml version="1.0" encoding="UTF-8"?>
503 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
504 <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&startIndex={startIndex}&count={count}</Url>
505 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
506 <ShortName>$lib</ShortName>
507 <LongName>Search $lib</LongName>
508 <Description>Search the $lib OPAC by $class.</Description>
509 <Tags>$lib book library</Tags>
510 <SampleSearch>harry+potter</SampleSearch>
511 <Developer>Mike Rylander for GPLS/PINES</Developer>
512 <Contact>feedback\@open-ils.org</Contact>
513 <SyndicationRight>open</SyndicationRight>
514 <AdultContent>false</AdultContent>
515 </OpenSearchDescription>
519 Content-type: application/opensearchdescription+xml; charset=utf-8
521 <?xml version="1.0" encoding="UTF-8"?>
522 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
523 <ShortName>$lib</ShortName>
524 <Description>Search the $lib OPAC by $class.</Description>
525 <Tags>$lib book library</Tags>
526 <Url type="application/atom+xml"
527 template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
528 <Url type="application/x-rss+xml"
529 template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
530 <Url type="application/x-mods3+xml"
531 template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
532 <Url type="application/x-mods+xml"
533 template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
534 <Url type="application/x-marcxml+xml"
535 template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
536 <LongName>Search $lib</LongName>
537 <Query role="example" searchTerms="harry+potter" />
538 <Developer>Mike Rylander for GPLS/PINES</Developer>
539 <SyndicationRight>open</SyndicationRight>
540 <AdultContent>false</AdultContent>
541 <Language>en-US</Language>
542 <OutputEncoding>UTF-8</OutputEncoding>
543 <InputEncoding>UTF-8</InputEncoding>
544 </OpenSearchDescription>
548 return Apache2::Const::OK;
551 sub opensearch_feed {
553 return Apache2::Const::DECLINED if (-e $apache->filename);
556 my $year = (gmtime())[5] + 1900;
558 my $host = $cgi->virtual_host || $cgi->server_name;
560 my $rel_name = quotemeta($cgi->url(-relative=>1));
563 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
565 my $url = $cgi->url(-path_info=>$add_path);
566 my $root = (split 'opensearch', $url)[0];
567 my $base = (split 'opensearch', $url)[0] . 'opensearch';
568 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
571 my $path = (split 'opensearch', $url)[1];
573 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
575 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
582 $lib = $actor->request(
583 'open-ils.actor.org_unit_list.search' => parent_ou => undef
584 )->gather(1)->[0]->shortname;
591 return opensearch_osd($version, $lib, $class, $base);
595 my $page = $cgi->param('startPage') || 1;
596 my $offset = $cgi->param('startIndex') || 1;
597 my $limit = $cgi->param('count') || 10;
598 my $lang = $cgi->param('language') || 'en-US';
600 $page = 1 if ($page !~ /^\d+$/);
601 $offset = 1 if ($offset !~ /^\d+$/);
602 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
603 $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
606 $offset = ($page - 1) * $limit;
611 my (undef,$version,$org,$type,$class,$terms) = split '/', $path;
613 $terms ||= $cgi->param('searchTerms');
614 $class ||= $cgi->param('searchClass') || '-';
615 $type ||= $cgi->param('responseType') || '-';
616 $org ||= $cgi->param('searchOrg') || '-';
618 if ($version eq '1.0') {
620 } elsif ($type eq '-') {
625 $class = 'keyword' if ($class eq '-');
626 $terms = decode_utf8($terms);
630 #warn "searching for $class -> [$terms] via OS $version, response type $type";
634 $org_unit = $actor->request(
635 'open-ils.actor.org_unit_list.search' => parent_ou => undef
638 $org_unit = $actor->request(
639 'open-ils.actor.org_unit_list.search' => shortname => $org
643 my $rs_name = $cgi->cookie('os_session');
644 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
647 if (!($recs = $$cached_res{os_results}{$class}{$terms}{$org})) {
648 warn "NOT pulling results from cache";
649 $rs_name = $cgi->remote_host . '::' . rand(time);
650 $recs = $search->request(
651 'open-ils.search.biblio.multiclass' => {
652 searches => { $class => { term => $terms, }, },
653 org_unit => $org_unit->[0]->id,
659 $$cached_res{os_results}{$class}{$terms}{$org} = $recs;
660 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
666 my $feed = create_record_feed(
668 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
674 $feed->search($terms);
675 $feed->class($class);
677 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
678 $feed->creator($host);
679 $feed->update_ts(gmtime_ISO8601());
683 'http://a9.com/-/spec/opensearch/1.1/',
690 'http://a9.com/-/spec/opensearch/1.1/',
697 'http://a9.com/-/spec/opensearch/1.1/',
704 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
705 'application/opensearch+xml'
706 ) if ($offset + $limit < $recs->{count});
710 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
711 'application/opensearch+xml'
716 $base . "/$version/$org/$type/$class?searchTerms=$terms" =>
717 'application/opensearch+xml'
720 $feed->link( unapi => $unapi);
724 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
725 # join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
731 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
732 join('&', map { 'rl=' . $_->[0] } grep { defined $_ } @{$recs->{ids}} ),
737 -type => $feed->type,
739 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
742 print entityize($feed->toString) . "\n";
744 return Apache2::Const::OK;
747 sub create_record_feed {
752 my $lib = shift || '';
755 my $base = $cgi->url;
756 my $host = $cgi->virtual_host || $cgi->server_name;
758 my $year = (gmtime())[5] + 1900;
760 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
762 $feed->unapi($unapi);
764 $type = 'atom' if ($type eq 'html');
765 $type = 'marcxml' if ($type eq 'htmlcard' or $type eq 'htmlholdings');
767 for my $rec (@$records) {
770 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
773 my $xml = $supercat->request(
774 "open-ils.supercat.record.$type.retrieve",
778 my $node = $feed->add_item($xml);
780 if ($lib && $type eq 'marcxml') {
781 $xml = $supercat->request( "open-ils.supercat.record.holdings_xml.retrieve", $rec, $lib )->gather(1);
782 $node->add_holdings($xml);
785 $node->id($item_tag);
786 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings" => 'text/html');
787 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac");
788 $node->link(unapi => $feed->unapi . "?id=$item_tag");
789 $node->link('unapi-id' => $item_tag);
796 my $stuff = NFC(shift());
797 $stuff =~ s/&(?!\S+;)/&/gso;
798 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;