1 package OpenILS::WWW::SuperCat;
2 use strict; use warnings;
6 use Apache2::Const -compile => qw(OK REDIRECT DECLINED :log);
7 use APR::Const -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use Apache2::RequestUtil;
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::Utils qw/:datetime/;
17 use OpenSRF::AppSession;
20 use Unicode::Normalize;
21 use OpenILS::Utils::Fieldmapper;
22 use OpenILS::WWW::SuperCat::Feed;
25 # set the bootstrap config when this module is loaded
26 my ($bootstrap, $supercat, $actor, $parser, $search);
35 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
36 $supercat = OpenSRF::AppSession->create('open-ils.supercat');
37 $actor = OpenSRF::AppSession->create('open-ils.actor');
38 $search = OpenSRF::AppSession->create('open-ils.search');
39 $parser = new XML::LibXML;
45 return Apache2::Const::DECLINED if (-e $apache->filename);
47 (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
50 ->request("open-ils.supercat.oisbn", $isbn)
53 print "Content-type: application/xml; charset=utf-8\n\n";
54 print "<?xml version='1.0' encoding='UTF-8' ?>\n";
56 unless (exists $$list{metarecord}) {
58 return Apache2::Const::OK;
61 print "<idlist metarecord='$$list{metarecord}'>\n";
63 for ( keys %{ $$list{record_list} } ) {
64 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
65 print " <isbn record='$_'>$o</isbn>\n"
70 return Apache2::Const::OK;
76 return Apache2::Const::DECLINED if (-e $apache->filename);
78 print "Content-type: application/xml; charset=utf-8\n";
82 my $uri = $cgi->param('uri') || '';
84 my $host = $cgi->virtual_host || $cgi->server_name;
86 my $format = $cgi->param('format');
87 my ($id,$type,$command) = ('','','');
90 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
93 $type = 'metarecord' if ($1 =~ /^m/o);
96 ->request("open-ils.supercat.$type.formats")
106 <type>text/html</type>
110 my ($type) = keys %$h;
111 $body .= "<format><name>$type</name><type>application/$type+xml</type>";
113 for my $part ( qw/namespace_uri docs schema_location/ ) {
114 $body .= "<$part>$$h{$type}{$part}</$part>"
115 if ($$h{$type}{$part});
118 $body .= '</format>';
121 $body .= "</formats>\n";
123 $apache->custom_response( 300, $body);
127 ->request("open-ils.supercat.record.formats")
132 ->request("open-ils.supercat.metarecord.formats")
136 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
137 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
142 <type>text/html</type>
146 my ($type) = keys %$h;
147 print "<format><name>$type</name><type>application/$type+xml</type>";
149 for my $part ( qw/namespace_uri docs schema_location/ ) {
150 print "<$part>$$h{$type}{$part}</$part>"
151 if ($$h{$type}{$part});
157 print "</formats>\n";
160 return Apache2::Const::OK;
165 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
168 $type = 'metarecord' if ($1 =~ /^m/o);
169 $command = 'retrieve';
172 if ($format eq 'opac') {
173 print "Location: $base/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
174 if ($type eq 'metarecord');
175 print "Location: $base/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
176 if ($type eq 'record');
180 print "\n" . $supercat->request("open-ils.supercat.$type.$format.$command",$id)->gather(1);
182 return Apache2::Const::OK;
188 return Apache2::Const::DECLINED if (-e $apache->filename);
190 my $path = $apache->path_info;
193 my $base = $cgi->url;
195 my ($id,$type,$format,$command) = reverse split '/', $path;
197 print "Content-type: application/xml; charset=utf-8\n";
199 if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
202 ->request("open-ils.supercat.$1.formats")
210 <type>text/html</type>
214 my ($type) = keys %$h;
215 print "<format><name>$type</name><type>application/$type+xml</type>";
217 for my $part ( qw/namespace_uri docs schema_location/ ) {
218 print "<$part>$$h{$type}{$part}</$part>"
219 if ($$h{$type}{$part});
225 print "</formats>\n";
227 return Apache2::Const::OK;
231 ->request("open-ils.supercat.record.formats")
236 ->request("open-ils.supercat.metarecord.formats")
240 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
241 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
246 <type>text/html</type>
250 my ($type) = keys %$h;
251 print "<format><name>$type</name><type>application/$type+xml</type>";
253 for my $part ( qw/namespace_uri docs schema_location/ ) {
254 print "<$part>$$h{$type}{$part}</$part>"
255 if ($$h{$type}{$part});
261 print "</formats>\n";
264 return Apache2::Const::OK;
267 if ($format eq 'opac') {
268 print "Location: $base/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
269 if ($type eq 'metarecord');
270 print "Location: $base/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
271 if ($type eq 'record');
275 print "\n" . $supercat->request("open-ils.supercat.$type.$format.$command",$id)->gather(1);
277 return Apache2::Const::OK;
283 return Apache2::Const::DECLINED if (-e $apache->filename);
286 (my $unapi = $cgi->url) =~ s{[^/]+/?$}{unapi};
288 my $year = (gmtime())[5];
290 my $host = $cgi->virtual_host || $cgi->server_name;
291 my $path = $apache->path_info;
292 my $base = $cgi->url;
294 my ($id,$type) = reverse split '/', $path;
296 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
297 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
299 if ($type eq 'opac') {
300 print "Location: $base/../../../opac/en-US/skin/default/xml/rresult.xml?rt=list&" .
301 join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
303 return Apache2::Const::OK;
306 my $feed = create_record_feed(
308 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
312 $feed->title("Items in Book Bag #".$bucket->id);
313 $feed->creator($host);
314 $feed->update_ts(gmtime_ISO8601());
316 $feed->link(atom => $base . "/bookbag/atom/$id");
317 $feed->link(rss2 => $base . "/bookbag/rss2/$id");
318 $feed->link(html => $base . "/bookbag/html/$id");
322 $base . '/../../../opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
323 join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
328 print "Content-type: application/xml; charset=utf-8\n\n";
329 print entityize($feed->toString) . "\n";
331 return Apache2::Const::OK;
339 if ($version eq '1.0') {
341 Content-type: application/opensearchdescription+xml; charset=utf-8
343 <?xml version="1.0" encoding="UTF-8"?>
344 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
345 <Url>$base/1.0/-/$class/-/{searchTerms}?startPage={startPage}&startIndex={startIndex}&count={count}</Url>
346 <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
347 <ShortName>$class</ShortName>
348 <LongName>Search by $class</LongName>
349 <Description>Search the OPAC by $class.</Description>
350 <Tags>book library</Tags>
351 <SampleSearch>harry+potter</SampleSearch>
352 <Developer>Mike Rylander for GPLS/PINES</Developer>
353 <Contact>feedback\@open-ils.org</Contact>
354 <SyndicationRight>open</SyndicationRight>
355 <AdultContent>false</AdultContent>
356 </OpenSearchDescription>
360 Content-type: application/opensearchdescription+xml; charset=utf-8
362 <?xml version="1.0" encoding="UTF-8"?>
363 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
364 <ShortName>$class</ShortName>
365 <Description>Search the OPAC by $class.</Description>
366 <Tags>book library</Tags>
367 <Url type="application/atom+xml"
369 template="$base/1.1/-/$class/atom/{searchTerms}">
370 <Param name="startPage" value="{startPage?}"/>
371 <Param name="startIndex" value="{startIndex?}"/>
372 <Param name="count" value="{count?}"/>
373 <Param name="language" value="{language?}"/>
375 <Url type="application/rss+xml"
377 template="$base/1.1/-/$class/rss2/{searchTerms}">
378 <Param name="startPage" value="{startPage?}"/>
379 <Param name="startIndex" value="{startIndex?}"/>
380 <Param name="count" value="{count?}"/>
381 <Param name="language" value="{language?}"/>
383 <Url type="application/mods+xml"
385 template="$base/1.1/-/$class/mods/{searchTerms}">
386 <Param name="startPage" value="{startPage?}"/>
387 <Param name="startIndex" value="{startIndex?}"/>
388 <Param name="count" value="{count?}"/>
389 <Param name="language" value="{language?}"/>
391 <Url type="application/marcxml+xml"
393 template="$base/1.1/-/$class/marcxml/{searchTerms}">
394 <Param name="startPage" value="{startPage?}"/>
395 <Param name="startIndex" value="{startIndex?}"/>
396 <Param name="count" value="{count?}"/>
397 <Param name="language" value="{language?}"/>
399 <LongName>Search by $class</LongName>
400 <Query role="example" searchTerms="harry+potter" />
401 <Developer>Mike Rylander for GPLS/PINES</Developer>
402 <SyndicationRight>open</SyndicationRight>
403 <AdultContent>false</AdultContent>
404 <Language>en-US</Language>
405 <OutputEncoding>UTF-8</OutputEncoding>
406 <InputEncoding>UTF-8</InputEncoding>
407 </OpenSearchDescription>
411 return Apache2::Const::OK;
414 sub opensearch_feed {
416 return Apache2::Const::DECLINED if (-e $apache->filename);
419 (my $unapi = $cgi->url) =~ s{[^/]+/?$}{unapi};
421 my $year = (gmtime())[5];
423 my $host = $cgi->virtual_host || $cgi->server_name;
424 my $base = $cgi->url;
425 my $path = $apache->path_info;
427 if ($path =~ m{^/?(1\.\d{1})/([^/]+)/osd.xml}o) {
436 return opensearch_osd($version, $class, $base);
440 my $page = $cgi->param('startPage') || 1;
441 my $offset = $cgi->param('startIndex') || 1;
442 my $limit = $cgi->param('count') || 10;
443 my $lang = $cgi->param('language') || 'en-US';
446 $offset = ($page - 1) * $limit;
451 my ($terms,$class,$type,$org,$version) = reverse split '/', $path;
453 if ($version eq '1.0') {
455 } elsif ($type eq '-') {
459 $class = 'keyword' if ($class eq '-');
462 warn "searching for $class -> [$terms] via OS $version, response type $type";
466 $org_unit = $actor->request(
467 'open-ils.actor.org_unit_list.search' => parent_ou => undef
470 $org_unit = $actor->request(
471 'open-ils.actor.org_unit_list.search' => shortname => $org
475 my $recs = $search->request(
476 'open-ils.search.biblio.record.class.search' => $class,
478 org_unit => $org_unit->[0]->id,
484 my $feed = create_record_feed(
486 [ map { $_->[0] } @{$recs->{ids}} ],
490 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
491 $feed->creator($host);
492 $feed->update_ts(gmtime_ISO8601());
495 my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
496 my $bucket_tag = "tag:$host,$year:record_bucket/$id";
498 my $feed = create_record_feed(
500 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
504 $feed->link(atom => $id);
505 $feed->link(rss2 => $id);
506 $feed->link(html => $id);
512 'http://a9.com/-/spec/opensearch/1.1/',
519 'http://a9.com/-/spec/opensearch/1.1/',
526 'http://a9.com/-/spec/opensearch/1.1/',
533 $base . $path . "?startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
534 'application/opensearch+xml'
535 ) if ($offset + $limit < $recs->{count});
539 $base . $path . "?startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
540 'application/opensearch+xml'
546 'application/opensearch+xml'
551 $base . "/../../opac/$lang/skin/default/xml/rresult.xml?rt=list&" .
552 join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
556 print "Content-type: application/xml; charset=utf-8\n\n";
558 print entityize($feed->toString) . "\n";
559 return Apache2::Const::OK;
562 sub create_record_feed {
568 my $base = $cgi->url;
569 my $host = $cgi->virtual_host || $cgi->server_name;
571 my $year = (gmtime())[5];
573 my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
575 $feed->unapi($unapi);
577 for my $rec (@$records) {
578 my $item_tag = "tag:$host,$year:biblio-record_entry/" . $rec;
580 my $xml = $supercat->request(
581 "open-ils.supercat.record.$type.retrieve",
585 my $node = $feed->add_item($xml);
587 $node->id($item_tag);
588 $node->link(opac => $feed->unapi . "?uri=$item_tag&format=opac");
589 $node->link(unapi => $feed->unapi . "?uri=$item_tag");
596 my $stuff = NFC(shift());
597 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;