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