utility to generate SQL to populate ccvm for MARC21 fixed fields
authorGalen Charlton <gmc@esilibrary.com>
Thu, 10 Apr 2014 21:00:16 +0000 (14:00 -0700)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 10 Apr 2014 21:00:16 +0000 (14:00 -0700)
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 <gmc@esilibrary.com>
generate_fixed_field_ccvm_from_koha [new file with mode: 0755]

diff --git a/generate_fixed_field_ccvm_from_koha b/generate_fixed_field_ccvm_from_koha
new file mode 100755 (executable)
index 0000000..4080737
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+
+# Copyright (C) 2014 Equinox Software, Inc.
+# Original author: Galen Charlton <gmc@esilibrary.com>
+# 
+# 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 <http://www.gnu.org/licenses/>.
+
+# 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;