]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/SuperCat.pm
4b6fd5a35179e67480e8be5f036d43551818a743
[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 use XML::LibXSLT;
21
22 use Encode;
23 use Unicode::Normalize;
24 use OpenILS::Utils::Fieldmapper;
25 use OpenILS::WWW::SuperCat::Feed;
26 use OpenSRF::Utils::Logger qw/$logger/;
27
28
29 # set the bootstrap config when this module is loaded
30 my ($bootstrap, $cstore, $supercat, $actor, $parser, $search, $xslt, $cn_browse_xslt, %browse_types);
31
32 $browse_types{call_number}{xml} = sub {
33         my $tree = shift;
34
35         my $year = (gmtime())[5] + 1900;
36         my $content = '';
37
38         $content .= "<hold:volumes  xmlns:hold='http://open-ils.org/spec/holdings/v1'>";
39
40         for my $cn (@$tree) {
41                 (my $cn_class = $cn->class_name) =~ s/::/-/gso;
42                 $cn_class =~ s/Fieldmapper-//gso;
43
44                 my $cn_tag = "tag:open-ils.org,$year:$cn_class/".$cn->id;
45                 my $cn_lib = $cn->owning_lib->shortname;
46                 my $cn_label = $cn->label;
47
48                 $cn_label =~ s/\n//gos;
49                 $cn_label =~ s/'/&apos;/go;
50
51                 (my $ou_class = $cn->owning_lib->class_name) =~ s/::/-/gso;
52                 $ou_class =~ s/Fieldmapper-//gso;
53
54                 my $ou_tag = "tag:open-ils.org,$year:$ou_class/".$cn->owning_lib->id;
55                 my $ou_name = $cn->owning_lib->name;
56
57                 $ou_name =~ s/\n//gos;
58                 $ou_name =~ s/'/&apos;/go;
59
60                 (my $rec_class = $cn->record->class_name) =~ s/::/-/gso;
61                 $rec_class =~ s/Fieldmapper-//gso;
62
63                 my $rec_tag = "tag:open-ils.org,$year:$rec_class/".$cn->record->id.'/'.$cn->owning_lib->shortname;
64
65                 $content .= "<hold:volume id='$cn_tag' lib='$cn_lib' label='$cn_label'>";
66                 $content .= "<act:owning_lib xmlns:act='http://open-ils.org/spec/actors/v1' id='$ou_tag' name='$ou_name'/>";
67
68                 my $r_doc = $parser->parse_string($cn->record->marc);
69                 $r_doc->documentElement->setAttribute( id => $rec_tag );
70                 $content .= entityize($r_doc->documentElement->toString);
71
72                 $content .= "</hold:volume>";
73         }
74
75         $content .= '</hold:volumes>';
76         return ("Content-type: application/xml\n\n",$content);
77 };
78
79
80 $browse_types{call_number}{html} = sub {
81         my $tree = shift;
82         my $p = shift;
83         my $n = shift;
84
85         if (!$cn_browse_xslt) {
86                 $cn_browse_xslt = $parser->parse_file(
87                         OpenSRF::Utils::SettingsClient
88                                 ->new
89                                 ->config_value( dirs => 'xsl' ).
90                         "/CNBrowse2HTML.xsl"
91                 );
92                 $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
93         }
94
95         my (undef,$xml) = $browse_types{call_number}{xml}->($tree);
96
97         return (
98                 "Content-type: text/html\n\n",
99                 entityize(
100                         $cn_browse_xslt->transform(
101                                 $parser->parse_string( $xml ),
102                                 'prev' => "'$p'",
103                                 'next' => "'$n'"
104                         )->toString(1)
105                 )
106         );
107 };
108
109 sub import {
110         my $self = shift;
111         $bootstrap = shift;
112 }
113
114
115 sub child_init {
116         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
117         
118         my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
119         Fieldmapper->import(IDL => $idl);
120
121         $supercat = OpenSRF::AppSession->create('open-ils.supercat');
122         $cstore = OpenSRF::AppSession->create('open-ils.cstore');
123         $actor = OpenSRF::AppSession->create('open-ils.actor');
124         $search = OpenSRF::AppSession->create('open-ils.search');
125         $parser = new XML::LibXML;
126         $xslt = new XML::LibXSLT;
127
128         $cn_browse_xslt = $parser->parse_file(
129                 OpenSRF::Utils::SettingsClient
130                         ->new
131                         ->config_value( dirs => 'xsl' ).
132                 "/CNBrowse2HTML.xsl"
133         );
134
135         $cn_browse_xslt = $xslt->parse_stylesheet( $cn_browse_xslt );
136
137 }
138
139 sub oisbn {
140
141         my $apache = shift;
142         return Apache2::Const::DECLINED if (-e $apache->filename);
143
144         (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
145
146         my $list = $supercat
147                 ->request("open-ils.supercat.oisbn", $isbn)
148                 ->gather(1);
149
150         print "Content-type: application/xml; charset=utf-8\n\n";
151         print "<?xml version='1.0' encoding='UTF-8' ?>\n";
152
153         unless (exists $$list{metarecord}) {
154                 print '<idlist/>';
155                 return Apache2::Const::OK;
156         }
157
158         print "<idlist metarecord='$$list{metarecord}'>\n";
159
160         for ( keys %{ $$list{record_list} } ) {
161                 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
162                 print "  <isbn record='$_'>$o</isbn>\n"
163         }
164
165         print "</idlist>\n";
166
167         return Apache2::Const::OK;
168 }
169
170 sub unapi {
171
172         my $apache = shift;
173         return Apache2::Const::DECLINED if (-e $apache->filename);
174
175         my $cgi = new CGI;
176
177         my $add_path = 0;
178         if ( $cgi->server_software !~ m|^Apache/2.2| ) {
179                 my $rel_name = $cgi->url(-relative=>1);
180                 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
181         }
182
183         my $url = $cgi->url(-path_info=>$add_path);
184         my $root = (split 'unapi', $url)[0];
185         my $base = (split 'unapi', $url)[0] . 'unapi';
186
187
188         my $uri = $cgi->param('id') || '';
189         my $host = $cgi->virtual_host || $cgi->server_name;
190
191         my $format = $cgi->param('format');
192         my $flesh_feed = ($format =~ /-full$/o) ? 1 : 0;
193         (my $base_format = $format) =~ s/-full$//o;
194         my ($id,$type,$command,$lib) = ('','','');
195
196         if (!$format) {
197                 my $body = "Content-type: application/xml; charset=utf-8\n\n";
198         
199                 if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))$}o) {
200                         $id = $2;
201                         $lib = uc($3);
202                         $type = 'record';
203                         $type = 'metarecord' if ($1 =~ /^m/o);
204
205                         my $list = $supercat
206                                 ->request("open-ils.supercat.$type.formats")
207                                 ->gather(1);
208
209                         if ($type eq 'record' or $type eq 'isbn') {
210                                 $body .= <<"                            FORMATS";
211 <formats id='$uri'>
212         <format name='opac' type='text/html'/>
213         <format name='html' type='text/html'/>
214         <format name='htmlholdings' type='text/html'/>
215         <format name='html-full' type='text/html'/>
216         <format name='htmlholdings-full' type='text/html'/>
217                                 FORMATS
218                         } elsif ($type eq 'metarecord') {
219                                 $body .= <<"                            FORMATS";
220                                 <formats id='$uri'>
221                                         <format name='opac' type='text/html'/>
222                                 FORMATS
223                         }
224
225                         for my $h (@$list) {
226                                 my ($type) = keys %$h;
227                                 $body .= "\t<format name='$type' type='application/xml'";
228
229                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
230                                         $body .= " $part='$$h{$type}{$part}'"
231                                                 if ($$h{$type}{$part});
232                                 }
233                                 
234                                 $body .= "/>\n";
235
236                                 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
237                                         $body .= "\t<format name='$type-full' type='application/xml'";
238
239                                         for my $part ( qw/namespace_uri docs schema_location/ ) {
240                                                 $body .= " $part='$$h{$type}{$part}'"
241                                                         if ($$h{$type}{$part});
242                                         }
243                                 
244                                         $body .= "/>\n";
245                                 }
246                         }
247
248                         $body .= "</formats>\n";
249
250                 } else {
251                         my $list = $supercat
252                                 ->request("open-ils.supercat.record.formats")
253                                 ->gather(1);
254                                 
255                         push @$list,
256                                 @{ $supercat
257                                         ->request("open-ils.supercat.metarecord.formats")
258                                         ->gather(1);
259                                 };
260
261                         my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
262                         $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
263
264                         $body .= <<"                    FORMATS";
265 <formats>
266         <format name='opac' type='text/html'/>
267         <format name='html' type='text/html'/>
268         <format name='htmlholdings' type='text/html'/>
269         <format name='html-full' type='text/html'/>
270         <format name='htmlholdings-full' type='text/html'/>
271                         FORMATS
272
273
274                         for my $h (@$list) {
275                                 my ($type) = keys %$h;
276                                 $body .= "\t<format name='$type' type='application/xml'";
277
278                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
279                                         $body .= " $part='$$h{$type}{$part}'"
280                                                 if ($$h{$type}{$part});
281                                 }
282                                 
283                                 $body .= "/>\n";
284
285                                 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
286                                         $body .= "\t<format name='$type-full' type='application/xml'";
287
288                                         for my $part ( qw/namespace_uri docs schema_location/ ) {
289                                                 $body .= " $part='$$h{$type}{$part}'"
290                                                         if ($$h{$type}{$part});
291                                         }
292                                 
293                                         $body .= "/>\n";
294                                 }
295                         }
296
297                         $body .= "</formats>\n";
298
299                 }
300                 print $body;
301                 return Apache2::Const::OK;
302         }
303
304         if ($uri =~ m{^tag:[^:]+:([^\/]+)/([^/]+)(?:/(.+))?}o) {
305                 $id = $2;
306                 $lib = uc($3);
307                 $type = 'record';
308                 $type = 'metarecord' if ($1 =~ /^metabib/o);
309                 $type = 'isbn' if ($1 =~ /^isbn/o);
310                 $type = 'call_number' if ($1 =~ /^call_number/o);
311                 $command = 'retrieve';
312                 $command = 'browse' if ($type eq 'call_number');
313         }
314
315         if (!$lib || $lib eq '-') {
316                 $lib = $actor->request(
317                         'open-ils.actor.org_unit_list.search' => parent_ou => undef
318                 )->gather(1)->[0]->shortname;
319         }
320
321         my $lib_object = $actor->request(
322                 'open-ils.actor.org_unit_list.search' => shortname => $lib
323         )->gather(1)->[0];
324         my $lib_id = $lib_object->id;
325
326         my $ou_types = $actor->request( 'open-ils.actor.org_types.retrieve' )->gather(1);
327         my $lib_depth = (grep { $_->id == $lib_object->ou_type } @$ou_types)[0]->depth;
328
329         if ($type eq 'call_number' and $command eq 'browse') {
330                 print "Location: $root/browse/$base_format/call_number/$lib/$id\n\n";
331                 return 302;
332         }
333
334         if ($type eq 'isbn') {
335                 my $rec = $supercat->request('open-ils.supercat.isbn.object.retrieve',$id)->gather(1);
336                 if (!@$rec) {
337                         print "Content-type: text/html; charset=utf-8\n\n";
338                         $apache->custom_response( 404, <<"                      HTML");
339                         <html>
340                                 <head>
341                                         <title>Type [$type] with id [$id] not found!</title>
342                                 </head>
343                                 <body>
344                                         <br/>
345                                         <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
346                                 </body>
347                         </html>
348                         HTML
349                         return 404;
350                 }
351                 $id = $rec->[0]->id;
352                 $type = 'record';
353         }
354
355         if ( !grep
356                { (keys(%$_))[0] eq $base_format }
357                @{ $supercat->request("open-ils.supercat.$type.formats")->gather(1) }
358              and !grep
359                { $_ eq $base_format }
360                qw/opac html htmlholdings/
361         ) {
362                 print "Content-type: text/html; charset=utf-8\n\n";
363                 $apache->custom_response( 406, <<"              HTML");
364                 <html>
365                         <head>
366                                 <title>Invalid format [$format] for type [$type]!</title>
367                         </head>
368                         <body>
369                                 <br/>
370                                 <center>Sorry, format $format is not valid for type $type.</center>
371                         </body>
372                 </html>
373                 HTML
374                 return 406;
375         }
376
377         if ($format eq 'opac') {
378                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id&l=$lib_id&d=$lib_depth\n\n"
379                         if ($type eq 'metarecord');
380                 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id&l=$lib_id&d=$lib_depth\n\n"
381                         if ($type eq 'record');
382                 return 302;
383         } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
384                 my $feed = create_record_feed(
385                         $type,
386                         $format => [ $id ],
387                         $base,
388                         $lib,
389                         $flesh_feed
390                 );
391
392                 if (!$feed->count) {
393                         print "Content-type: text/html; charset=utf-8\n\n";
394                         $apache->custom_response( 404, <<"                      HTML");
395                         <html>
396                                 <head>
397                                         <title>Type [$type] with id [$id] not found!</title>
398                                 </head>
399                                 <body>
400                                         <br/>
401                                         <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
402                                 </body>
403                         </html>
404                         HTML
405                         return 404;
406                 }
407
408                 $feed->root($root);
409                 $feed->creator($host);
410                 $feed->update_ts(gmtime_ISO8601());
411                 $feed->link( unapi => $base) if ($flesh_feed);
412
413                 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
414                 print entityize($feed->toString) . "\n";
415
416                 return Apache2::Const::OK;
417         }
418
419         my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
420         my $data = $req->gather(1);
421
422         if ($req->failed || !$data) {
423                 print "Content-type: text/html; charset=utf-8\n\n";
424                 $apache->custom_response( 404, <<"              HTML");
425                 <html>
426                         <head>
427                                 <title>$type $id not found!</title>
428                         </head>
429                         <body>
430                                 <br/>
431                                 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
432                         </body>
433                 </html>
434                 HTML
435                 return 404;
436         }
437
438         print "Content-type: application/xml; charset=utf-8\n\n$data";
439
440         return Apache2::Const::OK;
441 }
442
443 sub supercat {
444
445         my $apache = shift;
446         return Apache2::Const::DECLINED if (-e $apache->filename);
447
448         my $cgi = new CGI;
449
450         my $add_path = 0;
451         if ( $cgi->server_software !~ m|^Apache/2.2| ) {
452                 my $rel_name = $cgi->url(-relative=>1);
453                 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
454         }
455
456         my $url = $cgi->url(-path_info=>$add_path);
457         my $root = (split 'supercat', $url)[0];
458         my $base = (split 'supercat', $url)[0] . 'supercat';
459         my $unapi = (split 'supercat', $url)[0] . 'unapi';
460
461         my $host = $cgi->virtual_host || $cgi->server_name;
462
463         my $path = $cgi->path_info;
464         my ($id,$type,$format,$command) = reverse split '/', $path;
465         my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
466         (my $base_format = $format) =~ s/-full$//o;
467         
468         if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
469                 print "Content-type: application/xml; charset=utf-8\n";
470                 if ($1) {
471                         my $list = $supercat
472                                 ->request("open-ils.supercat.$1.formats")
473                                 ->gather(1);
474
475                         print "\n";
476
477                         print "<formats>
478                                    <format>
479                                      <name>opac</name>
480                                      <type>text/html</type>
481                                    </format>";
482
483                         if ($1 eq 'record' or $1 eq 'isbn') {
484                                 print "<format>
485                                      <name>htmlholdings</name>
486                                      <type>text/html</type>
487                                    </format>
488                                    <format>
489                                      <name>html</name>
490                                      <type>text/html</type>
491                                    </format>
492                                    <format>
493                                      <name>htmlholdings-full</name>
494                                      <type>text/html</type>
495                                    </format>
496                                    <format>
497                                      <name>html-full</name>
498                                      <type>text/html</type>
499                                    </format>";
500                         }
501
502                         for my $h (@$list) {
503                                 my ($type) = keys %$h;
504                                 print "<format><name>$type</name><type>application/xml</type>";
505
506                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
507                                         print "<$part>$$h{$type}{$part}</$part>"
508                                                 if ($$h{$type}{$part});
509                                 }
510                                 
511                                 print '</format>';
512
513                                 if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
514                                         print "<format><name>$type-full</name><type>application/xml</type>";
515
516                                         for my $part ( qw/namespace_uri docs schema_location/ ) {
517                                                 print "<$part>$$h{$type}{$part}</$part>"
518                                                         if ($$h{$type}{$part});
519                                         }
520                                 
521                                         print '</format>';
522                                 }
523
524                         }
525
526                         print "</formats>\n";
527
528                         return Apache2::Const::OK;
529                 }
530
531                 my $list = $supercat
532                         ->request("open-ils.supercat.record.formats")
533                         ->gather(1);
534                                 
535                 push @$list,
536                         @{ $supercat
537                                 ->request("open-ils.supercat.metarecord.formats")
538                                 ->gather(1);
539                         };
540
541                 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
542                 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
543
544                 print "\n<formats>
545                            <format>
546                              <name>opac</name>
547                              <type>text/html</type>
548                            </format>
549                            <format>
550                              <name>htmlholdings</name>
551                              <type>text/html</type>
552                            </format>
553                            <format>
554                              <name>html</name>
555                              <type>text/html</type>
556                            </format>
557                            <format>
558                              <name>htmlholdings-full</name>
559                              <type>text/html</type>
560                            </format>
561                            <format>
562                              <name>html-full</name>
563                              <type>text/html</type>
564                            </format>";
565
566                 for my $h (@$list) {
567                         my ($type) = keys %$h;
568                         print "<format><name>$type</name><type>application/xml</type>";
569
570                         for my $part ( qw/namespace_uri docs schema_location/ ) {
571                                 print "<$part>$$h{$type}{$part}</$part>"
572                                         if ($$h{$type}{$part});
573                         }
574                         
575                         print '</format>';
576
577                         if (OpenILS::WWW::SuperCat::Feed->exists($type)) {
578                                 print "<format><name>$type-full</name><type>application/xml</type>";
579
580                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
581                                         print "<$part>$$h{$type}{$part}</$part>"
582                                                 if ($$h{$type}{$part});
583                                 }
584                                 
585                                 print '</format>';
586                         }
587
588                 }
589
590                 print "</formats>\n";
591
592
593                 return Apache2::Const::OK;
594         }
595
596         if ($format eq 'opac') {
597                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
598                         if ($type eq 'metarecord');
599                 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
600                         if ($type eq 'record');
601                 return 302;
602         } elsif (OpenILS::WWW::SuperCat::Feed->exists($base_format)) {
603                 my $feed = create_record_feed(
604                         $type,
605                         $format => [ $id ],
606                         undef, undef,
607                         $flesh_feed
608                 );
609
610                 $feed->root($root);
611                 $feed->creator($host);
612                 $feed->update_ts(gmtime_ISO8601());
613                 $feed->link( unapi => $base) if ($flesh_feed);
614
615                 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
616                 print entityize($feed->toString) . "\n";
617
618                 return Apache2::Const::OK;
619         }
620
621         my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
622         $req->wait_complete;
623
624         if ($req->failed) {
625                 print "Content-type: text/html; charset=utf-8\n\n";
626                 $apache->custom_response( 404, <<"              HTML");
627                 <html>
628                         <head>
629                                 <title>$type $id not found!</title>
630                         </head>
631                         <body>
632                                 <br/>
633                                 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
634                         </body>
635                 </html>
636                 HTML
637                 return 404;
638         }
639
640         print "Content-type: application/xml; charset=utf-8\n\n";
641         print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
642
643         return Apache2::Const::OK;
644 }
645
646
647 sub bookbag_feed {
648         my $apache = shift;
649         return Apache2::Const::DECLINED if (-e $apache->filename);
650
651         my $cgi = new CGI;
652
653         my $year = (gmtime())[5] + 1900;
654         my $host = $cgi->virtual_host || $cgi->server_name;
655
656         my $add_path = 0;
657         if ( $cgi->server_software !~ m|^Apache/2.2| ) {
658                 my $rel_name = $cgi->url(-relative=>1);
659                 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
660         }
661
662         my $url = $cgi->url(-path_info=>$add_path);
663         my $root = (split 'feed', $url)[0];
664         my $base = (split 'bookbag', $url)[0] . 'bookbag';
665         my $unapi = (split 'feed', $url)[0] . 'unapi';
666
667
668         my $path = $cgi->path_info;
669         #warn "URL breakdown: $url -> $root -> $base -> $path -> $unapi";
670
671         my ($id,$type) = reverse split '/', $path;
672         my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
673
674         my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
675         return Apache2::Const::NOT_FOUND unless($bucket);
676
677         my $bucket_tag = "tag:$host,$year:record_bucket/$id";
678         if ($type eq 'opac') {
679                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
680                         join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
681                         "\n\n";
682                 return 302;
683         }
684
685         my $feed = create_record_feed(
686                 'record',
687                 $type,
688                 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
689                 $unapi,
690                 undef,
691                 $flesh_feed
692         );
693         $feed->root($root);
694
695         $feed->title("Items in Book Bag [".$bucket->name."]");
696         $feed->creator($host);
697         $feed->update_ts(gmtime_ISO8601());
698
699         $feed->link(rss => $base . "/rss2-full/$id" => 'application/rss+xml');
700         $feed->link(alternate => $base . "/atom-full/$id" => 'application/atom+xml');
701         $feed->link(html => $base . "/html-full/$id" => 'text/html');
702         $feed->link(unapi => $unapi);
703
704         $feed->link(
705                 OPAC =>
706                 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
707                         join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
708                 'text/html'
709         );
710
711
712         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
713         print entityize($feed->toString) . "\n";
714
715         return Apache2::Const::OK;
716 }
717
718 sub changes_feed {
719         my $apache = shift;
720         return Apache2::Const::DECLINED if (-e $apache->filename);
721
722         my $cgi = new CGI;
723
724         my $year = (gmtime())[5] + 1900;
725         my $host = $cgi->virtual_host || $cgi->server_name;
726
727         my $add_path = 0;
728         if ( $cgi->server_software !~ m|^Apache/2.2| ) {
729                 my $rel_name = $cgi->url(-relative=>1);
730                 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
731         }
732
733         my $url = $cgi->url(-path_info=>$add_path);
734         my $root = (split 'feed', $url)[0];
735         my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
736         my $unapi = (split 'feed', $url)[0] . 'unapi';
737
738         my $path = $cgi->path_info;
739         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
740
741         $path =~ s/^\/(?:feed\/)?freshmeat\///og;
742         
743         my ($type,$rtype,$axis,$limit,$date) = split '/', $path;
744         my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
745         $limit ||= 10;
746
747         my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
748
749         #if ($type eq 'opac') {
750         #       print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
751         #               join('&', map { "rl=" . $_ } @$list) .
752         #               "\n\n";
753         #       return 302;
754         #}
755
756         my $feed = create_record_feed( 'record', $type, $list, $unapi, undef, $flesh_feed);
757         $feed->root($root);
758
759         if ($date) {
760                 $feed->title("Up to $limit recent $rtype ${axis}s from $date forward");
761         } else {
762                 $feed->title("$limit most recent $rtype ${axis}s");
763         }
764
765         $feed->creator($host);
766         $feed->update_ts(gmtime_ISO8601());
767
768         $feed->link(rss => $base . "/rss2-full/$rtype/$axis/$limit/$date" => 'application/rss+xml');
769         $feed->link(alternate => $base . "/atom-full/$rtype/$axis/$limit/$date" => 'application/atom+xml');
770         $feed->link(html => $base . "/html-full/$rtype/$axis/$limit/$date" => 'text/html');
771         $feed->link(unapi => $unapi);
772
773         $feed->link(
774                 OPAC =>
775                 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
776                         join('&', map { 'rl=' . $_} @$list ),
777                 'text/html'
778         );
779
780
781         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
782         print entityize($feed->toString) . "\n";
783
784         return Apache2::Const::OK;
785 }
786
787 sub opensearch_osd {
788         my $version = shift;
789         my $lib = shift;
790         my $class = shift;
791         my $base = shift;
792
793         if ($version eq '1.0') {
794                 print <<OSD;
795 Content-type: application/opensearchdescription+xml; charset=utf-8
796
797 <?xml version="1.0" encoding="UTF-8"?>
798 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
799   <Url>$base/1.0/$lib/-/$class/?searchTerms={searchTerms}&startPage={startPage}&amp;startIndex={startIndex}&amp;count={count}</Url>
800   <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
801   <ShortName>$lib</ShortName>
802   <LongName>Search $lib</LongName>
803   <Description>Search the $lib OPAC by $class.</Description>
804   <Tags>$lib book library</Tags>
805   <SampleSearch>harry+potter</SampleSearch>
806   <Developer>Mike Rylander for GPLS/PINES</Developer>
807   <Contact>feedback\@open-ils.org</Contact>
808   <SyndicationRight>open</SyndicationRight>
809   <AdultContent>false</AdultContent>
810 </OpenSearchDescription>
811 OSD
812         } else {
813                 print <<OSD;
814 Content-type: application/opensearchdescription+xml; charset=utf-8
815
816 <?xml version="1.0" encoding="UTF-8"?>
817 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
818   <ShortName>$lib</ShortName>
819   <Description>Search the $lib OPAC by $class.</Description>
820   <Tags>$lib book library</Tags>
821   <Url type="application/rss+xml"
822        template="$base/1.1/$lib/rss2-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;searchLang={language?}"/>
823   <Url type="application/atom+xml"
824        template="$base/1.1/$lib/atom-full/$class/?searchTerms={searchTerms}&startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;searchLang={language?}"/>
825   <Url type="application/x-mods3+xml"
826        template="$base/1.1/$lib/mods3/$class/?searchTerms={searchTerms}&startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;searchLang={language?}"/>
827   <Url type="application/x-mods+xml"
828        template="$base/1.1/$lib/mods/$class/?searchTerms={searchTerms}&startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;searchLang={language?}"/>
829   <Url type="application/x-marcxml+xml"
830        template="$base/1.1/$lib/marcxml/$class/?searchTerms={searchTerms}&startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;searchLang={language?}"/>
831   <LongName>Search $lib</LongName>
832   <Query role="example" searchTerms="harry+potter" />
833   <Developer>Mike Rylander for GPLS/PINES</Developer>
834   <Contact>feedback\@open-ils.org</Contact>
835   <SyndicationRight>open</SyndicationRight>
836   <AdultContent>false</AdultContent>
837   <Language>en-US</Language>
838   <OutputEncoding>UTF-8</OutputEncoding>
839   <InputEncoding>UTF-8</InputEncoding>
840 </OpenSearchDescription>
841 OSD
842         }
843
844         return Apache2::Const::OK;
845 }
846
847 sub opensearch_feed {
848         my $apache = shift;
849         return Apache2::Const::DECLINED if (-e $apache->filename);
850
851         my $cgi = new CGI;
852         my $year = (gmtime())[5] + 1900;
853
854         my $host = $cgi->virtual_host || $cgi->server_name;
855
856         my $add_path = 0;
857         if ( $cgi->server_software !~ m|^Apache/2.2| ) {
858                 my $rel_name = $cgi->url(-relative=>1);
859                 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
860         }
861
862         my $url = $cgi->url(-path_info=>$add_path);
863         my $root = (split 'opensearch', $url)[0];
864         my $base = (split 'opensearch', $url)[0] . 'opensearch';
865         my $unapi = (split 'opensearch', $url)[0] . 'unapi';
866
867         my $path = $cgi->path_info;
868         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
869
870         if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
871                 
872                 my $version = $1;
873                 my $lib = uc($2);
874                 my $class = $3;
875
876                 if (!$lib || $lib eq '-') {
877                         $lib = $actor->request(
878                                 'open-ils.actor.org_unit_list.search' => parent_ou => undef
879                         )->gather(1)->[0]->shortname;
880                 }
881
882                 if ($class eq '-') {
883                         $class = 'keyword';
884                 }
885
886                 return opensearch_osd($version, $lib, $class, $base);
887         }
888
889
890         my $page = $cgi->param('startPage') || 1;
891         my $offset = $cgi->param('startIndex') || 1;
892         my $limit = $cgi->param('count') || 10;
893
894         $page = 1 if ($page !~ /^\d+$/);
895         $offset = 1 if ($offset !~ /^\d+$/);
896         $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
897
898         if ($page > 1) {
899                 $offset = ($page - 1) * $limit;
900         } else {
901                 $offset -= 1;
902         }
903
904         my ($version,$org,$type,$class,$terms,$sort,$sortdir,$lang);
905         (undef,$version,$org,$type,$class,$terms,$sort,$sortdir,$lang) = split '/', $path;
906
907         $lang ||= $cgi->param('searchLang');
908         $sort ||= $cgi->param('searchSort');
909         $sortdir ||= $cgi->param('searchSortDir');
910         $terms ||= $cgi->param('searchTerms');
911         $class ||= $cgi->param('searchClass') || '-';
912         $type ||= $cgi->param('responseType') || '-';
913         $org ||= $cgi->param('searchOrg') || '-';
914
915         if ($version eq '1.0') {
916                 $type = 'rss2';
917         } elsif ($type eq '-') {
918                 $type = 'atom';
919         }
920         my $flesh_feed = ($type =~ /-full$/o) ? 1 : 0;
921
922         $terms = decode_utf8($terms);
923         $terms =~ s/\+/ /go;
924         $terms =~ s/'//go;
925         my $term_copy = $terms;
926
927         my $complex_terms = 0;
928         if ($terms eq 'help') {
929                 print $cgi->header(-type => 'text/html');
930                 print <<"               HTML";
931                         <html>
932                          <head>
933                           <title>just type something!</title>
934                          </head>
935                          <body>
936                           <p>You are in a maze of dark, twisty stacks, all alike.</p>
937                          </body>
938                         </html>
939                 HTML
940                 return Apache2::Const::OK;
941         }
942
943         my $cache_key = '';
944         my $searches = {};
945         while ($term_copy =~ s/((?:keyword|title|author|subject|series|site|dir|sort|lang):[^:]+)$//so) {
946                 my ($c,$t) = split ':' => $1;
947                 if ($c eq 'site') {
948                         $org = $t;
949                         $org =~ s/^\s*//o;
950                         $org =~ s/\s*$//o;
951                 } elsif ($c eq 'sort') {
952                         ($sort = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
953                 } elsif ($c eq 'dir') {
954                         ($sortdir = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
955                 } elsif ($c eq 'lang') {
956                         ($lang = lc($t)) =~ s/^\s*(\w+)\s*$/$1/go;
957                 } else {
958                         $$searches{$c}{term} .= ' '.$t;
959                         $cache_key .= $c . $t;
960                         $complex_terms = 1;
961                 }
962         }
963
964         if ($term_copy) {
965                 no warnings;
966                 $class = 'keyword' if ($class eq '-');
967                 $$searches{$class}{term} .= " $term_copy";
968                 $cache_key .= $class . $term_copy;
969         }
970
971         my $org_unit;
972         if ($org eq '-') {
973                 $org_unit = $actor->request(
974                         'open-ils.actor.org_unit_list.search' => parent_ou => undef
975                 )->gather(1);
976         } else {
977                 $org_unit = $actor->request(
978                         'open-ils.actor.org_unit_list.search' => shortname => uc($org)
979                 )->gather(1);
980         }
981
982         $cache_key .= $org.$sort.$sortdir.$lang;
983
984         my $rs_name = $cgi->cookie('os_session');
985         my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
986
987         my $recs;
988         if (!($recs = $$cached_res{os_results}{$cache_key})) {
989                 $rs_name = $cgi->remote_host . '::' . rand(time);
990                 $recs = $search->request(
991                         'open-ils.search.biblio.multiclass' => {
992                                 searches        => $searches,
993                                 org_unit        => $org_unit->[0]->id,
994                                 offset          => 0,
995                                 limit           => 5000,
996                                 ($sort ?    ( 'sort'     => $sort    ) : ()),
997                                 ($sortdir ? ( 'sort_dir' => $sortdir ) : ($sort ? (sort_dir => 'asc') : (sort_dir => 'desc') )),
998                                 ($lang ?    ( 'language' => $lang    ) : ()),
999                         }
1000                 )->gather(1);
1001                 try {
1002                         $$cached_res{os_results}{$cache_key} = $recs;
1003                         OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
1004                 } catch Error with {
1005                         warn "supercat unable to store IDs in memcache server\n";
1006                         $logger->error("supercat unable to store IDs in memcache server");
1007                 };
1008         }
1009
1010         my $feed = create_record_feed(
1011                 'record',
1012                 $type,
1013                 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
1014                 $unapi,
1015                 $org,
1016                 $flesh_feed
1017         );
1018         $feed->root($root);
1019         $feed->lib($org);
1020         $feed->search($terms);
1021         $feed->class($class);
1022
1023         if ($complex_terms) {
1024                 $feed->title("Search results for [$terms] at ".$org_unit->[0]->name);
1025         } else {
1026                 $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
1027         }
1028
1029         $feed->creator($host);
1030         $feed->update_ts(gmtime_ISO8601());
1031
1032         $feed->_create_node(
1033                 $feed->{item_xpath},
1034                 'http://a9.com/-/spec/opensearch/1.1/',
1035                 'totalResults',
1036                 $recs->{count},
1037         );
1038
1039         $feed->_create_node(
1040                 $feed->{item_xpath},
1041                 'http://a9.com/-/spec/opensearch/1.1/',
1042                 'startIndex',
1043                 $offset + 1,
1044         );
1045
1046         $feed->_create_node(
1047                 $feed->{item_xpath},
1048                 'http://a9.com/-/spec/opensearch/1.1/',
1049                 'itemsPerPage',
1050                 $limit,
1051         );
1052
1053         $feed->link(
1054                 next =>
1055                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
1056                 'application/opensearch+xml'
1057         ) if ($offset + $limit < $recs->{count});
1058
1059         $feed->link(
1060                 previous =>
1061                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
1062                 'application/opensearch+xml'
1063         ) if ($offset);
1064
1065         $feed->link(
1066                 self =>
1067                 $base .  "/$version/$org/$type/$class?searchTerms=$terms" =>
1068                 'application/opensearch+xml'
1069         );
1070
1071         $feed->link(
1072                 rss =>
1073                 $base .  "/$version/$org/rss2-full/$class?searchTerms=$terms" =>
1074                 'application/rss+xml'
1075         );
1076
1077         $feed->link(
1078                 alternate =>
1079                 $base .  "/$version/$org/atom-full/$class?searchTerms=$terms" =>
1080                 'application/atom+xml'
1081         );
1082
1083         $feed->link(
1084                 'html' =>
1085                 $base .  "/$version/$org/html/$class?searchTerms=$terms" =>
1086                 'text/html'
1087         );
1088
1089         $feed->link(
1090                 'html-full' =>
1091                 $base .  "/$version/$org/html-full/$class?searchTerms=$terms" =>
1092                 'text/html'
1093         );
1094
1095         $feed->link( 'unapi-server' => $unapi);
1096
1097 #       $feed->link(
1098 #               opac =>
1099 #               $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
1100 #                       join('&', map { 'rl=' . $_->[0] } grep { ref $_ && defined $_->[0] } @{$recs->{ids}} ),
1101 #               'text/html'
1102 #       );
1103
1104         print $cgi->header(
1105                 -type           => $feed->type,
1106                 -charset        => 'UTF-8',
1107                 -cookie         => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
1108         );
1109
1110         print entityize($feed->toString) . "\n";
1111
1112         return Apache2::Const::OK;
1113 }
1114
1115 sub create_record_feed {
1116         my $search = shift;
1117         my $type = shift;
1118         my $records = shift;
1119         my $unapi = shift;
1120
1121         my $lib = uc(shift()) || '-';
1122         my $flesh = shift;
1123         $flesh = 1 if (!defined($flesh));
1124
1125         my $cgi = new CGI;
1126         my $base = $cgi->url;
1127         my $host = $cgi->virtual_host || $cgi->server_name;
1128
1129         my $year = (gmtime())[5] + 1900;
1130
1131         my $flesh_feed = ($type =~ s/-full$//o) ? 1 : 0;
1132
1133         my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
1134         $feed->base($base) if ($flesh);
1135         $feed->unapi($unapi) if ($flesh);
1136
1137         $type = 'atom' if ($type eq 'html');
1138         $type = 'marcxml' if ($type eq 'htmlholdings');
1139
1140         #$records = $supercat->request( "open-ils.supercat.record.object.retrieve", $records )->gather(1);
1141
1142         my $count = 0;
1143         for my $record (@$records) {
1144                 next unless($record);
1145
1146                 #my $rec = $record->id;
1147                 my $rec = $record;
1148
1149                 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
1150                 $item_tag = "tag:$host,$year:isbn/$rec/$lib" if ($search eq 'isbn');
1151
1152                 my $xml = $supercat->request(
1153                         "open-ils.supercat.$search.$type.retrieve",
1154                         $rec
1155                 )->gather(1);
1156                 next unless $xml;
1157
1158                 my $node = $feed->add_item($xml);
1159                 next unless $node;
1160
1161                 if ($lib && $type eq 'marcxml' &&  $flesh) {
1162                         $xml = $supercat->request( "open-ils.supercat.$search.holdings_xml.retrieve", $rec, $lib )->gather(1);
1163                         $node->add_holdings($xml);
1164                 }
1165
1166                 $node->id($item_tag) if ($flesh);
1167                 #$node->update_ts(clense_ISO8601($record->edit_date));
1168                 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings-full" => 'text/html') if ($flesh);
1169                 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac") if ($flesh);
1170                 $node->link(unapi => $feed->unapi . "?id=$item_tag") if ($flesh);
1171                 $node->link('unapi-id' => $item_tag) if ($flesh);
1172         }
1173
1174         return $feed;
1175 }
1176
1177 sub entityize {
1178         my $stuff = NFC(shift());
1179         $stuff =~ s/&(?!\S+;)/&amp;/gso;
1180         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1181         return $stuff;
1182 }
1183
1184 sub string_browse {
1185         my $apache = shift;
1186         return Apache2::Const::DECLINED if (-e $apache->filename);
1187
1188         my $cgi = new CGI;
1189         my $year = (gmtime())[5] + 1900;
1190
1191         my $host = $cgi->virtual_host || $cgi->server_name;
1192
1193         my $add_path = 0;
1194         if ( $cgi->server_software !~ m|^Apache/2.2| ) {
1195                 my $rel_name = $cgi->url(-relative=>1);
1196                 $add_path = 1 if ($cgi->url(-path_info=>1) !~ /$rel_name$/);
1197         }
1198
1199         my $url = $cgi->url(-path_info=>$add_path);
1200         my $root = (split 'browse', $url)[0];
1201         my $base = (split 'browse', $url)[0] . 'browse';
1202         my $unapi = (split 'browse', $url)[0] . 'unapi';
1203
1204         my $path = $cgi->path_info;
1205         $path =~ s/^\///og;
1206
1207         my ($format,$axis,$site,$string,$page,$page_size) = split '/', $path;
1208         #warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1209
1210         $site ||= $cgi->param('searchOrg');
1211         $page ||= $cgi->param('startPage') || 0;
1212         $page_size ||= $cgi->param('count') || 9;
1213
1214         $page = 0 if ($page !~ /^-?\d+$/);
1215
1216         my $prev = join('/', $base,$format,$axis,$site,$string,$page - 1,$page_size);
1217         my $next = join('/', $base,$format,$axis,$site,$string,$page + 1,$page_size);
1218
1219         unless ($string and $axis and grep { $axis eq $_ } keys %browse_types) {
1220                 warn "something's wrong...";
1221                 warn " >>> $format -> $axis -> $site -> $string -> $page -> $page_size ";
1222                 return undef;
1223         }
1224
1225         $string = decode_utf8($string);
1226         $string =~ s/\+/ /go;
1227         $string =~ s/'//go;
1228
1229         my $tree = $supercat->request(
1230                 "open-ils.supercat.$axis.browse",
1231                 $string,
1232                 $site,
1233                 $page_size,
1234                 $page
1235         )->gather(1);
1236
1237         my ($header,$content) = $browse_types{$axis}{$format}->($tree,$prev,$next);
1238         print $header.$content;
1239         return Apache2::Const::OK;
1240 }
1241
1242 1;