Bug 26627: Print and confirming a hold can cause an infinite loop
[koha.git] / misc / maintenance / generate_MARC21Languages.pl
blobe5a151679d199f85a5038eab69299738048c69de
1 #!/usr/bin/perl
3 # Copyright (C) 2018 Koha-Suomi Oy
5 # This file is part of Koha
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.
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.
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>.
21 use Modern::Perl;
22 use XML::Simple;
23 use Pod::Usage;
24 use Getopt::Long;
25 use Carp;
27 use open ':std', ':encoding(UTF-8)';
29 sub usage {
30 pod2usage( -verbose => 2 );
31 exit;
34 # Options
35 my $sourceurl = 'http://www.loc.gov/standards/codelists/languages.xml';
36 my $help;
37 my $outfile;
38 my $tempfile = '/tmp/languages.xml';
40 GetOptions(
41 'o|output:s' => \$outfile,
42 'url:s' => \$sourceurl,
43 'help|h' => \$help,
46 usage() if $help;
48 system( qq{/usr/bin/wget $sourceurl -O $tempfile } ) == 0
49 or croak "Can't wget $sourceurl ($?)";
51 my $ref = XMLin($tempfile);
52 my $languages = $ref->{'languages'}->{'language'};
54 # output log or STDOUT
55 my $out_handle;
56 if (defined $outfile) {
57 open( $out_handle, ">", $outfile ) || croak("Cannot open output file");
58 } else {
59 open( $out_handle, ">&STDOUT" ) || croak("Couldn't duplicate STDOUT: $!");
61 generate_header($out_handle);
62 generate_body($out_handle, $languages);
63 generate_footer($out_handle);
64 close $out_handle;
67 sub generate_body {
68 my ( $file_handle, $language_list ) = @_;
70 foreach my $l ( @{$language_list} ) {
71 my $code = $l->{'code'};
72 my $name = (
73 ref( $l->{'name'} ) eq 'HASH'
74 ? $l->{'name'}{'content'}
75 : $l->{'name'}
77 next if ( ref($code) eq 'HASH' && $code->{'status'} eq 'obsolete' );
78 print {$file_handle} " <xsl:when test=\"\$code='$code'\">";
79 print {$file_handle} "<xsl:text>$name</xsl:text>";
80 print {$file_handle} "</xsl:when>";
81 print {$file_handle} "\n";
83 return;
86 sub generate_header {
87 my ($file_handle) = @_;
88 print {$file_handle} <<"HEADER";
89 <?xml version="1.0" encoding="UTF-8"?>
90 <!DOCTYPE stylesheet [<!ENTITY nbsp "&#160;" >]>
91 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
92 <!-- This file generated by generate_MARC21Languages.pl -->
93 <xsl:template name="languageCodeText">
94 <xsl:param name="code"/>
95 <xsl:choose>
96 HEADER
97 return;
100 sub generate_footer {
101 my ($file_handle) = @_;
102 print {$file_handle} <<"FOOTER";
103 <xsl:otherwise>
104 <!-- when 041\$a is mapped to an authorised value list, this will show the description -->
105 <xsl:value-of select="\$code" />
106 </xsl:otherwise>
107 </xsl:choose>
108 </xsl:template>
109 </xsl:stylesheet>
110 FOOTER
111 return;
114 =head1 NAME
116 generate_MARC21Languages.pl
118 =head1 SYNOPSIS
120 generate_MARC21Languages.pl
121 generate_MARC21Languages.pl --url='http://www.loc.gov/standards/codelists/languages.xml'
123 =head1 DESCRIPTION
125 Create MARC21Languages.xsl from the loc.gov MARC21 Code List for Languages
127 =over 8
129 =item B<--help>
131 Prints this help
133 =item B<--url>
135 Fetch the languages XML from this url. Defaults to http://www.loc.gov/standards/codelists/languages.xml
137 =item B<--output>
139 Writes the output XML into this file. Defaults to STDOUT.
141 =back
143 =cut