1 package OpenILS::WWW::SuperCat;
2 use strict; use warnings;
5 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
6 use APR::Const -compile => qw(:error SUCCESS);
7 use Apache2::RequestRec ();
8 use Apache2::RequestIO ();
9 use Apache2::RequestUtil;
13 use OpenSRF::EX qw(:try);
14 use OpenSRF::Utils qw/:datetime/;
15 use OpenSRF::Utils::Cache;
17 use OpenSRF::AppSession;
22 use Unicode::Normalize;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::WWW::SuperCat::Feed;
25 use OpenSRF::Utils::Logger qw/$logger/;
28 # set the bootstrap config when this module is loaded
29 my ($bootstrap, $cstore, $supercat, $actor, $parser, $search, $xslt, $cn_browse_xslt, %browse_types);
31 $browse_types{call_number}{xml} = sub {
34 my $year = (gmtime())[5] + 1900;
37 $content .= "<hold:volumes xmlns:hold='http://open-ils.org/spec/holdings/v1'>";
40 (my $cn_class = $cn->class_name) =~ s/::/-/gso;
41 $cn_class =~ s/Fieldmapper-//gso;
43 my $cn_tag = "tag:open-ils.org,$year:$cn_class/".$cn->id;
44 my $cn_lib = $cn->owning_lib->shortname;
45 my $cn_label = $cn->label;
47 $cn_label =~ s/\n//gos;
48 $cn_label =~ s/'/'/go;
50 (my $ou_class = $cn->owning_lib->class_name) =~ s/::/-/gso;
51 $ou_class =~ s/Fieldmapper-//gso;
53 my $ou_tag = "tag:open-ils.org,$year:$ou_class/".$cn->owning_lib->id;
54 my $ou_name = $cn->owning_lib->name;
56 $ou_name =~ s/\n//gos;
57 $ou_name =~ s/'/'/go;
59 (my $rec_class = $cn->record->class_name) =~ s/::/-/gso;
60 $rec_class =~ s/Fieldmapper-//gso;
62 my $rec_tag = "tag:open-ils.org,$year:$rec_class/".$cn->record->id.'/'.$cn->owning_lib->shortname;
64 $content .= "<hold:volume id='$cn_tag' lib='$cn_lib' label='$cn_label'>";
65 $content .= "<act:owning_lib xmlns:act='http://open-ils.org/spec/actors/v1' id='$ou_tag' name='$ou_name'/>";
67 my $r_doc = $parser->parse_string($cn->record->marc);
68 $r_doc->documentElement->setAttribute( id => $rec_tag );
69 $content .= entityize($r_doc->documentElement->toString);
71 $content .= "</hold:volume>";
74 $content .= '</hold:volumes>';
75 return ("Content-type: application/xml\n\n",$content);
79 $browse_types{call_number}{html} = sub {
84 if (!$cn_browse_xslt) {
85 $cn_browse_xslt = $parser->parse_file(
86 OpenSRF::Utils::SettingsClient
88 ->config_value( dirs => 'xsl' ).
91 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
94 my (undef,$xml) = $browse_types{call_number}{xml}->($tree);
97 "Content-type: text/html\n\n",
99 $cn_browse_xslt->transform(
100 $parser->parse_string( $xml ),
115 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
117 my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
118 Fieldmapper->import(IDL => $idl);
120 $supercat = OpenSRF::AppSession->create('open-ils.supercat');
121 $cstore = OpenSRF::AppSession->create('open-ils.cstore');
122 $actor = OpenSRF::AppSession->create('open-ils.actor');
123 $search = OpenSRF::AppSession->create('open-ils.search');
124 $parser = new XML::LibXML;
125 $xslt = new XML::LibXSLT;
127 $cn_browse_xslt = $parser->parse_file(
128 OpenSRF::Utils::SettingsClient
130 ->config_value( dirs => 'xsl' ).
134 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
141 return Apache2::Const::DECLINED if (-e $apache->filename);
143 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
146 ->request("open-ils.supercat.oisbn", $isbn)
149 print "Content-type: application/xml; charset=utf-8\n\n";
150 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
152 unless (exists $$list{metarecord}) {
154 return Apache2::Const::OK;
157 print "<idlist metarecord='$$list{metarecord}'>\n";
159 for ( keys %{ $$list{record_list} } ) {
160 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
161 print " <isbn record='$_'>$o</isbn>\n"
166 return Apache2::Const::OK;
172 return Apache2::Const::DECLINED if (-e $apache->filename);
177 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
178 my $rel_name = $cgi->url(-relative=>1);
179 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
182 my $url = $cgi->url(-path_info=>$add_path);
183 my $root = (split 'unapi', $url)[0];
184 my $base = (split 'unapi', $url)[0] . 'unapi';
187 my $uri = $cgi->param('id') || '';
188 my $host = $cgi->virtual_host || $cgi->server_name;
190 my $format = $cgi->param('format');
191 my $flesh_feed = ($format =~ /-full$/o) ? 1 : 0;
192 (my $base_format = $format) =~ s/-full$//o;
193 my ($id,$type,$command,$lib) = ('','','');
196 my $body = "Content-type: application/xml; charset=utf-8\n\n";
198 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))$}o) {
202 $type = 'metarecord' if ($1 =~ /^m/o);
205 ->request("open-ils.supercat.$type.formats")
208 if ($type eq 'record' or $type eq 'isbn') {
209 $body .= <<" FORMATS";
211 <format name='opac' type='text/html'/>
212 <format name='html' type='text/html'/>
213 <format name='htmlholdings' type='text/html'/>
214 <format name='html-full' type='text/html'/>
215 <format name='htmlholdings-full' type='text/html'/>
217 } elsif ($type eq 'metarecord') {
218 $body .= <<" FORMATS";
220 <format name='opac' type='text/html'/>
225 my ($type) = keys %$h;
226 $body .= "\t<format name='$type' type='application/xml'";
228 for my $part ( qw/namespace_uri docs schema_location/ ) {
229 $body .= " $part='$$h{$type}{$part}'"
230 if ($$h{$type}{$part});
235 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
236 $body .= "\t<format name='$type-full' type='application/xml'";
238 for my $part ( qw/namespace_uri docs schema_location/ ) {
239 $body .= " $part='$$h{$type}{$part}'"
240 if ($$h{$type}{$part});
247 $body .= "</formats>\n";
251 ->request("open-ils.supercat.record.formats")
256 ->request("open-ils.supercat.metarecord.formats")
260 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
261 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
263 $body .= <<" FORMATS";
265 <format name='opac' type='text/html'/>
266 <format name='html' type='text/html'/>
267 <format name='htmlholdings' type='text/html'/>
268 <format name='html-full' type='text/html'/>
269 <format name='htmlholdings-full' type='text/html'/>
274 my ($type) = keys %$h;
275 $body .= "\t<format name='$type' type='application/xml'";
277 for my $part ( qw/namespace_uri docs schema_location/ ) {
278 $body .= " $part='$$h{$type}{$part}'"
279 if ($$h{$type}{$part});
284 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
285 $body .= "\t<format name='$type-full' type='application/xml'";
287 for my $part ( qw/namespace_uri docs schema_location/ ) {
288 $body .= " $part='$$h{$type}{$part}'"
289 if ($$h{$type}{$part});
296 $body .= "</formats>\n";
300 return Apache2::Const::OK;
303 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))?}o) {
307 $type = 'metarecord' if ($1 =~ /^metabib/o);
308 $type = 'isbn' if ($1 =~ /^isbn/o);
309 $type = 'call_number' if ($1 =~ /^call_number/o);
310 $command = 'retrieve';
311 $command = 'browse' if ($type eq 'call_number');
314 if (!$lib || $lib eq '-') {
315 $lib = $actor->request(
316 'open-ils.actor.org_unit_list.search' => parent_ou => undef
317 )->gather(1)->[0]->shortname;
320 my $lib_object = $actor->request(
321 'open-ils.actor.org_unit_list.search' => shortname => $lib
323 my $lib_id = $lib_object->id;
325 my $ou_types = $actor->request( 'open-ils.actor.org_types.retrieve' )->gather(1);
326 my $lib_depth = (grep { $_->id == $lib_object->ou_type } @$ou_types)[0]->depth;
328 if ($type eq 'call_number' and $command eq 'browse') {
329 print "Location: $root/browse/$base_format/call_number/$lib/$id\n\n";
333 if ($type eq 'isbn') {
334 my $rec = $supercat->request('open-ils.supercat.isbn.object.retrieve',$id)->gather(1);
336 print "Content-type: text/html; charset=utf-8\n\n";
337 $apache->custom_response( 404, <<" HTML");
340 <title>Type [$type] with id [$id] not found!</title>
344 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
355 { (keys(%$_))[0] eq $base_format }
356 @{ $supercat->request("open-ils.supercat.$type.formats")->gather(1) }
358 { $_ eq $base_format }
359 qw/opac html htmlholdings/
361 print "Content-type: text/html; charset=utf-8\n\n";
362 $apache->custom_response( 406, <<" HTML");
365 <title>Invalid format [$format] for type [$type]!</title>
369 <center>Sorry, format $format is not valid for type $type.</center>
376 if ($format eq 'opac') {
377 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id&l=$lib_id&d=$lib_depth\n\n"
378 if ($type eq 'metarecord');
379 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id&l=$lib_id&d=$lib_depth\n\n"
380 if ($type eq 'record');
382 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
383 my $feed = create_record_feed(
392 print "Content-type: text/html; charset=utf-8\n\n";
393 $apache->custom_response( 404, <<" HTML");
396 <title>Type [$type] with id [$id] not found!</title>
400 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
408 $feed->creator($host);
409 $feed->update_ts(gmtime_ISO8601());
410 $feed->link( unapi => $base) if ($flesh_feed);
412 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
413 print entityize($feed->toString) . "\n";
415 return Apache2::Const::OK;
418 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
419 my $data = $req->gather(1);
421 if ($req->failed || !$data) {
422 print "Content-type: text/html; charset=utf-8\n\n";
423 $apache->custom_response( 404, <<" HTML");
426 <title>$type $id not found!</title>
430 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
437 print "Content-type: application/xml; charset=utf-8\n\n$data";
439 return Apache2::Const::OK;
445 return Apache2::Const::DECLINED if (-e $apache->filename);
450 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
451 my $rel_name = $cgi->url(-relative=>1);
452 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
455 my $url = $cgi->url(-path_info=>$add_path);
456 my $root = (split 'supercat', $url)[0];
457 my $base = (split 'supercat', $url)[0] . 'supercat';
458 my $unapi = (split 'supercat', $url)[0] . 'unapi';
460 my $host = $cgi->virtual_host || $cgi->server_name;
462 my $path = $cgi->path_info;
463 my ($id,$type,$format,$command) = reverse split '/', $path;
464 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
465 (my $base_format = $format) =~ s/-full$//o;
467 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
468 print "Content-type: application/xml; charset=utf-8\n";
471 ->request("open-ils.supercat.$1.formats")
479 <type>text/html</type>
482 if ($1 eq 'record' or $1 eq 'isbn') {
484 <name>htmlholdings</name>
485 <type>text/html</type>
489 <type>text/html</type>
492 <name>htmlholdings-full</name>
493 <type>text/html</type>
496 <name>html-full</name>
497 <type>text/html</type>
502 my ($type) = keys %$h;
503 print "<format><name>$type</name><type>application/xml</type>";
505 for my $part ( qw/namespace_uri docs schema_location/ ) {
506 print "<$part>$$h{$type}{$part}</$part>"
507 if ($$h{$type}{$part});
512 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
513 print "<format><name>$type-full</name><type>application/xml</type>";
515 for my $part ( qw/namespace_uri docs schema_location/ ) {
516 print "<$part>$$h{$type}{$part}</$part>"
517 if ($$h{$type}{$part});
525 print "</formats>\n";
527 return Apache2::Const::OK;
531 ->request("open-ils.supercat.record.formats")
536 ->request("open-ils.supercat.metarecord.formats")
540 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
541 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
546 <type>text/html</type>
549 <name>htmlholdings</name>
550 <type>text/html</type>
554 <type>text/html</type>
557 <name>htmlholdings-full</name>
558 <type>text/html</type>
561 <name>html-full</name>
562 <type>text/html</type>
566 my ($type) = keys %$h;
567 print "<format><name>$type</name><type>application/xml</type>";
569 for my $part ( qw/namespace_uri docs schema_location/ ) {
570 print "<$part>$$h{$type}{$part}</$part>"
571 if ($$h{$type}{$part});
576 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
577 print "<format><name>$type-full</name><type>application/xml</type>";
579 for my $part ( qw/namespace_uri docs schema_location/ ) {
580 print "<$part>$$h{$type}{$part}</$part>"
581 if ($$h{$type}{$part});
589 print "</formats>\n";
592 return Apache2::Const::OK;
595 if ($format eq 'opac') {
596 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
597 if ($type eq 'metarecord');
598 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
599 if ($type eq 'record');
601 } elsif ($base_format eq 'marc21') {
605 my $bib = $supercat->request( "open-ils.supercat.record.object.retrieve", $id )->gather(1)->[0];
607 my $r = MARC::Record->new_from_xml( $bib->marc, 'UTF-8', 'USMARC' );
608 $r->delete_field( $_ ) for ($r->field(901));
613 a => $bib->tcn_value,
614 b => $bib->tcn_source,
619 print "Content-type: application/octet-stream\n\n";
624 print "Content-type: text/html; charset=utf-8\n\n";
625 $apache->custom_response( 404, <<" HTML");
628 <title>$type $id not found!</title>
632 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
641 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
642 my $feed = create_record_feed(
650 $feed->creator($host);
651 $feed->update_ts(gmtime_ISO8601());
652 $feed->link( unapi => $base) if ($flesh_feed);
654 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
655 print entityize($feed->toString) . "\n";
657 return Apache2::Const::OK;
660 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
664 print "Content-type: text/html; charset=utf-8\n\n";
665 $apache->custom_response( 404, <<" HTML");
668 <title>$type $id not found!</title>
672 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
679 print "Content-type: application/xml; charset=utf-8\n\n";
680 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
682 return Apache2::Const::OK;
688 return Apache2::Const::DECLINED if (-e $apache->filename);
692 my $year = (gmtime())[5] + 1900;
693 my $host = $cgi->virtual_host || $cgi->server_name;
696 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
697 my $rel_name = $cgi->url(-relative=>1);
698 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
701 my $url = $cgi->url(-path_info=>$add_path);
702 my $root = (split 'feed', $url)[0] . '/';
703 my $base = (split 'bookbag', $url)[0] . '/bookbag';
704 my $unapi = (split 'feed', $url)[0] . '/unapi';
706 $root =~ s{(?<!http:)//}{/}go;
707 $base =~ s{(?<!http:)//}{/}go;
708 $unapi =~ s{(?<!http:)//}{/}go;
710 my $path = $cgi->path_info;
711 #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
713 my ($id,$type) = reverse split '/', $path;
714 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
716 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
717 return Apache2::Const::NOT_FOUND unless($bucket);
719 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
720 if ($type eq 'opac') {
721 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
722 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
727 my $feed = create_record_feed(
730 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
737 $feed->title("Items in Book Bag [".$bucket->name."]");
738 $feed->creator($host);
739 $feed->update_ts(gmtime_ISO8601());
741 $feed->link(rss => $base . "/rss2-full/$id" => 'application/rss+xml');
742 $feed->link(alternate => $base . "/atom-full/$id" => 'application/atom+xml');
743 $feed->link(html => $base . "/html-full/$id" => 'text/html');
744 $feed->link(unapi => $unapi);
748 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
749 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
754 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
755 print entityize($feed->toString) . "\n";
757 return Apache2::Const::OK;
762 return Apache2::Const::DECLINED if (-e $apache->filename);
766 my $year = (gmtime())[5] + 1900;
767 my $host = $cgi->virtual_host || $cgi->server_name;
770 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
771 my $rel_name = $cgi->url(-relative=>1);
772 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
775 my $url = $cgi->url(-path_info=>$add_path);
776 my $root = (split 'feed', $url)[0];
777 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
778 my $unapi = (split 'feed', $url)[0] . 'unapi';
780 my $path = $cgi->path_info;
781 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
783 $path =~ s/^\/(?:feed\/)?freshmeat\///og;
785 my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
786 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
789 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
791 #if ($type eq 'opac') {
792 # print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
793 # join('&', map { "rl=" . $_ } @$list) .
798 my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
802 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
804 $feed->title("$limit most recent $rtype ${axis}s");
807 $feed->creator($host);
808 $feed->update_ts(gmtime_ISO8601());
810 $feed->link(rss => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
811 $feed->link(alternate => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
812 $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
813 $feed->link(unapi => $unapi);
817 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
818 join('&', map { 'rl=' . $_} @$list ),
823 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
824 print entityize($feed->toString) . "\n";
826 return Apache2::Const::OK;
835 if ($version eq '1.0') {
837 Content-type: application/opensearchdescription+xml; charset=utf-8
839 <?xml version="1.0" encoding="UTF-8"?>
840 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
841 <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&startIndex={startIndex}&count={count}</Url>
842 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
843 <ShortName>$lib</ShortName>
844 <LongName>Search $lib</LongName>
845 <Description>Search the $lib OPAC by $class.</Description>
846 <Tags>$lib book library</Tags>
847 <SampleSearch>harry+potter</SampleSearch>
848 <Developer>Mike Rylander for GPLS/PINES</Developer>
849 <Contact>feedback\@open-ils.org</Contact>
850 <SyndicationRight>open</SyndicationRight>
851 <AdultContent>false</AdultContent>
852 </OpenSearchDescription>
856 Content-type: application/opensearchdescription+xml; charset=utf-8
858 <?xml version="1.0" encoding="UTF-8"?>
859 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
860 <ShortName>$lib</ShortName>
861 <Description>Search the $lib OPAC by $class.</Description>
862 <Tags>$lib book library</Tags>
863 <Url type="application/rss+xml"
864 template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
865 <Url type="application/atom+xml"
866 template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
867 <Url type="application/x-mods3+xml"
868 template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
869 <Url type="application/x-mods+xml"
870 template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
871 <Url type="application/x-marcxml+xml"
872 template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
873 <Url type="text/html"
874 template="$base/1.1/$lib/html-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
875 <LongName>Search $lib</LongName>
876 <Query role="example" searchTerms="harry+potter" />
877 <Developer>Mike Rylander for GPLS/PINES</Developer>
878 <Contact>feedback\@open-ils.org</Contact>
879 <SyndicationRight>open</SyndicationRight>
880 <AdultContent>false</AdultContent>
881 <Language>en-US</Language>
882 <OutputEncoding>UTF-8</OutputEncoding>
883 <InputEncoding>UTF-8</InputEncoding>
884 </OpenSearchDescription>
888 return Apache2::Const::OK;
891 sub opensearch_feed {
893 return Apache2::Const::DECLINED if (-e $apache->filename);
896 my $year = (gmtime())[5] + 1900;
898 my $host = $cgi->virtual_host || $cgi->server_name;
901 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
902 my $rel_name = $cgi->url(-relative=>1);
903 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
906 my $url = $cgi->url(-path_info=>$add_path);
907 my $root = (split 'opensearch', $url)[0];
908 my $base = (split 'opensearch', $url)[0] . 'opensearch';
909 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
911 my $path = $cgi->path_info;
912 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
914 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
920 if (!$lib || $lib eq '-') {
921 $lib = $actor->request(
922 'open-ils.actor.org_unit_list.search' => parent_ou => undef
923 )->gather(1)->[0]->shortname;
930 return opensearch_osd($version, $lib, $class, $base);
934 my $page = $cgi->param('startPage') || 1;
935 my $offset = $cgi->param('startIndex') || 1;
936 my $limit = $cgi->param('count') || 10;
938 $page = 1 if ($page !~ /^\d+$/);
939 $offset = 1 if ($offset !~ /^\d+$/);
940 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
943 $offset = ($page - 1) * $limit;
948 my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = ('','','','','','','','');
949 (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
951 $lang = $cgi->param('searchLang') if $cgi->param('searchLang');
952 $lang = '' if ($lang eq '*');
954 $sort = $cgi->param('searchSort') if $cgi->param('searchSort');
955 $sortdir = $cgi->param('searchSortDir') if $cgi->param('searchSortDir');
956 $terms .= " " . $cgi->param('searchTerms') if $cgi->param('searchTerms');
958 $class = $cgi->param('searchClass') if $cgi->param('searchClass');
961 $type = $cgi->param('responseType') if $cgi->param('responseType');
964 $org = $cgi->param('searchOrg') if $cgi->param('searchOrg');
968 my $kwt = $cgi->param('kw');
969 my $tit = $cgi->param('ti');
970 my $aut = $cgi->param('au');
971 my $sut = $cgi->param('su');
972 my $set = $cgi->param('se');
974 $terms .= " keyword: $kwt" if ($kwt);
975 $terms .= " title: $tit" if ($tit);
976 $terms .= " author: $aut" if ($aut);
977 $terms .= " subject: $sut" if ($sut);
978 $terms .= " series: $set" if ($set);
980 if ($version eq '1.0') {
982 } elsif ($type eq '-') {
985 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
987 $terms = decode_utf8($terms);
990 $terms =~ s/^\s+//go;
991 my $term_copy = $terms;
993 my $complex_terms = 0;
994 if ($terms eq 'help') {
995 print $cgi->header(-type => 'text/html');
999 <title>just type something!</title>
1002 <p>You are in a maze of dark, twisty stacks, all alike.</p>
1006 return Apache2::Const::OK;
1011 while ($term_copy =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang):[^:]+)$//so) {
1012 my ($c,$t) = split ':' => $1;
1017 } elsif ($c eq 'sort') {
1018 ($sort = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1019 } elsif ($c eq 'dir') {
1020 ($sortdir = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1021 } elsif ($c eq 'lang') {
1022 ($lang = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1024 $$searches{$c}{term} .= ' '.$t;
1025 $cache_key .= $c . $t;
1030 $lang = 'eng' if ($lang eq 'en-US');
1034 $class = 'keyword' if ($class eq '-');
1035 $$searches{$class}{term} .= " $term_copy";
1036 $cache_key .= $class . $term_copy;
1041 $org_unit = $actor->request(
1042 'open-ils.actor.org_unit_list.search' => parent_ou => undef
1045 $org_unit = $actor->request(
1046 'open-ils.actor.org_unit_list.search' => shortname => uc($org)
1050 { no warnings; $cache_key .= $org.$sort.$sortdir.$lang; }
1052 my $rs_name = $cgi->cookie('os_session');
1053 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
1056 if (!($recs = $$cached_res{os_results}{$cache_key})) {
1057 $rs_name = $cgi->remote_host . '::' . rand(time);
1058 $recs = $search->request(
1059 'open-ils.search.biblio.multiclass' => {
1060 searches => $searches,
1061 org_unit => $org_unit->[0]->id,
1064 ($sort ? ( 'sort' => $sort ) : ()),
1065 ($sortdir ? ( 'sort_dir' => $sortdir ) : ($sort ? (sort_dir => 'asc') : (sort_dir => 'desc') )),
1066 ($lang ? ( 'language' => $lang ) : ()),
1070 $$cached_res{os_results}{$cache_key} = $recs;
1071 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
1072 } catch Error with {
1073 warn "supercat unable to store IDs in memcache server\n";
1074 $logger->error("supercat unable to store IDs in memcache server");
1078 my $feed = create_record_feed(
1081 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
1088 $feed->search($terms);
1089 $feed->class($class);
1091 if ($complex_terms) {
1092 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1094 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
1097 $feed->creator($host);
1098 $feed->update_ts(gmtime_ISO8601());
1100 $feed->_create_node(
1101 $feed->{item_xpath},
1102 'http://a9.com/-/spec/opensearch/1.1/',
1107 $feed->_create_node(
1108 $feed->{item_xpath},
1109 'http://a9.com/-/spec/opensearch/1.1/',
1114 $feed->_create_node(
1115 $feed->{item_xpath},
1116 'http://a9.com/-/spec/opensearch/1.1/',
1123 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
1124 'application/opensearch+xml'
1125 ) if ($offset + $limit < $recs->{count});
1129 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1130 'application/opensearch+xml'
1135 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1136 'application/opensearch+xml'
1141 $base . "/$version/$org/rss2-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1142 'application/rss+xml'
1147 $base . "/$version/$org/atom-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1148 'application/atom+xml'
1153 $base . "/$version/$org/html/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1159 $base . "/$version/$org/html-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1163 $feed->link( 'unapi-server' => $unapi);
1167 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1168 # join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1173 -type => $feed->type,
1174 -charset => 'UTF-8',
1175 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
1178 print entityize($feed->toString) . "\n";
1180 return Apache2::Const::OK;
1183 sub create_record_feed {
1186 my $records = shift;
1189 my $lib = uc(shift()) || '-';
1191 $flesh = 1 if (!defined($flesh));
1194 my $base = $cgi->url;
1195 my $host = $cgi->virtual_host || $cgi->server_name;
1197 my $year = (gmtime())[5] + 1900;
1199 my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1201 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1202 $feed->base($base) if ($flesh);
1203 $feed->unapi($unapi) if ($flesh);
1205 $type = 'atom' if ($type eq 'html');
1206 $type = 'marcxml' if ($type eq 'htmlholdings');
1208 #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1211 for my $record (@$records) {
1212 next unless($record);
1214 #my $rec = $record->id;
1217 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1218 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1220 my $xml = $supercat->request(
1221 "open-ils.supercat.$search.$type.retrieve",
1226 my $node = $feed->add_item($xml);
1230 if ($lib && $type eq 'marcxml' && $flesh) {
1231 my $r = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib );
1232 while ( !$r->complete ) {
1233 $xml .= join('', map {$_->content} $r->recv);
1235 $xml .= join('', map {$_->content} $r->recv);
1236 $node->add_holdings($xml);
1239 $node->id($item_tag) if ($flesh);
1240 #$node->update_ts(clense_ISO8601($record->edit_date));
1241 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1242 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1243 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1244 $node->link('unapi-id' => $item_tag) if ($flesh);
1251 my $stuff = NFC(shift());
1252 $stuff =~ s/&(?!\S+;)/&/gso;
1253 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1259 return Apache2::Const::DECLINED if (-e $apache->filename);
1262 my $year = (gmtime())[5] + 1900;
1264 my $host = $cgi->virtual_host || $cgi->server_name;
1267 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1268 my $rel_name = $cgi->url(-relative=>1);
1269 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1272 my $url = $cgi->url(-path_info=>$add_path);
1273 my $root = (split 'browse', $url)[0];
1274 my $base = (split 'browse', $url)[0] . 'browse';
1275 my $unapi = (split 'browse', $url)[0] . 'unapi';
1277 my $path = $cgi->path_info;
1280 my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1281 #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1283 $site ||= $cgi->param('searchOrg');
1284 $page ||= $cgi->param('startPage') || 0;
1285 $page_size ||= $cgi->param('count') || 9;
1287 $page = 0 if ($page !~ /^-?\d+$/);
1289 my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1290 my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1292 unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1293 warn "something's wrong...";
1294 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1298 $string = decode_utf8($string);
1299 $string =~ s/\+/ /go;
1302 my $tree = $supercat->request(
1303 "open-ils.supercat.$axis.browse",
1310 my ($header,$content) = $browse_types{$axis}{$format}->($tree,$prev,$next);
1311 print $header.$content;
1312 return Apache2::Const::OK;