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');
602 } elsif ($base_format eq 'marc21') {
606 my $bib = $supercat->request( "open-ils.supercat.record.object.retrieve", $id )->gather(1)->[0];
608 my $r = MARC::Record->new_from_xml( $bib->marc, 'UTF-8', 'USMARC' );
609 $r->delete_field( $_ ) for ($r->field(901));
614 a => $bib->tcn_value,
615 b => $bib->tcn_source,
620 print "Content-type: application/octet-stream\n\n";
626 print "Content-type: text/html; charset=utf-8\n\n";
627 $apache->custom_response( 404, <<" HTML");
634 <center>Couldn't fetch $id as MARC21.</center>
643 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
644 my $feed = create_record_feed(
652 $feed->creator($host);
653 $feed->update_ts(gmtime_ISO8601());
654 $feed->link( unapi => $base) if ($flesh_feed);
656 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
657 print entityize($feed->toString) . "\n";
659 return Apache2::Const::OK;
662 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
666 print "Content-type: text/html; charset=utf-8\n\n";
667 $apache->custom_response( 404, <<" HTML");
670 <title>$type $id not found!</title>
674 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
681 print "Content-type: application/xml; charset=utf-8\n\n";
682 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
684 return Apache2::Const::OK;
690 return Apache2::Const::DECLINED if (-e $apache->filename);
694 my $year = (gmtime())[5] + 1900;
695 my $host = $cgi->virtual_host || $cgi->server_name;
698 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
699 my $rel_name = $cgi->url(-relative=>1);
700 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
703 my $url = $cgi->url(-path_info=>$add_path);
704 my $root = (split 'feed', $url)[0] . '/';
705 my $base = (split 'bookbag', $url)[0] . '/bookbag';
706 my $unapi = (split 'feed', $url)[0] . '/unapi';
708 $root =~ s{(?<!http:)//}{/}go;
709 $base =~ s{(?<!http:)//}{/}go;
710 $unapi =~ s{(?<!http:)//}{/}go;
712 my $path = $cgi->path_info;
713 #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
715 my ($id,$type) = reverse split '/', $path;
716 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
718 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
719 return Apache2::Const::NOT_FOUND unless($bucket);
721 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
722 if ($type eq 'opac') {
723 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
724 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
729 my $feed = create_record_feed(
732 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
739 $feed->title("Items in Book Bag [".$bucket->name."]");
740 $feed->creator($host);
741 $feed->update_ts(gmtime_ISO8601());
743 $feed->link(rss => $base . "/rss2-full/$id" => 'application/rss+xml');
744 $feed->link(alternate => $base . "/atom-full/$id" => 'application/atom+xml');
745 $feed->link(html => $base . "/html-full/$id" => 'text/html');
746 $feed->link(unapi => $unapi);
750 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
751 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
756 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
757 print entityize($feed->toString) . "\n";
759 return Apache2::Const::OK;
764 return Apache2::Const::DECLINED if (-e $apache->filename);
768 my $year = (gmtime())[5] + 1900;
769 my $host = $cgi->virtual_host || $cgi->server_name;
772 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
773 my $rel_name = $cgi->url(-relative=>1);
774 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
777 my $url = $cgi->url(-path_info=>$add_path);
778 my $root = (split 'feed', $url)[0];
779 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
780 my $unapi = (split 'feed', $url)[0] . 'unapi';
782 my $path = $cgi->path_info;
783 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
785 $path =~ s/^\/(?:feed\/)?freshmeat\///og;
787 my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
788 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
791 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
793 #if ($type eq 'opac') {
794 # print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
795 # join('&', map { "rl=" . $_ } @$list) .
800 my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
804 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
806 $feed->title("$limit most recent $rtype ${axis}s");
809 $feed->creator($host);
810 $feed->update_ts(gmtime_ISO8601());
812 $feed->link(rss => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
813 $feed->link(alternate => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
814 $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
815 $feed->link(unapi => $unapi);
819 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
820 join('&', map { 'rl=' . $_} @$list ),
825 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
826 print entityize($feed->toString) . "\n";
828 return Apache2::Const::OK;
837 if ($version eq '1.0') {
839 Content-type: application/opensearchdescription+xml; charset=utf-8
841 <?xml version="1.0" encoding="UTF-8"?>
842 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
843 <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&startIndex={startIndex}&count={count}</Url>
844 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
845 <ShortName>$lib</ShortName>
846 <LongName>Search $lib</LongName>
847 <Description>Search the $lib OPAC by $class.</Description>
848 <Tags>$lib book library</Tags>
849 <SampleSearch>harry+potter</SampleSearch>
850 <Developer>Mike Rylander for GPLS/PINES</Developer>
851 <Contact>feedback\@open-ils.org</Contact>
852 <SyndicationRight>open</SyndicationRight>
853 <AdultContent>false</AdultContent>
854 </OpenSearchDescription>
858 Content-type: application/opensearchdescription+xml; charset=utf-8
860 <?xml version="1.0" encoding="UTF-8"?>
861 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
862 <ShortName>$lib</ShortName>
863 <Description>Search the $lib OPAC by $class.</Description>
864 <Tags>$lib book library</Tags>
865 <Url type="application/rss+xml"
866 template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
867 <Url type="application/atom+xml"
868 template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
869 <Url type="application/x-mods3+xml"
870 template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
871 <Url type="application/x-mods+xml"
872 template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
873 <Url type="application/x-marcxml+xml"
874 template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
875 <Url type="text/html"
876 template="$base/1.1/$lib/html-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
877 <LongName>Search $lib</LongName>
878 <Query role="example" searchTerms="harry+potter" />
879 <Developer>Mike Rylander for GPLS/PINES</Developer>
880 <Contact>feedback\@open-ils.org</Contact>
881 <SyndicationRight>open</SyndicationRight>
882 <AdultContent>false</AdultContent>
883 <Language>en-US</Language>
884 <OutputEncoding>UTF-8</OutputEncoding>
885 <InputEncoding>UTF-8</InputEncoding>
886 </OpenSearchDescription>
890 return Apache2::Const::OK;
893 sub opensearch_feed {
895 return Apache2::Const::DECLINED if (-e $apache->filename);
898 my $year = (gmtime())[5] + 1900;
900 my $host = $cgi->virtual_host || $cgi->server_name;
903 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
904 my $rel_name = $cgi->url(-relative=>1);
905 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
908 my $url = $cgi->url(-path_info=>$add_path);
909 my $root = (split 'opensearch', $url)[0];
910 my $base = (split 'opensearch', $url)[0] . 'opensearch';
911 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
913 my $path = $cgi->path_info;
914 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
916 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
922 if (!$lib || $lib eq '-') {
923 $lib = $actor->request(
924 'open-ils.actor.org_unit_list.search' => parent_ou => undef
925 )->gather(1)->[0]->shortname;
932 return opensearch_osd($version, $lib, $class, $base);
936 my $page = $cgi->param('startPage') || 1;
937 my $offset = $cgi->param('startIndex') || 1;
938 my $limit = $cgi->param('count') || 10;
940 $page = 1 if ($page !~ /^\d+$/);
941 $offset = 1 if ($offset !~ /^\d+$/);
942 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
945 $offset = ($page - 1) * $limit;
950 my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = ('','','','','','','','');
951 (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
953 $lang = $cgi->param('searchLang') if $cgi->param('searchLang');
954 $lang = '' if ($lang eq '*');
956 $sort = $cgi->param('searchSort') if $cgi->param('searchSort');
957 $sortdir = $cgi->param('searchSortDir') if $cgi->param('searchSortDir');
958 $terms .= " " . $cgi->param('searchTerms') if $cgi->param('searchTerms');
960 $class = $cgi->param('searchClass') if $cgi->param('searchClass');
963 $type = $cgi->param('responseType') if $cgi->param('responseType');
966 $org = $cgi->param('searchOrg') if $cgi->param('searchOrg');
970 my $kwt = $cgi->param('kw');
971 my $tit = $cgi->param('ti');
972 my $aut = $cgi->param('au');
973 my $sut = $cgi->param('su');
974 my $set = $cgi->param('se');
976 $terms .= " keyword: $kwt" if ($kwt);
977 $terms .= " title: $tit" if ($tit);
978 $terms .= " author: $aut" if ($aut);
979 $terms .= " subject: $sut" if ($sut);
980 $terms .= " series: $set" if ($set);
982 if ($version eq '1.0') {
984 } elsif ($type eq '-') {
987 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
989 $terms = decode_utf8($terms);
992 $terms =~ s/^\s+//go;
993 my $term_copy = $terms;
995 my $complex_terms = 0;
996 if ($terms eq 'help') {
997 print $cgi->header(-type => 'text/html');
1001 <title>just type something!</title>
1004 <p>You are in a maze of dark, twisty stacks, all alike.</p>
1008 return Apache2::Const::OK;
1013 while ($term_copy =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang):[^:]+)$//so) {
1014 my ($c,$t) = split ':' => $1;
1019 } elsif ($c eq 'sort') {
1020 ($sort = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1021 } elsif ($c eq 'dir') {
1022 ($sortdir = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1023 } elsif ($c eq 'lang') {
1024 ($lang = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1026 $$searches{$c}{term} .= ' '.$t;
1027 $cache_key .= $c . $t;
1032 $lang = 'eng' if ($lang eq 'en-US');
1036 $class = 'keyword' if ($class eq '-');
1037 $$searches{$class}{term} .= " $term_copy";
1038 $cache_key .= $class . $term_copy;
1043 $org_unit = $actor->request(
1044 'open-ils.actor.org_unit_list.search' => parent_ou => undef
1047 $org_unit = $actor->request(
1048 'open-ils.actor.org_unit_list.search' => shortname => uc($org)
1052 { no warnings; $cache_key .= $org.$sort.$sortdir.$lang; }
1054 my $rs_name = $cgi->cookie('os_session');
1055 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
1058 if (!($recs = $$cached_res{os_results}{$cache_key})) {
1059 $rs_name = $cgi->remote_host . '::' . rand(time);
1060 $recs = $search->request(
1061 'open-ils.search.biblio.multiclass' => {
1062 searches => $searches,
1063 org_unit => $org_unit->[0]->id,
1066 ($sort ? ( 'sort' => $sort ) : ()),
1067 ($sortdir ? ( 'sort_dir' => $sortdir ) : ($sort ? (sort_dir => 'asc') : (sort_dir => 'desc') )),
1068 ($lang ? ( 'language' => $lang ) : ()),
1072 $$cached_res{os_results}{$cache_key} = $recs;
1073 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
1074 } catch Error with {
1075 warn "supercat unable to store IDs in memcache server\n";
1076 $logger->error("supercat unable to store IDs in memcache server");
1080 my $feed = create_record_feed(
1083 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
1090 $feed->search($terms);
1091 $feed->class($class);
1093 if ($complex_terms) {
1094 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1096 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
1099 $feed->creator($host);
1100 $feed->update_ts(gmtime_ISO8601());
1102 $feed->_create_node(
1103 $feed->{item_xpath},
1104 'http://a9.com/-/spec/opensearch/1.1/',
1109 $feed->_create_node(
1110 $feed->{item_xpath},
1111 'http://a9.com/-/spec/opensearch/1.1/',
1116 $feed->_create_node(
1117 $feed->{item_xpath},
1118 'http://a9.com/-/spec/opensearch/1.1/',
1125 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
1126 'application/opensearch+xml'
1127 ) if ($offset + $limit < $recs->{count});
1131 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1132 'application/opensearch+xml'
1137 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1138 'application/opensearch+xml'
1143 $base . "/$version/$org/rss2-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1144 'application/rss+xml'
1149 $base . "/$version/$org/atom-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1150 'application/atom+xml'
1155 $base . "/$version/$org/html/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1161 $base . "/$version/$org/html-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1165 $feed->link( 'unapi-server' => $unapi);
1169 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1170 # join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1175 -type => $feed->type,
1176 -charset => 'UTF-8',
1177 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
1180 print entityize($feed->toString) . "\n";
1182 return Apache2::Const::OK;
1185 sub create_record_feed {
1188 my $records = shift;
1191 my $lib = uc(shift()) || '-';
1193 $flesh = 1 if (!defined($flesh));
1196 my $base = $cgi->url;
1197 my $host = $cgi->virtual_host || $cgi->server_name;
1199 my $year = (gmtime())[5] + 1900;
1201 my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1203 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1204 $feed->base($base) if ($flesh);
1205 $feed->unapi($unapi) if ($flesh);
1207 $type = 'atom' if ($type eq 'html');
1208 $type = 'marcxml' if ($type eq 'htmlholdings');
1210 #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1213 for my $record (@$records) {
1214 next unless($record);
1216 #my $rec = $record->id;
1219 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1220 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1222 my $xml = $supercat->request(
1223 "open-ils.supercat.$search.$type.retrieve",
1228 my $node = $feed->add_item($xml);
1232 if ($lib && $type eq 'marcxml' && $flesh) {
1233 my $r = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib );
1234 while ( !$r->complete ) {
1235 $xml .= join('', map {$_->content} $r->recv);
1237 $xml .= join('', map {$_->content} $r->recv);
1238 $node->add_holdings($xml);
1241 $node->id($item_tag) if ($flesh);
1242 #$node->update_ts(clense_ISO8601($record->edit_date));
1243 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1244 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1245 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1246 $node->link('unapi-id' => $item_tag) if ($flesh);
1253 my $stuff = NFC(shift());
1254 $stuff =~ s/&(?!\S+;)/&/gso;
1255 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1261 return Apache2::Const::DECLINED if (-e $apache->filename);
1264 my $year = (gmtime())[5] + 1900;
1266 my $host = $cgi->virtual_host || $cgi->server_name;
1269 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1270 my $rel_name = $cgi->url(-relative=>1);
1271 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1274 my $url = $cgi->url(-path_info=>$add_path);
1275 my $root = (split 'browse', $url)[0];
1276 my $base = (split 'browse', $url)[0] . 'browse';
1277 my $unapi = (split 'browse', $url)[0] . 'unapi';
1279 my $path = $cgi->path_info;
1282 my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1283 #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1285 $site ||= $cgi->param('searchOrg');
1286 $page ||= $cgi->param('startPage') || 0;
1287 $page_size ||= $cgi->param('count') || 9;
1289 $page = 0 if ($page !~ /^-?\d+$/);
1291 my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1292 my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1294 unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1295 warn "something's wrong...";
1296 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1300 $string = decode_utf8($string);
1301 $string =~ s/\+/ /go;
1304 my $tree = $supercat->request(
1305 "open-ils.supercat.$axis.browse",
1312 my ($header,$content) = $browse_types{$axis}{$format}->($tree,$prev,$next);
1313 print $header.$content;
1314 return Apache2::Const::OK;