Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / opac / unapi
index 7b21ee0..1721f41 100755 (executable)
@@ -17,8 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 =head1 NAME
 
@@ -40,8 +39,7 @@ an XML format such as OAI DC, RSS2, MARCXML, or MODS.
 use CGI qw ( -utf8 );
 use C4::Context;
 use C4::Biblio;
-use XML::LibXML;
-use XML::LibXSLT;
+use Koha::XSLT::Base;
 
 my $cgi = CGI->new();
 binmode(STDOUT, ":encoding(UTF-8)"); #output as utf8
@@ -92,6 +90,8 @@ my $format_to_stylesheet_map = {
         'marcxml'      => 'identity.xsl',
         'marcxml-full' => 'identity.xsl',
         'oai_dc'       => 'UNIMARCslim2OAIDC.xsl',
+        'rdfdc',       => 'UNIMARCslim2RDFDC.xsl',
+        'srw_dc'       => 'UNIMARCslim2SRWDC.xsl',
     },
 };
 
@@ -128,30 +128,31 @@ if (not defined $format) {
         my $biblionumber = $1;
 
         my $content;
-        eval {
-            my $marcxml = GetXmlBiblio($biblionumber);
-            unless (defined $marcxml) {
-                # no bib, so 404
-                print $cgi->header( -status => '404 record not found');
-                exit 0;
-            }
-
-            my $transformer = get_transformer($format, $format_to_stylesheet_map, $format_info);
-            unless (defined $transformer) {
-                print $cgi->header( -status => '406 invalid format requested' );
-                exit 0;
-            }
-            my $parser = XML::LibXML->new();
-            my $record_dom = $parser->parse_string( $marcxml );
-            $record_dom = $transformer->transform( $record_dom );
-            $content = $record_dom->toString();
-        };
-        if ($@) {
-            print $cgi->header( -status => '500 internal error ' . $@->code() . ": " . $@->message() );
+
+        my $marcxml = GetXmlBiblio($biblionumber);
+        unless (defined $marcxml) {
+            # no bib, so 404
+            print $cgi->header( -status => '404 record not found');
             exit 0;
         }
 
-        print $cgi->header( -type =>'application/xml' );
+        my $xslt_file = get_xslt_file( $format, $format_to_stylesheet_map, $format_info );
+        unless( defined $xslt_file ) {
+            print $cgi->header( -status => '406 invalid format requested' );
+            exit 0;
+        }
+        my $xslt_engine = Koha::XSLT::Base->new;
+        $content = $xslt_engine->transform({
+            xml => $marcxml,
+            file => $xslt_file,
+        });
+
+        if( !defined $content || $xslt_engine->err ) {
+            print $cgi->header( -status => '500 internal error' );
+            exit 0;
+        }
+
+        print $cgi->header( -type =>'application/xml', -charset => 'UTF-8' );
         print $content;
     } else {
         # ID is obviously wrong, so 404
@@ -172,7 +173,7 @@ sub emit_formats {
     if (defined $id) {
         print $cgi->header( -type =>'application/xml', -status => '300 multiple choices' );
     } else {
-        print $cgi->header( -type =>'application/xml' );
+        print $cgi->header( -type =>'application/xml', -status => '200 Ok' );
     }
 
     print "<?xml version='1.0' encoding='utf-8'  ?>\n";
@@ -191,7 +192,7 @@ sub emit_formats {
 }
 
 
-sub get_transformer {
+sub get_xslt_file {
     my ($format, $format_to_stylesheet_map, $format_info) = @_;
     $format = lc $format;
 
@@ -202,12 +203,7 @@ sub get_transformer {
                     "/prog/en/xslt/" .
                     $format_to_stylesheet_map->{$marcflavour}->{$format};
 
-    my $parser = XML::LibXML->new();
-    my $xslt = XML::LibXSLT->new();
-    my $style_doc = $parser->parse_file( $xslt_file );
-    my $stylesheet = $xslt->parse_stylesheet( $style_doc );
-
-    return $stylesheet;
+    return $xslt_file;
 }
 
 =head1 AUTHOR