]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/SuperCat.pm
556fd96c2f6066ef199ac22558bd665ec3e7b1d0
[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 :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::System;
17 use OpenSRF::AppSession;
18 use XML::LibXML;
19
20 use Unicode::Normalize;
21 use OpenILS::Utils::Fieldmapper;
22 use OpenILS::WWW::SuperCat::Feed;
23
24
25 # set the bootstrap config when this module is loaded
26 my ($bootstrap, $supercat, $actor, $parser, $search);
27
28 sub import {
29         my $self = shift;
30         $bootstrap = shift;
31 }
32
33
34 sub child_init {
35         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
36         $supercat = OpenSRF::AppSession->create('open-ils.supercat');
37         $actor = OpenSRF::AppSession->create('open-ils.actor');
38         $search = OpenSRF::AppSession->create('open-ils.search');
39         $parser = new XML::LibXML;
40 }
41
42 sub oisbn {
43
44         my $apache = shift;
45         return Apache2::Const::DECLINED if (-e $apache->filename);
46
47         (my $isbn = $apache->path_info) =~ s{^.*?([^/]+)$}{$1}o;
48
49         my $list = $supercat
50                 ->request("open-ils.supercat.oisbn", $isbn)
51                 ->gather(1);
52
53         print "Content-type: application/xml; charset=utf-8\n\n";
54         print "<?xml version='1.0' encoding='UTF-8' ?>\n";
55
56         unless (exists $$list{metarecord}) {
57                 print '<idlist/>';
58                 return Apache2::Const::OK;
59         }
60
61         print "<idlist metarecord='$$list{metarecord}'>\n";
62
63         for ( keys %{ $$list{record_list} } ) {
64                 (my $o = $$list{record_list}{$_}) =~s/^(\S+).*?$/$1/o;
65                 print "  <isbn record='$_'>$o</isbn>\n"
66         }
67
68         print "</idlist>\n";
69
70         return Apache2::Const::OK;
71 }
72
73 sub unapi {
74
75         my $apache = shift;
76         return Apache2::Const::DECLINED if (-e $apache->filename);
77
78         print "Content-type: application/xml; charset=utf-8\n";
79         
80         my $cgi = new CGI;
81
82         my $uri = $cgi->param('uri') || '';
83         my $base = $cgi->url;
84         my $host = $cgi->virtual_host || $cgi->server_name;
85
86         my $format = $cgi->param('format');
87         my ($id,$type,$command) = ('','','');
88
89         if (!$format) {
90                 if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
91                         $id = $2;
92                         $type = 'record';
93                         $type = 'metarecord' if ($1 =~ /^m/o);
94
95                         my $list = $supercat
96                         ->request("open-ils.supercat.$type.formats")
97                                 ->gather(1);
98
99                         print "\n";
100
101                         my $body =
102                                 "<formats>
103                                  <uri>$uri</uri>
104                                    <format>
105                                      <name>opac</name>
106                                      <type>text/html</type>
107                                    </format>";
108
109                         for my $h (@$list) {
110                                 my ($type) = keys %$h;
111                                 $body .= "<format><name>$type</name><type>application/$type+xml</type>";
112
113                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
114                                         $body .= "<$part>$$h{$type}{$part}</$part>"
115                                                 if ($$h{$type}{$part});
116                                 }
117                                 
118                                 $body .= '</format>';
119                         }
120
121                         $body .= "</formats>\n";
122
123                         $apache->custom_response( 300, $body);
124                         return 300;
125                 } else {
126                         my $list = $supercat
127                                 ->request("open-ils.supercat.record.formats")
128                                 ->gather(1);
129                                 
130                         push @$list,
131                                 @{ $supercat
132                                         ->request("open-ils.supercat.metarecord.formats")
133                                         ->gather(1);
134                                 };
135
136                         my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
137                         $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
138
139                         print "\n<formats>
140                                    <format>
141                                      <name>opac</name>
142                                      <type>text/html</type>
143                                    </format>";
144
145                         for my $h (@$list) {
146                                 my ($type) = keys %$h;
147                                 print "<format><name>$type</name><type>application/$type+xml</type>";
148
149                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
150                                         print "<$part>$$h{$type}{$part}</$part>"
151                                                 if ($$h{$type}{$part});
152                                 }
153                                 
154                                 print '</format>';
155                         }
156
157                         print "</formats>\n";
158
159
160                         return Apache2::Const::OK;
161                 }
162         }
163
164                 
165         if ($uri =~ m{^tag:[^:]+:([^\/]+)/(\d+)}o) {
166                 $id = $2;
167                 $type = 'record';
168                 $type = 'metarecord' if ($1 =~ /^m/o);
169                 $command = 'retrieve';
170         }
171
172         if ($format eq 'opac') {
173                 print "Location: $base/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
174                         if ($type eq 'metarecord');
175                 print "Location: $base/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
176                         if ($type eq 'record');
177                 return 302;
178         }
179
180         print "\n" . $supercat->request("open-ils.supercat.$type.$format.$command",$id)->gather(1);
181
182         return Apache2::Const::OK;
183 }
184
185 sub supercat {
186
187         my $apache = shift;
188         return Apache2::Const::DECLINED if (-e $apache->filename);
189
190         my $path = $apache->path_info;
191
192         my $cgi = new CGI;
193         my $base = $cgi->url;
194
195         my ($id,$type,$format,$command) = reverse split '/', $path;
196
197         print "Content-type: application/xml; charset=utf-8\n";
198         
199         if ( $path =~ m{^/formats(?:/([^\/]+))?$}o ) {
200                 if ($1) {
201                         my $list = $supercat
202                                 ->request("open-ils.supercat.$1.formats")
203                                 ->gather(1);
204
205                         print "\n";
206
207                         print "<formats>
208                                    <format>
209                                      <name>opac</name>
210                                      <type>text/html</type>
211                                    </format>";
212
213                         for my $h (@$list) {
214                                 my ($type) = keys %$h;
215                                 print "<format><name>$type</name><type>application/$type+xml</type>";
216
217                                 for my $part ( qw/namespace_uri docs schema_location/ ) {
218                                         print "<$part>$$h{$type}{$part}</$part>"
219                                                 if ($$h{$type}{$part});
220                                 }
221                                 
222                                 print '</format>';
223                         }
224
225                         print "</formats>\n";
226
227                         return Apache2::Const::OK;
228                 }
229
230                 my $list = $supercat
231                         ->request("open-ils.supercat.record.formats")
232                         ->gather(1);
233                                 
234                 push @$list,
235                         @{ $supercat
236                                 ->request("open-ils.supercat.metarecord.formats")
237                                 ->gather(1);
238                         };
239
240                 my %hash = map { ( (keys %$_)[0] => (values %$_)[0] ) } @$list;
241                 $list = [ map { { $_ => $hash{$_} } } sort keys %hash ];
242
243                 print "\n<formats>
244                            <format>
245                              <name>opac</name>
246                              <type>text/html</type>
247                            </format>";
248
249                 for my $h (@$list) {
250                         my ($type) = keys %$h;
251                         print "<format><name>$type</name><type>application/$type+xml</type>";
252
253                         for my $part ( qw/namespace_uri docs schema_location/ ) {
254                                 print "<$part>$$h{$type}{$part}</$part>"
255                                         if ($$h{$type}{$part});
256                         }
257                         
258                         print '</format>';
259                 }
260
261                 print "</formats>\n";
262
263
264                 return Apache2::Const::OK;
265         }
266
267         if ($format eq 'opac') {
268                 print "Location: $base/../../en-US/skin/default/xml/rresult.xml?m=$id\n\n"
269                         if ($type eq 'metarecord');
270                 print "Location: $base/../../en-US/skin/default/xml/rdetail.xml?r=$id\n\n"
271                         if ($type eq 'record');
272                 return 302;
273         }
274
275         print "\n" . $supercat->request("open-ils.supercat.$type.$format.$command",$id)->gather(1);
276
277         return Apache2::Const::OK;
278 }
279
280
281 sub bookbag_feed {
282         my $apache = shift;
283         return Apache2::Const::DECLINED if (-e $apache->filename);
284
285         my $cgi = new CGI;
286
287         my $year = (gmtime())[5] + 1900;
288         my $host = $cgi->virtual_host || $cgi->server_name;
289
290         my $url = $cgi->url(-path_info=>1);
291         my $root = (split 'feed', $url)[0];
292         my $base = (split 'bookbag', $url)[0] . 'bookbag';
293         my $path = (split 'bookbag', $url)[1];
294         my $unapi = (split 'feed', $url)[0] . 'unapi';
295
296
297
298         my ($id,$type) = reverse split '/', $path;
299
300         my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
301         my $bucket_tag = "tag:$host,$year:record_bucket/$id";
302
303         if ($type eq 'opac') {
304                 print "Location: $base/../../../opac/en-US/skin/default/xml/rresult.xml?rt=list&" .
305                         join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
306                         "\n\n";
307                 return Apache2::Const::OK;
308         }
309
310         my $feed = create_record_feed(
311                 $type,
312                 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
313                 $unapi,
314         );
315
316         $feed->title("Items in Book Bag #".$bucket->id);
317         $feed->creator($host);
318         $feed->update_ts(gmtime_ISO8601());
319
320         $feed->link(atom => $base . "/bookbag/atom/$id");
321         $feed->link(rss2 => $base . "/bookbag/rss2/$id");
322         $feed->link(html => $base . "/bookbag/html/$id");
323
324         $feed->link(
325                 OPAC =>
326                 $root . '../en-US/skin/default/xml/rresult.xml?rt=list&' .
327                         join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
328                 'text/xhtml'
329         );
330
331
332         print "Content-type: application/xml; charset=utf-8\n\n";
333         print entityize($feed->toString) . "\n";
334
335         return Apache2::Const::OK;
336 }
337
338 sub opensearch_osd {
339         my $version = shift;
340         my $lib = shift;
341         my $class = shift;
342         my $base = shift;
343
344         if ($version eq '1.0') {
345                 print <<OSD;
346 Content-type: application/opensearchdescription+xml; charset=utf-8
347
348 <?xml version="1.0" encoding="UTF-8"?>
349 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
350   <Url>$base/1.0/$lib/$class/-/{searchTerms}?startPage={startPage}&amp;startIndex={startIndex}&amp;count={count}</Url>
351   <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
352   <ShortName>$class</ShortName>
353   <LongName>Search by $class</LongName>
354   <Description>Search the $lib OPAC by $class.</Description>
355   <Tags>$lib book library</Tags>
356   <SampleSearch>harry+potter</SampleSearch>
357   <Developer>Mike Rylander for GPLS/PINES</Developer>
358   <Contact>feedback\@open-ils.org</Contact>
359   <SyndicationRight>open</SyndicationRight>
360   <AdultContent>false</AdultContent>
361 </OpenSearchDescription>
362 OSD
363         } else {
364                 print <<OSD;
365 Content-type: application/opensearchdescription+xml; charset=utf-8
366
367 <?xml version="1.0" encoding="UTF-8"?>
368 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
369   <ShortName>$class</ShortName>
370   <Description>Search the $lib OPAC by $class.</Description>
371   <Tags>$lib book library</Tags>
372   <Url type="application/atom+xml"
373        method="post"
374        template="$base/1.1/$lib/$class/atom/{searchTerms}">
375     <Param name="startPage" value="{startPage?}"/>
376     <Param name="startIndex" value="{startIndex?}"/>
377     <Param name="count" value="{count?}"/>
378     <Param name="language" value="{language?}"/>
379   </Url>
380   <Url type="application/rss+xml"
381        method="post"
382        template="$base/1.1/$lib/$class/rss2/{searchTerms}">
383     <Param name="startPage" value="{startPage?}"/>
384     <Param name="startIndex" value="{startIndex?}"/>
385     <Param name="count" value="{count?}"/>
386     <Param name="language" value="{language?}"/>
387   </Url>
388   <Url type="application/mods+xml"
389        method="post"
390        template="$base/1.1/$lib/$class/mods/{searchTerms}">
391     <Param name="startPage" value="{startPage?}"/>
392     <Param name="startIndex" value="{startIndex?}"/>
393     <Param name="count" value="{count?}"/>
394     <Param name="language" value="{language?}"/>
395   </Url>
396   <Url type="application/marcxml+xml"
397        method="post"
398        template="$base/1.1/$lib/$class/marcxml/{searchTerms}">
399     <Param name="startPage" value="{startPage?}"/>
400     <Param name="startIndex" value="{startIndex?}"/>
401     <Param name="count" value="{count?}"/>
402     <Param name="language" value="{language?}"/>
403   </Url>
404   <LongName>Search by $class</LongName>
405   <Query role="example" searchTerms="harry+potter" />
406   <Developer>Mike Rylander for GPLS/PINES</Developer>
407   <SyndicationRight>open</SyndicationRight>
408   <AdultContent>false</AdultContent>
409   <Language>en-US</Language>
410   <OutputEncoding>UTF-8</OutputEncoding>
411   <InputEncoding>UTF-8</InputEncoding>
412 </OpenSearchDescription>
413 OSD
414         }
415
416         return Apache2::Const::OK;
417 }
418
419 sub opensearch_feed {
420         my $apache = shift;
421         return Apache2::Const::DECLINED if (-e $apache->filename);
422
423         my $cgi = new CGI;
424         my $year = (gmtime())[5] + 1900;
425
426         my $host = $cgi->virtual_host || $cgi->server_name;
427
428         my $url = $cgi->url(-path_info=>1);
429         my $root = (split 'opensearch', $url)[0];
430         my $base = (split 'opensearch', $url)[0] . 'opensearch';
431         my $unapi = (split 'opensearch', $url)[0] . 'unapi';
432
433         my $path = (split 'opensearch', $url)[1];
434
435         if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
436                 
437                 my $version = $1;
438                 my $lib = $2;
439                 my $class = $3;
440
441                 if (!$lib) {
442                         $lib = 'PINES';
443                 }
444
445                 if ($class eq '-') {
446                         $class = 'keyword';
447                 }
448
449                 return opensearch_osd($version, $lib, $class, $base);
450         }
451
452
453         my $page = $cgi->param('startPage') || 1;
454         my $offset = $cgi->param('startIndex') || 1;
455         my $limit = $cgi->param('count') || 10;
456         my $lang = $cgi->param('language') || 'en-US';
457
458         if ($page > 1) {
459                 $offset = ($page - 1) * $limit;
460         } else {
461                 $offset -= 1;
462         }
463
464         my ($terms,$class,$type,$org,$version) = reverse split '/', $path;
465
466         if ($version eq '1.0') {
467                 $type = 'rss2';
468         } elsif ($type eq '-') {
469                 $type = 'atom';
470         }
471
472         $class = 'keyword' if ($class eq '-');
473         $terms =~ s/\+/ /go;
474
475         warn "searching for $class -> [$terms] via OS $version, response type $type";
476
477         my $org_unit;
478         if ($org eq '-') {
479                 $org_unit = $actor->request(
480                         'open-ils.actor.org_unit_list.search' => parent_ou => undef
481                 )->gather(1);
482         } else {
483                 $org_unit = $actor->request(
484                         'open-ils.actor.org_unit_list.search' => shortname => $org
485                 )->gather(1);
486         }
487
488         my $recs = $search->request(
489                 'open-ils.search.biblio.record.class.search' => $class,
490                 { term          => $terms,
491                   org_unit      => $org_unit->[0]->id,
492                   limit         => $limit,
493                   offset        => $offset,
494                 }
495         )->gather(1);
496
497         my $feed = create_record_feed(
498                 $type,
499                 [ map { $_->[0] } @{$recs->{ids}} ],
500                 $unapi,
501         );
502
503         $feed->title("$class search results for [$terms] at ".$org_unit->[0]->name);
504         $feed->creator($host);
505         $feed->update_ts(gmtime_ISO8601());
506
507 =head
508         my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
509         my $bucket_tag = "tag:$host,$year:record_bucket/$id";
510
511         my $feed = create_record_feed(
512                 $type,
513                 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
514                 $unapi,
515         );
516
517         $feed->link(atom => $id);
518         $feed->link(rss2 => $id);
519         $feed->link(html => $id);
520
521 =cut
522
523         $feed->_create_node(
524                 $feed->{item_xpath},
525                 'http://a9.com/-/spec/opensearch/1.1/',
526                 'totalResults',
527                 $recs->{count},
528         );
529
530         $feed->_create_node(
531                 $feed->{item_xpath},
532                 'http://a9.com/-/spec/opensearch/1.1/',
533                 'startIndex',
534                 $offset + 1,
535         );
536
537         $feed->_create_node(
538                 $feed->{item_xpath},
539                 'http://a9.com/-/spec/opensearch/1.1/',
540                 'itemsPerPage',
541                 $limit,
542         );
543
544         $feed->link(
545                 next =>
546                 $base . $path . "?startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
547                 'application/opensearch+xml'
548         ) if ($offset + $limit < $recs->{count});
549
550         $feed->link(
551                 previous =>
552                 $base . $path . "?startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
553                 'application/opensearch+xml'
554         ) if ($offset);
555
556         $feed->link(
557                 self =>
558                 $base . $path =>
559                 'application/opensearch+xml'
560         );
561
562         $feed->link(
563                 opac =>
564                 $root . "../opac/$lang/skin/default/xml/rresult.xml?rt=list&" .
565                         join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
566                 'text/xhtml'
567         );
568
569         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
570         print entityize($feed->toString) . "\n";
571
572         return Apache2::Const::OK;
573 }
574
575 sub create_record_feed {
576         my $type = shift;
577         my $records = shift;
578         my $unapi = shift;
579
580         my $cgi = new CGI;
581         my $base = $cgi->url;
582         my $host = $cgi->virtual_host || $cgi->server_name;
583
584         my $year = (gmtime())[5] + 1900;
585
586         my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
587         $feed->base($base);
588         $feed->unapi($unapi);
589
590         $type = 'atom' if ($type eq 'html');
591
592         for my $rec (@$records) {
593                 my $item_tag = "tag:$host,$year:biblio-record_entry/" . $rec;
594
595
596                 my $xml = $supercat->request(
597                         "open-ils.supercat.record.$type.retrieve",
598                         $rec
599                 )->gather(1);
600
601                 my $node = $feed->add_item($xml);
602
603                 $node->id($item_tag);
604                 $node->link(alternate => $feed->unapi . "?uri=$item_tag&format=opac" => 'application/xml');
605                 $node->link(opac => $feed->unapi . "?uri=$item_tag&format=opac");
606                 $node->link(unapi => $feed->unapi . "?uri=$item_tag");
607         }
608
609         return $feed;
610 }
611
612 sub entityize {
613         my $stuff = NFC(shift());
614         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
615         return $stuff;
616 }
617
618 1;