From ffc8c5e347f9c3f7d93dc14ec7ec0dc44920b63f Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Thu, 10 Apr 2014 14:00:16 -0700 Subject: [PATCH] utility to generate SQL to populate ccvm for MARC21 fixed fields This is a little script to generate seed SQL to populate ccvm for various MARC21 fixed fields, using an XML file from the Koha project as input. Usage: generate_fixed_field_ccvm_koha marc21_field_008.xml ff_seed.sql The output is meant to be included in 950.data.seed-values.sql. Signed-off-by: Galen Charlton --- generate_fixed_field_ccvm_from_koha | 101 ++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 generate_fixed_field_ccvm_from_koha diff --git a/generate_fixed_field_ccvm_from_koha b/generate_fixed_field_ccvm_from_koha new file mode 100755 index 000000000..4080737dd --- /dev/null +++ b/generate_fixed_field_ccvm_from_koha @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +# Copyright (C) 2014 Equinox Software, Inc. +# Original author: Galen Charlton +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Generate seed SQL to populate ccvm for various MARC21 +# fixed fields, using an XML file from the Koha project +# as input. +# +# Usage: generate_fixed_field_ccvm_koha marc21_field_008.xml ff_seed.sql + +use strict; +use warnings; + +use XML::LibXML; + +# XML file containing fixed field definitions; +# meant to be something like the Koha project's marc21_field_008.xml +my $datafile = $ARGV[0]; + +# starting point for the config.coded_value_map IDs +my $ccvm_id = 633; + +# name of output file; contents meant +# for +my $seed_sql_file = $ARGV[1]; +open SEED, '>', $seed_sql_file; + +my $xpc = XML::LibXML::XPathContext->new; +$xpc->registerNs('koha', 'http://koha-community.org'); + +my $doc = XML::LibXML->new->parse_file($datafile); + +# Mapping from the field label in the Koha XML +# file to config.record_attr_definition.name; only +# fields listed in this hash will have SQL to populate +# be generated +my %koha_eg = ( + 'Original alphabet or script of title' => 'alph', + 'Type of date/Publication status' => 'pub_status', + 'Biography' => 'biog', + 'Conference publication' => 'conf', + 'Illustrations - code 1' => 'ills', + 'Festschrift' => 'fest', + 'Government publication' => 'gpub', + 'Index' => 'indx', + 'Modified record' => 'mrec', + 'Entry convention' => 'ff_sl', + 'Type of visual material' => 'type_mat', +); + +# hash to store fixed field values +my %ff_values; + +my @pos = $xpc->findnodes('//koha:Position', $doc->documentElement); +foreach my $pos (@pos) { + my $name = $xpc->findvalue('koha:name', $pos); + foreach my $value ($xpc->findnodes('koha:Value', $pos)) { + my $desc = $xpc->findvalue('koha:description', $value); + my $code = $value->getAttribute('code'); + $code = ' ' if $code eq '#'; + if (exists $ff_values{$name}->{$code} && $desc ne $ff_values{$name}->{$code}) { + print "WARNING: difference in value for $name/$code\n"; + print " previous: $ff_values{$name}->{$code}\n"; + print " new: $desc\n"; + } + $ff_values{$name}->{$code} = $desc; + } +} + +print SEED "DELETE FROM config.coded_value_map WHERE id >= 634 AND id < 10000;\n"; +foreach my $key (sort keys %ff_values) { + if (exists $koha_eg{$key}) { + print "Generating for $key\n"; + print SEED "-- $koha_eg{$key} / $key\n"; + print SEED "INSERT INTO config.coded_value_map (id, ctype, code, value) VALUES\n"; + print SEED join( + ",\n", + map { + my $code = $_; + $ccvm_id++; + " ($ccvm_id, '$koha_eg{$key}', '$code', oils_i18n_gettext('$ccvm_id', '$ff_values{$key}->{$code}', 'ccvm', 'value'))"; + } sort keys %{ $ff_values{$key} } + ); + print SEED "\n;\n\n"; + } +} +close SEED; -- 2.11.0