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