]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/SuperCat.pm
adding xml CN browsing interface
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / SuperCat.pm
1 package OpenILS::WWW::SuperCat;
2 use strict; use warnings;
3
4 use Apache2 ();
5 use Apache2::Log;
6 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
7 use APR::Const    -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use Apache2::RequestUtil;
11 use CGI;
12 use Data::Dumper;
13
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::Utils qw/:datetime/;
16 use OpenSRF::Utils::Cache;
17 use OpenSRF::System;
18 use OpenSRF::AppSession;
19 use XML::LibXML;
20
21 use Encode;
22 use Unicode::Normalize;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::WWW::SuperCat::Feed;
25
26
27 # set the bootstrap config when this module is loaded
28 my ($bootstrap, $supercat, $actor, $parser, $search);
29
30 sub import {
31         my $self = shift;
32         $bootstrap = shift;
33 }
34
35
36 sub child_init {
37         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
38         $supercat = OpenSRF::AppSession->create('open-ils.supercat');
39         $actor = OpenSRF::AppSession->create('open-ils.actor');
40         $search = OpenSRF::AppSession->create('open-ils.search');
41         $parser = new XML::LibXML;
42 }
43
44 sub oisbn {
45
46         my $apache = shift;
47         return Apache2::Const::DECLINED if (-e $apache->filename);
48
49         (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
50
51         my $list = $supercat
52                 ->request("open-ils.supercat.oisbn", $isbn)
53                 ->gather(1);
54
55         print "Content-type: application/xml; charset=utf-8\n\n";
56         print "<?xml version='1.0' encoding='UTF-8' ?>\n";
57
58         unless (exists $$list{metarecord}) {
59                 print '<idlist/>';
60                 return Apache2::Const::OK;
61         }
62
63         print "<idlist metarecord='$$list{metarecord}'>\n";
64
65         for ( keys %{ $$list{record_list} } ) {
66                 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
67                 print "  <isbn record='$_'>$o</isbn>\n"
68         }
69
70         print "</idlist>\n";
71
72         return Apache2::Const::OK;
73 }
74
75 sub unapi {
76
77         my $apache = shift;
78         return Apache2::Const::DECLINED if (-e $apache->filename);
79
80         my $cgi = new CGI;
81         my $rel_name = quotemeta($cgi->url(-relative=>1));
82
83         my $add_path = 1;
84         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
85
86
87         my $url = $cgi->url(-path_info=>$add_path);
88         my $root = (split 'unapi', $url)[0];
89         my $base = (split 'unapi', $url)[0] . 'unapi';
90
91
92         my $uri = $cgi->param('id') || '';
93         my $host = $cgi->virtual_host || $cgi->server_name;
94
95         my $format = $cgi->param('format');
96         my ($id,$type,$command,$lib) = ('','','');
97
98         if (!$format) {
99                 print "Content-type: application/xml; charset=utf-8\n";
100         
101                 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
102                         $id = $2;
103                         $lib = $3;
104                         $type = 'record';
105                         $type = 'metarecord' if ($1 =~ /^m/o);
106
107                         my $list = $supercat
108                                 ->request("open-ils.supercat.$type.formats")
109                                 ->gather(1);
110
111                         print "\n";
112
113                         my $body = "<formats id='$uri'><format name='opac' type='text/html'/>";
114
115                         for my $h (@$list) {
116                                 my ($type) = keys %$h;
117                                 $body .= "<format name='$type' type='application/xml'";
118
119                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
120                                         $body .= " $part='$$h{$type}{$part}'"
121                                                 if ($$h{$type}{$part});
122                                 }
123                                 
124                                 $body .= '/>';
125                         }
126
127                         $body .= "</formats>\n";
128
129                         $apache->custom_response( 300, $body);
130                         return 300;
131                 } else {
132                         my $list = $supercat
133                                 ->request("open-ils.supercat.record.formats")
134                                 ->gather(1);
135                                 
136                         push @$list,
137                                 @{ $supercat
138                                         ->request("open-ils.supercat.metarecord.formats")
139                                         ->gather(1);
140                                 };
141
142                         my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
143                         $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
144
145                         print "<formats><format name='opac' type='text/html'/>";
146
147                         for my $h (@$list) {
148                                 my ($type) = keys %$h;
149                                 print "<format name='$type' type='application/xml'";
150
151                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
152                                         print " $part='$$h{$type}{$part}'"
153                                                 if ($$h{$type}{$part});
154                                 }
155                                 
156                                 print '/>';
157                         }
158
159                         print "</formats>\n";
160
161
162                         return Apache2::Const::OK;
163                 }
164         }
165
166                 
167         if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)(?:/(.+))?}o) {
168                 $id = $2;
169                 $lib = $3;
170                 $type = 'record';
171                 $type = 'metarecord' if ($1 =~ /^m/o);
172                 $command = 'retrieve';
173         }
174
175         if ($format eq 'opac') {
176                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
177                         if ($type eq 'metarecord');
178                 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
179                         if ($type eq 'record');
180                 return 302;
181         } else {
182                 my $feed = create_record_feed(
183                         $format => [ $id ],
184                         $base,
185                         $lib,
186                 );
187
188                 $feed->root($root);
189                 $feed->creator($host);
190                 $feed->update_ts(gmtime_ISO8601());
191
192                 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
193                 print entityize($feed->toString) . "\n";
194
195                 return Apache2::Const::OK;
196         }
197
198         my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
199         $req->wait_complete;
200
201         if ($req->failed) {
202                 print "Content-type: text/html; charset=utf-8\n\n";
203                 $apache->custom_response( 404, <<"              HTML");
204                 <html>
205                         <head>
206                                 <title>$type $id not found!</title>
207                         </head>
208                         <body>
209                                 <br/>
210                                 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
211                         </body>
212                 </html>
213                 HTML
214                 return 404;
215         }
216
217         print "Content-type: application/xml; charset=utf-8\n\n";
218         print $req->gather(1);
219
220         return Apache2::Const::OK;
221 }
222
223 sub supercat {
224
225         my $apache = shift;
226         return Apache2::Const::DECLINED if (-e $apache->filename);
227
228         my $cgi = new CGI;
229
230         my $rel_name = quotemeta($cgi->url(-relative=>1));
231
232         my $add_path = 1;
233         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
234
235
236         my $url = $cgi->url(-path_info=>$add_path);
237         my $root = (split 'supercat', $url)[0];
238         my $base = (split 'supercat', $url)[0] . 'supercat';
239         my $path = (split 'supercat', $url)[1];
240         my $unapi = (split 'supercat', $url)[0] . 'unapi';
241
242         my $host = $cgi->virtual_host || $cgi->server_name;
243
244         my ($id,$type,$format,$command) = reverse split '/', $path;
245
246         
247         if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
248                 print "Content-type: application/xml; charset=utf-8\n";
249                 if ($1) {
250                         my $list = $supercat
251                                 ->request("open-ils.supercat.$1.formats")
252                                 ->gather(1);
253
254                         print "\n";
255
256                         print "<formats>
257                                    <format>
258                                      <name>opac</name>
259                                      <type>text/html</type>
260                                    </format>";
261
262                         for my $h (@$list) {
263                                 my ($type) = keys %$h;
264                                 print "<format><name>$type</name><type>application/xml</type>";
265
266                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
267                                         print "<$part>$$h{$type}{$part}</$part>"
268                                                 if ($$h{$type}{$part});
269                                 }
270                                 
271                                 print '</format>';
272                         }
273
274                         print "</formats>\n";
275
276                         return Apache2::Const::OK;
277                 }
278
279                 my $list = $supercat
280                         ->request("open-ils.supercat.record.formats")
281                         ->gather(1);
282                                 
283                 push @$list,
284                         @{ $supercat
285                                 ->request("open-ils.supercat.metarecord.formats")
286                                 ->gather(1);
287                         };
288
289                 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
290                 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
291
292                 print "\n<formats>
293                            <format>
294                              <name>opac</name>
295                              <type>text/html</type>
296                            </format>";
297
298                 for my $h (@$list) {
299                         my ($type) = keys %$h;
300                         print "<format><name>$type</name><type>application/xml</type>";
301
302                         for my $part ( qw/namespace_uri docs schema_location/ ) {
303                                 print "<$part>$$h{$type}{$part}</$part>"
304                                         if ($$h{$type}{$part});
305                         }
306                         
307                         print '</format>';
308                 }
309
310                 print "</formats>\n";
311
312
313                 return Apache2::Const::OK;
314         }
315
316         if ($format eq 'opac') {
317                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
318                         if ($type eq 'metarecord');
319                 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
320                         if ($type eq 'record');
321                 return 302;
322         } elsif ($format =~ /^html/o) {
323                 my $feed = create_record_feed( $format => [ $id ], $unapi,);
324
325                 $feed->root($root);
326                 $feed->creator($host);
327                 $feed->update_ts(gmtime_ISO8601());
328
329                 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
330                 print entityize($feed->toString) . "\n";
331
332                 return Apache2::Const::OK;
333         }
334
335         my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
336         $req->wait_complete;
337
338         if ($req->failed) {
339                 print "Content-type: text/html; charset=utf-8\n\n";
340                 $apache->custom_response( 404, <<"              HTML");
341                 <html>
342                         <head>
343                                 <title>$type $id not found!</title>
344                         </head>
345                         <body>
346                                 <br/>
347                                 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
348                         </body>
349                 </html>
350                 HTML
351                 return 404;
352         }
353
354         print "Content-type: application/xml; charset=utf-8\n\n";
355         print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
356
357         return Apache2::Const::OK;
358 }
359
360
361 sub bookbag_feed {
362         my $apache = shift;
363         return Apache2::Const::DECLINED if (-e $apache->filename);
364
365         my $cgi = new CGI;
366
367         my $year = (gmtime())[5] + 1900;
368         my $host = $cgi->virtual_host || $cgi->server_name;
369
370         my $rel_name = quotemeta($cgi->url(-relative=>1));
371
372         my $add_path = 1;
373         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
374
375         my $url = $cgi->url(-path_info=>$add_path);
376         my $root = (split 'feed', $url)[0];
377         my $base = (split 'bookbag', $url)[0] . 'bookbag';
378         my $path = (split 'bookbag', $url)[1];
379         my $unapi = (split 'feed', $url)[0] . 'unapi';
380
381
382         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
383
384         my ($id,$type) = reverse split '/', $path;
385
386         my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
387         return Apache2::Const::NOT_FOUND unless($bucket);
388
389         my $bucket_tag = "tag:$host,$year:record_bucket/$id";
390         if ($type eq 'opac') {
391                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
392                         join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
393                         "\n\n";
394                 return 302;
395         }
396
397         my $feed = create_record_feed(
398                 $type,
399                 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
400                 $unapi,
401         );
402         $feed->root($root);
403
404         $feed->title("Items in Book Bag [".$bucket->name."]");
405         $feed->creator($host);
406         $feed->update_ts(gmtime_ISO8601());
407
408         $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
409         $feed->link(rss2 => $base . "/rss2/$id");
410         $feed->link(html => $base . "/html/$id" => 'text/html');
411         $feed->link(unapi => $unapi);
412
413         $feed->link(
414                 OPAC =>
415                 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
416                         join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
417                 'text/html'
418         );
419
420
421         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
422         print entityize($feed->toString) . "\n";
423
424         return Apache2::Const::OK;
425 }
426
427 sub changes_feed {
428         my $apache = shift;
429         return Apache2::Const::DECLINED if (-e $apache->filename);
430
431         my $cgi = new CGI;
432
433         my $year = (gmtime())[5] + 1900;
434         my $host = $cgi->virtual_host || $cgi->server_name;
435
436         my $rel_name = quotemeta($cgi->url(-relative=>1));
437
438         my $add_path = 1;
439         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
440
441         my $url = $cgi->url(-path_info=>$add_path);
442         my $root = (split 'feed', $url)[0];
443         my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
444         my $path = (split 'freshmeat', $url)[1];
445         my $unapi = (split 'feed', $url)[0] . 'unapi';
446
447
448         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
449
450         $path =~ s/^\///og;
451         
452         my ($type,$rtype,$axis,$date,$limit) = split '/', $path;
453         $date ||= 'today';
454         $limit ||= 10;
455
456         my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
457
458         if ($type eq 'opac') {
459                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
460                         join('&', map { "rl=" . $_ } @$list) .
461                         "\n\n";
462                 return 302;
463         }
464
465         my $feed = create_record_feed( $type, $list, $unapi);
466         $feed->root($root);
467
468         $feed->title("$limit most recent $rtype changes from $date forward");
469         $feed->creator($host);
470         $feed->update_ts(gmtime_ISO8601());
471
472         $feed->link(atom => $base . "/atom/$rtype/$axis/$date/$limit" => 'application/atom+xml');
473         $feed->link(rss2 => $base . "/rss2/$rtype/$axis/$date/$limit");
474         $feed->link(html => $base . "/html/$rtype/$axis/$date/$limit" => 'text/html');
475         $feed->link(unapi => $unapi);
476
477         $feed->link(
478                 OPAC =>
479                 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
480                         join('&', map { 'rl=' . $_} @$list ),
481                 'text/html'
482         );
483
484
485         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
486         print entityize($feed->toString) . "\n";
487
488         return Apache2::Const::OK;
489 }
490
491 sub opensearch_osd {
492         my $version = shift;
493         my $lib = shift;
494         my $class = shift;
495         my $base = shift;
496
497         if ($version eq '1.0') {
498                 print <<OSD;
499 Content-type: application/opensearchdescription+xml; charset=utf-8
500
501 <?xml version="1.0" encoding="UTF-8"?>
502 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
503   <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&amp;startIndex={startIndex}&amp;count={count}</Url>
504   <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
505   <ShortName>$lib</ShortName>
506   <LongName>Search $lib</LongName>
507   <Description>Search the $lib OPAC by $class.</Description>
508   <Tags>$lib book library</Tags>
509   <SampleSearch>harry+potter</SampleSearch>
510   <Developer>Mike Rylander for GPLS/PINES</Developer>
511   <Contact>feedback\@open-ils.org</Contact>
512   <SyndicationRight>open</SyndicationRight>
513   <AdultContent>false</AdultContent>
514 </OpenSearchDescription>
515 OSD
516         } else {
517                 print <<OSD;
518 Content-type: application/opensearchdescription+xml; charset=utf-8
519
520 <?xml version="1.0" encoding="UTF-8"?>
521 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
522   <ShortName>$lib</ShortName>
523   <Description>Search the $lib OPAC by $class.</Description>
524   <Tags>$lib book library</Tags>
525   <Url type="application/atom+xml"
526        template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
527   <Url type="application/x-rss+xml"
528        template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
529   <Url type="application/x-mods3+xml"
530        template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
531   <Url type="application/x-mods+xml"
532        template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
533   <Url type="application/x-marcxml+xml"
534        template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
535   <LongName>Search $lib</LongName>
536   <Query role="example" searchTerms="harry+potter" />
537   <Developer>Mike Rylander for GPLS/PINES</Developer>
538   <Contact>feedback\@open-ils.org</Contact>
539   <SyndicationRight>open</SyndicationRight>
540   <AdultContent>false</AdultContent>
541   <Language>en-US</Language>
542   <OutputEncoding>UTF-8</OutputEncoding>
543   <InputEncoding>UTF-8</InputEncoding>
544 </OpenSearchDescription>
545 OSD
546         }
547
548         return Apache2::Const::OK;
549 }
550
551 sub opensearch_feed {
552         my $apache = shift;
553         return Apache2::Const::DECLINED if (-e $apache->filename);
554
555         my $cgi = new CGI;
556         my $year = (gmtime())[5] + 1900;
557
558         my $host = $cgi->virtual_host || $cgi->server_name;
559
560         my $rel_name = quotemeta($cgi->url(-relative=>1));
561
562         my $add_path = 1;
563         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
564
565         my $url = $cgi->url(-path_info=>$add_path);
566         my $root = (split 'opensearch', $url)[0];
567         my $base = (split 'opensearch', $url)[0] . 'opensearch';
568         my $unapi = (split 'opensearch', $url)[0] . 'unapi';
569
570
571         my $path = (split 'opensearch', $url)[1];
572
573         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
574
575         if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
576                 
577                 my $version = $1;
578                 my $lib = $2;
579                 my $class = $3;
580
581                 if (!$lib) {
582                         $lib = $actor->request(
583                                 'open-ils.actor.org_unit_list.search' => parent_ou => undef
584                         )->gather(1)->[0]->shortname;
585                 }
586
587                 if ($class eq '-') {
588                         $class = 'keyword';
589                 }
590
591                 return opensearch_osd($version, $lib, $class, $base);
592         }
593
594
595         my $page = $cgi->param('startPage') || 1;
596         my $offset = $cgi->param('startIndex') || 1;
597         my $limit = $cgi->param('count') || 10;
598         my $lang = $cgi->param('language') || 'en-US';
599
600         $page = 1 if ($page !~ /^\d+$/);
601         $offset = 1 if ($offset !~ /^\d+$/);
602         $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
603         $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
604
605         if ($page > 1) {
606                 $offset = ($page - 1) * $limit;
607         } else {
608                 $offset -= 1;
609         }
610
611         my ($version,$org,$type,$class,$terms,$sort);
612         (undef,$version,$org,$type,$class,$terms,$sort,$lang) = split '/', $path;
613
614         $lang ||= $cgi->param('searchLang');
615         $sort ||= $cgi->param('searchSort');
616         $terms ||= $cgi->param('searchTerms');
617         $class ||= $cgi->param('searchClass') || '-';
618         $type ||= $cgi->param('responseType') || '-';
619         $org ||= $cgi->param('searchOrg') || '-';
620
621         if ($version eq '1.0') {
622                 $type = 'rss2';
623         } elsif ($type eq '-') {
624                 $type = 'atom';
625         }
626
627
628         $terms = decode_utf8($terms);
629         $terms =~ s/\+/ /go;
630         $terms =~ s/'//go;
631         my $term_copy = $terms;
632
633         my $complex_terms = 0;
634         if ($terms eq 'help') {
635                 print $cgi->header(-type => 'text/html');
636                 print <<"               HTML";
637                         <html>
638                          <head>
639                           <title>just type something!</title>
640                          </head>
641                          <body>
642                           <p>You are in a maze of dark, twisty stacks, all alike.</p>
643                          </body>
644                         </html>
645                 HTML
646                 return Apache2::Const::OK;
647         }
648
649         my $cache_key = '';
650         my $searches = {};
651         while ($term_copy =~ /(keyword|title|author|subject|series|site|sort|lang):([^:]+?)$/o) {
652                 my $c = $1;
653                 my $t = $2;
654                 $term_copy =~ s/(keyword|title|author|subject|series|site|sort|lang):([^:]+?)$//o;
655                 if ($c eq 'site') {
656                         ($org = uc($t)) =~ s/\s+//go;
657                 } elsif ($c eq 'sort') {
658                         ($sort = lc($t)) =~ s/^\s*(\w+)/$1/go;
659                 } elsif ($c eq 'lang') {
660                         ($lang = lc($t)) =~ s/^\s*(\w+)/$1/go;
661                 } else {
662                         $$searches{$c} = { term => $t };
663                         $cache_key .= $c . $t;
664                         $complex_terms = 1;
665                 }
666         }
667
668         if ($term_copy) {
669                 no warnings;
670                 $class = 'keyword' if ($class eq '-');
671                 $$searches{$class}{term} .= " $term_copy";
672                 $cache_key .= $class . $term_copy;
673         }
674
675         my $org_unit;
676         if ($org eq '-') {
677                 $org_unit = $actor->request(
678                         'open-ils.actor.org_unit_list.search' => parent_ou => undef
679                 )->gather(1);
680         } else {
681                 $org_unit = $actor->request(
682                         'open-ils.actor.org_unit_list.search' => shortname => $org
683                 )->gather(1);
684         }
685
686         $cache_key .= $org.$sort.$lang;
687
688         my $rs_name = $cgi->cookie('os_session');
689         my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
690
691         my $recs;
692         if (!($recs = $$cached_res{os_results}{$cache_key})) {
693                 $rs_name = $cgi->remote_host . '::' . rand(time);
694                 $recs = $search->request(
695                         'open-ils.search.biblio.multiclass' => {
696                                 searches        => $searches,
697                                 org_unit        => $org_unit->[0]->id,
698                                 ($sort ? ( 'sort' => $sort, sort_dir => 'asc' ) : ()),
699                                 ($lang ? ( 'language' => $lang) : ()),
700                                 offset          => 0,
701                                 limit           => 5000,
702                         }
703                 )->gather(1);
704                 try {
705                         $$cached_res{os_results}{$cache_key} = $recs;
706                         OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
707                 } catch Error with {
708                         warn shift();
709                 };
710         }
711
712         my $feed = create_record_feed(
713                 $type,
714                 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
715                 $unapi,
716                 $org
717         );
718         $feed->root($root);
719         $feed->lib($org);
720         $feed->search($terms);
721         $feed->class($class);
722
723         if ($complex_terms) {
724                 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
725         } else {
726                 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
727         }
728
729         $feed->creator($host);
730         $feed->update_ts(gmtime_ISO8601());
731
732         $feed->_create_node(
733                 $feed->{item_xpath},
734                 'http://a9.com/-/spec/opensearch/1.1/',
735                 'totalResults',
736                 $recs->{count},
737         );
738
739         $feed->_create_node(
740                 $feed->{item_xpath},
741                 'http://a9.com/-/spec/opensearch/1.1/',
742                 'startIndex',
743                 $offset + 1,
744         );
745
746         $feed->_create_node(
747                 $feed->{item_xpath},
748                 'http://a9.com/-/spec/opensearch/1.1/',
749                 'itemsPerPage',
750                 $limit,
751         );
752
753         $feed->link(
754                 next =>
755                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
756                 'application/opensearch+xml'
757         ) if ($offset + $limit < $recs->{count});
758
759         $feed->link(
760                 previous =>
761                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
762                 'application/opensearch+xml'
763         ) if ($offset);
764
765         $feed->link(
766                 self =>
767                 $base .  "/$version/$org/$type/$class?searchTerms=$terms" =>
768                 'application/opensearch+xml'
769         );
770
771         $feed->link( unapi => $unapi);
772
773 #       $feed->link(
774 #               alternate =>
775 #               $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
776 #                       join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
777 #               'text/html'
778 #       );
779
780         $feed->link(
781                 opac =>
782                 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
783                         join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
784                 'text/html'
785         );
786
787         print $cgi->header(
788                 -type           => $feed->type,
789                 -charset        => 'UTF-8',
790                 -cookie         => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
791         );
792
793         print entityize($feed->toString) . "\n";
794
795         return Apache2::Const::OK;
796 }
797
798 sub create_record_feed {
799         my $type = shift;
800         my $records = shift;
801         my $unapi = shift;
802
803         my $lib = shift || '';
804
805         my $cgi = new CGI;
806         my $base = $cgi->url;
807         my $host = $cgi->virtual_host || $cgi->server_name;
808
809         my $year = (gmtime())[5] + 1900;
810
811         my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
812         $feed->base($base);
813         $feed->unapi($unapi);
814
815         $type = 'atom' if ($type eq 'html');
816         $type = 'marcxml' if ($type eq 'htmlcard' or $type eq 'htmlholdings');
817
818         for my $rec (@$records) {
819                 next unless($rec);
820
821                 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
822
823
824                 my $xml = $supercat->request(
825                         "open-ils.supercat.record.$type.retrieve",
826                         $rec
827                 )->gather(1);
828
829                 my $node = $feed->add_item($xml);
830
831                 if ($lib && $type eq 'marcxml') {
832                         $xml = $supercat->request( "open-ils.supercat.record.holdings_xml.retrieve", $rec, $lib )->gather(1);
833                         $node->add_holdings($xml);
834                 }
835
836                 $node->id($item_tag);
837                 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings" => 'text/html');
838                 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac");
839                 $node->link(unapi => $feed->unapi . "?id=$item_tag");
840                 $node->link('unapi-id' => $item_tag);
841         }
842
843         return $feed;
844 }
845
846 sub entityize {
847         my $stuff = NFC(shift());
848         $stuff =~ s/&(?!\S+;)/&amp;/gso;
849         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
850         return $stuff;
851 }
852
853 my %browse_types = (
854         call_number => {
855                 xml => sub {
856                         my $tree = shift;
857
858                         my $year = (gmtime())[5] + 1900;
859                         my $content = '';
860
861                         $content .= "Content-type: application/xml\n\n";
862                         $content .= "<hold:volumes  xmlns:hold='http://open-ils.org/spec/holdings/v1'>";
863
864                         for my $cn (@$tree) {
865                                 (my $cn_class = $cn->class_name) =~ s/::/-/gso;
866                                 $cn_class =~ s/Fieldmapper-//gso;
867
868                                 my $cn_tag = "tag:open-ils.org,$year:$cn_class/".$cn->id;
869                                 my $cn_lib = $cn->owning_lib->shortname;
870                                 my $cn_label = $cn->label;
871
872                                 (my $ou_class = $cn->owning_lib->class_name) =~ s/::/-/gso;
873                                 $ou_class =~ s/Fieldmapper-//gso;
874
875                                 my $ou_tag = "tag:open-ils.org,$year:$ou_class/".$cn->owning_lib->id;
876                                 my $ou_name = $cn->owning_lib->name;
877
878                                 $content .= "<hold:volume id='$cn_tag' lib='$cn_lib' label='$cn_label'>";
879                                 $content .= "<act:owning_lib xmlns:act='http://open-ils.org/spec/actors/v1' id='$ou_tag' name='$ou_name'/>";
880                                 $content .= $cn->record->marc;
881                                 $content .= "</hold:volume>";
882                         }
883
884                         $content .= '</hold:volumes>';
885                         return $content;
886                 }
887         }
888                         
889 );
890 sub string_browse {
891         my $apache = shift;
892         return Apache2::Const::DECLINED if (-e $apache->filename);
893
894         my $cgi = new CGI;
895         my $year = (gmtime())[5] + 1900;
896
897         my $host = $cgi->virtual_host || $cgi->server_name;
898
899         my $rel_name = quotemeta($cgi->url(-relative=>1));
900
901         my $add_path = 1;
902         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
903
904         my $url = $cgi->url(-path_info=>$add_path);
905         my $root = (split 'browse', $url)[0];
906         my $base = (split 'browse', $url)[0] . 'browse';
907         my $unapi = (split 'browse', $url)[0] . 'unapi';
908
909
910         my $path = (split 'browse', $url)[1];
911
912         my (undef,$format,$axis,$site,$string,$page,$page_size) = split '/', $path;
913
914
915         $site ||= $cgi->param('searchOrg');
916         $page ||= $cgi->param('startPage') || 0;
917         $page_size ||= $cgi->param('count') || 9;
918
919         $page = 0 if ($page !~ /^\d+$/);
920
921         unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
922                 warn "something's wrong...";
923                 return undef;
924         }
925
926         $string = decode_utf8($string);
927         $string =~ s/\+/ /go;
928         $string =~ s/'//go;
929
930         my $tree = $supercat->request(
931                 "open-ils.supercat.$axis.browse",
932                 $string,
933                 $site,
934                 $page_size,
935                 $page
936         )->gather(1);
937
938         my $content = $browse_types{$axis}{$format}->($tree);
939         print $content;
940         return Apache2::Const::OK;
941 }
942
943 1;