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