(bug #4954) allow date expiry modification only on step 0 and 3
[koha.git] / opac / unapi
blob5cfe9a5975f723c25893571b3054e6b54299eaa7
1 #!/usr/bin/perl
3 # Copyright 2008-2009 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 =head1 NAME
25 unapi - implement unAPI for the OPAC
27 =head1 SYNOPSIS
29 Retrieve http://library.example.org/cgi-bin/koha/unapi?id=koha:biblionumber:123&format=oai_dc
31 =head1 DESCRIPTION
33 Implements unAPI <http://unapi.info>, a small HTTP API for retrieving structured
34 content from a web application. The primary application of unAPI in Koha is to
35 allow tools such as Zotero to identify and grab bibliographic record metadata in
36 an XML format such as OAI DC, RSS2, MARCXML, or MODS.
38 =cut
40 use CGI;
41 use C4::Context;
42 use C4::Biblio;
43 use XML::LibXML;
44 use XML::LibXSLT;
46 my $cgi = CGI->new();
47 binmode(STDOUT, "utf8"); #output as utf8
49 =head1 VARIABLES
51 =head2 $format_to_stylesheet_map
53 This hashref of hashrefs maps from a MARC flavour and unAPI format
54 to the stylesheet that should be used to transform the bib MARCXML
55 to the desired output format. As new MARC XSLT stylesheets are added,
56 (particularly for UNIMARC), this map should be updated. Of course,
57 if/when we add support for emitting a format that is not genreated
58 by a stylesheet, the structure of this variable will have to be changed.
59 At present, this doubles as the list of output formats supported by
60 this unAPI implementation.
62 =cut
64 my $format_to_stylesheet_map = {
65 'MARC21' => {
66 'marcxml' => 'identity.xsl',
67 'marcxml-full' => 'identity.xsl',
68 'mods' => 'MARC21slim2MODS.xsl',
69 'mods-full' => 'MARC21slim2MODS.xsl',
70 'mods3' => 'MARC21slim2MODS3-1.xsl',
71 'mods3-full' => 'MARC21slim2MODS3-1.xsl',
72 'oai_dc' => 'MARC21slim2OAIDC.xsl',
73 'rdfdc', => 'MARC21slim2RDFDC.xsl',
74 'rss2' => 'MARC21slim2RSS2.xsl',
75 'rss2-full' => 'MARC21slim2RSS2.xsl',
76 'srw_dc' => 'MARC21slim2SRWDC.xsl',
78 'UNIMARC' => {
79 'marcxml' => 'identity.xsl',
80 'marcxml-full' => 'identity.xsl',
81 'oai_dc' => 'UNIMARCslim2OAIDC.xsl',
85 =head2 $format_info
87 This hashref maps from unAPI output formats to the <format> elements
88 used to describe them in an unAPI format request.
90 =cut
92 my $format_info = {
93 '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"/>),
94 '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"/>),
95 '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"/>),
96 '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"/>),
97 '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"/>),
98 '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"/>),
99 '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"/>),
100 '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/"/>),
101 'rss2' => q(<format name="rss2" type="application/xml"/>),
102 'rss2-full' => q(<format name="rss2-full" type="application/xml"/>),
103 '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"/>),
106 my $id = $cgi->param('id');
107 my $format = $cgi->param('format');
109 if (not defined $format) {
110 emit_formats($id);
111 } elsif ($id) {
113 # koha:biblionumber:0152018484
114 if ($id =~ /koha:biblionumber:(\d+)/) {
115 my $biblionumber = $1;
117 my $content;
118 eval {
119 my $marcxml = GetXmlBiblio($biblionumber);
120 unless (defined $marcxml) {
121 # no bib, so 404
122 print $cgi->header( -status => '404 record not found');
123 exit 0;
126 my $transformer = get_transformer($format);
127 unless (defined $transformer) {
128 print $cgi->header( -status => '406 invalid format requested' );
129 exit 0;
131 my $parser = XML::LibXML->new();
132 my $record_dom = $parser->parse_string( $marcxml );
133 $record_dom = $transformer->transform( $record_dom );
134 $content = $record_dom->toString();
136 if ($@) {
137 print $cgi->header( -status => '500 internal error ' . $@->code() . ": " . $@->message() );
138 exit 0;
141 print $cgi->header( -type =>'application/xml' );
142 print $content;
143 } else {
144 # ID is obviously wrong, so 404
145 print $cgi->header( -status => '404 record not found');
146 exit 0;
148 } else {
149 # supplied a format but no id - caller is doing it wrong
150 print $cgi->header( -status => '400 bad request - if you specify format, must specify id');
151 exit 0;
154 exit 0;
156 sub emit_formats {
157 my $id = shift;
159 if (defined $id) {
160 print $cgi->header( -type =>'application/xml', -status => '300 multiple choices' );
161 } else {
162 print $cgi->header( -type =>'application/xml' );
165 print "<?xml version='1.0' encoding='utf-8' ?>\n";
166 if (defined $id) {
167 print qq(<formats id="$id">\n);
168 } else {
169 print "<formats>\n";
172 my $marcflavour = uc(C4::Context->preference('marcflavour'));
173 foreach my $format (sort keys %{ $format_to_stylesheet_map->{$marcflavour} }) {
174 print $format_info->{$format}, "\n";
176 print "</formats>\n";
177 return;
181 sub get_transformer {
182 my $format = lc shift;
184 my $marcflavour = uc(C4::Context->preference('marcflavour'));
185 return unless $format_to_stylesheet_map->{$marcflavour}->{$format};
187 my $xslt_file = C4::Context->config('intranetdir') .
188 "/koha-tmpl/intranet-tmpl/prog/en/xslt/" .
189 $format_to_stylesheet_map->{$marcflavour}->{$format};
191 my $parser = XML::LibXML->new();
192 my $xslt = XML::LibXSLT->new();
193 my $style_doc = $parser->parse_file( $xslt_file );
194 my $stylesheet = $xslt->parse_stylesheet( $style_doc );
196 return $stylesheet;
199 =head1 AUTHOR
201 Koha Development team <info@koha.org>
203 Originally written by Joshua Ferraro <jmf@liblime.com>
205 Improved by Galen Charlton <galen.charlton@liblime.com>
207 =cut