]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/SuperCat.pm
improving the dumpac
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / SuperCat.pm
1 package OpenILS::WWW::SuperCat;
2 use strict; use warnings;
3
4 use Apache2 ();
5 use Apache2::Log;
6 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
7 use APR::Const    -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use Apache2::RequestUtil;
11 use CGI;
12 use Data::Dumper;
13
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::Utils qw/:datetime/;
16 use OpenSRF::Utils::Cache;
17 use OpenSRF::System;
18 use OpenSRF::AppSession;
19 use XML::LibXML;
20
21 use Encode;
22 use Unicode::Normalize;
23 use OpenILS::Utils::Fieldmapper;
24 use OpenILS::WWW::SuperCat::Feed;
25
26
27 # set the bootstrap config when this module is loaded
28 my ($bootstrap, $supercat, $actor, $parser, $search);
29
30 sub import {
31         my $self = shift;
32         $bootstrap = shift;
33 }
34
35
36 sub child_init {
37         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
38         $supercat = OpenSRF::AppSession->create('open-ils.supercat');
39         $actor = OpenSRF::AppSession->create('open-ils.actor');
40         $search = OpenSRF::AppSession->create('open-ils.search');
41         $parser = new XML::LibXML;
42 }
43
44 sub oisbn {
45
46         my $apache = shift;
47         return Apache2::Const::DECLINED if (-e $apache->filename);
48
49         (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
50
51         my $list = $supercat
52                 ->request("open-ils.supercat.oisbn", $isbn)
53                 ->gather(1);
54
55         print "Content-type: application/xml; charset=utf-8\n\n";
56         print "<?xml version='1.0' encoding='UTF-8' ?>\n";
57
58         unless (exists $$list{metarecord}) {
59                 print '<idlist/>';
60                 return Apache2::Const::OK;
61         }
62
63         print "<idlist metarecord='$$list{metarecord}'>\n";
64
65         for ( keys %{ $$list{record_list} } ) {
66                 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
67                 print "  <isbn record='$_'>$o</isbn>\n"
68         }
69
70         print "</idlist>\n";
71
72         return Apache2::Const::OK;
73 }
74
75 sub unapi {
76
77         my $apache = shift;
78         return Apache2::Const::DECLINED if (-e $apache->filename);
79
80         my $cgi = new CGI;
81         my $rel_name = quotemeta($cgi->url(-relative=>1));
82
83         my $add_path = 1;
84         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
85
86
87         my $url = $cgi->url(-path_info=>$add_path);
88         my $root = (split 'unapi', $url)[0];
89         my $base = (split 'unapi', $url)[0] . 'unapi';
90
91
92         my $uri = $cgi->param('id') || '';
93         my $host = $cgi->virtual_host || $cgi->server_name;
94
95         my $format = $cgi->param('format');
96         my ($id,$type,$command,$lib) = ('','','');
97
98         if (!$format) {
99                 print "Content-type: application/xml; charset=utf-8\n";
100         
101                 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
102                         $id = $2;
103                         $lib = $3;
104                         $type = 'record';
105                         $type = 'metarecord' if ($1 =~ /^m/o);
106
107                         my $list = $supercat
108                         ->request("open-ils.supercat.$type.formats")
109                                 ->gather(1);
110
111                         print "\n";
112
113                         my $body = "<formats id='$uri'><format name='opac' type='text/html'/>";
114
115                         for my $h (@$list) {
116                                 my ($type) = keys %$h;
117                                 $body .= "<format name='$type' type='application/xml'";
118
119                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
120                                         $body .= " $part='$$h{$type}{$part}'"
121                                                 if ($$h{$type}{$part});
122                                 }
123                                 
124                                 $body .= '/>';
125                         }
126
127                         $body .= "</formats>\n";
128
129                         $apache->custom_response( 300, $body);
130                         return 300;
131                 } else {
132                         my $list = $supercat
133                                 ->request("open-ils.supercat.record.formats")
134                                 ->gather(1);
135                                 
136                         push @$list,
137                                 @{ $supercat
138                                         ->request("open-ils.supercat.metarecord.formats")
139                                         ->gather(1);
140                                 };
141
142                         my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
143                         $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
144
145                         print "<formats><format name='opac' type='text/html'/>";
146
147                         for my $h (@$list) {
148                                 my ($type) = keys %$h;
149                                 print "<format name='$type' type='application/xml'";
150
151                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
152                                         print " $part='$$h{$type}{$part}'"
153                                                 if ($$h{$type}{$part});
154                                 }
155                                 
156                                 print '/>';
157                         }
158
159                         print "</formats>\n";
160
161
162                         return Apache2::Const::OK;
163                 }
164         }
165
166                 
167         if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)(?:/(.+))?}o) {
168                 $id = $2;
169                 $lib = $3;
170                 $type = 'record';
171                 $type = 'metarecord' if ($1 =~ /^m/o);
172                 $command = 'retrieve';
173         }
174
175         if ($format eq 'opac') {
176                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
177                         if ($type eq 'metarecord');
178                 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
179                         if ($type eq 'record');
180                 return 302;
181         } else {
182         #} elsif ($format =~ /^html/o) {
183                 my $feed = create_record_feed(
184                         $format => [ $id ],
185                         $base,
186                         $lib,
187                 );
188
189                 $feed->root($root);
190                 $feed->creator($host);
191                 $feed->update_ts(gmtime_ISO8601());
192
193                 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
194                 print entityize($feed->toString) . "\n";
195
196                 return Apache2::Const::OK;
197         }
198
199         my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
200         $req->wait_complete;
201
202         if ($req->failed) {
203                 print "Content-type: text/html; charset=utf-8\n\n";
204                 $apache->custom_response( 404, <<"              HTML");
205                 <html>
206                         <head>
207                                 <title>$type $id not found!</title>
208                         </head>
209                         <body>
210                                 <br/>
211                                 <center>Sorry, we couldn't $command a $type with the id of $id in format $format.</center>
212                         </body>
213                 </html>
214                 HTML
215                 return 404;
216         }
217
218         print "Content-type: application/xml; charset=utf-8\n\n";
219         print $req->gather(1);
220
221         return Apache2::Const::OK;
222 }
223
224 sub supercat {
225
226         my $apache = shift;
227         return Apache2::Const::DECLINED if (-e $apache->filename);
228
229         my $cgi = new CGI;
230
231         my $rel_name = quotemeta($cgi->url(-relative=>1));
232
233         my $add_path = 1;
234         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
235
236
237         my $url = $cgi->url(-path_info=>$add_path);
238         my $root = (split 'supercat', $url)[0];
239         my $base = (split 'supercat', $url)[0] . 'supercat';
240         my $path = (split 'supercat', $url)[1];
241         my $unapi = (split 'supercat', $url)[0] . 'unapi';
242
243         my $host = $cgi->virtual_host || $cgi->server_name;
244
245         my ($id,$type,$format,$command) = reverse split '/', $path;
246
247         
248         if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
249                 print "Content-type: application/xml; charset=utf-8\n";
250                 if ($1) {
251                         my $list = $supercat
252                                 ->request("open-ils.supercat.$1.formats")
253                                 ->gather(1);
254
255                         print "\n";
256
257                         print "<formats>
258                                    <format>
259                                      <name>opac</name>
260                                      <type>text/html</type>
261                                    </format>";
262
263                         for my $h (@$list) {
264                                 my ($type) = keys %$h;
265                                 print "<format><name>$type</name><type>application/xml</type>";
266
267                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
268                                         print "<$part>$$h{$type}{$part}</$part>"
269                                                 if ($$h{$type}{$part});
270                                 }
271                                 
272                                 print '</format>';
273                         }
274
275                         print "</formats>\n";
276
277                         return Apache2::Const::OK;
278                 }
279
280                 my $list = $supercat
281                         ->request("open-ils.supercat.record.formats")
282                         ->gather(1);
283                                 
284                 push @$list,
285                         @{ $supercat
286                                 ->request("open-ils.supercat.metarecord.formats")
287                                 ->gather(1);
288                         };
289
290                 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
291                 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
292
293                 print "\n<formats>
294                            <format>
295                              <name>opac</name>
296                              <type>text/html</type>
297                            </format>";
298
299                 for my $h (@$list) {
300                         my ($type) = keys %$h;
301                         print "<format><name>$type</name><type>application/xml</type>";
302
303                         for my $part ( qw/namespace_uri docs schema_location/ ) {
304                                 print "<$part>$$h{$type}{$part}</$part>"
305                                         if ($$h{$type}{$part});
306                         }
307                         
308                         print '</format>';
309                 }
310
311                 print "</formats>\n";
312
313
314                 return Apache2::Const::OK;
315         }
316
317         if ($format eq 'opac') {
318                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
319                         if ($type eq 'metarecord');
320                 print "Location: $root/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
321                         if ($type eq 'record');
322                 return 302;
323         } elsif ($format =~ /^html/o) {
324                 my $feed = create_record_feed( $format => [ $id ], $unapi,);
325
326                 $feed->root($root);
327                 $feed->creator($host);
328                 $feed->update_ts(gmtime_ISO8601());
329
330                 print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
331                 print entityize($feed->toString) . "\n";
332
333                 return Apache2::Const::OK;
334         }
335
336         my $req = $supercat->request("open-ils.supercat.$type.$format.$command",$id);
337         $req->wait_complete;
338
339         if ($req->failed) {
340                 print "Content-type: text/html; charset=utf-8\n\n";
341                 $apache->custom_response( 404, <<"              HTML");
342                 <html>
343                         <head>
344                                 <title>$type $id not found!</title>
345                         </head>
346                         <body>
347                                 <br/>
348                                 <center>Sorry, we couldn't $command a $type with the id of $id.</center>
349                         </body>
350                 </html>
351                 HTML
352                 return 404;
353         }
354
355         print "Content-type: application/xml; charset=utf-8\n\n";
356         print entityize( $parser->parse_string( $req->gather(1) )->documentElement->toString );
357
358         return Apache2::Const::OK;
359 }
360
361
362 sub bookbag_feed {
363         my $apache = shift;
364         return Apache2::Const::DECLINED if (-e $apache->filename);
365
366         my $cgi = new CGI;
367
368         my $year = (gmtime())[5] + 1900;
369         my $host = $cgi->virtual_host || $cgi->server_name;
370
371         my $rel_name = quotemeta($cgi->url(-relative=>1));
372
373         my $add_path = 1;
374         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
375
376         my $url = $cgi->url(-path_info=>$add_path);
377         my $root = (split 'feed', $url)[0];
378         my $base = (split 'bookbag', $url)[0] . 'bookbag';
379         my $path = (split 'bookbag', $url)[1];
380         my $unapi = (split 'feed', $url)[0] . 'unapi';
381
382
383         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
384
385         my ($id,$type) = reverse split '/', $path;
386
387         my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
388         return Apache2::Const::NOT_FOUND unless($bucket);
389
390         my $bucket_tag = "tag:$host,$year:record_bucket/$id";
391         if ($type eq 'opac') {
392                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
393                         join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
394                         "\n\n";
395                 return 302;
396         }
397
398         my $feed = create_record_feed(
399                 $type,
400                 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
401                 $unapi,
402         );
403         $feed->root($root);
404
405         $feed->title("Items in Book Bag [".$bucket->name."]");
406         $feed->creator($host);
407         $feed->update_ts(gmtime_ISO8601());
408
409         $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
410         $feed->link(rss2 => $base . "/rss2/$id");
411         $feed->link(html => $base . "/html/$id" => 'text/html');
412         $feed->link(unapi => $unapi);
413
414         $feed->link(
415                 OPAC =>
416                 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
417                         join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
418                 'text/html'
419         );
420
421
422         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
423         print entityize($feed->toString) . "\n";
424
425         return Apache2::Const::OK;
426 }
427
428 sub changes_feed {
429         my $apache = shift;
430         return Apache2::Const::DECLINED if (-e $apache->filename);
431
432         my $cgi = new CGI;
433
434         my $year = (gmtime())[5] + 1900;
435         my $host = $cgi->virtual_host || $cgi->server_name;
436
437         my $rel_name = quotemeta($cgi->url(-relative=>1));
438
439         my $add_path = 1;
440         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
441
442         my $url = $cgi->url(-path_info=>$add_path);
443         my $root = (split 'feed', $url)[0];
444         my $base = (split 'freshmeat', $url)[0] . 'freshmeat';
445         my $path = (split 'freshmeat', $url)[1];
446         my $unapi = (split 'feed', $url)[0] . 'unapi';
447
448
449         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
450
451         $path =~ s/^\///og;
452         
453         my ($type,$rtype,$axis,$date,$limit) = split '/', $path;
454         $date ||= 'today';
455         $limit ||= 10;
456
457         my $list = $supercat->request("open-ils.supercat.$rtype.record.$axis.recent", $date, $limit)->gather(1);
458
459         if ($type eq 'opac') {
460                 print "Location: $root/../../en-US/skin/default/xml/rresult.xml?rt=list&" .
461                         join('&', map { "rl=" . $_ } @$list) .
462                         "\n\n";
463                 return 302;
464         }
465
466         my $feed = create_record_feed( $type, $list, $unapi);
467         $feed->root($root);
468
469         $feed->title("$limit most recent $rtype changes from $date forward");
470         $feed->creator($host);
471         $feed->update_ts(gmtime_ISO8601());
472
473         $feed->link(atom => $base . "/atom/$rtype/$axis/$date/$limit" => 'application/atom+xml');
474         $feed->link(rss2 => $base . "/rss2/$rtype/$axis/$date/$limit");
475         $feed->link(html => $base . "/html/$rtype/$axis/$date/$limit" => 'text/html');
476         $feed->link(unapi => $unapi);
477
478         $feed->link(
479                 OPAC =>
480                 '/opac/en-US/skin/default/xml/rresult.xml?rt=list&' .
481                         join('&', map { 'rl=' . $_} @$list ),
482                 'text/html'
483         );
484
485
486         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
487         print entityize($feed->toString) . "\n";
488
489         return Apache2::Const::OK;
490 }
491
492 sub opensearch_osd {
493         my $version = shift;
494         my $lib = shift;
495         my $class = shift;
496         my $base = shift;
497
498         if ($version eq '1.0') {
499                 print <<OSD;
500 Content-type: application/opensearchdescription+xml; charset=utf-8
501
502 <?xml version="1.0" encoding="UTF-8"?>
503 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
504   <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&amp;startIndex={startIndex}&amp;count={count}</Url>
505   <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
506   <ShortName>$lib</ShortName>
507   <LongName>Search $lib</LongName>
508   <Description>Search the $lib OPAC by $class.</Description>
509   <Tags>$lib book library</Tags>
510   <SampleSearch>harry+potter</SampleSearch>
511   <Developer>Mike Rylander for GPLS/PINES</Developer>
512   <Contact>feedback\@open-ils.org</Contact>
513   <SyndicationRight>open</SyndicationRight>
514   <AdultContent>false</AdultContent>
515 </OpenSearchDescription>
516 OSD
517         } else {
518                 print <<OSD;
519 Content-type: application/opensearchdescription+xml; charset=utf-8
520
521 <?xml version="1.0" encoding="UTF-8"?>
522 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
523   <ShortName>$lib</ShortName>
524   <Description>Search the $lib OPAC by $class.</Description>
525   <Tags>$lib book library</Tags>
526   <Url type="application/atom+xml"
527        template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
528   <Url type="application/x-rss+xml"
529        template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
530   <Url type="application/x-mods3+xml"
531        template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
532   <Url type="application/x-mods+xml"
533        template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
534   <Url type="application/x-marcxml+xml"
535        template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
536   <LongName>Search $lib</LongName>
537   <Query role="example" searchTerms="harry+potter" />
538   <Developer>Mike Rylander for GPLS/PINES</Developer>
539   <SyndicationRight>open</SyndicationRight>
540   <AdultContent>false</AdultContent>
541   <Language>en-US</Language>
542   <OutputEncoding>UTF-8</OutputEncoding>
543   <InputEncoding>UTF-8</InputEncoding>
544 </OpenSearchDescription>
545 OSD
546         }
547
548         return Apache2::Const::OK;
549 }
550
551 sub opensearch_feed {
552         my $apache = shift;
553         return Apache2::Const::DECLINED if (-e $apache->filename);
554
555         my $cgi = new CGI;
556         my $year = (gmtime())[5] + 1900;
557
558         my $host = $cgi->virtual_host || $cgi->server_name;
559
560         my $rel_name = quotemeta($cgi->url(-relative=>1));
561
562         my $add_path = 1;
563         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
564
565         my $url = $cgi->url(-path_info=>$add_path);
566         my $root = (split 'opensearch', $url)[0];
567         my $base = (split 'opensearch', $url)[0] . 'opensearch';
568         my $unapi = (split 'opensearch', $url)[0] . 'unapi';
569
570
571         my $path = (split 'opensearch', $url)[1];
572
573         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
574
575         if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
576                 
577                 my $version = $1;
578                 my $lib = $2;
579                 my $class = $3;
580
581                 if (!$lib) {
582                         $lib = $actor->request(
583                                 'open-ils.actor.org_unit_list.search' => parent_ou => undef
584                         )->gather(1)->[0]->shortname;
585                 }
586
587                 if ($class eq '-') {
588                         $class = 'keyword';
589                 }
590
591                 return opensearch_osd($version, $lib, $class, $base);
592         }
593
594
595         my $page = $cgi->param('startPage') || 1;
596         my $offset = $cgi->param('startIndex') || 1;
597         my $limit = $cgi->param('count') || 10;
598         my $lang = $cgi->param('language') || 'en-US';
599
600         $page = 1 if ($page !~ /^\d+$/);
601         $offset = 1 if ($offset !~ /^\d+$/);
602         $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
603         $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
604
605         if ($page > 1) {
606                 $offset = ($page - 1) * $limit;
607         } else {
608                 $offset -= 1;
609         }
610
611         my (undef,$version,$org,$type,$class,$terms) = split '/', $path;
612
613         $terms ||= $cgi->param('searchTerms');
614         $class ||= $cgi->param('searchClass') || '-';
615         $type ||= $cgi->param('responseType') || '-';
616         $org ||= $cgi->param('searchOrg') || '-';
617
618         if ($version eq '1.0') {
619                 $type = 'rss2';
620         } elsif ($type eq '-') {
621                 $type = 'atom';
622         }
623
624
625         $class = 'keyword' if ($class eq '-');
626         $terms = decode_utf8($terms);
627         $terms =~ s/\+/ /go;
628         $terms =~ s/'//go;
629
630         #warn "searching for $class -> [$terms] via OS $version, response type $type";
631
632         my $org_unit;
633         if ($org eq '-') {
634                 $org_unit = $actor->request(
635                         'open-ils.actor.org_unit_list.search' => parent_ou => undef
636                 )->gather(1);
637         } else {
638                 $org_unit = $actor->request(
639                         'open-ils.actor.org_unit_list.search' => shortname => $org
640                 )->gather(1);
641         }
642
643         my $rs_name = $cgi->cookie('os_session');
644         my $cached_res = OpenSRF::Utils::Cache->new->get_cache( "os_session:$rs_name" ) if ($rs_name);
645
646         my $recs;
647         if (!($recs = $$cached_res{os_results}{$class}{$terms}{$org})) {
648                 warn "NOT pulling results from cache";
649                 $rs_name = $cgi->remote_host . '::' . rand(time);
650                 $recs = $search->request(
651                         'open-ils.search.biblio.multiclass' => {
652                                 searches        => { $class => { term => $terms, }, },
653                                 org_unit        => $org_unit->[0]->id,
654                                 offset          => 0,
655                                 limit           => 5000,
656                         }
657                 )->gather(1);
658                 try {
659                         $$cached_res{os_results}{$class}{$terms}{$org} = $recs;
660                         OpenSRF::Utils::Cache->new->put_cache( "os_session:$rs_name", $cached_res, 1800 );
661                 } catch Error with {
662                         warn shift();
663                 };
664         }
665
666         my $feed = create_record_feed(
667                 $type,
668                 [ map { $_->[0] } @{$recs->{ids}}[$offset .. $offset + $limit - 1] ],
669                 $unapi,
670                 $org
671         );
672         $feed->root($root);
673         $feed->lib($org);
674         $feed->search($terms);
675         $feed->class($class);
676
677         $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
678         $feed->creator($host);
679         $feed->update_ts(gmtime_ISO8601());
680
681         $feed->_create_node(
682                 $feed->{item_xpath},
683                 'http://a9.com/-/spec/opensearch/1.1/',
684                 'totalResults',
685                 $recs->{count},
686         );
687
688         $feed->_create_node(
689                 $feed->{item_xpath},
690                 'http://a9.com/-/spec/opensearch/1.1/',
691                 'startIndex',
692                 $offset + 1,
693         );
694
695         $feed->_create_node(
696                 $feed->{item_xpath},
697                 'http://a9.com/-/spec/opensearch/1.1/',
698                 'itemsPerPage',
699                 $limit,
700         );
701
702         $feed->link(
703                 next =>
704                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
705                 'application/opensearch+xml'
706         ) if ($offset + $limit < $recs->{count});
707
708         $feed->link(
709                 previous =>
710                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
711                 'application/opensearch+xml'
712         ) if ($offset);
713
714         $feed->link(
715                 self =>
716                 $base .  "/$version/$org/$type/$class?searchTerms=$terms" =>
717                 'application/opensearch+xml'
718         );
719
720         $feed->link( unapi => $unapi);
721
722 #       $feed->link(
723 #               alternate =>
724 #               $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
725 #                       join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
726 #               'text/html'
727 #       );
728
729         $feed->link(
730                 opac =>
731                 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
732                         join('&', map { 'rl=' . $_->[0] } grep { defined $_ } @{$recs->{ids}} ),
733                 'text/html'
734         );
735
736         print $cgi->header(
737                 -type           => $feed->type,
738                 -charset        => 'UTF-8',
739                 -cookie         => $cgi->cookie( -name => 'os_session', -value => $rs_name, -expires => '+30m' ),
740         );
741
742         print entityize($feed->toString) . "\n";
743
744         return Apache2::Const::OK;
745 }
746
747 sub create_record_feed {
748         my $type = shift;
749         my $records = shift;
750         my $unapi = shift;
751
752         my $lib = shift || '';
753
754         my $cgi = new CGI;
755         my $base = $cgi->url;
756         my $host = $cgi->virtual_host || $cgi->server_name;
757
758         my $year = (gmtime())[5] + 1900;
759
760         my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
761         $feed->base($base);
762         $feed->unapi($unapi);
763
764         $type = 'atom' if ($type eq 'html');
765         $type = 'marcxml' if ($type eq 'htmlcard' or $type eq 'htmlholdings');
766
767         for my $rec (@$records) {
768                 next unless($rec);
769
770                 my $item_tag = "tag:$host,$year:biblio-record_entry/$rec/$lib";
771
772
773                 my $xml = $supercat->request(
774                         "open-ils.supercat.record.$type.retrieve",
775                         $rec
776                 )->gather(1);
777
778                 my $node = $feed->add_item($xml);
779
780                 if ($lib && $type eq 'marcxml') {
781                         $xml = $supercat->request( "open-ils.supercat.record.holdings_xml.retrieve", $rec, $lib )->gather(1);
782                         $node->add_holdings($xml);
783                 }
784
785                 $node->id($item_tag);
786                 $node->link(alternate => $feed->unapi . "?id=$item_tag&format=htmlholdings" => 'text/html');
787                 $node->link(opac => $feed->unapi . "?id=$item_tag&format=opac");
788                 $node->link(unapi => $feed->unapi . "?id=$item_tag");
789                 $node->link('unapi-id' => $item_tag);
790         }
791
792         return $feed;
793 }
794
795 sub entityize {
796         my $stuff = NFC(shift());
797         $stuff =~ s/&(?!\S+;)/&amp;/gso;
798         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
799         return $stuff;
800 }
801
802 1;