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/;
17 use OpenSRF::AppSession;
21 use Unicode::Normalize;
22 use OpenILS::Utils::Fieldmapper;
23 use OpenILS::WWW::SuperCat::Feed;
26 # set the bootstrap config when this module is loaded
27 my ($bootstrap, $supercat, $actor, $parser, $search);
36 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
37 $supercat = OpenSRF::AppSession->create('open-ils.supercat');
38 $actor = OpenSRF::AppSession->create('open-ils.actor');
39 $search = OpenSRF::AppSession->create('open-ils.search');
40 $parser = new XML::LibXML;
46 return Apache2::Const::DECLINED if (-e $apache->filename);
48 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
51 ->request("open-ils.supercat.oisbn", $isbn)
54 print "Content-type: application/xml; charset=utf-8\n\n";
55 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
57 unless (exists $$list{metarecord}) {
59 return Apache2::Const::OK;
62 print "<idlist metarecord='$$list{metarecord}'>\n";
64 for ( keys %{ $$list{record_list} } ) {
65 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
66 print " <isbn record='$_'>$o</isbn>\n"
71 return Apache2::Const::OK;
77 return Apache2::Const::DECLINED if (-e $apache->filename);
80 my $rel_name = quotemeta($cgi->url(-relative=>1));
83 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
86 my $url = $cgi->url(-path_info=>$add_path);
87 my $root = (split 'unapi', $url)[0];
88 my $base = (split 'unapi', $url)[0] . 'unapi';
91 my $uri = $cgi->param('id') || '';
92 my $host = $cgi->virtual_host || $cgi->server_name;
94 my $format = $cgi->param('format');
95 my ($id,$type,$command) = ('','','');
98 print "Content-type: application/xml; charset=utf-8\n";
100 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
103 $type = 'metarecord' if ($1 =~ /^m/o);
106 ->request("open-ils.supercat.$type.formats")
111 my $body = "<formats id='$uri'><format name='opac' type='text/html'/>";
114 my ($type) = keys %$h;
115 $body .= "<format name='$type' type='application/xml'";
117 for my $part ( qw/namespace_uri docs schema_location/ ) {
118 $body .= " $part='$$h{$type}{$part}'"
119 if ($$h{$type}{$part});
125 $body .= "</formats>\n";
127 $apache->custom_response( 300, $body);
131 ->request("open-ils.supercat.record.formats")
136 ->request("open-ils.supercat.metarecord.formats")
140 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
141 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
143 print "<formats><format name='opac' type='text/html'/>";
146 my ($type) = keys %$h;
147 print "<format name='$type' type='application/xml'";
149 for my $part ( qw/namespace_uri docs schema_location/ ) {
150 print " $part='$$h{$type}{$part}'"
151 if ($$h{$type}{$part});
157 print "</formats>\n";
160 return Apache2::Const::OK;
165 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
168 $type = 'metarecord' if ($1 =~ /^m/o);
169 $command = 'retrieve';
172 if ($format eq 'opac') {
173 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
174 if ($type eq 'metarecord');
175 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
176 if ($type eq 'record');
178 } elsif ($format =~ /^html/o) {
179 my $feed = create_record_feed(
185 $feed->creator($host);
186 $feed->update_ts(gmtime_ISO8601());
188 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
189 print entityize($feed->toString) . "\n";
191 return Apache2::Const::OK;
194 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
198 print "Content-type: text/html; charset=utf-8\n\n";
199 $apache->custom_response( 404, <<" HTML");
202 <title>$type $id not found!</title>
206 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
213 print "Content-type: application/xml; charset=utf-8\n\n";
214 print $req->gather(1);
216 return Apache2::Const::OK;
222 return Apache2::Const::DECLINED if (-e $apache->filename);
226 my $rel_name = quotemeta($cgi->url(-relative=>1));
229 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
232 my $url = $cgi->url(-path_info=>$add_path);
233 my $root = (split 'supercat', $url)[0];
234 my $base = (split 'supercat', $url)[0] . 'supercat';
235 my $path = (split 'supercat', $url)[1];
236 my $unapi = (split 'supercat', $url)[0] . 'unapi';
238 my $host = $cgi->virtual_host || $cgi->server_name;
240 my ($id,$type,$format,$command) = reverse split '/', $path;
243 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
244 print "Content-type: application/xml; charset=utf-8\n";
247 ->request("open-ils.supercat.$1.formats")
255 <type>text/html</type>
259 my ($type) = keys %$h;
260 print "<format><name>$type</name><type>application/xml</type>";
262 for my $part ( qw/namespace_uri docs schema_location/ ) {
263 print "<$part>$$h{$type}{$part}</$part>"
264 if ($$h{$type}{$part});
270 print "</formats>\n";
272 return Apache2::Const::OK;
276 ->request("open-ils.supercat.record.formats")
281 ->request("open-ils.supercat.metarecord.formats")
285 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
286 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
291 <type>text/html</type>
295 my ($type) = keys %$h;
296 print "<format><name>$type</name><type>application/xml</type>";
298 for my $part ( qw/namespace_uri docs schema_location/ ) {
299 print "<$part>$$h{$type}{$part}</$part>"
300 if ($$h{$type}{$part});
306 print "</formats>\n";
309 return Apache2::Const::OK;
312 if ($format eq 'opac') {
313 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
314 if ($type eq 'metarecord');
315 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
316 if ($type eq 'record');
318 } elsif ($format =~ /^html/o) {
319 my $feed = create_record_feed( $format => [ $id ], $unapi,);
322 $feed->creator($host);
323 $feed->update_ts(gmtime_ISO8601());
325 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
326 print entityize($feed->toString) . "\n";
328 return Apache2::Const::OK;
331 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
335 print "Content-type: text/html; charset=utf-8\n\n";
336 $apache->custom_response( 404, <<" HTML");
339 <title>$type $id not found!</title>
343 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
350 print "Content-type: application/xml; charset=utf-8\n\n";
351 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
353 return Apache2::Const::OK;
359 return Apache2::Const::DECLINED if (-e $apache->filename);
363 my $year = (gmtime())[5] + 1900;
364 my $host = $cgi->virtual_host || $cgi->server_name;
366 my $rel_name = quotemeta($cgi->url(-relative=>1));
369 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
371 my $url = $cgi->url(-path_info=>$add_path);
372 my $root = (split 'feed', $url)[0];
373 my $base = (split 'bookbag', $url)[0] . 'bookbag';
374 my $path = (split 'bookbag', $url)[1];
375 my $unapi = (split 'feed', $url)[0] . 'unapi';
378 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
380 my ($id,$type) = reverse split '/', $path;
382 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
383 return Apache2::Const::NOT_FOUND unless($bucket);
385 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
386 if ($type eq 'opac') {
387 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
388 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
393 my $feed = create_record_feed(
395 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
400 $feed->title("Items in Book Bag [".$bucket->name."]");
401 $feed->creator($host);
402 $feed->update_ts(gmtime_ISO8601());
404 $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
405 $feed->link(rss2 => $base . "/rss2/$id");
406 $feed->link(html => $base . "/html/$id" => 'text/html');
407 $feed->link(unapi => $unapi);
411 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
412 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
417 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
418 print entityize($feed->toString) . "\n";
420 return Apache2::Const::OK;
425 return Apache2::Const::DECLINED if (-e $apache->filename);
429 my $year = (gmtime())[5] + 1900;
430 my $host = $cgi->virtual_host || $cgi->server_name;
432 my $rel_name = quotemeta($cgi->url(-relative=>1));
435 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
437 my $url = $cgi->url(-path_info=>$add_path);
438 my $root = (split 'feed', $url)[0];
439 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
440 my $path = (split 'freshmeat', $url)[1];
441 my $unapi = (split 'feed', $url)[0] . 'unapi';
444 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
448 my ($type,$rtype,$axis,$date,$limit) = split '/', $path;
452 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
454 if ($type eq 'opac') {
455 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
456 join('&', map { "rl=" . $_ } @$list) .
461 my $feed = create_record_feed( $type, $list, $unapi);
464 $feed->title("$limit most recent $rtype changes from $date forward");
465 $feed->creator($host);
466 $feed->update_ts(gmtime_ISO8601());
468 $feed->link(atom => $base . "/atom/$rtype/$axis/$date/$limit" => 'application/atom+xml');
469 $feed->link(rss2 => $base . "/rss2/$rtype/$axis/$date/$limit");
470 $feed->link(html => $base . "/html/$rtype/$axis/$date/$limit" => 'text/html');
471 $feed->link(unapi => $unapi);
475 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
476 join('&', map { 'rl=' . $_} @$list ),
481 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
482 print entityize($feed->toString) . "\n";
484 return Apache2::Const::OK;
493 if ($version eq '1.0') {
495 Content-type: application/opensearchdescription+xml; charset=utf-8
497 <?xml version="1.0" encoding="UTF-8"?>
498 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
499 <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&startIndex={startIndex}&count={count}</Url>
500 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
501 <ShortName>$lib</ShortName>
502 <LongName>Search $lib</LongName>
503 <Description>Search the $lib OPAC by $class.</Description>
504 <Tags>$lib book library</Tags>
505 <SampleSearch>harry+potter</SampleSearch>
506 <Developer>Mike Rylander for GPLS/PINES</Developer>
507 <Contact>feedback\@open-ils.org</Contact>
508 <SyndicationRight>open</SyndicationRight>
509 <AdultContent>false</AdultContent>
510 </OpenSearchDescription>
514 Content-type: application/opensearchdescription+xml; charset=utf-8
516 <?xml version="1.0" encoding="UTF-8"?>
517 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
518 <ShortName>$lib</ShortName>
519 <Description>Search the $lib OPAC by $class.</Description>
520 <Tags>$lib book library</Tags>
521 <Url type="application/atom+xml"
522 template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
523 <Url type="application/x-rss+xml"
524 template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
525 <Url type="application/x-mods3+xml"
526 template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
527 <Url type="application/x-mods+xml"
528 template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
529 <Url type="application/x-marcxml+xml"
530 template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
531 <LongName>Search $lib</LongName>
532 <Query role="example" searchTerms="harry+potter" />
533 <Developer>Mike Rylander for GPLS/PINES</Developer>
534 <SyndicationRight>open</SyndicationRight>
535 <AdultContent>false</AdultContent>
536 <Language>en-US</Language>
537 <OutputEncoding>UTF-8</OutputEncoding>
538 <InputEncoding>UTF-8</InputEncoding>
539 </OpenSearchDescription>
543 return Apache2::Const::OK;
546 sub opensearch_feed {
548 return Apache2::Const::DECLINED if (-e $apache->filename);
551 my $year = (gmtime())[5] + 1900;
553 my $host = $cgi->virtual_host || $cgi->server_name;
555 my $rel_name = quotemeta($cgi->url(-relative=>1));
558 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
560 my $url = $cgi->url(-path_info=>$add_path);
561 my $root = (split 'opensearch', $url)[0];
562 my $base = (split 'opensearch', $url)[0] . 'opensearch';
563 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
565 my $path = (split 'opensearch', $url)[1];
567 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
569 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
576 $lib = $actor->request(
577 'open-ils.actor.org_unit_list.search' => parent_ou => undef
578 )->gather(1)->[0]->shortname;
585 return opensearch_osd($version, $lib, $class, $base);
589 my $page = $cgi->param('startPage') || 1;
590 my $offset = $cgi->param('startIndex') || 1;
591 my $limit = $cgi->param('count') || 10;
592 my $lang = $cgi->param('language') || 'en-US';
594 $page = 1 if ($page !~ /^\d+$/);
595 $offset = 1 if ($offset !~ /^\d+$/);
596 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
597 $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
600 $offset = ($page - 1) * $limit;
605 my (undef,$version,$org,$type,$class,$terms) = split '/', $path;
607 $terms ||= $cgi->param('searchTerms');
608 $class ||= $cgi->param('searchClass') || '-';
609 $type ||= $cgi->param('responseType') || '-';
610 $org ||= $cgi->param('searchOrg') || '-';
612 if ($version eq '1.0') {
614 } elsif ($type eq '-') {
619 $class = 'keyword' if ($class eq '-');
620 $terms = decode_utf8($terms);
624 #warn "searching for $class -> [$terms] via OS $version, response type $type";
628 $org_unit = $actor->request(
629 'open-ils.actor.org_unit_list.search' => parent_ou => undef
632 $org_unit = $actor->request(
633 'open-ils.actor.org_unit_list.search' => shortname => $org
637 my $recs = $search->request(
638 'open-ils.search.biblio.multiclass' => {
639 searches => { $class => { term => $terms, }, },
640 org_unit => $org_unit->[0]->id,
646 my $feed = create_record_feed(
648 [ map { $_->[0] } @{$recs->{ids}} ],
653 $feed->search($terms);
655 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
656 $feed->creator($host);
657 $feed->update_ts(gmtime_ISO8601());
661 'http://a9.com/-/spec/opensearch/1.1/',
668 'http://a9.com/-/spec/opensearch/1.1/',
675 'http://a9.com/-/spec/opensearch/1.1/',
682 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
683 'application/opensearch+xml'
684 ) if ($offset + $limit < $recs->{count});
688 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
689 'application/opensearch+xml'
694 $base . "/$version/$org/$type/$class?searchTerms=$terms" =>
695 'application/opensearch+xml'
698 $feed->link( unapi => $unapi);
702 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
703 # join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
709 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
710 join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
714 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
715 print entityize($feed->toString) . "\n";
717 return Apache2::Const::OK;
720 sub create_record_feed {
726 my $base = $cgi->url;
727 my $host = $cgi->virtual_host || $cgi->server_name;
729 my $year = (gmtime())[5] + 1900;
731 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
733 $feed->unapi($unapi);
735 $type = 'atom' if ($type eq 'html');
736 $type = 'marcxml' if ($type eq 'htmlcard');
738 for my $rec (@$records) {
739 my $item_tag = "tag:$host,$year:biblio-record_entry/" . $rec;
742 my $xml = $supercat->request(
743 "open-ils.supercat.record.$type.retrieve",
747 my $node = $feed->add_item($xml);
749 $node->id($item_tag);
750 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=opac" => 'text/html');
751 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac");
752 $node->link(unapi => $feed->unapi . "?id=$item_tag");
753 $node->link('unapi-id' => $item_tag);
760 my $stuff = NFC(shift());
761 $stuff =~ s/&(?!\S+;)/&/gso;
762 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;