Bug 21890: Allow restricting password resets by patron category
[koha.git] / opac / unapi
1 #!/usr/bin/perl
2
3 # Copyright 2008-2009 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 =head1 NAME
23
24 unapi - implement unAPI for the OPAC
25
26 =head1 SYNOPSIS
27
28 Retrieve http://library.example.org/cgi-bin/koha/unapi?id=koha:biblionumber:123&format=oai_dc
29
30 =head1 DESCRIPTION
31
32 Implements unAPI <http://unapi.info>, a small HTTP API for retrieving structured
33 content from a web application.  The primary application of unAPI in Koha is to
34 allow tools such as Zotero to identify and grab bibliographic record metadata in
35 an XML format such as OAI DC, RSS2, MARCXML, or MODS.
36
37 =cut
38
39 use CGI qw ( -utf8 );
40 use C4::Context;
41 use C4::Biblio;
42 use Koha::XSLT_Handler;
43
44 my $cgi = CGI->new();
45 binmode(STDOUT, ":encoding(UTF-8)"); #output as utf8
46
47 =head1 VARIABLES
48
49 =head2 $format_to_stylesheet_map
50
51 This hashref of hashrefs maps from a MARC flavour and unAPI format
52 to the stylesheet that should be used to transform the bib MARCXML
53 to the desired output format.  As new MARC XSLT stylesheets are added,
54 (particularly for UNIMARC), this map should be updated.  Of course,
55 if/when we add support for emitting a format that is not genreated
56 by a stylesheet, the structure of this variable will have to be changed.
57 At present, this doubles as the list of output formats supported by
58 this unAPI implementation.
59
60 =cut
61
62 my $format_to_stylesheet_map = {
63     'MARC21' => {
64         'marcxml'      => 'identity.xsl',
65         'marcxml-full' => 'identity.xsl',
66         'mods'         => 'MARC21slim2MODS.xsl',
67         'mods-full'    => 'MARC21slim2MODS.xsl',
68         'mods3'        => 'MARC21slim2MODS3-1.xsl',
69         'mods3-full'   => 'MARC21slim2MODS3-1.xsl',
70         'oai_dc'       => 'MARC21slim2OAIDC.xsl',
71         'rdfdc',       => 'MARC21slim2RDFDC.xsl',
72         'rss2'         => 'MARC21slim2RSS2.xsl',
73         'rss2-full'    => 'MARC21slim2RSS2.xsl',
74         'srw_dc'       => 'MARC21slim2SRWDC.xsl',
75     },
76     'NORMARC' => {
77         'marcxml'      => 'identity.xsl',
78         'marcxml-full' => 'identity.xsl',
79         'mods'         => 'MARC21slim2MODS.xsl',
80         'mods-full'    => 'MARC21slim2MODS.xsl',
81         'mods3'        => 'MARC21slim2MODS3-1.xsl',
82         'mods3-full'   => 'MARC21slim2MODS3-1.xsl',
83         'oai_dc'       => 'MARC21slim2OAIDC.xsl',
84         'rdfdc',       => 'MARC21slim2RDFDC.xsl',
85         'rss2'         => 'MARC21slim2RSS2.xsl',
86         'rss2-full'    => 'MARC21slim2RSS2.xsl',
87         'srw_dc'       => 'MARC21slim2SRWDC.xsl',
88     },
89     'UNIMARC' => {
90         'marcxml'      => 'identity.xsl',
91         'marcxml-full' => 'identity.xsl',
92         'oai_dc'       => 'UNIMARCslim2OAIDC.xsl',
93         'rdfdc',       => 'UNIMARCslim2RDFDC.xsl',
94         'srw_dc'       => 'UNIMARCslim2SRWDC.xsl',
95     },
96 };
97
98 =head2 $format_info
99
100 This hashref maps from unAPI output formats to the <format> elements
101 used to describe them in an unAPI format request.
102
103 =cut
104
105 my $format_info = {
106     'marcxml' => q(<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"/>),
107     'marcxml-full' => q(<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"/>),
108     'mods' => q(<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"/>),
109     'mods-full' => q(<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"/>),
110     'mods3' => q(<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"/>),
111     'mods3-full' => q(<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"/>),
112     'oai_dc' => q(<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"/>),
113     'rdfdc' => q(<format name="rdfdc" type="application/xml" namespace_uri="http://purl.org/dc/elements/1.1/" schema_location="http://purl.org/dc/elements/1.1/"/>),
114     'rss2' => q(<format name="rss2" type="application/xml"/>),
115     'rss2-full' => q(<format name="rss2-full" type="application/xml"/>),
116     'srw_dc' => q(<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"/>),
117 };
118
119 my $id     = $cgi->param('id');
120 my $format = $cgi->param('format');
121
122 if (not defined $format) {
123     emit_formats($id, $format_to_stylesheet_map, $format_info, $cgi);
124 } elsif ($id) {
125
126     # koha:biblionumber:0152018484
127     if ($id =~ /koha:biblionumber:(\d+)/) {
128         my $biblionumber = $1;
129
130         my $content;
131
132         my $marcxml = GetXmlBiblio($biblionumber);
133         unless (defined $marcxml) {
134             # no bib, so 404
135             print $cgi->header( -status => '404 record not found');
136             exit 0;
137         }
138
139         my $xslt_file = get_xslt_file( $format, $format_to_stylesheet_map, $format_info );
140         unless( defined $xslt_file ) {
141             print $cgi->header( -status => '406 invalid format requested' );
142             exit 0;
143         }
144         my $xslt_engine = Koha::XSLT_Handler->new;
145         $content = $xslt_engine->transform({
146             xml => $marcxml,
147             file => $xslt_file,
148         });
149
150         if( !defined $content || $xslt_engine->err ) {
151             print $cgi->header( -status => '500 internal error' );
152             exit 0;
153         }
154
155         print $cgi->header( -type =>'application/xml', -charset => 'UTF-8' );
156         print $content;
157     } else {
158         # ID is obviously wrong, so 404
159         print $cgi->header( -status => '404 record not found');
160         exit 0;
161     }
162 } else {
163     # supplied a format but no id - caller is doing it wrong
164     print $cgi->header( -status => '400 bad request - if you specify format, must specify id');
165     exit 0;
166 }
167
168 exit 0;
169
170 sub emit_formats {
171     my ($id, $format_to_stylesheet_map, $format_info, $cgi) = @_;
172
173     if (defined $id) {
174         print $cgi->header( -type =>'application/xml', -status => '300 multiple choices' );
175     } else {
176         print $cgi->header( -type =>'application/xml', -status => '200 Ok' );
177     }
178
179     print "<?xml version='1.0' encoding='utf-8'  ?>\n";
180     if (defined $id) {
181         print qq(<formats id="$id">\n);
182     } else {
183         print "<formats>\n";
184     }
185
186     my $marcflavour = uc(C4::Context->preference('marcflavour'));
187     foreach my $format (sort keys %{ $format_to_stylesheet_map->{$marcflavour} }) {
188         print $format_info->{$format}, "\n";
189     }
190     print "</formats>\n";
191     return;
192 }
193
194
195 sub get_xslt_file {
196     my ($format, $format_to_stylesheet_map, $format_info) = @_;
197     $format = lc $format;
198
199     my $marcflavour = uc(C4::Context->preference('marcflavour'));
200     return unless $format_to_stylesheet_map->{$marcflavour}->{$format};
201
202     my $xslt_file = C4::Context->config('intrahtdocs') .
203                     "/prog/en/xslt/" .
204                     $format_to_stylesheet_map->{$marcflavour}->{$format};
205
206     return $xslt_file;
207 }
208
209 =head1 AUTHOR
210
211 Koha Development Team <http://koha-community.org/>
212
213 Originally written by Joshua Ferraro <jmf@liblime.com>
214
215 Improved by Galen Charlton <galen.charlton@liblime.com>
216
217 =cut