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 my $feed = create_record_feed(
189 $feed->creator($host);
190 $feed->update_ts(gmtime_ISO8601());
192 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
193 print entityize($feed->toString) . "\n";
195 return Apache2::Const::OK;
198 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
202 print "Content-type: text/html; charset=utf-8\n\n";
203 $apache->custom_response( 404, <<" HTML");
206 <title>$type $id not found!</title>
210 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
217 print "Content-type: application/xml; charset=utf-8\n\n";
218 print $req->gather(1);
220 return Apache2::Const::OK;
226 return Apache2::Const::DECLINED if (-e $apache->filename);
230 my $rel_name = quotemeta($cgi->url(-relative=>1));
233 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
236 my $url = $cgi->url(-path_info=>$add_path);
237 my $root = (split 'supercat', $url)[0];
238 my $base = (split 'supercat', $url)[0] . 'supercat';
239 my $path = (split 'supercat', $url)[1];
240 my $unapi = (split 'supercat', $url)[0] . 'unapi';
242 my $host = $cgi->virtual_host || $cgi->server_name;
244 my ($id,$type,$format,$command) = reverse split '/', $path;
247 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
248 print "Content-type: application/xml; charset=utf-8\n";
251 ->request("open-ils.supercat.$1.formats")
259 <type>text/html</type>
263 my ($type) = keys %$h;
264 print "<format><name>$type</name><type>application/xml</type>";
266 for my $part ( qw/namespace_uri docs schema_location/ ) {
267 print "<$part>$$h{$type}{$part}</$part>"
268 if ($$h{$type}{$part});
274 print "</formats>\n";
276 return Apache2::Const::OK;
280 ->request("open-ils.supercat.record.formats")
285 ->request("open-ils.supercat.metarecord.formats")
289 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
290 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
295 <type>text/html</type>
299 my ($type) = keys %$h;
300 print "<format><name>$type</name><type>application/xml</type>";
302 for my $part ( qw/namespace_uri docs schema_location/ ) {
303 print "<$part>$$h{$type}{$part}</$part>"
304 if ($$h{$type}{$part});
310 print "</formats>\n";
313 return Apache2::Const::OK;
316 if ($format eq 'opac') {
317 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
318 if ($type eq 'metarecord');
319 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
320 if ($type eq 'record');
322 } elsif ($format =~ /^html/o) {
323 my $feed = create_record_feed( $format => [ $id ], $unapi,);
326 $feed->creator($host);
327 $feed->update_ts(gmtime_ISO8601());
329 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
330 print entityize($feed->toString) . "\n";
332 return Apache2::Const::OK;
335 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
339 print "Content-type: text/html; charset=utf-8\n\n";
340 $apache->custom_response( 404, <<" HTML");
343 <title>$type $id not found!</title>
347 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
354 print "Content-type: application/xml; charset=utf-8\n\n";
355 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
357 return Apache2::Const::OK;
363 return Apache2::Const::DECLINED if (-e $apache->filename);
367 my $year = (gmtime())[5] + 1900;
368 my $host = $cgi->virtual_host || $cgi->server_name;
370 my $rel_name = quotemeta($cgi->url(-relative=>1));
373 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
375 my $url = $cgi->url(-path_info=>$add_path);
376 my $root = (split 'feed', $url)[0];
377 my $base = (split 'bookbag', $url)[0] . 'bookbag';
378 my $path = (split 'bookbag', $url)[1];
379 my $unapi = (split 'feed', $url)[0] . 'unapi';
382 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
384 my ($id,$type) = reverse split '/', $path;
386 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
387 return Apache2::Const::NOT_FOUND unless($bucket);
389 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
390 if ($type eq 'opac') {
391 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
392 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
397 my $feed = create_record_feed(
399 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
404 $feed->title("Items in Book Bag [".$bucket->name."]");
405 $feed->creator($host);
406 $feed->update_ts(gmtime_ISO8601());
408 $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
409 $feed->link(rss2 => $base . "/rss2/$id");
410 $feed->link(html => $base . "/html/$id" => 'text/html');
411 $feed->link(unapi => $unapi);
415 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
416 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
421 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
422 print entityize($feed->toString) . "\n";
424 return Apache2::Const::OK;
429 return Apache2::Const::DECLINED if (-e $apache->filename);
433 my $year = (gmtime())[5] + 1900;
434 my $host = $cgi->virtual_host || $cgi->server_name;
436 my $rel_name = quotemeta($cgi->url(-relative=>1));
439 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
441 my $url = $cgi->url(-path_info=>$add_path);
442 my $root = (split 'feed', $url)[0];
443 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
444 my $path = (split 'freshmeat', $url)[1];
445 my $unapi = (split 'feed', $url)[0] . 'unapi';
448 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
452 my ($type,$rtype,$axis,$date,$limit) = split '/', $path;
456 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
458 if ($type eq 'opac') {
459 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
460 join('&', map { "rl=" . $_ } @$list) .
465 my $feed = create_record_feed( $type, $list, $unapi);
468 $feed->title("$limit most recent $rtype changes from $date forward");
469 $feed->creator($host);
470 $feed->update_ts(gmtime_ISO8601());
472 $feed->link(atom => $base . "/atom/$rtype/$axis/$date/$limit" => 'application/atom+xml');
473 $feed->link(rss2 => $base . "/rss2/$rtype/$axis/$date/$limit");
474 $feed->link(html => $base . "/html/$rtype/$axis/$date/$limit" => 'text/html');
475 $feed->link(unapi => $unapi);
479 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
480 join('&', map { 'rl=' . $_} @$list ),
485 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
486 print entityize($feed->toString) . "\n";
488 return Apache2::Const::OK;
497 if ($version eq '1.0') {
499 Content-type: application/opensearchdescription+xml; charset=utf-8
501 <?xml version="1.0" encoding="UTF-8"?>
502 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
503 <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&startIndex={startIndex}&count={count}</Url>
504 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
505 <ShortName>$lib</ShortName>
506 <LongName>Search $lib</LongName>
507 <Description>Search the $lib OPAC by $class.</Description>
508 <Tags>$lib book library</Tags>
509 <SampleSearch>harry+potter</SampleSearch>
510 <Developer>Mike Rylander for GPLS/PINES</Developer>
511 <Contact>feedback\@open-ils.org</Contact>
512 <SyndicationRight>open</SyndicationRight>
513 <AdultContent>false</AdultContent>
514 </OpenSearchDescription>
518 Content-type: application/opensearchdescription+xml; charset=utf-8
520 <?xml version="1.0" encoding="UTF-8"?>
521 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
522 <ShortName>$lib</ShortName>
523 <Description>Search the $lib OPAC by $class.</Description>
524 <Tags>$lib book library</Tags>
525 <Url type="application/atom+xml"
526 template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
527 <Url type="application/x-rss+xml"
528 template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
529 <Url type="application/x-mods3+xml"
530 template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
531 <Url type="application/x-mods+xml"
532 template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
533 <Url type="application/x-marcxml+xml"
534 template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
535 <LongName>Search $lib</LongName>
536 <Query role="example" searchTerms="harry+potter" />
537 <Developer>Mike Rylander for GPLS/PINES</Developer>
538 <Contact>feedback\@open-ils.org</Contact>
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 my $lang = $cgi->param('searchLang');
614 my $sort = $cgi->param('searchSort');
615 my $site = $cgi->param('searchSite');
617 $terms ||= $cgi->param('searchTerms');
618 $class ||= $cgi->param('searchClass') || '-';
619 $type ||= $cgi->param('responseType') || '-';
620 $org ||= $cgi->param('searchOrg') || '-';
622 if ($version eq '1.0') {
624 } elsif ($type eq '-') {
629 $terms = decode_utf8($terms);
632 my $term_copy = $terms;
634 my $complex_terms = 0;
635 if ($terms eq 'help') {
636 print $cgi->header(-type => 'text/html');
640 <title>just type something!</title>
643 <p>You are in a maze of dark, twisty stacks, all alike.</p>
647 return Apache2::Const::OK;
652 while ($term_copy =~ /(keyword|title|author|subject|series|site|sort|lang):([^:]+?)$/o) {
655 $term_copy =~ s/(keyword|title|author|subject|series|site|sort|lang):([^:]+?)$//o;
657 ($org = uc($t)) =~ s/\s+//go;
658 } elsif ($c eq 'sort') {
659 ($sort = lc($t)) =~ s/^\s*(\w+)/$1/go;
660 } elsif ($c eq 'lang') {
661 ($lang = lc($t)) =~ s/^\s*(\w+)/$1/go;
663 $$searches{$c} = { term => $t };
664 $cache_key .= $c . $t;
671 $class = 'keyword' if ($class eq '-');
672 $$searches{$class}{term} .= " $term_copy";
673 $cache_key .= $class . $term_copy;
678 $org_unit = $actor->request(
679 'open-ils.actor.org_unit_list.search' => parent_ou => undef
682 $org_unit = $actor->request(
683 'open-ils.actor.org_unit_list.search' => shortname => $org
687 $cache_key .= $org.$sort.$lang;
689 my $rs_name = $cgi->cookie('os_session');
690 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
693 if (!($recs = $$cached_res{os_results}{$cache_key})) {
694 $rs_name = $cgi->remote_host . '::' . rand(time);
695 $recs = $search->request(
696 'open-ils.search.biblio.multiclass' => {
697 searches => $searches,
698 org_unit => $org_unit->[0]->id,
699 ($sort ? ( 'sort' => $sort, sort_dir => 'asc' ) : ()),
700 ($lang ? ( 'language' => $lang) : ()),
706 $$cached_res{os_results}{$cache_key} = $recs;
707 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
713 my $feed = create_record_feed(
715 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
721 $feed->search($terms);
722 $feed->class($class);
724 if ($complex_terms) {
725 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
727 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
730 $feed->creator($host);
731 $feed->update_ts(gmtime_ISO8601());
735 'http://a9.com/-/spec/opensearch/1.1/',
742 'http://a9.com/-/spec/opensearch/1.1/',
749 'http://a9.com/-/spec/opensearch/1.1/',
756 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
757 'application/opensearch+xml'
758 ) if ($offset + $limit < $recs->{count});
762 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
763 'application/opensearch+xml'
768 $base . "/$version/$org/$type/$class?searchTerms=$terms" =>
769 'application/opensearch+xml'
772 $feed->link( unapi => $unapi);
776 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
777 # join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
783 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
784 join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
789 -type => $feed->type,
791 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
794 print entityize($feed->toString) . "\n";
796 return Apache2::Const::OK;
799 sub create_record_feed {
804 my $lib = shift || '';
807 my $base = $cgi->url;
808 my $host = $cgi->virtual_host || $cgi->server_name;
810 my $year = (gmtime())[5] + 1900;
812 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
814 $feed->unapi($unapi);
816 $type = 'atom' if ($type eq 'html');
817 $type = 'marcxml' if ($type eq 'htmlcard' or $type eq 'htmlholdings');
819 for my $rec (@$records) {
822 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
825 my $xml = $supercat->request(
826 "open-ils.supercat.record.$type.retrieve",
830 my $node = $feed->add_item($xml);
832 if ($lib && $type eq 'marcxml') {
833 $xml = $supercat->request( "open-ils.supercat.record.holdings_xml.retrieve", $rec, $lib )->gather(1);
834 $node->add_holdings($xml);
837 $node->id($item_tag);
838 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings" => 'text/html');
839 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac");
840 $node->link(unapi => $feed->unapi . "?id=$item_tag");
841 $node->link('unapi-id' => $item_tag);
848 my $stuff = NFC(shift());
849 $stuff =~ s/&(?!\S+;)/&/gso;
850 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;