Bug 22075: Fix encoding problem with RIS export in OPAC
[koha.git] / opac / opac-export.pl
1 #!/usr/bin/perl
2
3 # Parts Copyright Catalyst IT 2011
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 use C4::Record;
23 use C4::Auth;
24 use C4::Output;
25 use C4::Biblio;
26 use CGI qw ( -utf8 );
27 use C4::Auth;
28 use C4::Ris;
29 use Koha::RecordProcessor;
30
31 my $query = CGI->new;
32 my $op=$query->param("op")||''; #op=export is currently the only use
33 my $format=$query->param("format")||'utf8';
34 my $biblionumber = $query->param("bib")||0;
35 $biblionumber = int($biblionumber);
36 my $error = q{};
37
38 # Determine logged in user's patron category.
39 # Blank if not logged in.
40 my $userenv = C4::Context->userenv;
41 my $borcat = q{};
42 if ($userenv) {
43     my $borrowernumber = $userenv->{'number'};
44     if ($borrowernumber) {
45         my $borrower = Koha::Patrons->find( { borrowernumber => $borrowernumber } );
46         $borcat = $borrower ? $borrower->categorycode : $borcat;
47     }
48 }
49
50 my $include_items = ($format =~ /bibtex/) ? 0 : 1;
51 my $marc = $biblionumber
52     ? GetMarcBiblio({
53         biblionumber => $biblionumber,
54         embed_items  => $include_items,
55         opac         => 1,
56         borcat       => $borcat })
57     : undef;
58
59 if(!$marc) {
60     print $query->redirect("/cgi-bin/koha/errors/404.pl");
61     exit;
62 }
63
64 # ASSERT: There is a biblionumber, because GetMarcBiblio returned something.
65 my $framework = GetFrameworkCode( $biblionumber );
66 my $record_processor = Koha::RecordProcessor->new({
67     filters => 'ViewPolicy',
68     options => {
69         interface => 'opac',
70         frameworkcode => $framework
71     }
72 });
73 $record_processor->process($marc);
74
75 if ($format =~ /endnote/) {
76     $marc = marc2endnote($marc);
77     $format = 'endnote';
78 }
79 elsif ($format =~ /marcxml/) {
80     $marc = marc2marcxml($marc);
81     $format = 'marcxml';
82 }
83 elsif ($format=~ /mods/) {
84     $marc = marc2modsxml($marc);
85     $format = 'mods';
86 }
87 elsif ($format =~ /ris/) {
88     $marc = marc2ris($marc);
89     $format = 'ris';
90 }
91 elsif ($format =~ /bibtex/) {
92     $marc = marc2bibtex($marc,$biblionumber);
93     $format = 'bibtex';
94 }
95 elsif ($format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
96     # TODO: Dublin Core leaks fields marked hidden by framework.
97     $marc = marc2dcxml($marc, undef, $biblionumber, $format);
98     $format = "dublin-core.xml";
99 }
100 elsif ($format =~ /marc8/) {
101     ($error,$marc) = changeEncoding($marc,"MARC","MARC21","MARC-8");
102     $marc = $marc->as_usmarc() unless $error;
103     $format = 'marc8';
104 }
105 elsif ($format =~ /utf8/) {
106     C4::Charset::SetUTF8Flag($marc,1);
107     $marc = $marc->as_usmarc();
108     $format = 'utf8';
109 }
110 elsif ($format =~ /marcstd/) {
111     C4::Charset::SetUTF8Flag($marc,1);
112     ($error,$marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
113     $format = 'marcstd';
114 }
115 elsif ( $format =~ /isbd/ ) {
116     $marc   = GetISBDView({
117         'record'    => $marc,
118         'template'  => 'opac',
119         'framework' => $framework,
120     });
121     $format = 'isbd';
122 }
123 else {
124     $error= "Format $format is not supported.";
125 }
126
127 if ($error){
128     print $query->header();
129     print $query->start_html();
130     print "<h1>An error occurred </h1>";
131     print $query->escapeHTML("$error");
132     print $query->end_html();
133 }
134 else {
135     if ($format eq 'marc8'){
136         print $query->header(
137             -type => 'application/marc',
138             -charset=>'ISO-2022',
139             -attachment=>"bib-$biblionumber.$format");
140     }
141     elsif ( $format eq 'isbd' ) {
142         print $query->header(
143             -type       => 'text/plain',
144             -charset    => 'utf-8',
145             -attachment =>  "bib-$biblionumber.txt"
146         );
147     }
148     elsif ( $format eq 'ris' ) {
149         print $query->header(
150             -type => 'text/plain',
151             -charset => 'utf-8',
152             -attachment => "bib-$biblionumber.$format"
153         );
154     } else {
155         binmode STDOUT, ':encoding(UTF-8)';
156         print $query->header(
157             -type => 'application/octet-stream',
158             -charset => 'utf-8',
159             -attachment => "bib-$biblionumber.$format"
160         );
161     }
162     print $marc;
163 }
164
165 1;