[Koha-cvs] koha/C4 Import.pm [rel_2_2]

Mason James szrj1m at yahoo.com
Thu Nov 15 04:48:58 CET 2007


CVSROOT:	/sources/koha
Module name:	koha
Branch:		rel_2_2
Changes by:	Mason James <sushi>	07/11/15 03:48:58

Added files:
	C4             : Import.pm 

Log message:
	some handy subs() for importing marc, 

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/C4/Import.pm?cvsroot=koha&only_with_tag=rel_2_2&rev=1.1.2.1

Patches:
Index: Import.pm
===================================================================
RCS file: Import.pm
diff -N Import.pm
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Import.pm	15 Nov 2007 03:48:58 -0000	1.1.2.1
@@ -0,0 +1,144 @@
+#!/usr/bin/perl -w
+
+package C4::Import;
+
+use strict;
+
+use MARC::File::USMARC;
+use MARC::Record;
+use MARC::Batch;
+use MARC::Charset;
+MARC::Charset->ignore_errors(1);
+
+use C4::Context;
+
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+
+$VERSION = 0.01;
+
+ at ISA = qw(Exporter);
+
+ at EXPORT = qw(
+  &fMARC8ToUTF8 strip_blank_subfields
+);
+
+sub strip_blank_subfields {
+    my ($record) = @_;
+### $record;
+
+    my @del_fields = $record->fields();
+    foreach my $del_field (@del_fields) {
+        my @subs = $del_field->subfields();
+        if ( $del_field->{'_tag'} > 10 ) {
+            foreach my $sub (@subs) {
+                my $name  = $$sub[0];
+                my $value = $$sub[1];
+                if ( !$value && $value ne '0' ) {
+                    $del_field->delete_subfield( code => $name );
+                }
+            }
+        }
+    }
+    return $record;
+}
+
+sub fMARC8ToUTF8($$) {
+    my ($record)  = shift;
+    my ($verbose) = shift;
+    if ($verbose) {
+        if ( $verbose >= 2 ) {
+            my $leader = $record->leader();
+            $leader =~ s/ /#/g;
+            print "\n000 " . $leader;
+        }
+    }
+    foreach my $field ( $record->fields() ) {
+        if ( $field->is_control_field() ) {
+            if ($verbose) {
+                if ( $verbose >= 2 ) {
+                    my $fieldName  = $field->tag();
+                    my $fieldValue = $field->data();
+                    $fieldValue =~ s/ /#/g;
+                    print "\n" . $fieldName;
+                    print ' ' . $fieldValue;
+                }
+            }
+        }
+        else {
+            my @subfieldsArray;
+            my $fieldName       = $field->tag();
+            my $indicator1Value = $field->indicator(1);
+            my $indicator2Value = $field->indicator(2);
+            if ($verbose) {
+                if ( $verbose >= 2 ) {
+                    $indicator1Value =~ s/ /#/;
+                    $indicator2Value =~ s/ /#/;
+                    print "\n"
+                      . $fieldName . ' '
+                      . $indicator1Value
+                      . $indicator2Value;
+                }
+            }
+            foreach my $subfield ( $field->subfields() ) {
+                my $subfieldName  = $subfield->[0];
+                my $subfieldValue = $subfield->[1];
+                $subfieldValue = MARC::Charset::marc8_to_utf8($subfieldValue);
+
+                # Alas, MARC::Field::update() does not work correctly.
+                ## push (@subfieldsArray, $subfieldName, $subfieldValue);
+
+                push @subfieldsArray, [ $subfieldName, $subfieldValue ];
+                if ($verbose) {
+                    if ( $verbose >= 2 ) {
+                        print " \$" . $subfieldName . ' ' . $subfieldValue;
+                    }
+                }
+            }
+
+            # Alas, MARC::Field::update() does not work correctly.
+            #
+            # The first instance in the field of a of a repeated subfield
+            # overwrites the content from later instances with the content
+            # from the first instance.
+            ## $field->update(@subfieldsArray);
+
+            foreach my $subfieldRow (@subfieldsArray) {
+                my $subfieldName = $subfieldRow->[0];
+                $field->delete_subfields($subfieldName);
+            }
+            foreach my $subfieldRow (@subfieldsArray) {
+                $field->add_subfields(@$subfieldRow);
+            }
+
+            if ($verbose) {
+                if ( $verbose >= 2 ) {
+
+                    # Reading the indicator values again is not necessary.
+                    # They were not converted.
+                    # $indicator1Value = $field->indicator(1);
+                    # $indicator2Value = $field->indicator(2);
+                    # $indicator1Value =~ s/ /#/;
+                    # $indicator2Value =~ s/ /#/;
+                    print "\nCONVERTED TO UTF-8:\n"
+                      . $fieldName . ' '
+                      . $indicator1Value
+                      . $indicator2Value;
+                    foreach my $subfield ( $field->subfields() ) {
+                        my $subfieldName  = $subfield->[0];
+                        my $subfieldValue = $subfield->[1];
+                        print " \$" . $subfieldName . ' ' . $subfieldValue;
+                    }
+                }
+            }
+            if ($verbose) {
+                if ( $verbose >= 2 ) {
+                    print "\n" if $verbose;
+                }
+            }
+        }
+    }
+    $record->encoding('UTF-8');
+    return $record;
+}
+
+1;





More information about the Koha-cvs mailing list