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";
625 print "Content-type: text/html; charset=utf-8\n\n";
626 $apache->custom_response( 404, <<" HTML");
633 <center>Couldn't fetch $id as MARC21.</center>
642 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
643 my $feed = create_record_feed(
651 $feed->creator($host);
652 $feed->update_ts(gmtime_ISO8601());
653 $feed->link( unapi => $base) if ($flesh_feed);
655 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
656 print entityize($feed->toString) . "\n";
658 return Apache2::Const::OK;
661 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
665 print "Content-type: text/html; charset=utf-8\n\n";
666 $apache->custom_response( 404, <<" HTML");
669 <title>$type $id not found!</title>
673 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
680 print "Content-type: application/xml; charset=utf-8\n\n";
681 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
683 return Apache2::Const::OK;
689 return Apache2::Const::DECLINED if (-e $apache->filename);
693 my $year = (gmtime())[5] + 1900;
694 my $host = $cgi->virtual_host || $cgi->server_name;
697 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
698 my $rel_name = $cgi->url(-relative=>1);
699 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
702 my $url = $cgi->url(-path_info=>$add_path);
703 my $root = (split 'feed', $url)[0] . '/';
704 my $base = (split 'bookbag', $url)[0] . '/bookbag';
705 my $unapi = (split 'feed', $url)[0] . '/unapi';
707 $root =~ s{(?<!http:)//}{/}go;
708 $base =~ s{(?<!http:)//}{/}go;
709 $unapi =~ s{(?<!http:)//}{/}go;
711 my $path = $cgi->path_info;
712 #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
714 my ($id,$type) = reverse split '/', $path;
715 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
717 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
718 return Apache2::Const::NOT_FOUND unless($bucket);
720 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
721 if ($type eq 'opac') {
722 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
723 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
728 my $feed = create_record_feed(
731 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
738 $feed->title("Items in Book Bag [".$bucket->name."]");
739 $feed->creator($host);
740 $feed->update_ts(gmtime_ISO8601());
742 $feed->link(rss => $base . "/rss2-full/$id" => 'application/rss+xml');
743 $feed->link(alternate => $base . "/atom-full/$id" => 'application/atom+xml');
744 $feed->link(html => $base . "/html-full/$id" => 'text/html');
745 $feed->link(unapi => $unapi);
749 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
750 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
755 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
756 print entityize($feed->toString) . "\n";
758 return Apache2::Const::OK;
763 return Apache2::Const::DECLINED if (-e $apache->filename);
767 my $year = (gmtime())[5] + 1900;
768 my $host = $cgi->virtual_host || $cgi->server_name;
771 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
772 my $rel_name = $cgi->url(-relative=>1);
773 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
776 my $url = $cgi->url(-path_info=>$add_path);
777 my $root = (split 'feed', $url)[0];
778 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
779 my $unapi = (split 'feed', $url)[0] . 'unapi';
781 my $path = $cgi->path_info;
782 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
784 $path =~ s/^\/(?:feed\/)?freshmeat\///og;
786 my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
787 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
790 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
792 #if ($type eq 'opac') {
793 # print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
794 # join('&', map { "rl=" . $_ } @$list) .
799 my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
803 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
805 $feed->title("$limit most recent $rtype ${axis}s");
808 $feed->creator($host);
809 $feed->update_ts(gmtime_ISO8601());
811 $feed->link(rss => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
812 $feed->link(alternate => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
813 $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
814 $feed->link(unapi => $unapi);
818 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
819 join('&', map { 'rl=' . $_} @$list ),
824 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
825 print entityize($feed->toString) . "\n";
827 return Apache2::Const::OK;
836 if ($version eq '1.0') {
838 Content-type: application/opensearchdescription+xml; charset=utf-8
840 <?xml version="1.0" encoding="UTF-8"?>
841 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
842 <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&startIndex={startIndex}&count={count}</Url>
843 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
844 <ShortName>$lib</ShortName>
845 <LongName>Search $lib</LongName>
846 <Description>Search the $lib OPAC by $class.</Description>
847 <Tags>$lib book library</Tags>
848 <SampleSearch>harry+potter</SampleSearch>
849 <Developer>Mike Rylander for GPLS/PINES</Developer>
850 <Contact>feedback\@open-ils.org</Contact>
851 <SyndicationRight>open</SyndicationRight>
852 <AdultContent>false</AdultContent>
853 </OpenSearchDescription>
857 Content-type: application/opensearchdescription+xml; charset=utf-8
859 <?xml version="1.0" encoding="UTF-8"?>
860 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
861 <ShortName>$lib</ShortName>
862 <Description>Search the $lib OPAC by $class.</Description>
863 <Tags>$lib book library</Tags>
864 <Url type="application/rss+xml"
865 template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
866 <Url type="application/atom+xml"
867 template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
868 <Url type="application/x-mods3+xml"
869 template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
870 <Url type="application/x-mods+xml"
871 template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
872 <Url type="application/x-marcxml+xml"
873 template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
874 <Url type="text/html"
875 template="$base/1.1/$lib/html-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
876 <LongName>Search $lib</LongName>
877 <Query role="example" searchTerms="harry+potter" />
878 <Developer>Mike Rylander for GPLS/PINES</Developer>
879 <Contact>feedback\@open-ils.org</Contact>
880 <SyndicationRight>open</SyndicationRight>
881 <AdultContent>false</AdultContent>
882 <Language>en-US</Language>
883 <OutputEncoding>UTF-8</OutputEncoding>
884 <InputEncoding>UTF-8</InputEncoding>
885 </OpenSearchDescription>
889 return Apache2::Const::OK;
892 sub opensearch_feed {
894 return Apache2::Const::DECLINED if (-e $apache->filename);
897 my $year = (gmtime())[5] + 1900;
899 my $host = $cgi->virtual_host || $cgi->server_name;
902 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
903 my $rel_name = $cgi->url(-relative=>1);
904 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
907 my $url = $cgi->url(-path_info=>$add_path);
908 my $root = (split 'opensearch', $url)[0];
909 my $base = (split 'opensearch', $url)[0] . 'opensearch';
910 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
912 my $path = $cgi->path_info;
913 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
915 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
921 if (!$lib || $lib eq '-') {
922 $lib = $actor->request(
923 'open-ils.actor.org_unit_list.search' => parent_ou => undef
924 )->gather(1)->[0]->shortname;
931 return opensearch_osd($version, $lib, $class, $base);
935 my $page = $cgi->param('startPage') || 1;
936 my $offset = $cgi->param('startIndex') || 1;
937 my $limit = $cgi->param('count') || 10;
939 $page = 1 if ($page !~ /^\d+$/);
940 $offset = 1 if ($offset !~ /^\d+$/);
941 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
944 $offset = ($page - 1) * $limit;
949 my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = ('','','','','','','','');
950 (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
952 $lang = $cgi->param('searchLang') if $cgi->param('searchLang');
953 $lang = '' if ($lang eq '*');
955 $sort = $cgi->param('searchSort') if $cgi->param('searchSort');
956 $sortdir = $cgi->param('searchSortDir') if $cgi->param('searchSortDir');
957 $terms .= " " . $cgi->param('searchTerms') if $cgi->param('searchTerms');
959 $class = $cgi->param('searchClass') if $cgi->param('searchClass');
962 $type = $cgi->param('responseType') if $cgi->param('responseType');
965 $org = $cgi->param('searchOrg') if $cgi->param('searchOrg');
969 my $kwt = $cgi->param('kw');
970 my $tit = $cgi->param('ti');
971 my $aut = $cgi->param('au');
972 my $sut = $cgi->param('su');
973 my $set = $cgi->param('se');
975 $terms .= " keyword: $kwt" if ($kwt);
976 $terms .= " title: $tit" if ($tit);
977 $terms .= " author: $aut" if ($aut);
978 $terms .= " subject: $sut" if ($sut);
979 $terms .= " series: $set" if ($set);
981 if ($version eq '1.0') {
983 } elsif ($type eq '-') {
986 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
988 $terms = decode_utf8($terms);
991 $terms =~ s/^\s+//go;
992 my $term_copy = $terms;
994 my $complex_terms = 0;
995 if ($terms eq 'help') {
996 print $cgi->header(-type => 'text/html');
1000 <title>just type something!</title>
1003 <p>You are in a maze of dark, twisty stacks, all alike.</p>
1007 return Apache2::Const::OK;
1012 while ($term_copy =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang):[^:]+)$//so) {
1013 my ($c,$t) = split ':' => $1;
1018 } elsif ($c eq 'sort') {
1019 ($sort = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1020 } elsif ($c eq 'dir') {
1021 ($sortdir = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1022 } elsif ($c eq 'lang') {
1023 ($lang = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1025 $$searches{$c}{term} .= ' '.$t;
1026 $cache_key .= $c . $t;
1031 $lang = 'eng' if ($lang eq 'en-US');
1035 $class = 'keyword' if ($class eq '-');
1036 $$searches{$class}{term} .= " $term_copy";
1037 $cache_key .= $class . $term_copy;
1042 $org_unit = $actor->request(
1043 'open-ils.actor.org_unit_list.search' => parent_ou => undef
1046 $org_unit = $actor->request(
1047 'open-ils.actor.org_unit_list.search' => shortname => uc($org)
1051 { no warnings; $cache_key .= $org.$sort.$sortdir.$lang; }
1053 my $rs_name = $cgi->cookie('os_session');
1054 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
1057 if (!($recs = $$cached_res{os_results}{$cache_key})) {
1058 $rs_name = $cgi->remote_host . '::' . rand(time);
1059 $recs = $search->request(
1060 'open-ils.search.biblio.multiclass' => {
1061 searches => $searches,
1062 org_unit => $org_unit->[0]->id,
1065 ($sort ? ( 'sort' => $sort ) : ()),
1066 ($sortdir ? ( 'sort_dir' => $sortdir ) : ($sort ? (sort_dir => 'asc') : (sort_dir => 'desc') )),
1067 ($lang ? ( 'language' => $lang ) : ()),
1071 $$cached_res{os_results}{$cache_key} = $recs;
1072 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
1073 } catch Error with {
1074 warn "supercat unable to store IDs in memcache server\n";
1075 $logger->error("supercat unable to store IDs in memcache server");
1079 my $feed = create_record_feed(
1082 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
1089 $feed->search($terms);
1090 $feed->class($class);
1092 if ($complex_terms) {
1093 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1095 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
1098 $feed->creator($host);
1099 $feed->update_ts(gmtime_ISO8601());
1101 $feed->_create_node(
1102 $feed->{item_xpath},
1103 'http://a9.com/-/spec/opensearch/1.1/',
1108 $feed->_create_node(
1109 $feed->{item_xpath},
1110 'http://a9.com/-/spec/opensearch/1.1/',
1115 $feed->_create_node(
1116 $feed->{item_xpath},
1117 'http://a9.com/-/spec/opensearch/1.1/',
1124 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
1125 'application/opensearch+xml'
1126 ) if ($offset + $limit < $recs->{count});
1130 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1131 'application/opensearch+xml'
1136 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1137 'application/opensearch+xml'
1142 $base . "/$version/$org/rss2-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1143 'application/rss+xml'
1148 $base . "/$version/$org/atom-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1149 'application/atom+xml'
1154 $base . "/$version/$org/html/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1160 $base . "/$version/$org/html-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1164 $feed->link( 'unapi-server' => $unapi);
1168 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1169 # join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1174 -type => $feed->type,
1175 -charset => 'UTF-8',
1176 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
1179 print entityize($feed->toString) . "\n";
1181 return Apache2::Const::OK;
1184 sub create_record_feed {
1187 my $records = shift;
1190 my $lib = uc(shift()) || '-';
1192 $flesh = 1 if (!defined($flesh));
1195 my $base = $cgi->url;
1196 my $host = $cgi->virtual_host || $cgi->server_name;
1198 my $year = (gmtime())[5] + 1900;
1200 my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1202 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1203 $feed->base($base) if ($flesh);
1204 $feed->unapi($unapi) if ($flesh);
1206 $type = 'atom' if ($type eq 'html');
1207 $type = 'marcxml' if ($type eq 'htmlholdings');
1209 #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1212 for my $record (@$records) {
1213 next unless($record);
1215 #my $rec = $record->id;
1218 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1219 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1221 my $xml = $supercat->request(
1222 "open-ils.supercat.$search.$type.retrieve",
1227 my $node = $feed->add_item($xml);
1231 if ($lib && $type eq 'marcxml' && $flesh) {
1232 my $r = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib );
1233 while ( !$r->complete ) {
1234 $xml .= join('', map {$_->content} $r->recv);
1236 $xml .= join('', map {$_->content} $r->recv);
1237 $node->add_holdings($xml);
1240 $node->id($item_tag) if ($flesh);
1241 #$node->update_ts(clense_ISO8601($record->edit_date));
1242 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1243 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1244 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1245 $node->link('unapi-id' => $item_tag) if ($flesh);
1252 my $stuff = NFC(shift());
1253 $stuff =~ s/&(?!\S+;)/&/gso;
1254 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1260 return Apache2::Const::DECLINED if (-e $apache->filename);
1263 my $year = (gmtime())[5] + 1900;
1265 my $host = $cgi->virtual_host || $cgi->server_name;
1268 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1269 my $rel_name = $cgi->url(-relative=>1);
1270 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1273 my $url = $cgi->url(-path_info=>$add_path);
1274 my $root = (split 'browse', $url)[0];
1275 my $base = (split 'browse', $url)[0] . 'browse';
1276 my $unapi = (split 'browse', $url)[0] . 'unapi';
1278 my $path = $cgi->path_info;
1281 my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1282 #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1284 $site ||= $cgi->param('searchOrg');
1285 $page ||= $cgi->param('startPage') || 0;
1286 $page_size ||= $cgi->param('count') || 9;
1288 $page = 0 if ($page !~ /^-?\d+$/);
1290 my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1291 my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1293 unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1294 warn "something's wrong...";
1295 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1299 $string = decode_utf8($string);
1300 $string =~ s/\+/ /go;
1303 my $tree = $supercat->request(
1304 "open-ils.supercat.$axis.browse",
1311 my ($header,$content) = $browse_types{$axis}{$format}->($tree,$prev,$next);
1312 print $header.$content;
1313 return Apache2::Const::OK;