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('uri') || '';
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")
116 <type>text/html</type>
120 my ($type) = keys %$h;
121 $body .= "<format><name>$type</name><type>application/xml</type>";
123 for my $part ( qw/namespace_uri docs schema_location/ ) {
124 $body .= "<$part>$$h{$type}{$part}</$part>"
125 if ($$h{$type}{$part});
128 $body .= '</format>';
131 $body .= "</formats>\n";
133 $apache->custom_response( 300, $body);
137 ->request("open-ils.supercat.record.formats")
142 ->request("open-ils.supercat.metarecord.formats")
146 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
147 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
152 <type>text/html</type>
156 my ($type) = keys %$h;
157 print "<format><name>$type</name><type>application/xml</type>";
159 for my $part ( qw/namespace_uri docs schema_location/ ) {
160 print "<$part>$$h{$type}{$part}</$part>"
161 if ($$h{$type}{$part});
167 print "</formats>\n";
170 return Apache2::Const::OK;
175 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
178 $type = 'metarecord' if ($1 =~ /^m/o);
179 $command = 'retrieve';
182 if ($format eq 'opac') {
183 print "Location: $base/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
184 if ($type eq 'metarecord');
185 print "Location: $base/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
186 if ($type eq 'record');
188 } elsif ($format =~ /^html/o) {
189 my $feed = create_record_feed(
195 $feed->creator($host);
196 $feed->update_ts(gmtime_ISO8601());
198 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
199 print entityize($feed->toString) . "\n";
201 return Apache2::Const::OK;
204 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
208 print "Content-type: text/html; charset=utf-8\n\n";
209 $apache->custom_response( 404, <<" HTML");
212 <title>$type $id not found!</title>
216 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
223 print "Content-type: application/xml; charset=utf-8\n\n";
224 print $req->gather(1);
226 return Apache2::Const::OK;
232 return Apache2::Const::DECLINED if (-e $apache->filename);
236 my $rel_name = quotemeta($cgi->url(-relative=>1));
239 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
242 my $url = $cgi->url(-path_info=>$add_path);
243 my $root = (split 'supercat', $url)[0];
244 my $base = (split 'supercat', $url)[0] . 'supercat';
245 my $path = (split 'supercat', $url)[1];
246 my $unapi = (split 'supercat', $url)[0] . 'unapi';
248 my $host = $cgi->virtual_host || $cgi->server_name;
250 my ($id,$type,$format,$command) = reverse split '/', $path;
253 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
254 print "Content-type: application/xml; charset=utf-8\n";
257 ->request("open-ils.supercat.$1.formats")
265 <type>text/html</type>
269 my ($type) = keys %$h;
270 print "<format><name>$type</name><type>application/xml</type>";
272 for my $part ( qw/namespace_uri docs schema_location/ ) {
273 print "<$part>$$h{$type}{$part}</$part>"
274 if ($$h{$type}{$part});
280 print "</formats>\n";
282 return Apache2::Const::OK;
286 ->request("open-ils.supercat.record.formats")
291 ->request("open-ils.supercat.metarecord.formats")
295 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
296 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
301 <type>text/html</type>
305 my ($type) = keys %$h;
306 print "<format><name>$type</name><type>application/xml</type>";
308 for my $part ( qw/namespace_uri docs schema_location/ ) {
309 print "<$part>$$h{$type}{$part}</$part>"
310 if ($$h{$type}{$part});
316 print "</formats>\n";
319 return Apache2::Const::OK;
322 if ($format eq 'opac') {
323 print "Location: $base/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
324 if ($type eq 'metarecord');
325 print "Location: $base/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
326 if ($type eq 'record');
328 } elsif ($format =~ /^html/o) {
329 my $feed = create_record_feed( $format => [ $id ], $unapi,);
332 $feed->creator($host);
333 $feed->update_ts(gmtime_ISO8601());
335 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
336 print entityize($feed->toString) . "\n";
338 return Apache2::Const::OK;
341 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
345 print "Content-type: text/html; charset=utf-8\n\n";
346 $apache->custom_response( 404, <<" HTML");
349 <title>$type $id not found!</title>
353 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
360 print "Content-type: application/xml; charset=utf-8\n\n";
361 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
363 return Apache2::Const::OK;
369 return Apache2::Const::DECLINED if (-e $apache->filename);
373 my $year = (gmtime())[5] + 1900;
374 my $host = $cgi->virtual_host || $cgi->server_name;
376 my $rel_name = quotemeta($cgi->url(-relative=>1));
379 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
381 my $url = $cgi->url(-path_info=>$add_path);
382 my $root = (split 'feed', $url)[0];
383 my $base = (split 'bookbag', $url)[0] . 'bookbag';
384 my $path = (split 'bookbag', $url)[1];
385 my $unapi = (split 'feed', $url)[0] . 'unapi';
388 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
390 my ($id,$type) = reverse split '/', $path;
392 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
393 return Apache2::Const::NOT_FOUND unless($bucket);
395 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
396 if ($type eq 'opac') {
397 print "Location: $root/../en-US/skin/default/xml/rresult.xml?rt=list&" .
398 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
400 return Apache2::Const::OK;
403 my $feed = create_record_feed(
405 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
410 $feed->title("Items in Book Bag [".$bucket->name."]");
411 $feed->creator($host);
412 $feed->update_ts(gmtime_ISO8601());
414 $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
415 $feed->link(rss2 => $base . "/rss2/$id");
416 $feed->link(html => $base . "/html/$id" => 'text/html');
417 $feed->link(unapi => $unapi);
421 $root . '../en-US/skin/default/xml/rresult.xml?rt=list&' .
422 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
427 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
428 print entityize($feed->toString) . "\n";
430 return Apache2::Const::OK;
439 if ($version eq '1.0') {
441 Content-type: application/opensearchdescription+xml; charset=utf-8
443 <?xml version="1.0" encoding="UTF-8"?>
444 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
445 <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&startIndex={startIndex}&count={count}</Url>
446 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
447 <ShortName>$lib</ShortName>
448 <LongName>Search $lib</LongName>
449 <Description>Search the $lib OPAC by $class.</Description>
450 <Tags>$lib book library</Tags>
451 <SampleSearch>harry+potter</SampleSearch>
452 <Developer>Mike Rylander for GPLS/PINES</Developer>
453 <Contact>feedback\@open-ils.org</Contact>
454 <SyndicationRight>open</SyndicationRight>
455 <AdultContent>false</AdultContent>
456 </OpenSearchDescription>
460 Content-type: application/opensearchdescription+xml; charset=utf-8
462 <?xml version="1.0" encoding="UTF-8"?>
463 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
464 <ShortName>$lib</ShortName>
465 <Description>Search the $lib OPAC by $class.</Description>
466 <Tags>$lib book library</Tags>
467 <Url type="application/atom+xml"
468 template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
469 <Url type="application/x-rss+xml"
470 template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
471 <Url type="application/x-mods3+xml"
472 template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
473 <Url type="application/x-mods+xml"
474 template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
475 <Url type="application/x-marcxml+xml"
476 template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&startIndex={startIndex?}&count={count?}&language={language?}"/>
477 <LongName>Search $lib</LongName>
478 <Query role="example" searchTerms="harry+potter" />
479 <Developer>Mike Rylander for GPLS/PINES</Developer>
480 <SyndicationRight>open</SyndicationRight>
481 <AdultContent>false</AdultContent>
482 <Language>en-US</Language>
483 <OutputEncoding>UTF-8</OutputEncoding>
484 <InputEncoding>UTF-8</InputEncoding>
485 </OpenSearchDescription>
489 return Apache2::Const::OK;
492 sub opensearch_feed {
494 return Apache2::Const::DECLINED if (-e $apache->filename);
497 my $year = (gmtime())[5] + 1900;
499 my $host = $cgi->virtual_host || $cgi->server_name;
501 my $rel_name = quotemeta($cgi->url(-relative=>1));
504 $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
506 my $url = $cgi->url(-path_info=>$add_path);
507 my $root = (split 'opensearch', $url)[0];
508 my $base = (split 'opensearch', $url)[0] . 'opensearch';
509 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
511 my $path = (split 'opensearch', $url)[1];
513 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
515 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
522 $lib = $actor->request(
523 'open-ils.actor.org_unit_list.search' => parent_ou => undef
524 )->gather(1)->[0]->shortname;
531 return opensearch_osd($version, $lib, $class, $base);
535 my $page = $cgi->param('startPage') || 1;
536 my $offset = $cgi->param('startIndex') || 1;
537 my $limit = $cgi->param('count') || 10;
538 my $lang = $cgi->param('language') || 'en-US';
540 $page = 1 if ($page !~ /^\d+$/);
541 $offset = 1 if ($offset !~ /^\d+$/);
542 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
543 $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
546 $offset = ($page - 1) * $limit;
551 my (undef,$version,$org,$type,$class,$terms) = split '/', $path;
553 $terms ||= $cgi->param('searchTerms');
554 $class ||= $cgi->param('searchClass') || '-';
555 $type ||= $cgi->param('responseType') || '-';
556 $org ||= $cgi->param('searchOrg') || '-';
558 if ($version eq '1.0') {
560 } elsif ($type eq '-') {
565 $class = 'keyword' if ($class eq '-');
566 $terms = decode_utf8($terms);
570 #warn "searching for $class -> [$terms] via OS $version, response type $type";
574 $org_unit = $actor->request(
575 'open-ils.actor.org_unit_list.search' => parent_ou => undef
578 $org_unit = $actor->request(
579 'open-ils.actor.org_unit_list.search' => shortname => $org
583 my $recs = $search->request(
584 'open-ils.search.biblio.multiclass' => {
585 searches => { $class => { term => $terms, }, },
586 org_unit => $org_unit->[0]->id,
592 my $feed = create_record_feed(
594 [ map { $_->[0] } @{$recs->{ids}} ],
599 $feed->search($terms);
601 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
602 $feed->creator($host);
603 $feed->update_ts(gmtime_ISO8601());
607 'http://a9.com/-/spec/opensearch/1.1/',
614 'http://a9.com/-/spec/opensearch/1.1/',
621 'http://a9.com/-/spec/opensearch/1.1/',
628 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
629 'application/opensearch+xml'
630 ) if ($offset + $limit < $recs->{count});
634 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
635 'application/opensearch+xml'
640 $base . "/$version/$org/$type/$class?searchTerms=$terms" =>
641 'application/opensearch+xml'
644 $feed->link( unapi => $unapi);
648 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
649 # join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
655 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
656 join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
660 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
661 print entityize($feed->toString) . "\n";
663 return Apache2::Const::OK;
666 sub create_record_feed {
672 my $base = $cgi->url;
673 my $host = $cgi->virtual_host || $cgi->server_name;
675 my $year = (gmtime())[5] + 1900;
677 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
679 $feed->unapi($unapi);
681 $type = 'atom' if ($type eq 'html');
682 $type = 'marcxml' if ($type eq 'htmlcard');
684 for my $rec (@$records) {
685 my $item_tag = "tag:$host,$year:biblio-record_entry/" . $rec;
688 my $xml = $supercat->request(
689 "open-ils.supercat.record.$type.retrieve",
693 my $node = $feed->add_item($xml);
695 $node->id($item_tag);
696 $node->link(alternate => $feed->unapi . "?uri=$item_tag&format=opac" => 'text/html');
697 $node->link(opac => $feed->unapi . "?uri=$item_tag&format=opac");
698 $node->link(unapi => $feed->unapi . "?uri=$item_tag");
699 $node->link('unapi-uri' => $item_tag);
706 my $stuff = NFC(shift());
707 $stuff =~ s/&(?!\S+;)/&/gso;
708 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;