Bug 18309: Add UNIMARC field 214 and its subfields
[koha.git] / misc / maintenance / generate_MARC21Languages.pl
blob2394ada38e74d82d02c9523976459dd810e37bae
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 <xsl:text>Unknown language code</xsl:text>
105 </xsl:otherwise>
106 </xsl:choose>
107 </xsl:template>
108 </xsl:stylesheet>
109 FOOTER
110 return;
113 =head1 NAME
115 generate_MARC21Languages.pl
117 =head1 SYNOPSIS
119 generate_MARC21Languages.pl
120 generate_MARC21Languages.pl --url='http://www.loc.gov/standards/codelists/languages.xml'
122 =head1 DESCRIPTION
124 Create MARC21Languages.xsl from the loc.gov MARC21 Code List for Languages
126 =over 8
128 =item B<--help>
130 Prints this help
132 =item B<--url>
134 Fetch the languages XML from this url. Defaults to http://www.loc.gov/standards/codelists/languages.xml
136 =item B<--output>
138 Writes the output XML into this file. Defaults to STDOUT.
140 =back
142 =cut