OAI package handle correctly unicode content
[koha.git] / opac / unapi
1 #!/usr/bin/perl
2 use CGI;
3 use strict;
4 use warnings;
5 use C4::Context;
6 use XML::Simple;
7 use LWP::Simple;
8
9 use LWP::UserAgent;
10 use HTTP::Request::Common;
11
12 my $cgi = new CGI;
13 binmode(STDOUT, "utf8"); #output as utf8
14 my $baseurl = C4::Context->preference('OPACBaseURL');
15 warn "Warning: OPACBaseURL not set in system preferences" unless $baseurl;
16
17 my $id = $cgi->param('id');
18 my $format = $cgi->param('format');
19 if ($id && $format) {
20
21     # koha:isbn:0152018484
22     if ($id =~ /isbn/) {
23         $id =~ s/koha:isbn://;
24
25         # two ways to do this, one via the SRU Zebra server (fast)
26         # FIXME - getting the SRU URL this way is purely guesswork
27         $baseurl =~ s/:\d+$//; # parse off OPAC port
28         my $url = "$baseurl:9998/biblios?version=1.1&operation=searchRetrieve&query=$id&startRecord=1&maximumRecords=20&recordSchema=$format";
29         my $content= get($url);
30
31         # the other via XSL parsing (not as fast)
32         unless ($content) {
33             
34         eval {
35             my $conn = C4::Context->Zconn('biblioserver');
36             $conn->option(preferredRecordSyntax => $format);
37             my $rs = $conn->search_pqf('@attr 1=7 '.$id);
38             my $n = $rs->size();
39             $content = $rs->record(0)->raw();
40         };
41         if ($@) {
42             print "Error ", $@->code(), ": ", $@->message(), "\n";
43         }
44
45         }
46         print $cgi->header( -type =>'application/xml' );
47         print $content;
48     }
49 }
50
51 else {
52
53 print $cgi->header( -type =>'application/xml' );
54
55 print "<?xml version='1.0' encoding='utf-8'  ?>
56 <formats>
57 <!-- <format name=\"opac\" type=\"text/html\"/> -->
58 <!-- <format name=\"html\" type=\"text/html\"/> -->
59 <!-- <format name=\"htmlholdings\" type=\"text/html\"/> -->
60 <!-- <format name=\"html-full\" type=\"text/html\"/> -->
61 <!-- <format name=\"htmlholdings-full\" type=\"text/html\"/> -->
62 <!-- <format name=\"atom\" type=\"application/xml\" namespace_uri=\"http://www.w3.org/2005/Atom\" docs=\"http://www.ietf.org/rfc/rfc4287.txt\"/> -->
63 <!-- <format name=\"atom-full\" type=\"application/xml\" namespace_uri=\"http://www.w3.org/2005/Atom\" docs=\"http://www.ietf.org/rfc/rfc4287.txt\"/> -->
64 <format name=\"marcxml\" type=\"application/xml\" namespace_uri=\"http://www.loc.gov/MARC21/slim\" docs=\"http://www.loc.gov/marcxml/\" schema_location=\"http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"/>
65 <format name=\"marcxml-full\" type=\"application/xml\" namespace_uri=\"http://www.loc.gov/MARC21/slim\" docs=\"http://www.loc.gov/marcxml/\" schema_location=\"http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"/>
66 <format name=\"mods\" type=\"application/xml\" namespace_uri=\"http://www.loc.gov/mods/\" docs=\"http://www.loc.gov/mods/\" schema_location=\"http://www.loc.gov/standards/mods/mods.xsd\"/>
67 <format name=\"mods-full\" type=\"application/xml\" namespace_uri=\"http://www.loc.gov/mods/\" docs=\"http://www.loc.gov/mods/\" schema_location=\"http://www.loc.gov/standards/mods/mods.xsd\"/>
68 <format name=\"mods3\" type=\"application/xml\" namespace_uri=\"http://www.loc.gov/mods/v3\" docs=\"http://www.loc.gov/mods/\" schema_location=\"http://www.loc.gov/standards/mods/v3/mods-3-1.xsd\"/>
69 <format name=\"mods3-full\" type=\"application/xml\" namespace_uri=\"http://www.loc.gov/mods/v3\" docs=\"http://www.loc.gov/mods/\" schema_location=\"http://www.loc.gov/standards/mods/v3/mods-3-1.xsd\"/>
70 <format name=\"oai_dc\" type=\"application/xml\" namespace_uri=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" schema_location=\"http://www.openarchives.org/OAI/2.0/oai_dc.xsd\"/>
71 <format name=\"rdfdc\" type=\"application/xml\" namespace_uri=\"http://purl.org/dc/elements/1.1/\" schema_location=\"http://purl.org/dc/elements/1.1/\"/>
72 <format name=\"rss2\" type=\"application/xml\"/>
73 <format name=\"rss2-full\" type=\"application/xml\"/>
74 <format name=\"srw_dc\" type=\"application/xml\" namespace_uri=\"info:srw/schema/1/dc-schema\" schema_location=\"http://www.loc.gov/z3950/agency/zing/srw/dc-schema.xsd\"/>
75 </formats>
76 ";
77 }