View | Details | Raw Unified | Return to bug 11826
Collapse All | Expand All

(-)a/C4/XSLT.pm (-51 / +48 lines)
Lines 1-4 Link Here
1
package C4::XSLT;
1
package C4::XSLT;
2
2
# Copyright (C) 2006 LibLime
3
# Copyright (C) 2006 LibLime
3
# <jmf at liblime dot com>
4
# <jmf at liblime dot com>
4
# Parts Copyright Katrin Fischer 2011
5
# Parts Copyright Katrin Fischer 2011
Lines 9-15 package C4::XSLT; Link Here
9
#
10
#
10
# Koha is free software; you can redistribute it and/or modify it under the
11
# Koha is free software; you can redistribute it and/or modify it under the
11
# terms of the GNU General Public License as published by the Free Software
12
# terms of the GNU General Public License as published by the Free Software
12
# Foundation; either version 2 of the License, or (at your option) any later
13
# Foundation; either version 3 of the License, or (at your option) any later
13
# version.
14
# version.
14
#
15
#
15
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
Lines 30-50 use C4::Koha; Link Here
30
use C4::Biblio;
31
use C4::Biblio;
31
use C4::Circulation;
32
use C4::Circulation;
32
use C4::Reserves;
33
use C4::Reserves;
34
use Koha::XSLT_Handler;
35
33
use Encode;
36
use Encode;
34
use XML::LibXML;
35
use XML::LibXSLT;
36
use LWP::Simple;
37
37
38
use vars qw($VERSION @ISA @EXPORT);
38
use vars qw($VERSION @ISA @EXPORT);
39
39
40
my $engine; #XSLT Handler object
41
my %authval_per_framework;
42
    # Cache for tagfield-tagsubfield to decode per framework.
43
    # Should be preferably be placed in Koha-core...
44
40
BEGIN {
45
BEGIN {
41
    require Exporter;
46
    require Exporter;
42
    $VERSION = 3.07.00.049;
47
    $VERSION = 3.07.00.049;
43
    @ISA = qw(Exporter);
48
    @ISA = qw(Exporter);
44
    @EXPORT = qw(
49
    @EXPORT = qw(
45
        &XSLTParse4Display
50
        &XSLTParse4Display
46
        &GetURI
47
    );
51
    );
52
    $engine=Koha::XSLT_Handler->new( { do_not_return_source => 1 } );
48
}
53
}
49
54
50
=head1 NAME
55
=head1 NAME
Lines 53-74 C4::XSLT - Functions for displaying XSLT-generated content Link Here
53
58
54
=head1 FUNCTIONS
59
=head1 FUNCTIONS
55
60
56
=head2 GetURI
57
58
GetURI file and returns the xslt as a string
59
60
=cut
61
62
sub GetURI {
63
    my ($uri) = @_;
64
    my $string;
65
    $string = get $uri ;
66
    return $string;
67
}
68
69
=head2 transformMARCXML4XSLT
61
=head2 transformMARCXML4XSLT
70
62
71
Replaces codes with authorized values in a MARC::Record object
63
Replaces codes with authorized values in a MARC::Record object
64
Is only used in this module currently.
72
65
73
=cut
66
=cut
74
67
Lines 108-120 sub transformMARCXML4XSLT { Link Here
108
=head2 getAuthorisedValues4MARCSubfields
101
=head2 getAuthorisedValues4MARCSubfields
109
102
110
Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
103
Returns a ref of hash of ref of hash for tag -> letter controled by authorised values
104
Is only used in this module currently.
111
105
112
=cut
106
=cut
113
107
114
# Cache for tagfield-tagsubfield to decode per framework.
115
# Should be preferably be placed in Koha-core...
116
my %authval_per_framework;
117
118
sub getAuthorisedValues4MARCSubfields {
108
sub getAuthorisedValues4MARCSubfields {
119
    my ($frameworkcode) = @_;
109
    my ($frameworkcode) = @_;
120
    unless ( $authval_per_framework{ $frameworkcode } ) {
110
    unless ( $authval_per_framework{ $frameworkcode } ) {
Lines 134-140 sub getAuthorisedValues4MARCSubfields { Link Here
134
    return $authval_per_framework{ $frameworkcode };
124
    return $authval_per_framework{ $frameworkcode };
135
}
125
}
136
126
137
my $stylesheet;
127
=head2 XSLTParse4Display
128
129
Returns xml for biblionumber and requested XSLT transformation.
130
Returns undef if the transform fails.
131
132
Used in OPAC results and detail, intranet results and detail, list display.
133
(Depending on the settings of your XSLT preferences.)
134
135
The helper function _get_best_default_xslt_filename is used in a unit test.
136
137
=cut
138
138
139
sub _get_best_default_xslt_filename {
139
sub _get_best_default_xslt_filename {
140
    my ($htdocs, $theme, $lang, $base_xslfile) = @_;
140
    my ($htdocs, $theme, $lang, $base_xslfile) = @_;
Lines 195-201 sub XSLTParse4Display { Link Here
195
195
196
    # grab the XML, run it through our stylesheet, push it out to the browser
196
    # grab the XML, run it through our stylesheet, push it out to the browser
197
    my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
197
    my $record = transformMARCXML4XSLT($biblionumber, $orig_record);
198
    #return $record->as_formatted();
199
    my $itemsxml  = buildKohaItemsNamespace($biblionumber, $hidden_items);
198
    my $itemsxml  = buildKohaItemsNamespace($biblionumber, $hidden_items);
200
    my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
199
    my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour'));
201
    my $sysxml = "<sysprefs>\n";
200
    my $sysxml = "<sysprefs>\n";
Lines 221-247 sub XSLTParse4Display { Link Here
221
    $xmlrecord =~ s/\& /\&amp\; /;
220
    $xmlrecord =~ s/\& /\&amp\; /;
222
    $xmlrecord =~ s/\&amp\;amp\; /\&amp\; /;
221
    $xmlrecord =~ s/\&amp\;amp\; /\&amp\; /;
223
222
224
    my $parser = XML::LibXML->new();
223
    #If the xslt should fail, we will return undef (old behavior was
225
    # don't die when you find &, >, etc
224
    #raw MARC)
226
    $parser->recover_silently(0);
225
    #Note that we did set do_not_return_source at object construction
227
    my $source = $parser->parse_string($xmlrecord);
226
    return $engine->transform($xmlrecord, $xslfilename ); #file or URL
228
    unless ( $stylesheet->{$xslfilename} ) {
229
        my $xslt = XML::LibXSLT->new();
230
        my $style_doc;
231
        if ( $xslfilename =~ /^https?:\/\// ) {
232
            my $xsltstring = GetURI($xslfilename);
233
            $style_doc = $parser->parse_string($xsltstring);
234
        } else {
235
            use Cwd;
236
            $style_doc = $parser->parse_file($xslfilename);
237
        }
238
        $stylesheet->{$xslfilename} = $xslt->parse_stylesheet($style_doc);
239
    }
240
    my $results      = $stylesheet->{$xslfilename}->transform($source);
241
    my $newxmlrecord = $stylesheet->{$xslfilename}->output_string($results);
242
    return $newxmlrecord;
243
}
227
}
244
228
229
=head2 buildKohaItemsNamespace
230
231
Returns XML for items.
232
Is only used in this module currently.
233
234
=cut
235
245
sub buildKohaItemsNamespace {
236
sub buildKohaItemsNamespace {
246
    my ($biblionumber, $hidden_items) = @_;
237
    my ($biblionumber, $hidden_items) = @_;
247
238
Lines 304-329 sub buildKohaItemsNamespace { Link Here
304
                "<holdingbranch>$holdingbranch</holdingbranch>".
295
                "<holdingbranch>$holdingbranch</holdingbranch>".
305
                "<location>$location</location>".
296
                "<location>$location</location>".
306
                "<ccode>$ccode</ccode>".
297
                "<ccode>$ccode</ccode>".
307
		"<status>$status</status>".
298
                "<status>$status</status>".
308
		"<itemcallnumber>".$itemcallnumber."</itemcallnumber>"
299
                "<itemcallnumber>".$itemcallnumber."</itemcallnumber>".
309
        . "</item>";
300
                "</item>";
310
311
    }
301
    }
312
    $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
302
    $xml = "<items xmlns=\"http://www.koha-community.org/items\">".$xml."</items>";
313
    return $xml;
303
    return $xml;
314
}
304
}
315
305
306
=head2 engine
316
307
308
Returns reference to XSLT handler object.
317
309
318
1;
310
=cut
319
__END__
320
311
321
=head1 NOTES
312
sub engine {
313
    return $engine;
314
}
322
315
323
=cut
316
1;
317
318
__END__
324
319
325
=head1 AUTHOR
320
=head1 AUTHOR
326
321
327
Joshua Ferraro <jmf@liblime.com>
322
Joshua Ferraro <jmf@liblime.com>
328
323
324
Koha Development Team <http://koha-community.org/>
325
329
=cut
326
=cut
(-)a/Koha/XSLT_Handler.pm (-2 / +2 lines)
Lines 131-136 __PACKAGE__->mk_accessors(qw( do_not_return_source )); Link Here
131
    my $output2= $xslt_engine->transform( $xml2 );
131
    my $output2= $xslt_engine->transform( $xml2 );
132
132
133
    Pass a xml string and a fully qualified path of a XSLT file.
133
    Pass a xml string and a fully qualified path of a XSLT file.
134
    Instead of a filename, you may also pass a URL.
134
    If you do not pass a filename, the last file used is assumed.
135
    If you do not pass a filename, the last file used is assumed.
135
    Returns the transformed string.
136
    Returns the transformed string.
136
    Check the error number in err to know if something went wrong.
137
    Check the error number in err to know if something went wrong.
Lines 240-246 sub _init { Link Here
240
sub _load {
241
sub _load {
241
    my ($self, $file)= @_;
242
    my ($self, $file)= @_;
242
243
243
    if( !$file || !-e $file ) {
244
    if( !$file || ( $file!~ /^https?:\/\// && !-e $file ) ) {
244
        $self->_set_error(2);
245
        $self->_set_error(2);
245
        return;
246
        return;
246
    }
247
    }
247
- 

Return to bug 11826