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 my $log = 'OpenSRF::Utils::Logger';
34 # set the bootstrap config when this module is loaded
35 my ($bootstrap, $cstore, $supercat, $actor, $parser, $search, $xslt, $cn_browse_xslt, %browse_types);
37 $browse_types{call_number}{xml} = sub {
40 my $year = (gmtime())[5] + 1900;
43 $content .= "<hold:volumes xmlns:hold='http://open-ils.org/spec/holdings/v1'>";
46 (my $cn_class = $cn->class_name) =~ s/::/-/gso;
47 $cn_class =~ s/Fieldmapper-//gso;
49 my $cn_tag = "tag:open-ils.org,$year:$cn_class/".$cn->id;
50 my $cn_lib = $cn->owning_lib->shortname;
51 my $cn_label = $cn->label;
53 $cn_label =~ s/\n//gos;
54 $cn_label =~ s/'/'/go;
56 (my $ou_class = $cn->owning_lib->class_name) =~ s/::/-/gso;
57 $ou_class =~ s/Fieldmapper-//gso;
59 my $ou_tag = "tag:open-ils.org,$year:$ou_class/".$cn->owning_lib->id;
60 my $ou_name = $cn->owning_lib->name;
62 $ou_name =~ s/\n//gos;
63 $ou_name =~ s/'/'/go;
65 (my $rec_class = $cn->record->class_name) =~ s/::/-/gso;
66 $rec_class =~ s/Fieldmapper-//gso;
68 my $rec_tag = "tag:open-ils.org,$year:$rec_class/".$cn->record->id.'/'.$cn->owning_lib->shortname;
70 $content .= "<hold:volume id='$cn_tag' lib='$cn_lib' label='$cn_label'>";
71 $content .= "<act:owning_lib xmlns:act='http://open-ils.org/spec/actors/v1' id='$ou_tag' name='$ou_name'/>";
73 my $r_doc = $parser->parse_string($cn->record->marc);
74 $r_doc->documentElement->setAttribute( id => $rec_tag );
75 $content .= entityize($r_doc->documentElement->toString);
77 $content .= "</hold:volume>";
80 $content .= '</hold:volumes>';
81 return ("Content-type: application/xml\n\n",$content);
85 $browse_types{call_number}{html} = sub {
90 if (!$cn_browse_xslt) {
91 $cn_browse_xslt = $parser->parse_file(
92 OpenSRF::Utils::SettingsClient
94 ->config_value( dirs => 'xsl' ).
97 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
100 my (undef,$xml) = $browse_types{call_number}{xml}->($tree);
103 "Content-type: text/html\n\n",
105 $cn_browse_xslt->transform(
106 $parser->parse_string( $xml ),
121 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
123 my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
124 Fieldmapper->import(IDL => $idl);
126 $supercat = OpenSRF::AppSession->create('open-ils.supercat');
127 $cstore = OpenSRF::AppSession->create('open-ils.cstore');
128 $actor = OpenSRF::AppSession->create('open-ils.actor');
129 $search = OpenSRF::AppSession->create('open-ils.search');
130 $parser = new XML::LibXML;
131 $xslt = new XML::LibXSLT;
133 $cn_browse_xslt = $parser->parse_file(
134 OpenSRF::Utils::SettingsClient
136 ->config_value( dirs => 'xsl' ).
140 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
143 ->request("open-ils.supercat.record.formats")
146 $list = [ map { (keys %$_)[0] } @$list ];
147 push @$list, 'htmlholdings','html';
149 for my $browse_axis ( qw/title author subject topic series/ ) {
150 for my $record_browse_format ( @$list ) {
152 my $__f = $record_browse_format;
153 my $__a = $browse_axis;
155 $browse_types{$__a}{$__f} = sub {
156 my $record_list = shift;
159 my $real_format = shift || $__f;
164 my $feed = create_record_feed( 'record', $real_format, $record_list, $unapi, $site, $real_format =~ /-full$/o ? 1 : 0 );
165 $feed->root( "$base/../" );
167 $feed->link( next => $next => $feed->type );
168 $feed->link( previous => $prev => $feed->type );
171 "Content-type: ". $feed->type ."; charset=utf-8\n\n",
183 return Apache2::Const::DECLINED if (-e $apache->filename);
185 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
188 ->request("open-ils.supercat.oisbn", $isbn)
191 print "Content-type: application/xml; charset=utf-8\n\n";
192 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
194 unless (exists $$list{metarecord}) {
196 return Apache2::Const::OK;
199 print "<idlist metarecord='$$list{metarecord}'>\n";
201 for ( keys %{ $$list{record_list} } ) {
202 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
203 print " <isbn record='$_'>$o</isbn>\n"
208 return Apache2::Const::OK;
214 return Apache2::Const::DECLINED if (-e $apache->filename);
219 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
220 my $rel_name = $cgi->url(-relative=>1);
221 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
224 my $url = $cgi->url(-path_info=>$add_path);
225 my $root = (split 'unapi', $url)[0];
226 my $base = (split 'unapi', $url)[0] . 'unapi';
229 my $uri = $cgi->param('id') || '';
230 my $host = $cgi->virtual_host || $cgi->server_name;
232 my $format = $cgi->param('format');
233 my $flesh_feed = ($format =~ /-full$/o) ? 1 : 0;
234 (my $base_format = $format) =~ s/-full$//o;
235 my ($id,$type,$command,$lib) = ('','','');
238 my $body = "Content-type: application/xml; charset=utf-8\n\n";
240 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))$}o) {
244 $type = 'metarecord' if ($1 =~ /^m/o);
247 ->request("open-ils.supercat.$type.formats")
250 if ($type eq 'record' or $type eq 'isbn') {
251 $body .= <<" FORMATS";
253 <format name='opac' type='text/html'/>
254 <format name='html' type='text/html'/>
255 <format name='htmlholdings' type='text/html'/>
256 <format name='html-full' type='text/html'/>
257 <format name='htmlholdings-full' type='text/html'/>
259 } elsif ($type eq 'metarecord') {
260 $body .= <<" FORMATS";
262 <format name='opac' type='text/html'/>
267 my ($type) = keys %$h;
268 $body .= "\t<format name='$type' type='application/xml'";
270 for my $part ( qw/namespace_uri docs schema_location/ ) {
271 $body .= " $part='$$h{$type}{$part}'"
272 if ($$h{$type}{$part});
277 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
278 $body .= "\t<format name='$type-full' type='application/xml'";
280 for my $part ( qw/namespace_uri docs schema_location/ ) {
281 $body .= " $part='$$h{$type}{$part}'"
282 if ($$h{$type}{$part});
289 $body .= "</formats>\n";
293 ->request("open-ils.supercat.record.formats")
298 ->request("open-ils.supercat.metarecord.formats")
302 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
303 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
305 $body .= <<" FORMATS";
307 <format name='opac' type='text/html'/>
308 <format name='html' type='text/html'/>
309 <format name='htmlholdings' type='text/html'/>
310 <format name='html-full' type='text/html'/>
311 <format name='htmlholdings-full' type='text/html'/>
316 my ($type) = keys %$h;
317 $body .= "\t<format name='$type' type='application/xml'";
319 for my $part ( qw/namespace_uri docs schema_location/ ) {
320 $body .= " $part='$$h{$type}{$part}'"
321 if ($$h{$type}{$part});
326 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
327 $body .= "\t<format name='$type-full' type='application/xml'";
329 for my $part ( qw/namespace_uri docs schema_location/ ) {
330 $body .= " $part='$$h{$type}{$part}'"
331 if ($$h{$type}{$part});
338 $body .= "</formats>\n";
342 return Apache2::Const::OK;
345 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))?}o) {
349 $type = 'metarecord' if ($1 =~ /^metabib/o);
350 $type = 'isbn' if ($1 =~ /^isbn/o);
351 $type = 'call_number' if ($1 =~ /^call_number/o);
352 $command = 'retrieve';
353 $command = 'browse' if ($type eq 'call_number');
356 if (!$lib || $lib eq '-') {
357 $lib = $actor->request(
358 'open-ils.actor.org_unit_list.search' => parent_ou => undef
359 )->gather(1)->[0]->shortname;
362 my $lib_object = $actor->request(
363 'open-ils.actor.org_unit_list.search' => shortname => $lib
365 my $lib_id = $lib_object->id;
367 my $ou_types = $actor->request( 'open-ils.actor.org_types.retrieve' )->gather(1);
368 my $lib_depth = (grep { $_->id == $lib_object->ou_type } @$ou_types)[0]->depth;
370 if ($type eq 'call_number' and $command eq 'browse') {
371 print "Location: $root/browse/$base_format/call_number/$lib/$id\n\n";
375 if ($type eq 'isbn') {
376 my $rec = $supercat->request('open-ils.supercat.isbn.object.retrieve',$id)->gather(1);
378 print "Content-type: text/html; charset=utf-8\n\n";
379 $apache->custom_response( 404, <<" HTML");
382 <title>Type [$type] with id [$id] not found!</title>
386 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
397 { (keys(%$_))[0] eq $base_format }
398 @{ $supercat->request("open-ils.supercat.$type.formats")->gather(1) }
400 { $_ eq $base_format }
401 qw/opac html htmlholdings/
403 print "Content-type: text/html; charset=utf-8\n\n";
404 $apache->custom_response( 406, <<" HTML");
407 <title>Invalid format [$format] for type [$type]!</title>
411 <center>Sorry, format $format is not valid for type $type.</center>
418 if ($format eq 'opac') {
419 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id&l=$lib_id&d=$lib_depth\n\n"
420 if ($type eq 'metarecord');
421 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id&l=$lib_id&d=$lib_depth\n\n"
422 if ($type eq 'record');
424 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
425 my $feed = create_record_feed(
434 print "Content-type: text/html; charset=utf-8\n\n";
435 $apache->custom_response( 404, <<" HTML");
438 <title>Type [$type] with id [$id] not found!</title>
442 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
450 $feed->creator($host);
452 $feed->link( unapi => $base) if ($flesh_feed);
454 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
455 print entityize($feed->toString) . "\n";
457 return Apache2::Const::OK;
460 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
461 my $data = $req->gather(1);
463 if ($req->failed || !$data) {
464 print "Content-type: text/html; charset=utf-8\n\n";
465 $apache->custom_response( 404, <<" HTML");
468 <title>$type $id not found!</title>
472 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
479 print "Content-type: application/xml; charset=utf-8\n\n$data";
481 return Apache2::Const::OK;
487 return Apache2::Const::DECLINED if (-e $apache->filename);
492 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
493 my $rel_name = $cgi->url(-relative=>1);
494 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
497 my $url = $cgi->url(-path_info=>$add_path);
498 my $root = (split 'supercat', $url)[0];
499 my $base = (split 'supercat', $url)[0] . 'supercat';
500 my $unapi = (split 'supercat', $url)[0] . 'unapi';
502 my $host = $cgi->virtual_host || $cgi->server_name;
504 my $path = $cgi->path_info;
505 my ($id,$type,$format,$command) = reverse split '/', $path;
506 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
507 (my $base_format = $format) =~ s/-full$//o;
509 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
510 print "Content-type: application/xml; charset=utf-8\n";
513 ->request("open-ils.supercat.$1.formats")
521 <type>text/html</type>
524 if ($1 eq 'record' or $1 eq 'isbn') {
526 <name>htmlholdings</name>
527 <type>text/html</type>
531 <type>text/html</type>
534 <name>htmlholdings-full</name>
535 <type>text/html</type>
538 <name>html-full</name>
539 <type>text/html</type>
544 my ($type) = keys %$h;
545 print "<format><name>$type</name><type>application/xml</type>";
547 for my $part ( qw/namespace_uri docs schema_location/ ) {
548 print "<$part>$$h{$type}{$part}</$part>"
549 if ($$h{$type}{$part});
554 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
555 print "<format><name>$type-full</name><type>application/xml</type>";
557 for my $part ( qw/namespace_uri docs schema_location/ ) {
558 print "<$part>$$h{$type}{$part}</$part>"
559 if ($$h{$type}{$part});
567 print "</formats>\n";
569 return Apache2::Const::OK;
573 ->request("open-ils.supercat.record.formats")
578 ->request("open-ils.supercat.metarecord.formats")
582 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
583 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
588 <type>text/html</type>
591 <name>htmlholdings</name>
592 <type>text/html</type>
596 <type>text/html</type>
599 <name>htmlholdings-full</name>
600 <type>text/html</type>
603 <name>html-full</name>
604 <type>text/html</type>
608 my ($type) = keys %$h;
609 print "<format><name>$type</name><type>application/xml</type>";
611 for my $part ( qw/namespace_uri docs schema_location/ ) {
612 print "<$part>$$h{$type}{$part}</$part>"
613 if ($$h{$type}{$part});
618 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
619 print "<format><name>$type-full</name><type>application/xml</type>";
621 for my $part ( qw/namespace_uri docs schema_location/ ) {
622 print "<$part>$$h{$type}{$part}</$part>"
623 if ($$h{$type}{$part});
631 print "</formats>\n";
634 return Apache2::Const::OK;
637 if ($format eq 'opac') {
638 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
639 if ($type eq 'metarecord');
640 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
641 if ($type eq 'record');
644 } elsif ($base_format eq 'marc21') {
648 my $bib = $supercat->request( "open-ils.supercat.record.object.retrieve", $id )->gather(1)->[0];
650 my $r = MARC::Record->new_from_xml( $bib->marc, 'UTF-8', 'USMARC' );
651 $r->delete_field( $_ ) for ($r->field(901));
656 a => $bib->tcn_value,
657 b => $bib->tcn_source,
662 print "Content-type: application/octet-stream\n\n";
668 print "Content-type: text/html; charset=utf-8\n\n";
669 $apache->custom_response( 404, <<" HTML");
676 <center>Couldn't fetch $id as MARC21.</center>
683 return Apache2::Const::OK;
685 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
686 my $feed = create_record_feed(
694 $feed->creator($host);
698 $feed->link( unapi => $base) if ($flesh_feed);
700 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
701 print entityize($feed->toString) . "\n";
703 return Apache2::Const::OK;
706 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
710 print "Content-type: text/html; charset=utf-8\n\n";
711 $apache->custom_response( 404, <<" HTML");
714 <title>$type $id not found!</title>
718 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
725 print "Content-type: application/xml; charset=utf-8\n\n";
726 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
728 return Apache2::Const::OK;
734 return Apache2::Const::DECLINED if (-e $apache->filename);
738 my $year = (gmtime())[5] + 1900;
739 my $host = $cgi->virtual_host || $cgi->server_name;
742 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
743 my $rel_name = $cgi->url(-relative=>1);
744 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
747 my $url = $cgi->url(-path_info=>$add_path);
748 my $root = (split 'feed', $url)[0] . '/';
749 my $base = (split 'bookbag', $url)[0] . '/bookbag';
750 my $unapi = (split 'feed', $url)[0] . '/unapi';
752 $root =~ s{(?<!http:)//}{/}go;
753 $base =~ s{(?<!http:)//}{/}go;
754 $unapi =~ s{(?<!http:)//}{/}go;
756 my $path = $cgi->path_info;
757 #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
759 my ($id,$type) = reverse split '/', $path;
760 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
762 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
763 return Apache2::Const::NOT_FOUND unless($bucket);
765 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
766 if ($type eq 'opac') {
767 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
768 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
773 my $feed = create_record_feed(
776 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
782 $feed->id($bucket_tag);
784 $feed->title("Items in Book Bag [".$bucket->name."]");
785 $feed->creator($host);
788 $feed->link(alternate => $base . "/rss2-full/$id" => 'application/rss+xml');
789 $feed->link(atom => $base . "/atom-full/$id" => 'application/atom+xml');
790 $feed->link(html => $base . "/html-full/$id" => 'text/html');
791 $feed->link(unapi => $unapi);
795 $host . '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
796 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
801 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
802 print entityize($feed->toString) . "\n";
804 return Apache2::Const::OK;
809 return Apache2::Const::DECLINED if (-e $apache->filename);
813 my $year = (gmtime())[5] + 1900;
814 my $host = $cgi->virtual_host || $cgi->server_name;
817 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
818 my $rel_name = $cgi->url(-relative=>1);
819 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
822 my $url = $cgi->url(-path_info=>$add_path);
823 my $root = (split 'feed', $url)[0];
824 my $base = (split 'freshmeat', $url)[0] . '/freshmeat';
825 my $unapi = (split 'feed', $url)[0] . 'unapi';
827 my $path = $cgi->path_info;
828 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
830 $path =~ s/^\/(?:feed\/)?freshmeat\///og;
832 my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
833 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
836 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
838 #if ($type eq 'opac') {
839 # print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
840 # join('&', map { "rl=" . $_ } @$list) .
845 my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
849 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
851 $feed->title("$limit most recent $rtype ${axis}s");
854 $feed->creator($host);
857 $feed->link(alternate => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
858 $feed->link(atom => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
859 $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
860 $feed->link(unapi => $unapi);
864 $host . '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
865 join('&', map { 'rl=' . $_} @$list ),
870 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
871 print entityize($feed->toString) . "\n";
873 return Apache2::Const::OK;
882 if ($version eq '1.0') {
884 Content-type: application/opensearchdescription+xml; charset=utf-8
886 <?xml version="1.0" encoding="UTF-8"?>
887 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
888 <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&startIndex={startIndex}&count={count}</Url>
889 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
890 <ShortName>$lib</ShortName>
891 <LongName>Search $lib</LongName>
892 <Description>Search the $lib OPAC by $class.</Description>
893 <Tags>$lib book library</Tags>
894 <SampleSearch>harry+potter</SampleSearch>
895 <Developer>Mike Rylander for GPLS/PINES</Developer>
896 <Contact>feedback\@open-ils.org</Contact>
897 <SyndicationRight>open</SyndicationRight>
898 <AdultContent>false</AdultContent>
899 </OpenSearchDescription>
903 Content-type: application/opensearchdescription+xml; charset=utf-8
905 <?xml version="1.0" encoding="UTF-8"?>
906 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
907 <ShortName>$lib</ShortName>
908 <Description>Search the $lib OPAC by $class.</Description>
909 <Tags>$lib book library</Tags>
910 <Url type="application/rss+xml"
911 template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
912 <Url type="application/atom+xml"
913 template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
914 <Url type="application/x-mods3+xml"
915 template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
916 <Url type="application/x-mods+xml"
917 template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
918 <Url type="application/x-marcxml+xml"
919 template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
920 <Url type="text/html"
921 template="$base/1.1/$lib/html-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
922 <LongName>Search $lib</LongName>
923 <Query role="example" searchTerms="harry+potter" />
924 <Developer>Mike Rylander for GPLS/PINES</Developer>
925 <Contact>feedback\@open-ils.org</Contact>
926 <SyndicationRight>open</SyndicationRight>
927 <AdultContent>false</AdultContent>
928 <Language>en-US</Language>
929 <OutputEncoding>UTF-8</OutputEncoding>
930 <InputEncoding>UTF-8</InputEncoding>
931 </OpenSearchDescription>
935 return Apache2::Const::OK;
938 sub opensearch_feed {
940 return Apache2::Const::DECLINED if (-e $apache->filename);
943 my $year = (gmtime())[5] + 1900;
945 my $host = $cgi->virtual_host || $cgi->server_name;
948 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
949 my $rel_name = $cgi->url(-relative=>1);
950 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
953 my $url = $cgi->url(-path_info=>$add_path);
954 my $root = (split 'opensearch', $url)[0];
955 my $base = (split 'opensearch', $url)[0] . 'opensearch';
956 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
958 my $path = $cgi->path_info;
959 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
961 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
967 if (!$lib || $lib eq '-') {
968 $lib = $actor->request(
969 'open-ils.actor.org_unit_list.search' => parent_ou => undef
970 )->gather(1)->[0]->shortname;
977 return opensearch_osd($version, $lib, $class, $base);
981 my $page = $cgi->param('startPage') || 1;
982 my $offset = $cgi->param('startIndex') || 1;
983 my $limit = $cgi->param('count') || 10;
985 $page = 1 if ($page !~ /^\d+$/);
986 $offset = 1 if ($offset !~ /^\d+$/);
987 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
990 $offset = ($page - 1) * $limit;
995 my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = ('','','','','','','','');
996 (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
998 $lang = $cgi->param('searchLang') if $cgi->param('searchLang');
999 $lang = '' if ($lang eq '*');
1001 $sort = $cgi->param('searchSort') if $cgi->param('searchSort');
1003 $sortdir = $cgi->param('searchSortDir') if $cgi->param('searchSortDir');
1006 $terms .= " " if ($terms && $cgi->param('searchTerms'));
1007 $terms .= $cgi->param('searchTerms') if $cgi->param('searchTerms');
1009 $class = $cgi->param('searchClass') if $cgi->param('searchClass');
1012 $type = $cgi->param('responseType') if $cgi->param('responseType');
1015 $org = $cgi->param('searchOrg') if $cgi->param('searchOrg');
1019 my $kwt = $cgi->param('kw');
1020 my $tit = $cgi->param('ti');
1021 my $aut = $cgi->param('au');
1022 my $sut = $cgi->param('su');
1023 my $set = $cgi->param('se');
1025 $terms .= " " if ($terms && $kwt);
1026 $terms .= "keyword: $kwt" if ($kwt);
1027 $terms .= " " if ($terms && $tit);
1028 $terms .= "title: $tit" if ($tit);
1029 $terms .= " " if ($terms && $aut);
1030 $terms .= "author: $aut" if ($aut);
1031 $terms .= " " if ($terms && $sut);
1032 $terms .= "subject: $sut" if ($sut);
1033 $terms .= " " if ($terms && $set);
1034 $terms .= "series: $set" if ($set);
1036 if ($version eq '1.0') {
1038 } elsif ($type eq '-') {
1041 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
1043 if ($terms eq 'help') {
1044 print $cgi->header(-type => 'text/html');
1048 <title>just type something!</title>
1051 <p>You are in a maze of dark, twisty stacks, all alike.</p>
1055 return Apache2::Const::OK;
1058 $terms = decode_utf8($terms);
1059 $lang = 'eng' if ($lang eq 'en-US');
1061 $log->debug("OpenSearch terms: $terms");
1065 $org_unit = $actor->request(
1066 'open-ils.actor.org_unit_list.search' => parent_ou => undef
1068 } elsif ($org !~ /^\d+$/o) {
1069 $org_unit = $actor->request(
1070 'open-ils.actor.org_unit_list.search' => shortname => uc($org)
1073 $org_unit = $actor->request(
1074 'open-ils.actor.org_unit_list.search' => id => $org
1078 my $recs = $search->request(
1079 'open-ils.search.biblio.multiclass.query' => {
1080 org_unit => $org_unit->[0]->id,
1084 sort_dir => $sortdir,
1085 ($lang ? ( 'language' => $lang ) : ()),
1089 $log->debug("Hits for [$terms]: $recs->{count}");
1091 my $feed = create_record_feed(
1094 [ map { $_->[0] } @{$recs->{ids}} ],
1100 $log->debug("Feed created...");
1104 $feed->search($terms);
1105 $feed->class($class);
1107 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1109 $feed->creator($host);
1112 $feed->_create_node(
1113 $feed->{item_xpath},
1114 'http://a9.com/-/spec/opensearch/1.1/',
1119 $feed->_create_node(
1120 $feed->{item_xpath},
1121 'http://a9.com/-/spec/opensearch/1.1/',
1126 $feed->_create_node(
1127 $feed->{item_xpath},
1128 'http://a9.com/-/spec/opensearch/1.1/',
1133 $log->debug("...basic feed data added...");
1137 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
1138 'application/opensearch+xml'
1139 ) if ($offset + $limit < $recs->{count});
1143 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1144 'application/opensearch+xml'
1149 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1150 'application/opensearch+xml'
1155 $base . "/$version/$org/rss2-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1156 'application/rss+xml'
1161 $base . "/$version/$org/atom-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1162 'application/atom+xml'
1167 $base . "/$version/$org/html/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1173 $base . "/$version/$org/html-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1177 $feed->link( 'unapi-server' => $unapi);
1179 $log->debug("...feed links added...");
1183 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1184 # join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1188 #print $cgi->header( -type => $feed->type, -charset => 'UTF-8') . entityize($feed->toString) . "\n";
1189 print $cgi->header( -type => $feed->type, -charset => 'UTF-8') . $feed->toString . "\n";
1191 $log->debug("...and feed returned.");
1193 return Apache2::Const::OK;
1196 sub create_record_feed {
1199 my $records = shift;
1202 my $lib = uc(shift()) || '-';
1204 $flesh = 1 if (!defined($flesh));
1207 my $base = $cgi->url;
1208 my $host = $cgi->virtual_host || $cgi->server_name;
1210 my $year = (gmtime())[5] + 1900;
1212 my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1214 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1215 $feed->base($base) if ($flesh);
1216 $feed->unapi($unapi) if ($flesh);
1218 $type = 'atom' if ($type eq 'html');
1219 $type = 'marcxml' if ($type eq 'htmlholdings');
1221 #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1224 for my $record (@$records) {
1225 next unless($record);
1227 #my $rec = $record->id;
1230 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1231 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1233 my $xml = $supercat->request(
1234 "open-ils.supercat.$search.$type.retrieve",
1239 my $node = $feed->add_item($xml);
1243 if ($lib && $type eq 'marcxml' && $flesh) {
1244 my $r = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib );
1245 while ( !$r->complete ) {
1246 $xml .= join('', map {$_->content} $r->recv);
1248 $xml .= join('', map {$_->content} $r->recv);
1249 $node->add_holdings($xml);
1252 $node->id($item_tag);
1253 #$node->update_ts(clense_ISO8601($record->edit_date));
1254 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1255 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1256 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1257 $node->link('unapi-id' => $item_tag) if ($flesh);
1264 my $stuff = NFC(shift());
1265 $stuff =~ s/&(?!\S+;)/&/gso;
1266 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1272 return Apache2::Const::DECLINED if (-e $apache->filename);
1275 my $year = (gmtime())[5] + 1900;
1277 my $host = $cgi->virtual_host || $cgi->server_name;
1280 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1281 my $rel_name = $cgi->url(-relative=>1);
1282 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1285 my $url = $cgi->url(-path_info=>$add_path);
1286 my $root = (split 'browse', $url)[0];
1287 my $base = (split 'browse', $url)[0] . 'browse';
1288 my $unapi = (split 'browse', $url)[0] . 'unapi';
1290 my $path = $cgi->path_info;
1293 my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1294 #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1296 $site ||= $cgi->param('searchOrg');
1297 $page ||= $cgi->param('startPage') || 0;
1298 $page_size ||= $cgi->param('count') || 9;
1300 $page = 0 if ($page !~ /^-?\d+$/);
1302 my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1303 my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1305 unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1306 warn "something's wrong...";
1307 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1311 $string = decode_utf8($string);
1312 $string =~ s/\+/ /go;
1315 my $tree = $supercat->request(
1316 "open-ils.supercat.$axis.browse",
1323 (my $norm_format = $format) =~ s/-full$//o;
1325 my ($header,$content) = $browse_types{$axis}{$norm_format}->($tree,$prev,$next,$format,$unapi,$base,$site);
1326 print $header.$content;
1327 return Apache2::Const::OK;
1330 our %qualifier_map = (
1332 # Some EG qualifiers
1333 'eg.site' => 'site',
1334 'eg.sort' => 'sort',
1335 'eg.direction' => 'dir',
1336 'eg.available' => 'available',
1339 'eg.title' => 'title',
1340 'dc.title' => 'title',
1341 'bib.titleabbreviated' => 'title|abbreviated',
1342 'bib.titleuniform' => 'title|uniform',
1343 'bib.titletranslated' => 'title|translated',
1344 'bib.titlealternative' => 'title',
1345 'bib.titleseries' => 'series',
1346 'eg.series' => 'title',
1348 # Author/Name class:
1349 'eg.author' => 'author',
1350 'eg.name' => 'author',
1351 'creator' => 'author',
1352 'dc.creator' => 'author',
1353 'dc.contributer' => 'author',
1354 'dc.publisher' => 'keyword',
1355 'bib.name' => 'author',
1356 'bib.namepersonal' => 'author|personal',
1357 'bib.namepersonalfamily'=> 'author|personal',
1358 'bib.namepersonalgiven' => 'author|personal',
1359 'bib.namecorporate' => 'author|corporate',
1360 'bib.nameconference' => 'author|conference',
1363 'eg.subject' => 'subject',
1364 'dc.subject' => 'subject',
1365 'bib.subjectplace' => 'subject|geographic',
1366 'bib.subjecttitle' => 'keyword',
1367 'bib.subjectname' => 'subject|name',
1368 'bib.subjectoccupation' => 'keyword',
1371 'eg.keyword' => 'keyword',
1372 'srw.serverchoice' => 'keyword',
1375 'dc.identifier' => 'keyword',
1378 'bib.dateissued' => undef,
1379 'bib.datecreated' => undef,
1380 'bib.datevalid' => undef,
1381 'bib.datemodified' => undef,
1382 'bib.datecopyright' => undef,
1388 'dc.format' => undef,
1391 'bib.genre' => 'keyword',
1394 'bib.audience' => undef,
1397 'bib.originplace' => undef,
1400 'dc.language' => 'lang',
1403 'bib.edition' => 'keyword',
1406 'bib.volume' => 'keyword',
1407 'bib.issue' => 'keyword',
1408 'bib.startpage' => 'keyword',
1409 'bib.endpage' => 'keyword',
1412 'bib.issuance' => 'keyword',
1415 our %qualifier_ids = (
1416 eg => 'http://open-ils.org/spec/SRU/context-set/evergreen/v1',
1417 dc => 'info:srw/cql-context-set/1/dc-v1.1',
1418 bib => 'info:srw/cql-context-set/1/bib-v1.0',
1422 our %nested_qualifier_map = (
1424 site => ['site','Evergreen Site Code (shortname)'],
1425 sort => ['sort','Sort on relevance, title, author, pubdate, create_date or edit_date'],
1426 direction => ['dir','Sort direction (asc|desc)'],
1427 available => ['available','Filter to available (true|false)'],
1429 author => ['author'],
1431 subject => ['subject'],
1432 keyword => ['keyword'],
1433 series => ['series'],
1437 creator => ['author'],
1438 contributor => ['author'],
1439 publisher => ['keyword'],
1440 subject => ['subject'],
1441 identifier => ['keyword'],
1444 language => ['lang'],
1448 titleAbbreviated => ['title'],
1449 titleUniform => ['title'],
1450 titleTranslated => ['title'],
1451 titleAlternative => ['title'],
1452 titleSeries => ['series'],
1454 # Author/Name class:
1456 namePersonal => ['author'],
1457 namePersonalFamily => ['author'],
1458 namePersonalGiven => ['author'],
1459 nameCorporate => ['author'],
1460 nameConference => ['author'],
1463 subjectPlace => ['subject'],
1464 subjectTitle => ['keyword'],
1465 subjectName => ['subject|name'],
1466 subjectOccupation => ['keyword'],
1471 dateIssued => [undef],
1472 dateCreated => [undef],
1473 dateValid => [undef],
1474 dateModified => [undef],
1475 dateCopyright => [undef],
1478 genre => ['keyword'],
1481 audience => [undef],
1484 originPlace => [undef],
1487 edition => ['keyword'],
1490 volume => ['keyword'],
1491 issue => ['keyword'],
1492 startPage => ['keyword'],
1493 endPage => ['keyword'],
1496 issuance => ['keyword'],
1499 serverChoice => ['keyword'],
1504 my $base_explain = <<XML;
1506 id="evergreen-sru-explain-full"
1507 authoritative="true"
1508 xmlns:z="http://explain.z3950.org/dtd/2.0/"
1509 xmlns="http://explain.z3950.org/dtd/2.0/">
1510 <serverInfo transport="http" protocol="SRU" version="1.1">
1517 <title primary="true"/>
1518 <description primary="true"/>
1522 <set identifier="info:srw/cql-context-set/1/cql-v1.2" name="cql"/>
1527 identifier="info:srw/schema/1/marcxml-v1.1"
1528 location="http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1532 <title>MARC21Slim (marcxml)</title>
1537 <default type="numberOfRecords">10</default>
1538 <default type="contextSet">eg</default>
1539 <default type="index">keyword</default>
1540 <default type="relation">all</default>
1541 <default type="sortSchema">marcxml</default>
1542 <default type="retrieveSchema">marcxml</default>
1543 <setting type="maximumRecords">10</setting>
1544 <supports type="relationModifier">relevant</supports>
1545 <supports type="relationModifier">stem</supports>
1546 <supports type="relationModifier">fuzzy</supports>
1547 <supports type="relationModifier">word</supports>
1558 my $req = SRU::Request->newFromCGI( $cgi );
1559 my $resp = SRU::Response->newFromRequest( $req );
1561 if ( $resp->type eq 'searchRetrieve' ) {
1562 my $cql_query = $req->query;
1563 my $search_string = $req->cql->toEvergreen;
1565 my $offset = $req->startRecord;
1566 $offset-- if ($offset);
1569 my $limit = $req->maximumRecords;
1572 $log->info("SRU search string [$cql_query] converted to [$search_string]\n");
1574 my $recs = $search->request(
1575 'open-ils.search.biblio.multiclass.query' => {offset => $offset, limit => $limit} => $search_string => 1
1578 my $bre = $supercat->request( 'open-ils.supercat.record.object.retrieve' => [ map { $_->[0] } @{$recs->{ids}} ] )->gather(1);
1581 SRU::Response::Record->new(
1582 recordSchema => 'info:srw/schema/1/marcxml-v1.1',
1583 recordData => $_->marc
1587 $resp->numberOfRecords($recs->{count});
1589 } elsif ( $resp->type eq 'explain' ) {
1591 my $host = $cgi->virtual_host || $cgi->server_name;
1594 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1595 my $rel_name = $cgi->url(-relative=>1);
1596 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1598 my $base = $cgi->url(-base=>1);
1599 my $url = $cgi->url(-path_info=>$add_path);
1600 $url =~ s/^$base\///o;
1602 my $doc = $parser->parse_string($base_explain);
1603 my $e = $doc->documentElement;
1604 $e->findnodes('/z:explain/z:serverInfo/z:host')->shift->appendText( $host );
1605 $e->findnodes('/z:explain/z:serverInfo/z:port')->shift->appendText( $cgi->server_port );
1606 $e->findnodes('/z:explain/z:serverInfo/z:database')->shift->appendText( $url );
1608 for my $name ( keys %OpenILS::WWW::SuperCat::nested_qualifier_map ) {
1610 my $identifier = $OpenILS::WWW::SuperCat::qualifier_ids{ $name };
1612 next unless $identifier;
1614 my $set_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'set' );
1615 $set_node->setAttribute( identifier => $identifier );
1616 $set_node->setAttribute( name => $name );
1618 $e->findnodes('/z:explain/z:indexInfo')->shift->appendChild( $set_node );
1620 for my $index ( keys %{ $OpenILS::WWW::SuperCat::nested_qualifier_map{$name} } ) {
1621 my $desc = $OpenILS::WWW::SuperCat::nested_qualifier_map{$name}{$index}[1] || $index;
1623 my $name_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'name' );
1625 my $map_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'map' );
1626 $map_node->appendChild( $name_node );
1628 my $title_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'title' );
1630 my $index_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'index' );
1631 $index_node->appendChild( $title_node );
1632 $index_node->appendChild( $map_node );
1634 $index_node->setAttribute( id => $name . '.' . $index );
1635 $title_node->appendText( $desc );
1636 $name_node->setAttribute( set => $name );
1637 $name_node->appendText($index );
1639 $e->findnodes('/z:explain/z:indexInfo')->shift->appendChild( $index_node );
1643 $ex_doc = $e->toString;
1647 SRU::Response::Record->new(
1648 recordSchema => 'info:srw/cql-context-set/2/zeerex-1.1',
1649 recordData => $ex_doc
1654 print $cgi->header( -type => 'application/xml' );
1655 print entityize($resp->asXML) . "\n";
1656 return Apache2::Const::OK;
1661 package CQL::BooleanNode;
1665 my $left = $self->left();
1666 my $right = $self->right();
1667 my $leftStr = $left->toEvergreen;
1668 my $rightStr = $right->toEvergreen();
1670 my $op = '||' if uc $self->op() eq 'OR';
1673 return "$leftStr $rightStr";
1676 package CQL::TermNode;
1680 my $qualifier = $self->getQualifier();
1681 my $term = $self->getTerm();
1682 my $relation = $self->getRelation();
1686 my ($qset, $qname) = split(/\./, $qualifier);
1688 $log->debug("SRU toEvergreen: $qset, $qname $OpenILS::WWW::SuperCat::nested_qualifier_map{$qset}{$qname}[0]\n");
1690 if ( exists($OpenILS::WWW::SuperCat::nested_qualifier_map{$qset}{$qname}) ) {
1691 $qualifier = $OpenILS::WWW::SuperCat::nested_qualifier_map{$qset}{$qname}[0] || 'kw';
1694 my @modifiers = $relation->getModifiers();
1696 my $base = $relation->getBase();
1697 if ( grep { $base eq $_ } qw/= scr exact all/ ) {
1700 foreach my $m ( @modifiers ) {
1701 if( grep { $m->[ 1 ] eq $_ } qw/cql.fuzzy cql.stem cql.relevant cql.word/ ) {
1707 $quote_it = 0 if ( $base eq 'all' );
1708 $term = maybeQuote($term) if $quote_it;
1711 croak( "Evergreen doesn't support the $base relations" );
1719 return "$qualifier:$term";