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