Bug 10691: use authority ID in see-also links in authority search results (staff)
[koha.git] / opac / unapi
bloba07434500a7f6fba561b2bc23b831e01c002ff99
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, ":encoding(UTF-8)"); #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 'NORMARC' => {
79 'marcxml' => 'identity.xsl',
80 'marcxml-full' => 'identity.xsl',
81 'mods' => 'MARC21slim2MODS.xsl',
82 'mods-full' => 'MARC21slim2MODS.xsl',
83 'mods3' => 'MARC21slim2MODS3-1.xsl',
84 'mods3-full' => 'MARC21slim2MODS3-1.xsl',
85 'oai_dc' => 'MARC21slim2OAIDC.xsl',
86 'rdfdc', => 'MARC21slim2RDFDC.xsl',
87 'rss2' => 'MARC21slim2RSS2.xsl',
88 'rss2-full' => 'MARC21slim2RSS2.xsl',
89 'srw_dc' => 'MARC21slim2SRWDC.xsl',
91 'UNIMARC' => {
92 'marcxml' => 'identity.xsl',
93 'marcxml-full' => 'identity.xsl',
94 'oai_dc' => 'UNIMARCslim2OAIDC.xsl',
98 =head2 $format_info
100 This hashref maps from unAPI output formats to the <format> elements
101 used to describe them in an unAPI format request.
103 =cut
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"/>),
119 my $id = $cgi->param('id');
120 my $format = $cgi->param('format');
122 if (not defined $format) {
123 emit_formats($id, $format_to_stylesheet_map, $format_info, $cgi);
124 } elsif ($id) {
126 # koha:biblionumber:0152018484
127 if ($id =~ /koha:biblionumber:(\d+)/) {
128 my $biblionumber = $1;
130 my $content;
131 eval {
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;
139 my $transformer = get_transformer($format, $format_to_stylesheet_map, $format_info);
140 unless (defined $transformer) {
141 print $cgi->header( -status => '406 invalid format requested' );
142 exit 0;
144 my $parser = XML::LibXML->new();
145 my $record_dom = $parser->parse_string( $marcxml );
146 $record_dom = $transformer->transform( $record_dom );
147 $content = $record_dom->toString();
149 if ($@) {
150 print $cgi->header( -status => '500 internal error ' . $@->code() . ": " . $@->message() );
151 exit 0;
154 print $cgi->header( -type =>'application/xml' );
155 print $content;
156 } else {
157 # ID is obviously wrong, so 404
158 print $cgi->header( -status => '404 record not found');
159 exit 0;
161 } else {
162 # supplied a format but no id - caller is doing it wrong
163 print $cgi->header( -status => '400 bad request - if you specify format, must specify id');
164 exit 0;
167 exit 0;
169 sub emit_formats {
170 my ($id, $format_to_stylesheet_map, $format_info, $cgi) = @_;
172 if (defined $id) {
173 print $cgi->header( -type =>'application/xml', -status => '300 multiple choices' );
174 } else {
175 print $cgi->header( -type =>'application/xml' );
178 print "<?xml version='1.0' encoding='utf-8' ?>\n";
179 if (defined $id) {
180 print qq(<formats id="$id">\n);
181 } else {
182 print "<formats>\n";
185 my $marcflavour = uc(C4::Context->preference('marcflavour'));
186 foreach my $format (sort keys %{ $format_to_stylesheet_map->{$marcflavour} }) {
187 print $format_info->{$format}, "\n";
189 print "</formats>\n";
190 return;
194 sub get_transformer {
195 my ($format, $format_to_stylesheet_map, $format_info) = @_;
196 $format = lc $format;
198 my $marcflavour = uc(C4::Context->preference('marcflavour'));
199 return unless $format_to_stylesheet_map->{$marcflavour}->{$format};
201 my $xslt_file = C4::Context->config('intranetdir') .
202 "/koha-tmpl/intranet-tmpl/prog/en/xslt/" .
203 $format_to_stylesheet_map->{$marcflavour}->{$format};
205 my $parser = XML::LibXML->new();
206 my $xslt = XML::LibXSLT->new();
207 my $style_doc = $parser->parse_file( $xslt_file );
208 my $stylesheet = $xslt->parse_stylesheet( $style_doc );
210 return $stylesheet;
213 =head1 AUTHOR
215 Koha Development Team <http://koha-community.org/>
217 Originally written by Joshua Ferraro <jmf@liblime.com>
219 Improved by Galen Charlton <galen.charlton@liblime.com>
221 =cut