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 );
147 return Apache2::Const::DECLINED if (-e $apache->filename);
149 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
152 ->request("open-ils.supercat.oisbn", $isbn)
155 print "Content-type: application/xml; charset=utf-8\n\n";
156 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
158 unless (exists $$list{metarecord}) {
160 return Apache2::Const::OK;
163 print "<idlist metarecord='$$list{metarecord}'>\n";
165 for ( keys %{ $$list{record_list} } ) {
166 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
167 print " <isbn record='$_'>$o</isbn>\n"
172 return Apache2::Const::OK;
178 return Apache2::Const::DECLINED if (-e $apache->filename);
183 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
184 my $rel_name = $cgi->url(-relative=>1);
185 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
188 my $url = $cgi->url(-path_info=>$add_path);
189 my $root = (split 'unapi', $url)[0];
190 my $base = (split 'unapi', $url)[0] . 'unapi';
193 my $uri = $cgi->param('id') || '';
194 my $host = $cgi->virtual_host || $cgi->server_name;
196 my $format = $cgi->param('format');
197 my $flesh_feed = ($format =~ /-full$/o) ? 1 : 0;
198 (my $base_format = $format) =~ s/-full$//o;
199 my ($id,$type,$command,$lib) = ('','','');
202 my $body = "Content-type: application/xml; charset=utf-8\n\n";
204 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))$}o) {
208 $type = 'metarecord' if ($1 =~ /^m/o);
211 ->request("open-ils.supercat.$type.formats")
214 if ($type eq 'record' or $type eq 'isbn') {
215 $body .= <<" FORMATS";
217 <format name='opac' type='text/html'/>
218 <format name='html' type='text/html'/>
219 <format name='htmlholdings' type='text/html'/>
220 <format name='html-full' type='text/html'/>
221 <format name='htmlholdings-full' type='text/html'/>
223 } elsif ($type eq 'metarecord') {
224 $body .= <<" FORMATS";
226 <format name='opac' type='text/html'/>
231 my ($type) = keys %$h;
232 $body .= "\t<format name='$type' type='application/xml'";
234 for my $part ( qw/namespace_uri docs schema_location/ ) {
235 $body .= " $part='$$h{$type}{$part}'"
236 if ($$h{$type}{$part});
241 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
242 $body .= "\t<format name='$type-full' type='application/xml'";
244 for my $part ( qw/namespace_uri docs schema_location/ ) {
245 $body .= " $part='$$h{$type}{$part}'"
246 if ($$h{$type}{$part});
253 $body .= "</formats>\n";
257 ->request("open-ils.supercat.record.formats")
262 ->request("open-ils.supercat.metarecord.formats")
266 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
267 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
269 $body .= <<" FORMATS";
271 <format name='opac' type='text/html'/>
272 <format name='html' type='text/html'/>
273 <format name='htmlholdings' type='text/html'/>
274 <format name='html-full' type='text/html'/>
275 <format name='htmlholdings-full' type='text/html'/>
280 my ($type) = keys %$h;
281 $body .= "\t<format name='$type' type='application/xml'";
283 for my $part ( qw/namespace_uri docs schema_location/ ) {
284 $body .= " $part='$$h{$type}{$part}'"
285 if ($$h{$type}{$part});
290 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
291 $body .= "\t<format name='$type-full' type='application/xml'";
293 for my $part ( qw/namespace_uri docs schema_location/ ) {
294 $body .= " $part='$$h{$type}{$part}'"
295 if ($$h{$type}{$part});
302 $body .= "</formats>\n";
306 return Apache2::Const::OK;
309 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))?}o) {
313 $type = 'metarecord' if ($1 =~ /^metabib/o);
314 $type = 'isbn' if ($1 =~ /^isbn/o);
315 $type = 'call_number' if ($1 =~ /^call_number/o);
316 $command = 'retrieve';
317 $command = 'browse' if ($type eq 'call_number');
320 if (!$lib || $lib eq '-') {
321 $lib = $actor->request(
322 'open-ils.actor.org_unit_list.search' => parent_ou => undef
323 )->gather(1)->[0]->shortname;
326 my $lib_object = $actor->request(
327 'open-ils.actor.org_unit_list.search' => shortname => $lib
329 my $lib_id = $lib_object->id;
331 my $ou_types = $actor->request( 'open-ils.actor.org_types.retrieve' )->gather(1);
332 my $lib_depth = (grep { $_->id == $lib_object->ou_type } @$ou_types)[0]->depth;
334 if ($type eq 'call_number' and $command eq 'browse') {
335 print "Location: $root/browse/$base_format/call_number/$lib/$id\n\n";
339 if ($type eq 'isbn') {
340 my $rec = $supercat->request('open-ils.supercat.isbn.object.retrieve',$id)->gather(1);
342 print "Content-type: text/html; charset=utf-8\n\n";
343 $apache->custom_response( 404, <<" HTML");
346 <title>Type [$type] with id [$id] not found!</title>
350 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
361 { (keys(%$_))[0] eq $base_format }
362 @{ $supercat->request("open-ils.supercat.$type.formats")->gather(1) }
364 { $_ eq $base_format }
365 qw/opac html htmlholdings/
367 print "Content-type: text/html; charset=utf-8\n\n";
368 $apache->custom_response( 406, <<" HTML");
371 <title>Invalid format [$format] for type [$type]!</title>
375 <center>Sorry, format $format is not valid for type $type.</center>
382 if ($format eq 'opac') {
383 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id&l=$lib_id&d=$lib_depth\n\n"
384 if ($type eq 'metarecord');
385 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id&l=$lib_id&d=$lib_depth\n\n"
386 if ($type eq 'record');
388 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
389 my $feed = create_record_feed(
398 print "Content-type: text/html; charset=utf-8\n\n";
399 $apache->custom_response( 404, <<" HTML");
402 <title>Type [$type] with id [$id] not found!</title>
406 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
414 $feed->creator($host);
415 $feed->update_ts(gmtime_ISO8601());
416 $feed->link( unapi => $base) if ($flesh_feed);
418 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
419 print entityize($feed->toString) . "\n";
421 return Apache2::Const::OK;
424 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
425 my $data = $req->gather(1);
427 if ($req->failed || !$data) {
428 print "Content-type: text/html; charset=utf-8\n\n";
429 $apache->custom_response( 404, <<" HTML");
432 <title>$type $id not found!</title>
436 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
443 print "Content-type: application/xml; charset=utf-8\n\n$data";
445 return Apache2::Const::OK;
451 return Apache2::Const::DECLINED if (-e $apache->filename);
456 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
457 my $rel_name = $cgi->url(-relative=>1);
458 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
461 my $url = $cgi->url(-path_info=>$add_path);
462 my $root = (split 'supercat', $url)[0];
463 my $base = (split 'supercat', $url)[0] . 'supercat';
464 my $unapi = (split 'supercat', $url)[0] . 'unapi';
466 my $host = $cgi->virtual_host || $cgi->server_name;
468 my $path = $cgi->path_info;
469 my ($id,$type,$format,$command) = reverse split '/', $path;
470 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
471 (my $base_format = $format) =~ s/-full$//o;
473 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
474 print "Content-type: application/xml; charset=utf-8\n";
477 ->request("open-ils.supercat.$1.formats")
485 <type>text/html</type>
488 if ($1 eq 'record' or $1 eq 'isbn') {
490 <name>htmlholdings</name>
491 <type>text/html</type>
495 <type>text/html</type>
498 <name>htmlholdings-full</name>
499 <type>text/html</type>
502 <name>html-full</name>
503 <type>text/html</type>
508 my ($type) = keys %$h;
509 print "<format><name>$type</name><type>application/xml</type>";
511 for my $part ( qw/namespace_uri docs schema_location/ ) {
512 print "<$part>$$h{$type}{$part}</$part>"
513 if ($$h{$type}{$part});
518 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
519 print "<format><name>$type-full</name><type>application/xml</type>";
521 for my $part ( qw/namespace_uri docs schema_location/ ) {
522 print "<$part>$$h{$type}{$part}</$part>"
523 if ($$h{$type}{$part});
531 print "</formats>\n";
533 return Apache2::Const::OK;
537 ->request("open-ils.supercat.record.formats")
542 ->request("open-ils.supercat.metarecord.formats")
546 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
547 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
552 <type>text/html</type>
555 <name>htmlholdings</name>
556 <type>text/html</type>
560 <type>text/html</type>
563 <name>htmlholdings-full</name>
564 <type>text/html</type>
567 <name>html-full</name>
568 <type>text/html</type>
572 my ($type) = keys %$h;
573 print "<format><name>$type</name><type>application/xml</type>";
575 for my $part ( qw/namespace_uri docs schema_location/ ) {
576 print "<$part>$$h{$type}{$part}</$part>"
577 if ($$h{$type}{$part});
582 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
583 print "<format><name>$type-full</name><type>application/xml</type>";
585 for my $part ( qw/namespace_uri docs schema_location/ ) {
586 print "<$part>$$h{$type}{$part}</$part>"
587 if ($$h{$type}{$part});
595 print "</formats>\n";
598 return Apache2::Const::OK;
601 if ($format eq 'opac') {
602 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
603 if ($type eq 'metarecord');
604 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
605 if ($type eq 'record');
608 } elsif ($base_format eq 'marc21') {
612 my $bib = $supercat->request( "open-ils.supercat.record.object.retrieve", $id )->gather(1)->[0];
614 my $r = MARC::Record->new_from_xml( $bib->marc, 'UTF-8', 'USMARC' );
615 $r->delete_field( $_ ) for ($r->field(901));
620 a => $bib->tcn_value,
621 b => $bib->tcn_source,
626 print "Content-type: application/octet-stream\n\n";
632 print "Content-type: text/html; charset=utf-8\n\n";
633 $apache->custom_response( 404, <<" HTML");
640 <center>Couldn't fetch $id as MARC21.</center>
647 return Apache2::Const::OK;
649 } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
650 my $feed = create_record_feed(
658 $feed->creator($host);
659 $feed->update_ts(gmtime_ISO8601());
660 $feed->link( unapi => $base) if ($flesh_feed);
662 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
663 print entityize($feed->toString) . "\n";
665 return Apache2::Const::OK;
668 my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
672 print "Content-type: text/html; charset=utf-8\n\n";
673 $apache->custom_response( 404, <<" HTML");
676 <title>$type $id not found!</title>
680 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
687 print "Content-type: application/xml; charset=utf-8\n\n";
688 print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
690 return Apache2::Const::OK;
696 return Apache2::Const::DECLINED if (-e $apache->filename);
700 my $year = (gmtime())[5] + 1900;
701 my $host = $cgi->virtual_host || $cgi->server_name;
704 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
705 my $rel_name = $cgi->url(-relative=>1);
706 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
709 my $url = $cgi->url(-path_info=>$add_path);
710 my $root = (split 'feed', $url)[0] . '/';
711 my $base = (split 'bookbag', $url)[0] . '/bookbag';
712 my $unapi = (split 'feed', $url)[0] . '/unapi';
714 $root =~ s{(?<!http:)//}{/}go;
715 $base =~ s{(?<!http:)//}{/}go;
716 $unapi =~ s{(?<!http:)//}{/}go;
718 my $path = $cgi->path_info;
719 #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
721 my ($id,$type) = reverse split '/', $path;
722 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
724 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
725 return Apache2::Const::NOT_FOUND unless($bucket);
727 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
728 if ($type eq 'opac') {
729 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
730 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
735 my $feed = create_record_feed(
738 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
744 $feed->id($bucket_tag);
746 $feed->title("Items in Book Bag [".$bucket->name."]");
747 $feed->creator($host);
748 $feed->update_ts(gmtime_ISO8601());
750 $feed->link(alternate => $base . "/rss2-full/$id" => 'application/rss+xml');
751 $feed->link(atom => $base . "/atom-full/$id" => 'application/atom+xml');
752 $feed->link(html => $base . "/html-full/$id" => 'text/html');
753 $feed->link(unapi => $unapi);
757 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
758 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
763 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
764 print entityize($feed->toString) . "\n";
766 return Apache2::Const::OK;
771 return Apache2::Const::DECLINED if (-e $apache->filename);
775 my $year = (gmtime())[5] + 1900;
776 my $host = $cgi->virtual_host || $cgi->server_name;
779 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
780 my $rel_name = $cgi->url(-relative=>1);
781 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
784 my $url = $cgi->url(-path_info=>$add_path);
785 my $root = (split 'feed', $url)[0];
786 my $base = (split 'freshmeat', $url)[0] . '/freshmeat';
787 my $unapi = (split 'feed', $url)[0] . 'unapi';
789 my $path = $cgi->path_info;
790 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
792 $path =~ s/^\/(?:feed\/)?freshmeat\///og;
794 my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
795 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
798 my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
800 #if ($type eq 'opac') {
801 # print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
802 # join('&', map { "rl=" . $_ } @$list) .
807 my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
811 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
813 $feed->title("$limit most recent $rtype ${axis}s");
816 $feed->creator($host);
817 $feed->update_ts(gmtime_ISO8601());
819 $feed->link(alternate => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
820 $feed->link(atom => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
821 $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
822 $feed->link(unapi => $unapi);
826 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
827 join('&', map { 'rl=' . $_} @$list ),
832 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
833 print entityize($feed->toString) . "\n";
835 return Apache2::Const::OK;
844 if ($version eq '1.0') {
846 Content-type: application/opensearchdescription+xml; charset=utf-8
848 <?xml version="1.0" encoding="UTF-8"?>
849 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
850 <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&startIndex={startIndex}&count={count}</Url>
851 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
852 <ShortName>$lib</ShortName>
853 <LongName>Search $lib</LongName>
854 <Description>Search the $lib OPAC by $class.</Description>
855 <Tags>$lib book library</Tags>
856 <SampleSearch>harry+potter</SampleSearch>
857 <Developer>Mike Rylander for GPLS/PINES</Developer>
858 <Contact>feedback\@open-ils.org</Contact>
859 <SyndicationRight>open</SyndicationRight>
860 <AdultContent>false</AdultContent>
861 </OpenSearchDescription>
865 Content-type: application/opensearchdescription+xml; charset=utf-8
867 <?xml version="1.0" encoding="UTF-8"?>
868 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
869 <ShortName>$lib</ShortName>
870 <Description>Search the $lib OPAC by $class.</Description>
871 <Tags>$lib book library</Tags>
872 <Url type="application/rss+xml"
873 template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
874 <Url type="application/atom+xml"
875 template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
876 <Url type="application/x-mods3+xml"
877 template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
878 <Url type="application/x-mods+xml"
879 template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
880 <Url type="application/x-marcxml+xml"
881 template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
882 <Url type="text/html"
883 template="$base/1.1/$lib/html-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&startIndex={startIndex?}&count={count?}&searchLang={language?}"/>
884 <LongName>Search $lib</LongName>
885 <Query role="example" searchTerms="harry+potter" />
886 <Developer>Mike Rylander for GPLS/PINES</Developer>
887 <Contact>feedback\@open-ils.org</Contact>
888 <SyndicationRight>open</SyndicationRight>
889 <AdultContent>false</AdultContent>
890 <Language>en-US</Language>
891 <OutputEncoding>UTF-8</OutputEncoding>
892 <InputEncoding>UTF-8</InputEncoding>
893 </OpenSearchDescription>
897 return Apache2::Const::OK;
900 sub opensearch_feed {
902 return Apache2::Const::DECLINED if (-e $apache->filename);
905 my $year = (gmtime())[5] + 1900;
907 my $host = $cgi->virtual_host || $cgi->server_name;
910 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
911 my $rel_name = $cgi->url(-relative=>1);
912 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
915 my $url = $cgi->url(-path_info=>$add_path);
916 my $root = (split 'opensearch', $url)[0];
917 my $base = (split 'opensearch', $url)[0] . 'opensearch';
918 my $unapi = (split 'opensearch', $url)[0] . 'unapi';
920 my $path = $cgi->path_info;
921 #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
923 if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
929 if (!$lib || $lib eq '-') {
930 $lib = $actor->request(
931 'open-ils.actor.org_unit_list.search' => parent_ou => undef
932 )->gather(1)->[0]->shortname;
939 return opensearch_osd($version, $lib, $class, $base);
943 my $page = $cgi->param('startPage') || 1;
944 my $offset = $cgi->param('startIndex') || 1;
945 my $limit = $cgi->param('count') || 10;
947 $page = 1 if ($page !~ /^\d+$/);
948 $offset = 1 if ($offset !~ /^\d+$/);
949 $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
952 $offset = ($page - 1) * $limit;
957 my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = ('','','','','','','','');
958 (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
960 $lang = $cgi->param('searchLang') if $cgi->param('searchLang');
961 $lang = '' if ($lang eq '*');
963 $sort = $cgi->param('searchSort') if $cgi->param('searchSort');
965 $sortdir = $cgi->param('searchSortDir') if $cgi->param('searchSortDir');
968 $terms .= " " if ($terms && $cgi->param('searchTerms'));
969 $terms .= $cgi->param('searchTerms') if $cgi->param('searchTerms');
971 $class = $cgi->param('searchClass') if $cgi->param('searchClass');
974 $type = $cgi->param('responseType') if $cgi->param('responseType');
977 $org = $cgi->param('searchOrg') if $cgi->param('searchOrg');
981 my $kwt = $cgi->param('kw');
982 my $tit = $cgi->param('ti');
983 my $aut = $cgi->param('au');
984 my $sut = $cgi->param('su');
985 my $set = $cgi->param('se');
987 $terms .= " " if ($terms && $kwt);
988 $terms .= "keyword: $kwt" if ($kwt);
989 $terms .= " " if ($terms && $tit);
990 $terms .= "title: $tit" if ($tit);
991 $terms .= " " if ($terms && $aut);
992 $terms .= "author: $aut" if ($aut);
993 $terms .= " " if ($terms && $sut);
994 $terms .= "subject: $sut" if ($sut);
995 $terms .= " " if ($terms && $set);
996 $terms .= "series: $set" if ($set);
998 if ($version eq '1.0') {
1000 } elsif ($type eq '-') {
1003 my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
1005 if ($terms eq 'help') {
1006 print $cgi->header(-type => 'text/html');
1010 <title>just type something!</title>
1013 <p>You are in a maze of dark, twisty stacks, all alike.</p>
1017 return Apache2::Const::OK;
1020 $terms = decode_utf8($terms);
1021 $lang = 'eng' if ($lang eq 'en-US');
1023 $log->debug("OpenSearch terms: $terms");
1027 $org_unit = $actor->request(
1028 'open-ils.actor.org_unit_list.search' => parent_ou => undef
1030 } elsif ($org !~ /^\d+$/o) {
1031 $org_unit = $actor->request(
1032 'open-ils.actor.org_unit_list.search' => shortname => uc($org)
1035 $org_unit = $actor->request(
1036 'open-ils.actor.org_unit_list.search' => id => $org
1040 my $recs = $search->request(
1041 'open-ils.search.biblio.multiclass.query' => {
1042 org_unit => $org_unit->[0]->id,
1046 sort_dir => $sortdir,
1047 ($lang ? ( 'language' => $lang ) : ()),
1051 $log->debug("Hits for [$terms]: $recs->{count}");
1053 my $feed = create_record_feed(
1056 [ map { $_->[0] } @{$recs->{ids}} ],
1062 $log->debug("Feed created...");
1066 $feed->search($terms);
1067 $feed->class($class);
1069 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1071 $feed->creator($host);
1072 $feed->update_ts(gmtime_ISO8601());
1074 $feed->_create_node(
1075 $feed->{item_xpath},
1076 'http://a9.com/-/spec/opensearch/1.1/',
1081 $feed->_create_node(
1082 $feed->{item_xpath},
1083 'http://a9.com/-/spec/opensearch/1.1/',
1088 $feed->_create_node(
1089 $feed->{item_xpath},
1090 'http://a9.com/-/spec/opensearch/1.1/',
1095 $log->debug("...basic feed data added...");
1099 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
1100 'application/opensearch+xml'
1101 ) if ($offset + $limit < $recs->{count});
1105 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1106 'application/opensearch+xml'
1111 $base . "/$version/$org/$type/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1112 'application/opensearch+xml'
1117 $base . "/$version/$org/rss2-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1118 'application/rss+xml'
1123 $base . "/$version/$org/atom-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1124 'application/atom+xml'
1129 $base . "/$version/$org/html/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1135 $base . "/$version/$org/html-full/$class?searchTerms=$terms&searchSort=$sort&searchSortDir=$sortdir&searchLang=$lang" =>
1139 $feed->link( 'unapi-server' => $unapi);
1141 $log->debug("...feed links added...");
1145 # $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1146 # join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1150 print $cgi->header( -type => $feed->type, -charset => 'UTF-8') . entityize($feed->toString) . "\n";
1152 $log->debug("...and feed returned.");
1154 return Apache2::Const::OK;
1157 sub create_record_feed {
1160 my $records = shift;
1163 my $lib = uc(shift()) || '-';
1165 $flesh = 1 if (!defined($flesh));
1168 my $base = $cgi->url;
1169 my $host = $cgi->virtual_host || $cgi->server_name;
1171 my $year = (gmtime())[5] + 1900;
1173 my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1175 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1176 $feed->base($base) if ($flesh);
1177 $feed->unapi($unapi) if ($flesh);
1179 $type = 'atom' if ($type eq 'html');
1180 $type = 'marcxml' if ($type eq 'htmlholdings');
1182 #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1185 for my $record (@$records) {
1186 next unless($record);
1188 #my $rec = $record->id;
1191 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1192 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1194 my $xml = $supercat->request(
1195 "open-ils.supercat.$search.$type.retrieve",
1200 my $node = $feed->add_item($xml);
1204 if ($lib && $type eq 'marcxml' && $flesh) {
1205 my $r = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib );
1206 while ( !$r->complete ) {
1207 $xml .= join('', map {$_->content} $r->recv);
1209 $xml .= join('', map {$_->content} $r->recv);
1210 $node->add_holdings($xml);
1213 $node->id($item_tag);
1214 #$node->update_ts(clense_ISO8601($record->edit_date));
1215 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1216 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1217 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1218 $node->link('unapi-id' => $item_tag) if ($flesh);
1225 my $stuff = NFC(shift());
1226 $stuff =~ s/&(?!\S+;)/&/gso;
1227 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1233 return Apache2::Const::DECLINED if (-e $apache->filename);
1236 my $year = (gmtime())[5] + 1900;
1238 my $host = $cgi->virtual_host || $cgi->server_name;
1241 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1242 my $rel_name = $cgi->url(-relative=>1);
1243 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1246 my $url = $cgi->url(-path_info=>$add_path);
1247 my $root = (split 'browse', $url)[0];
1248 my $base = (split 'browse', $url)[0] . 'browse';
1249 my $unapi = (split 'browse', $url)[0] . 'unapi';
1251 my $path = $cgi->path_info;
1254 my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1255 #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1257 $site ||= $cgi->param('searchOrg');
1258 $page ||= $cgi->param('startPage') || 0;
1259 $page_size ||= $cgi->param('count') || 9;
1261 $page = 0 if ($page !~ /^-?\d+$/);
1263 my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1264 my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1266 unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1267 warn "something's wrong...";
1268 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1272 $string = decode_utf8($string);
1273 $string =~ s/\+/ /go;
1276 my $tree = $supercat->request(
1277 "open-ils.supercat.$axis.browse",
1284 my ($header,$content) = $browse_types{$axis}{$format}->($tree,$prev,$next);
1285 print $header.$content;
1286 return Apache2::Const::OK;
1289 our %qualifier_map = (
1292 'eg.site' => 'site',
1293 'eg.sort' => 'sort',
1294 'eg.direction' => 'dir',
1295 'eg.available' => 'available',
1298 'eg.title' => 'title',
1299 'dc.title' => 'title',
1300 'bib.titleabbreviated' => 'title|abbreviated',
1301 'bib.titleuniform' => 'title|uniform',
1302 'bib.titletranslated' => 'title|translated',
1303 'bib.titlealternative' => 'title',
1304 'bib.titleseries' => 'series',
1305 'eg.series' => 'title',
1307 # Author/Name class:
1308 'eg.author' => 'title',
1309 'eg.name' => 'title',
1310 'creator' => 'author',
1311 'dc.creator' => 'author',
1312 'dc.contributer' => 'author',
1313 'dc.publisher' => 'keyword',
1314 'bib.name' => 'author',
1315 'bib.namepersonal' => 'author|personal',
1316 'bib.namepersonalfamily'=> 'author|personal',
1317 'bib.namepersonalgiven' => 'author|personal',
1318 'bib.namecorporate' => 'author|corporate',
1319 'bib.nameconference' => 'author|conference',
1322 'eg.subject' => 'subject',
1323 'dc.subject' => 'subject',
1324 'bib.subjectplace' => 'subject|geographic',
1325 'bib.subjecttitle' => 'keyword',
1326 'bib.subjectname' => 'subject|name',
1327 'bib.subjectoccupation' => 'keyword',
1330 'eg.keyword' => 'keyword',
1331 'srw.serverchoice' => 'keyword',
1334 'dc.identifier' => 'keyword',
1337 'bib.dateissued' => undef,
1338 'bib.datecreated' => undef,
1339 'bib.datevalid' => undef,
1340 'bib.datemodified' => undef,
1341 'bib.datecopyright' => undef,
1347 'dc.format' => undef,
1350 'bib.genre' => 'keyword',
1353 'bib.audience' => undef,
1356 'bib.originplace' => undef,
1359 'dc.language' => 'lang',
1362 'bib.edition' => 'keyword',
1365 'bib.volume' => 'keyword',
1366 'bib.issue' => 'keyword',
1367 'bib.startpage' => 'keyword',
1368 'bib.endpage' => 'keyword',
1371 'bib.issuance' => 'keyword',
1374 our %qualifier_ids = (
1375 eg => 'http://open-ils.org/spec/SRU/context-set/evergreen/v1',
1376 dc => 'info:srw/cql-context-set/1/dc-v1.1',
1377 bib => 'info:srw/cql-context-set/1/bib-v1.0',
1381 our %nested_qualifier_map = (
1383 site => ['site','Evergreen Site Code (shortname)'],
1384 sort => ['sort','Sort on relevance, title, author, pubdate, create_date or edit_date'],
1385 direction => ['dir','Sort direction (asc|desc)'],
1386 available => ['available','Filter to availble (true|false)'],
1388 author => ['author'],
1390 subject => ['subject'],
1391 keyword => ['keyword'],
1392 series => ['series'],
1396 creator => ['author'],
1397 contributor => ['author'],
1398 publisher => ['keyword'],
1399 subject => ['subject'],
1400 identifier => ['keyword'],
1403 language => ['lang'],
1407 titleAbbreviated => ['title'],
1408 titleUniform => ['title'],
1409 titleTranslated => ['title'],
1410 titleAlternative => ['title'],
1411 titleSeries => ['series'],
1413 # Author/Name class:
1415 namePersonal => ['author'],
1416 namePersonalFamily => ['author'],
1417 namePersonalGiven => ['author'],
1418 nameCorporate => ['author'],
1419 nameConference => ['author'],
1422 subjectPlace => ['subject'],
1423 subjectTitle => ['keyword'],
1424 subjectName => ['subject|name'],
1425 subjectOccupation => ['keyword'],
1430 dateIssued => [undef],
1431 dateCreated => [undef],
1432 dateValid => [undef],
1433 dateModified => [undef],
1434 dateCopyright => [undef],
1437 genre => ['keyword'],
1440 audience => [undef],
1443 originPlace => [undef],
1446 edition => ['keyword'],
1449 volume => ['keyword'],
1450 issue => ['keyword'],
1451 startPage => ['keyword'],
1452 endPage => ['keyword'],
1455 issuance => ['keyword'],
1458 serverChoice => ['keyword'],
1463 my $base_explain = <<XML;
1465 id="evergreen-sru-explain-full"
1466 authoritative="true"
1467 xmlns:z="http://explain.z3950.org/dtd/2.0/"
1468 xmlns="http://explain.z3950.org/dtd/2.0/">
1469 <serverInfo transport="http" protocol="SRU" version="1.1">
1476 <title primary="true"/>
1477 <description primary="true"/>
1481 <set identifier="info:srw/cql-context-set/1/cql-v1.2" name="cql"/>
1486 identifier="info:srw/schema/1/marcxml-v1.1"
1487 location="http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1491 <title>MARC21Slim (marcxml)</title>
1496 <default type="numberOfRecords">10</default>
1497 <default type="contextSet">eg</default>
1498 <default type="index">keyword</default>
1499 <default type="relation">all</default>
1500 <default type="sortSchema">marcxml</default>
1501 <default type="retrieveSchema">marcxml</default>
1502 <setting type="maximumRecords">10</setting>
1503 <supports type="relationModifier">relevant</supports>
1504 <supports type="relationModifier">stem</supports>
1505 <supports type="relationModifier">fuzzy</supports>
1506 <supports type="relationModifier">word</supports>
1517 my $req = SRU::Request->newFromCGI( $cgi );
1518 my $resp = SRU::Response->newFromRequest( $req );
1520 if ( $resp->type eq 'searchRetrieve' ) {
1521 my $cql_query = $req->query;
1522 my $search_string = $req->cql->toEvergreen;
1524 my $offset = $req->startRecord;
1525 $offset-- if ($offset);
1528 my $limit = $req->maximumRecords;
1531 warn "SRU search string [$cql_query] converted to [$search_string]\n";
1533 my $recs = $search->request(
1534 'open-ils.search.biblio.multiclass.query' => {offset => $offset, limit => $limit} => $search_string => 1
1537 my $bre = $supercat->request( 'open-ils.supercat.record.object.retrieve' => [ map { $_->[0] } @{$recs->{ids}} ] )->gather(1);
1540 SRU::Response::Record->new(
1541 recordSchema => 'info:srw/schema/1/marcxml-v1.1',
1542 recordData => $_->marc
1546 $resp->numberOfRecords($recs->{count});
1548 } elsif ( $resp->type eq 'explain' ) {
1550 my $host = $cgi->virtual_host || $cgi->server_name;
1553 if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1554 my $rel_name = $cgi->url(-relative=>1);
1555 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1557 my $base = $cgi->url(-base=>1);
1558 my $url = $cgi->url(-path_info=>$add_path);
1559 $url =~ s/^$base\///o;
1561 my $doc = $parser->parse_string($base_explain);
1562 my $e = $doc->documentElement;
1563 $e->findnodes('/z:explain/z:serverInfo/z:host')->shift->appendText( $host );
1564 $e->findnodes('/z:explain/z:serverInfo/z:port')->shift->appendText( $cgi->server_port );
1565 $e->findnodes('/z:explain/z:serverInfo/z:database')->shift->appendText( $url );
1567 for my $name ( keys %OpenILS::WWW::SuperCat::nested_qualifier_map ) {
1569 my $identifier = $OpenILS::WWW::SuperCat::qualifier_ids{ $name };
1571 next unless $identifier;
1573 my $set_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'set' );
1574 $set_node->setAttribute( identifier => $identifier );
1575 $set_node->setAttribute( name => $name );
1577 $e->findnodes('/z:explain/z:indexInfo')->shift->appendChild( $set_node );
1579 for my $index ( keys %{ $OpenILS::WWW::SuperCat::nested_qualifier_map{$name} } ) {
1580 my $desc = $OpenILS::WWW::SuperCat::nested_qualifier_map{$name}{$index}[1] || $index;
1582 my $name_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'name' );
1584 my $map_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'map' );
1585 $map_node->appendChild( $name_node );
1587 my $title_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'title' );
1589 my $index_node = $doc->createElementNS( 'http://explain.z3950.org/dtd/2.0/', 'index' );
1590 $index_node->appendChild( $title_node );
1591 $index_node->appendChild( $map_node );
1593 $index_node->setAttribute( id => $name . '.' . $index );
1594 $title_node->appendText( $desc );
1595 $name_node->setAttribute( set => $name );
1596 $name_node->appendText($index );
1598 $e->findnodes('/z:explain/z:indexInfo')->shift->appendChild( $index_node );
1602 $ex_doc = $e->toString;
1606 SRU::Response::Record->new(
1607 recordSchema => 'info:srw/cql-context-set/2/zeerex-1.1',
1608 recordData => $ex_doc
1613 print $cgi->header( -type => 'application/xml' );
1614 print entityize($resp->asXML) . "\n";
1615 return Apache2::Const::OK;
1620 package CQL::BooleanNode;
1624 my $left = $self->left();
1625 my $right = $self->right();
1626 my $leftStr = $left->toEvergreen;
1627 my $rightStr = $right->toEvergreen();
1629 my $op = '||' if uc $self->op() eq 'OR';
1632 return "$leftStr $rightStr";
1635 package CQL::TermNode;
1639 my $qualifier = $self->getQualifier();
1640 my $term = $self->getTerm();
1641 my $relation = $self->getRelation();
1645 my ($qset, $qname) = split(/\./, $qualifier);
1647 warn "!!! $qset, $qname $OpenILS::WWW::SuperCat::nested_qualifier_map{$qset}{$qname}[0]\n";
1649 if ( exists($OpenILS::WWW::SuperCat::nested_qualifier_map{$qset}{$qname}) ) {
1650 $qualifier = $OpenILS::WWW::SuperCat::nested_qualifier_map{$qset}{$qname}[0] || 'kw';
1653 my @modifiers = $relation->getModifiers();
1655 my $base = $relation->getBase();
1656 if ( grep { $base eq $_ } qw/= scr exact all/ ) {
1659 foreach my $m ( @modifiers ) {
1660 if( grep { $m->[ 1 ] eq $_ } qw/cql.fuzzy cql.stem cql.relevant cql.word/ ) {
1666 $quote_it = 0 if ( $base eq 'all' );
1667 $term = maybeQuote($term) if $quote_it;
1670 croak( "Evergreen doesn't support the $base relations" );
1678 return "$qualifier:$term";