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;
15 use OpenSRF::EX qw(:try);
16 use OpenSRF::Utils qw/:datetime/;
17 use OpenSRF::Utils::Cache;
19 use OpenSRF::AppSession;
24 use Unicode::Normalize;
25 use OpenILS::Utils::Fieldmapper;
26 use OpenILS::WWW::SuperCat::Feed;
27 use OpenSRF::Utils::Logger qw/$logger/;
32 # set the bootstrap config when this module is loaded
33 my ($bootstrap, $cstore, $supercat, $actor, $parser, $search, $xslt, $cn_browse_xslt, %browse_types);
35 $browse_types{call_number}{xml} = sub {
38 my $year = (gmtime())[5] + 1900;
41 $content .= "<hold:volumes xmlns:hold='http://open-ils.org/spec/holdings/v1'>";
44 (my $cn_class = $cn->class_name) =~ s/::/-/gso;
45 $cn_class =~ s/Fieldmapper-//gso;
47 my $cn_tag = "tag:open-ils.org,$year:$cn_class/".$cn->id;
48 my $cn_lib = $cn->owning_lib->shortname;
49 my $cn_label = $cn->label;
51 $cn_label =~ s/\n//gos;
52 $cn_label =~ s/'/'/go;
54 (my $ou_class = $cn->owning_lib->class_name) =~ s/::/-/gso;
55 $ou_class =~ s/Fieldmapper-//gso;
57 my $ou_tag = "tag:open-ils.org,$year:$ou_class/".$cn->owning_lib->id;
58 my $ou_name = $cn->owning_lib->name;
60 $ou_name =~ s/\n//gos;
61 $ou_name =~ s/'/'/go;
63 (my $rec_class = $cn->record->class_name) =~ s/::/-/gso;
64 $rec_class =~ s/Fieldmapper-//gso;
66 my $rec_tag = "tag:open-ils.org,$year:$rec_class/".$cn->record->id.'/'.$cn->owning_lib->shortname;
68 $content .= "<hold:volume id='$cn_tag' lib='$cn_lib' label='$cn_label'>";
69 $content .= "<act:owning_lib xmlns:act='http://open-ils.org/spec/actors/v1' id='$ou_tag' name='$ou_name'/>";
71 my $r_doc = $parser->parse_string($cn->record->marc);
72 $r_doc->documentElement->setAttribute( id => $rec_tag );
73 $content .= entityize($r_doc->documentElement->toString);
75 $content .= "</hold:volume>";
78 $content .= '</hold:volumes>';
79 return ("Content-type: application/xml\n\n",$content);
83 $browse_types{call_number}{html} = sub {
88 if (!$cn_browse_xslt) {
89 $cn_browse_xslt = $parser->parse_file(
90 OpenSRF::Utils::SettingsClient
92 ->config_value( dirs => 'xsl' ).
95 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
98 my (undef,$xml) = $browse_types{call_number}{xml}->($tree);
101 "Content-type: text/html\n\n",
103 $cn_browse_xslt->transform(
104 $parser->parse_string( $xml ),
119 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
121 my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
122 Fieldmapper->import(IDL => $idl);
124 $supercat = OpenSRF::AppSession->create('open-ils.supercat');
125 $cstore = OpenSRF::AppSession->create('open-ils.cstore');
126 $actor = OpenSRF::AppSession->create('open-ils.actor');
127 $search = OpenSRF::AppSession->create('open-ils.search');
128 $parser = new XML::LibXML;
129 $xslt = new XML::LibXSLT;
131 $cn_browse_xslt = $parser->parse_file(
132 OpenSRF::Utils::SettingsClient
134 ->config_value( dirs => 'xsl' ).
138 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
145 return Apache2::Const::DECLINED if (-e $apache->filename);
147 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
150 ->request("open-ils.supercat.oisbn", $isbn)
153 print "Content-type: application/xml; charset=utf-8\n\n";
154 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
156 unless (exists $$list{metarecord}) {
158 return Apache2::Const::OK;
161 print "<idlist metarecord='$$list{metarecord}'>\n";
163 for ( keys %{ $$list{record_list} } ) {
164 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
165 print " <isbn record='$_'>$o</isbn>\n"
170 return Apache2::Const::OK;
176 return Apache2::Const::DECLINED if (-e $apache->filename);
181 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
182 my $rel_name = $cgi->url(-relative=>1);
183 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
186 my $url = $cgi->url(-path_info=>$add_path);
187 my $root = (split 'unapi', $url)[0];
188 my $base = (split 'unapi', $url)[0] . 'unapi';
191 my $uri = $cgi->param('id') || '';
192 my $host = $cgi->virtual_host || $cgi->server_name;
194 my $format = $cgi->param('format');
195 my $flesh_feed = ($format =~ /-full$/o) ? 1 : 0;
196 (my $base_format = $format) =~ s/-full$//o;
197 my ($id,$type,$command,$lib) = ('','','');
200 my $body = "Content-type: application/xml; charset=utf-8\n\n";
202 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))$}o) {
206 $type = 'metarecord' if ($1 =~ /^m/o);
209 ->request("open-ils.supercat.$type.formats")
212 if ($type eq 'record' or $type eq 'isbn') {
213 $body .= <<" FORMATS";
215 <format name='opac' type='text/html'/>
216 <format name='html' type='text/html'/>
217 <format name='htmlholdings' type='text/html'/>
218 <format name='html-full' type='text/html'/>
219 <format name='htmlholdings-full' type='text/html'/>
221 } elsif ($type eq 'metarecord') {
222 $body .= <<" FORMATS";
224 <format name='opac' type='text/html'/>
229 my ($type) = keys %$h;
230 $body .= "\t<format name='$type' type='application/xml'";
232 for my $part ( qw/namespace_uri docs schema_location/ ) {
233 $body .= " $part='$$h{$type}{$part}'"
234 if ($$h{$type}{$part});
239 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
240 $body .= "\t<format name='$type-full' type='application/xml'";
242 for my $part ( qw/namespace_uri docs schema_location/ ) {
243 $body .= " $part='$$h{$type}{$part}'"
244 if ($$h{$type}{$part});
251 $body .= "</formats>\n";
255 ->request("open-ils.supercat.record.formats")
260 ->request("open-ils.supercat.metarecord.formats")
264 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
265 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
267 $body .= <<" FORMATS";
269 <format name='opac' type='text/html'/>
270 <format name='html' type='text/html'/>
271 <format name='htmlholdings' type='text/html'/>
272 <format name='html-full' type='text/html'/>
273 <format name='htmlholdings-full' type='text/html'/>
278 my ($type) = keys %$h;
279 $body .= "\t<format name='$type' type='application/xml'";
281 for my $part ( qw/namespace_uri docs schema_location/ ) {
282 $body .= " $part='$$h{$type}{$part}'"
283 if ($$h{$type}{$part});
288 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
289 $body .= "\t<format name='$type-full' type='application/xml'";
291 for my $part ( qw/namespace_uri docs schema_location/ ) {
292 $body .= " $part='$$h{$type}{$part}'"
293 if ($$h{$type}{$part});
300 $body .= "</formats>\n";
304 return Apache2::Const::OK;
307 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))?}o) {
311 $type = 'metarecord' if ($1 =~ /^metabib/o);
312 $type = 'isbn' if ($1 =~ /^isbn/o);
313 $type = 'call_number' if ($1 =~ /^call_number/o);
314 $command = 'retrieve';
315 $command = 'browse' if ($type eq 'call_number');
318 if (!$lib || $lib eq '-') {
319 $lib = $actor->request(
320 'open-ils.actor.org_unit_list.search' => parent_ou => undef
321 )->gather(1)->[0]->shortname;
324 my $lib_object = $actor->request(
325 'open-ils.actor.org_unit_list.search' => shortname => $lib
327 my $lib_id = $lib_object->id;
329 my $ou_types = $actor->request( 'open-ils.actor.org_types.retrieve' )->gather(1);
330 my $lib_depth = (grep { $_->id == $lib_object->ou_type } @$ou_types)[0]->depth;
332 if ($type eq 'call_number' and $command eq 'browse') {
333 print "Location: $root/browse/$base_format/call_number/$lib/$id\n\n";
337 if ($type eq 'isbn') {
338 my $rec = $supercat->request('open-ils.supercat.isbn.object.retrieve',$id)->gather(1);
340 print "Content-type: text/html; charset=utf-8\n\n";
341 $apache->custom_response( 404, <<" HTML");
344 <title>Type [$type] with id [$id] not found!</title>
348 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
359 { (keys(%$_))[0] eq $base_format }
360 @{ $supercat->request("open-ils.supercat.$type.formats")->gather(1) }
362 { $_ eq $base_format }
363 qw/opac html htmlholdings/
365 print "Content-type: text/html; charset=utf-8\n\n";
366 $apache->custom_response( 406, <<" HTML");
369 <title>Invalid format [$format] for type [$type]!</title>
373 <center>Sorry, format $format is not valid for type $type.</center>
380 if ($format eq 'opac') {
381 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id&l=$lib_id&d=$lib_depth\n\n"
382 if ($type eq 'metarecord');
383 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id&l=$lib_id&d=$lib_depth\n\n"
384 if ($type eq 'record');
386 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
387 my $feed = create_record_feed(
396 print "Content-type: text/html; charset=utf-8\n\n";
397 $apache->custom_response( 404, <<" HTML");
400 <title>Type [$type] with id [$id] not found!</title>
404 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
412 $feed->creator($host);
413 $feed->update_ts(gmtime_ISO8601());
414 $feed->link( unapi => $base) if ($flesh_feed);
416 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
417 print entityize($feed->toString) . "\n";
419 return Apache2::Const::OK;
422 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
423 my $data = $req->gather(1);
425 if ($req->failed || !$data) {
426 print "Content-type: text/html; charset=utf-8\n\n";
427 $apache->custom_response( 404, <<" HTML");
430 <title>$type $id not found!</title>
434 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
441 print "Content-type: application/xml; charset=utf-8\n\n$data";
443 return Apache2::Const::OK;
449 return Apache2::Const::DECLINED if (-e $apache->filename);
454 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
455 my $rel_name = $cgi->url(-relative=>1);
456 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
459 my $url = $cgi->url(-path_info=>$add_path);
460 my $root = (split 'supercat', $url)[0];
461 my $base = (split 'supercat', $url)[0] . 'supercat';
462 my $unapi = (split 'supercat', $url)[0] . 'unapi';
464 my $host = $cgi->virtual_host || $cgi->server_name;
466 my $path = $cgi->path_info;
467 my ($id,$type,$format,$command) = reverse split '/', $path;
468 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
469 (my $base_format = $format) =~ s/-full$//o;
471 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
472 print "Content-type: application/xml; charset=utf-8\n";
475 ->request("open-ils.supercat.$1.formats")
483 <type>text/html</type>
486 if ($1 eq 'record' or $1 eq 'isbn') {
488 <name>htmlholdings</name>
489 <type>text/html</type>
493 <type>text/html</type>
496 <name>htmlholdings-full</name>
497 <type>text/html</type>
500 <name>html-full</name>
501 <type>text/html</type>
506 my ($type) = keys %$h;
507 print "<format><name>$type</name><type>application/xml</type>";
509 for my $part ( qw/namespace_uri docs schema_location/ ) {
510 print "<$part>$$h{$type}{$part}</$part>"
511 if ($$h{$type}{$part});
516 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
517 print "<format><name>$type-full</name><type>application/xml</type>";
519 for my $part ( qw/namespace_uri docs schema_location/ ) {
520 print "<$part>$$h{$type}{$part}</$part>"
521 if ($$h{$type}{$part});
529 print "</formats>\n";
531 return Apache2::Const::OK;
535 ->request("open-ils.supercat.record.formats")
540 ->request("open-ils.supercat.metarecord.formats")
544 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
545 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
550 <type>text/html</type>
553 <name>htmlholdings</name>
554 <type>text/html</type>
558 <type>text/html</type>
561 <name>htmlholdings-full</name>
562 <type>text/html</type>
565 <name>html-full</name>
566 <type>text/html</type>
570 my ($type) = keys %$h;
571 print "<format><name>$type</name><type>application/xml</type>";
573 for my $part ( qw/namespace_uri docs schema_location/ ) {
574 print "<$part>$$h{$type}{$part}</$part>"
575 if ($$h{$type}{$part});
580 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
581 print "<format><name>$type-full</name><type>application/xml</type>";
583 for my $part ( qw/namespace_uri docs schema_location/ ) {
584 print "<$part>$$h{$type}{$part}</$part>"
585 if ($$h{$type}{$part});
593 print "</formats>\n";
596 return Apache2::Const::OK;
599 if ($format eq 'opac') {
600 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
601 if ($type eq 'metarecord');
602 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
603 if ($type eq 'record');
606 } elsif ($base_format eq 'marc21') {
610 my $bib = $supercat->request( "open-ils.supercat.record.object.retrieve", $id )->gather(1)->[0];
612 my $r = MARC::Record->new_from_xml( $bib->marc, 'UTF-8', 'USMARC' );
613 $r->delete_field( $_ ) for ($r->field(901));
618 a => $bib->tcn_value,
619 b => $bib->tcn_source,
624 print "Content-type: application/octet-stream\n\n";
630 print "Content-type: text/html; charset=utf-8\n\n";
631 $apache->custom_response( 404, <<" HTML");
638 <center>Couldn't fetch $id as MARC21.</center>
645 return Apache2::Const::OK;
647 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
648 my $feed = create_record_feed(
656 $feed->creator($host);
657 $feed->update_ts(gmtime_ISO8601());
658 $feed->link( unapi => $base) if ($flesh_feed);
660 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
661 print entityize($feed->toString) . "\n";
663 return Apache2::Const::OK;
666 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
670 print "Content-type: text/html; charset=utf-8\n\n";
671 $apache->custom_response( 404, <<" HTML");
674 <title>$type $id not found!</title>
678 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
685 print "Content-type: application/xml; charset=utf-8\n\n";
686 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
688 return Apache2::Const::OK;
694 return Apache2::Const::DECLINED if (-e $apache->filename);
698 my $year = (gmtime())[5] + 1900;
699 my $host = $cgi->virtual_host || $cgi->server_name;
702 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
703 my $rel_name = $cgi->url(-relative=>1);
704 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
707 my $url = $cgi->url(-path_info=>$add_path);
708 my $root = (split 'feed', $url)[0] . '/';
709 my $base = (split 'bookbag', $url)[0] . '/bookbag';
710 my $unapi = (split 'feed', $url)[0] . '/unapi';
712 $root =~ s{(?<!http:)//}{/}go;
713 $base =~ s{(?<!http:)//}{/}go;
714 $unapi =~ s{(?<!http:)//}{/}go;
716 my $path = $cgi->path_info;
717 #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
719 my ($id,$type) = reverse split '/', $path;
720 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
722 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
723 return Apache2::Const::NOT_FOUND unless($bucket);
725 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
726 if ($type eq 'opac') {
727 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
728 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
733 my $feed = create_record_feed(
736 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
743 $feed->title("Items in Book Bag [".$bucket->name."]");
744 $feed->creator($host);
745 $feed->update_ts(gmtime_ISO8601());
747 $feed->link(rss => $base . "/rss2-full/$id" => 'application/rss+xml');
748 $feed->link(alternate => $base . "/atom-full/$id" => 'application/atom+xml');
749 $feed->link(html => $base . "/html-full/$id" => 'text/html');
750 $feed->link(unapi => $unapi);
754 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
755 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
760 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
761 print entityize($feed->toString) . "\n";
763 return Apache2::Const::OK;
768 return Apache2::Const::DECLINED if (-e $apache->filename);
772 my $year = (gmtime())[5] + 1900;
773 my $host = $cgi->virtual_host || $cgi->server_name;
776 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
777 my $rel_name = $cgi->url(-relative=>1);
778 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
781 my $url = $cgi->url(-path_info=>$add_path);
782 my $root = (split 'feed', $url)[0];
783 my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
784 my $unapi = (split 'feed', $url)[0] . 'unapi';
786 my $path = $cgi->path_info;
787 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
789 $path =~ s/^\/(?:feed\/)?freshmeat\///og;
791 my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
792 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
795 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
797 #if ($type eq 'opac') {
798 # print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
799 # join('&', map { "rl=" . $_ } @$list) .
804 my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
808 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
810 $feed->title("$limit most recent $rtype ${axis}s");
813 $feed->creator($host);
814 $feed->update_ts(gmtime_ISO8601());
816 $feed->link(rss => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
817 $feed->link(alternate => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
818 $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
819 $feed->link(unapi => $unapi);
823 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
824 join('&', map { 'rl=' . $_} @$list ),
829 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
830 print entityize($feed->toString) . "\n";
832 return Apache2::Const::OK;
841 if ($version eq '1.0') {
843 Content-type: application/opensearchdescription+xml; charset=utf-8
845 <?xml version="1.0" encoding="UTF-8"?>
846 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
847 <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&startIndex={startIndex}&count={count}</Url>
848 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
849 <ShortName>$lib</ShortName>
850 <LongName>Search $lib</LongName>
851 <Description>Search the $lib OPAC by $class.</Description>
852 <Tags>$lib book library</Tags>
853 <SampleSearch>harry+potter</SampleSearch>
854 <Developer>Mike Rylander for GPLS/PINES</Developer>
855 <Contact>feedback\@open-ils.org</Contact>
856 <SyndicationRight>open</SyndicationRight>
857 <AdultContent>false</AdultContent>
858 </OpenSearchDescription>
862 Content-type: application/opensearchdescription+xml; charset=utf-8
864 <?xml version="1.0" encoding="UTF-8"?>
865 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
866 <ShortName>$lib</ShortName>
867 <Description>Search the $lib OPAC by $class.</Description>
868 <Tags>$lib book library</Tags>
869 <Url type="application/rss+xml"
870 template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
871 <Url type="application/atom+xml"
872 template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
873 <Url type="application/x-mods3+xml"
874 template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
875 <Url type="application/x-mods+xml"
876 template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
877 <Url type="application/x-marcxml+xml"
878 template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
879 <Url type="text/html"
880 template="$base/1.1/$lib/html-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
881 <LongName>Search $lib</LongName>
882 <Query role="example" searchTerms="harry+potter" />
883 <Developer>Mike Rylander for GPLS/PINES</Developer>
884 <Contact>feedback\@open-ils.org</Contact>
885 <SyndicationRight>open</SyndicationRight>
886 <AdultContent>false</AdultContent>
887 <Language>en-US</Language>
888 <OutputEncoding>UTF-8</OutputEncoding>
889 <InputEncoding>UTF-8</InputEncoding>
890 </OpenSearchDescription>
894 return Apache2::Const::OK;
897 sub opensearch_feed {
899 return Apache2::Const::DECLINED if (-e $apache->filename);
902 my $year = (gmtime())[5] + 1900;
904 my $host = $cgi->virtual_host || $cgi->server_name;
907 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
908 my $rel_name = $cgi->url(-relative=>1);
909 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
912 my $url = $cgi->url(-path_info=>$add_path);
913 my $root = (split 'opensearch', $url)[0];
914 my $base = (split 'opensearch', $url)[0] . 'opensearch';
915 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
917 my $path = $cgi->path_info;
918 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
920 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
926 if (!$lib || $lib eq '-') {
927 $lib = $actor->request(
928 'open-ils.actor.org_unit_list.search' => parent_ou => undef
929 )->gather(1)->[0]->shortname;
936 return opensearch_osd($version, $lib, $class, $base);
940 my $page = $cgi->param('startPage') || 1;
941 my $offset = $cgi->param('startIndex') || 1;
942 my $limit = $cgi->param('count') || 10;
944 $page = 1 if ($page !~ /^\d+$/);
945 $offset = 1 if ($offset !~ /^\d+$/);
946 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
949 $offset = ($page - 1) * $limit;
954 my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = ('','','','','','','','');
955 (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
957 $lang = $cgi->param('searchLang') if $cgi->param('searchLang');
958 $lang = '' if ($lang eq '*');
960 $sort = $cgi->param('searchSort') if $cgi->param('searchSort');
961 $sortdir = $cgi->param('searchSortDir') if $cgi->param('searchSortDir');
962 $terms .= " " . $cgi->param('searchTerms') if $cgi->param('searchTerms');
964 $class = $cgi->param('searchClass') if $cgi->param('searchClass');
967 $type = $cgi->param('responseType') if $cgi->param('responseType');
970 $org = $cgi->param('searchOrg') if $cgi->param('searchOrg');
974 my $kwt = $cgi->param('kw');
975 my $tit = $cgi->param('ti');
976 my $aut = $cgi->param('au');
977 my $sut = $cgi->param('su');
978 my $set = $cgi->param('se');
980 $terms .= " keyword: $kwt" if ($kwt);
981 $terms .= " title: $tit" if ($tit);
982 $terms .= " author: $aut" if ($aut);
983 $terms .= " subject: $sut" if ($sut);
984 $terms .= " series: $set" if ($set);
986 if ($version eq '1.0') {
988 } elsif ($type eq '-') {
991 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
993 $terms = decode_utf8($terms);
996 $terms =~ s/^\s+//go;
997 my $term_copy = $terms;
999 my $complex_terms = 0;
1000 if ($terms eq 'help') {
1001 print $cgi->header(-type => 'text/html');
1005 <title>just type something!</title>
1008 <p>You are in a maze of dark, twisty stacks, all alike.</p>
1012 return Apache2::Const::OK;
1017 while ($term_copy =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang):[^:]+)$//so) {
1018 my ($c,$t) = split ':' => $1;
1023 } elsif ($c eq 'sort') {
1024 ($sort = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1025 } elsif ($c eq 'dir') {
1026 ($sortdir = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1027 } elsif ($c eq 'lang') {
1028 ($lang = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
1030 $$searches{$c}{term} .= ' '.$t;
1031 $cache_key .= $c . $t;
1036 $lang = 'eng' if ($lang eq 'en-US');
1040 $class = 'keyword' if ($class eq '-');
1041 $$searches{$class}{term} .= " $term_copy";
1042 $cache_key .= $class . $term_copy;
1047 $org_unit = $actor->request(
1048 'open-ils.actor.org_unit_list.search' => parent_ou => undef
1051 $org_unit = $actor->request(
1052 'open-ils.actor.org_unit_list.search' => shortname => uc($org)
1056 { no warnings; $cache_key .= $org.$sort.$sortdir.$lang; }
1058 my $rs_name = $cgi->cookie('os_session');
1059 my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
1062 if (!($recs = $$cached_res{os_results}{$cache_key})) {
1063 $rs_name = $cgi->remote_host . '::' . rand(time);
1064 $recs = $search->request(
1065 'open-ils.search.biblio.multiclass' => {
1066 searches => $searches,
1067 org_unit => $org_unit->[0]->id,
1070 ($sort ? ( 'sort' => $sort ) : ()),
1071 ($sortdir ? ( 'sort_dir' => $sortdir ) : ($sort ? (sort_dir => 'asc') : (sort_dir => 'desc') )),
1072 ($lang ? ( 'language' => $lang ) : ()),
1076 $$cached_res{os_results}{$cache_key} = $recs;
1077 OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
1078 } catch Error with {
1079 warn "supercat unable to store IDs in memcache server\n";
1080 $logger->error("supercat unable to store IDs in memcache server");
1084 my $feed = create_record_feed(
1087 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
1094 $feed->search($terms);
1095 $feed->class($class);
1097 if ($complex_terms) {
1098 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1100 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
1103 $feed->creator($host);
1104 $feed->update_ts(gmtime_ISO8601());
1106 $feed->_create_node(
1107 $feed->{item_xpath},
1108 'http://a9.com/-/spec/opensearch/1.1/',
1113 $feed->_create_node(
1114 $feed->{item_xpath},
1115 'http://a9.com/-/spec/opensearch/1.1/',
1120 $feed->_create_node(
1121 $feed->{item_xpath},
1122 'http://a9.com/-/spec/opensearch/1.1/',
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'
1131 ) if ($offset + $limit < $recs->{count});
1135 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1136 'application/opensearch+xml'
1141 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1142 'application/opensearch+xml'
1147 $base . "/$version/$org/rss2-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1148 'application/rss+xml'
1153 $base . "/$version/$org/atom-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1154 'application/atom+xml'
1159 $base . "/$version/$org/html/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1165 $base . "/$version/$org/html-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1169 $feed->link( 'unapi-server' => $unapi);
1173 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1174 # join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1179 -type => $feed->type,
1180 -charset => 'UTF-8',
1181 -cookie => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
1184 print entityize($feed->toString) . "\n";
1186 return Apache2::Const::OK;
1189 sub create_record_feed {
1192 my $records = shift;
1195 my $lib = uc(shift()) || '-';
1197 $flesh = 1 if (!defined($flesh));
1200 my $base = $cgi->url;
1201 my $host = $cgi->virtual_host || $cgi->server_name;
1203 my $year = (gmtime())[5] + 1900;
1205 my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1207 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1208 $feed->base($base) if ($flesh);
1209 $feed->unapi($unapi) if ($flesh);
1211 $type = 'atom' if ($type eq 'html');
1212 $type = 'marcxml' if ($type eq 'htmlholdings');
1214 #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1217 for my $record (@$records) {
1218 next unless($record);
1220 #my $rec = $record->id;
1223 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1224 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1226 my $xml = $supercat->request(
1227 "open-ils.supercat.$search.$type.retrieve",
1232 my $node = $feed->add_item($xml);
1236 if ($lib && $type eq 'marcxml' && $flesh) {
1237 my $r = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib );
1238 while ( !$r->complete ) {
1239 $xml .= join('', map {$_->content} $r->recv);
1241 $xml .= join('', map {$_->content} $r->recv);
1242 $node->add_holdings($xml);
1245 $node->id($item_tag) if ($flesh);
1246 #$node->update_ts(clense_ISO8601($record->edit_date));
1247 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1248 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1249 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1250 $node->link('unapi-id' => $item_tag) if ($flesh);
1257 my $stuff = NFC(shift());
1258 $stuff =~ s/&(?!\S+;)/&/gso;
1259 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1265 return Apache2::Const::DECLINED if (-e $apache->filename);
1268 my $year = (gmtime())[5] + 1900;
1270 my $host = $cgi->virtual_host || $cgi->server_name;
1273 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1274 my $rel_name = $cgi->url(-relative=>1);
1275 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1278 my $url = $cgi->url(-path_info=>$add_path);
1279 my $root = (split 'browse', $url)[0];
1280 my $base = (split 'browse', $url)[0] . 'browse';
1281 my $unapi = (split 'browse', $url)[0] . 'unapi';
1283 my $path = $cgi->path_info;
1286 my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1287 #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1289 $site ||= $cgi->param('searchOrg');
1290 $page ||= $cgi->param('startPage') || 0;
1291 $page_size ||= $cgi->param('count') || 9;
1293 $page = 0 if ($page !~ /^-?\d+$/);
1295 my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1296 my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1298 unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1299 warn "something's wrong...";
1300 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1304 $string = decode_utf8($string);
1305 $string =~ s/\+/ /go;
1308 my $tree = $supercat->request(
1309 "open-ils.supercat.$axis.browse",
1316 my ($header,$content) = $browse_types{$axis}{$format}->($tree,$prev,$next);
1317 print $header.$content;
1318 return Apache2::Const::OK;
1324 my $req = SRU::Request->newFromCGI( $cgi );
1325 my $resp = SRU::Response->newFromRequest( $req );
1327 if ( $resp->type eq 'searchRetrieve' ) {
1328 my $cql_query = $req->query;
1329 my $search_string = $req->cql->toEvergreen;
1331 warn "SRU search string [$cql_query] converted to [$search_string]\n";
1333 my $recs = $search->request(
1334 'open-ils.search.biblio.multiclass.query' => {} => $search_string
1337 $recs = $supercat->request( 'open-ils.supercat.record.object.retrieve' => [ map { $_->[0] } @{$recs->{ids}} ] )->gather(1);
1340 SRU::Response::Record->new(
1341 recordSchema => 'info:srw/schema/1/marcxml-v1.1',
1342 recordData => $_->marc
1346 print $cgi->header( -type => 'application/xml' );
1347 print entityize($resp->asXML) . "\n";
1348 return Apache2::Const::OK;
1353 package CQL::BooleanNode;
1357 my $left = $self->left();
1358 my $right = $self->right();
1359 my $leftStr = $left->isa('CQL::TermNode') ? $left->toEvergreen()
1360 : '('.$left->toEvergreen().')';
1361 my $rightStr = $right->isa('CQL::TermNode') ? $right->toEvergreen()
1362 : '('.$right->toEvergreen().')';
1364 my $op = '||' if uc $self->op() eq 'OR';
1367 return "$leftStr $rightStr";
1370 package CQL::TermNode;
1372 our %qualifier_map = (
1375 'dc.title' => 'title',
1376 'bib.titleabbreviated' => 'title|abbreviated',
1377 'bib.titleuniform' => 'title|uniform',
1378 'bib.titletranslated' => 'title|translated',
1379 'bib.titlealternative' => 'title',
1380 'bib.titleseries' => 'series',
1382 # Author/Name class:
1383 'creator' => 'author',
1384 'dc.creator' => 'author',
1385 'dc.contributer' => 'author',
1386 'dc.publisher' => 'keyword',
1387 'bib.name' => 'author',
1388 'bib.namepersonal' => 'author|personal',
1389 'bib.namepersonalfamily'=> 'author|personal',
1390 'bib.namepersonalgiven' => 'author|personal',
1391 'bib.namecorporate' => 'author|corporate',
1392 'bib.nameconference' => 'author|converence',
1395 'dc.subject' => 'subject',
1396 'bib.subjectplace' => 'subject|geographic',
1397 'bib.subjecttitle' => 'keyword',
1398 'bib.subjectname' => 'subject|name',
1399 'bib.subjectoccupation' => 'keyword',
1402 'srw.serverchoice' => 'keyword',
1405 'dc.identifier' => 'keyword',
1408 'bib.dateissued' => undef,
1409 'bib.datecreated' => undef,
1410 'bib.datevalid' => undef,
1411 'bib.datemodified' => undef,
1412 'bib.datecopyright' => undef,
1418 'dc.format' => undef,
1421 'bib.genre' => undef,
1424 'bib.audience' => undef,
1427 'bib.originplace' => undef,
1430 'dc.language' => 'lang',
1433 'bib.edition' => undef,
1436 'bib.volume' => undef,
1437 'bib.issue' => undef,
1438 'bib.startpage' => undef,
1439 'bib.endpage' => undef,
1442 'bib.issuance' => undef,
1447 my $qualifier = maybeQuote( $self->getQualifier() );
1448 my $term = $self->getTerm();
1449 my $relation = $self->getRelation();
1454 if ( exists($qualifier_map{lc($qualifier)}) ) {
1455 $qualifier = $qualifier_map{lc($qualifier)} || 'kw';
1459 #my @modifiers = $relation->getModifiers();
1461 #foreach my $m ( @modifiers ) {
1462 # if( $m->[ 1 ] eq 'fuzzy' ) {
1467 my $base = $relation->getBase();
1468 if( $base eq '=' ) {
1471 croak( "Evergreen doesn't support relations other than '='" );
1474 return "$qualifier$base$term";