]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/SuperCat.pm
basic searching within the opensearch results
[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::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/x-$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 $rel_name = quotemeta($cgi->url(-relative=>1));
291
292         my $add_path = 1;
293         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
294
295         my $url = $cgi->url(-path_info=>$add_path);
296         my $root = (split 'feed', $url)[0];
297         my $base = (split 'bookbag', $url)[0] . 'bookbag';
298         my $path = (split 'bookbag', $url)[1];
299         my $unapi = (split 'feed', $url)[0] . 'unapi';
300
301
302         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
303
304         my ($id,$type) = reverse split '/', $path;
305
306         my $bucket = $actor->request("open-ils.actor.container.public.flesh", 'biblio', $id)->gather(1);
307         return Apache2::Const::NOT_FOUND unless($bucket);
308
309         my $bucket_tag = "tag:$host,$year:record_bucket/$id";
310         if ($type eq 'opac') {
311                 print "Location: $root/../en-US/skin/default/xml/rresult.xml?rt=list&" .
312                         join('&', map { "rl=" . $_->target_biblio_record_entry } @{ $bucket->items }) .
313                         "\n\n";
314                 return Apache2::Const::OK;
315         }
316
317         my $feed = create_record_feed(
318                 $type,
319                 [ map { $_->target_biblio_record_entry } @{ $bucket->items } ],
320                 $unapi,
321         );
322         $feed->root($root);
323
324         $feed->title("Items in Book Bag [".$bucket->name."]");
325         $feed->creator($host);
326         $feed->update_ts(gmtime_ISO8601());
327
328         $feed->link(atom => $base . "/atom/$id" => 'application/atom+xml');
329         $feed->link(rss2 => $base . "/rss2/$id");
330         $feed->link(html => $base . "/html/$id" => 'text/html');
331         $feed->link(unapi => $unapi);
332
333         $feed->link(
334                 OPAC =>
335                 $root . '../en-US/skin/default/xml/rresult.xml?rt=list&' .
336                         join('&', map { 'rl=' . $_->target_biblio_record_entry } @{$bucket->items} ),
337                 'text/html'
338         );
339
340
341         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
342         print entityize($feed->toString) . "\n";
343
344         return Apache2::Const::OK;
345 }
346
347 sub opensearch_osd {
348         my $version = shift;
349         my $lib = shift;
350         my $class = shift;
351         my $base = shift;
352
353         if ($version eq '1.0') {
354                 print <<OSD;
355 Content-type: application/opensearchdescription+xml; charset=utf-8
356
357 <?xml version="1.0" encoding="UTF-8"?>
358 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearchdescription/1.0/">
359   <Url>$base/1.0/$lib/-/$class/{searchTerms}?startPage={startPage}&amp;startIndex={startIndex}&amp;count={count}</Url>
360   <Format>http://a9.com/-/spec/opensearchrss/1.0/</Format>
361   <ShortName>$lib</ShortName>
362   <LongName>Search $lib</LongName>
363   <Description>Search the $lib OPAC by $class.</Description>
364   <Tags>$lib book library</Tags>
365   <SampleSearch>harry+potter</SampleSearch>
366   <Developer>Mike Rylander for GPLS/PINES</Developer>
367   <Contact>feedback\@open-ils.org</Contact>
368   <SyndicationRight>open</SyndicationRight>
369   <AdultContent>false</AdultContent>
370 </OpenSearchDescription>
371 OSD
372         } else {
373                 print <<OSD;
374 Content-type: application/opensearchdescription+xml; charset=utf-8
375
376 <?xml version="1.0" encoding="UTF-8"?>
377 <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
378   <ShortName>$lib</ShortName>
379   <Description>Search the $lib OPAC by $class.</Description>
380   <Tags>$lib book library</Tags>
381   <Url type="application/atom+xml"
382        template="$base/1.1/$lib/atom/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
383   <Url type="application/x-rss+xml"
384        template="$base/1.1/$lib/rss2/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
385   <Url type="application/x-mods3+xml"
386        template="$base/1.1/$lib/mods3/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
387   <Url type="application/x-mods+xml"
388        template="$base/1.1/$lib/mods/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
389   <Url type="application/x-marcxml+xml"
390        template="$base/1.1/$lib/marcxml/$class/{searchTerms}?startPage={startPage?}&amp;startIndex={startIndex?}&amp;count={count?}&amp;language={language?}"/>
391   <LongName>Search $lib</LongName>
392   <Query role="example" searchTerms="harry+potter" />
393   <Developer>Mike Rylander for GPLS/PINES</Developer>
394   <SyndicationRight>open</SyndicationRight>
395   <AdultContent>false</AdultContent>
396   <Language>en-US</Language>
397   <OutputEncoding>UTF-8</OutputEncoding>
398   <InputEncoding>UTF-8</InputEncoding>
399 </OpenSearchDescription>
400 OSD
401         }
402
403         return Apache2::Const::OK;
404 }
405
406 sub opensearch_feed {
407         my $apache = shift;
408         return Apache2::Const::DECLINED if (-e $apache->filename);
409
410         my $cgi = new CGI;
411         my $year = (gmtime())[5] + 1900;
412
413         my $host = $cgi->virtual_host || $cgi->server_name;
414
415         my $rel_name = quotemeta($cgi->url(-relative=>1));
416
417         my $add_path = 1;
418         $add_path = 0 if ($cgi->url(-path_info=>1) =~ /$rel_name$/);
419
420         my $url = $cgi->url(-path_info=>$add_path);
421         my $root = (split 'opensearch', $url)[0];
422         my $base = (split 'opensearch', $url)[0] . 'opensearch';
423         my $unapi = (split 'opensearch', $url)[0] . 'unapi';
424
425         my $path = (split 'opensearch', $url)[1];
426
427         #warn "URL breakdown: $url ($rel_name) -> $root -> $base -> $path -> $unapi";
428
429         if ($path =~ m{^/?(1\.\d{1})/(?:([^/]+)/)?([^/]+)/osd.xml}o) {
430                 
431                 my $version = $1;
432                 my $lib = $2;
433                 my $class = $3;
434
435                 if (!$lib) {
436                         $lib = $actor->request(
437                                 'open-ils.actor.org_unit_list.search' => parent_ou => undef
438                         )->gather(1)->[0]->shortname;
439                 }
440
441                 if ($class eq '-') {
442                         $class = 'keyword';
443                 }
444
445                 return opensearch_osd($version, $lib, $class, $base);
446         }
447
448
449         my $page = $cgi->param('startPage') || 1;
450         my $offset = $cgi->param('startIndex') || 1;
451         my $limit = $cgi->param('count') || 10;
452         my $lang = $cgi->param('language') || 'en-US';
453
454         $page = 1 if ($page !~ /^\d+$/);
455         $offset = 1 if ($offset !~ /^\d+$/);
456         $limit = 10 if ($limit !~ /^\d+$/); $limit = 25 if ($limit > 25);
457         $lang = 'en-US' if ($lang =~ /^{/ or $lang eq '*');
458
459         if ($page > 1) {
460                 $offset = ($page - 1) * $limit;
461         } else {
462                 $offset -= 1;
463         }
464
465         my (undef,$version,$org,$type,$class,$terms) = split '/', $path;
466
467         $terms ||= $cgi->param('searchTerms');
468         $class ||= $cgi->param('searchClass') || '-';
469         $type ||= $cgi->param('responseType') || '-';
470         $org ||= $cgi->param('searchOrg') || '-';
471
472         if ($version eq '1.0') {
473                 $type = 'rss2';
474         } elsif ($type eq '-') {
475                 $type = 'atom';
476         }
477
478         $class = 'keyword' if ($class eq '-');
479         $terms =~ s/\+/ /go;
480
481         #warn "searching for $class -> [$terms] via OS $version, response type $type";
482
483         my $org_unit;
484         if ($org eq '-') {
485                 $org_unit = $actor->request(
486                         'open-ils.actor.org_unit_list.search' => parent_ou => undef
487                 )->gather(1);
488         } else {
489                 $org_unit = $actor->request(
490                         'open-ils.actor.org_unit_list.search' => shortname => $org
491                 )->gather(1);
492         }
493
494         my $recs = $search->request(
495                 'open-ils.search.biblio.record.class.search' => $class,
496                 { term          => $terms,
497                   org_unit      => $org_unit->[0]->id,
498                   limit         => $limit,
499                   offset        => $offset,
500                 }
501         )->gather(1);
502
503         my $feed = create_record_feed(
504                 $type,
505                 [ map { $_->[0] } @{$recs->{ids}} ],
506                 $unapi,
507         );
508         $feed->root($root);
509         $feed->lib($org);
510         $feed->search($terms);
511
512         $feed->title("Search results for [$class => $terms] at ".$org_unit->[0]->name);
513         $feed->creator($host);
514         $feed->update_ts(gmtime_ISO8601());
515
516         $feed->_create_node(
517                 $feed->{item_xpath},
518                 'http://a9.com/-/spec/opensearch/1.1/',
519                 'totalResults',
520                 $recs->{count},
521         );
522
523         $feed->_create_node(
524                 $feed->{item_xpath},
525                 'http://a9.com/-/spec/opensearch/1.1/',
526                 'startIndex',
527                 $offset + 1,
528         );
529
530         $feed->_create_node(
531                 $feed->{item_xpath},
532                 'http://a9.com/-/spec/opensearch/1.1/',
533                 'itemsPerPage',
534                 $limit,
535         );
536
537         $feed->link(
538                 next =>
539                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int($offset + $limit + 1) . "&count=" . $limit =>
540                 'application/opensearch+xml'
541         ) if ($offset + $limit < $recs->{count});
542
543         $feed->link(
544                 previous =>
545                 $base . "/$version/$org/$type/$class?searchTerms=$terms&startIndex=" . int(($offset - $limit) + 1) . "&count=" . $limit =>
546                 'application/opensearch+xml'
547         ) if ($offset);
548
549         $feed->link(
550                 self =>
551                 $base .  "/$version/$org/$type/$class?searchTerms=$terms" =>
552                 'application/opensearch+xml'
553         );
554
555         $feed->link( unapi => $unapi);
556
557         $feed->link(
558                 alternate =>
559                 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
560                         join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
561                 'text/html'
562         );
563
564         $feed->link(
565                 opac =>
566                 $root . "../$lang/skin/default/xml/rresult.xml?rt=list&" .
567                         join('&', map { 'rl=' . $_->[0] } @{$recs->{ids}} ),
568                 'text/html'
569         );
570
571         print "Content-type: ". $feed->type ."; charset=utf-8\n\n";
572         print entityize($feed->toString) . "\n";
573
574         return Apache2::Const::OK;
575 }
576
577 sub create_record_feed {
578         my $type = shift;
579         my $records = shift;
580         my $unapi = shift;
581
582         my $cgi = new CGI;
583         my $base = $cgi->url;
584         my $host = $cgi->virtual_host || $cgi->server_name;
585
586         my $year = (gmtime())[5] + 1900;
587
588         my $feed = new OpenILS::WWW::SuperCat::Feed ($type);
589         $feed->base($base);
590         $feed->unapi($unapi);
591
592         $type = 'atom' if ($type eq 'html');
593
594         for my $rec (@$records) {
595                 my $item_tag = "tag:$host,$year:biblio-record_entry/" . $rec;
596
597
598                 my $xml = $supercat->request(
599                         "open-ils.supercat.record.$type.retrieve",
600                         $rec
601                 )->gather(1);
602
603                 my $node = $feed->add_item($xml);
604
605                 $node->id($item_tag);
606                 $node->link(alternate => $feed->unapi . "?uri=$item_tag&format=opac" => 'text/html');
607                 $node->link(opac => $feed->unapi . "?uri=$item_tag&format=opac");
608                 $node->link(unapi => $feed->unapi . "?uri=$item_tag");
609                 $node->link('unapi-uri' => $item_tag);
610         }
611
612         return $feed;
613 }
614
615 sub entityize {
616         my $stuff = NFC(shift());
617         $stuff =~ s/&(?!\S+;)/&amp;/gso;
618         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
619         return $stuff;
620 }
621
622 1;