/[webpac]/trunk2/tools/phpmylib2marc.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk2/tools/phpmylib2marc.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 208 by dpavlin, Sun Feb 1 15:57:17 2004 UTC revision 216 by dpavlin, Sun Feb 1 22:18:05 2004 UTC
# Line 2  Line 2 
2  #  #
3  # This script will try (hard) to convert database from  # This script will try (hard) to convert database from
4  # PhpMyLibrary (http://phpmylibrary.sourceforge.net/) back  # PhpMyLibrary (http://phpmylibrary.sourceforge.net/) back
5  # to MARC format  # to MARC format (ISO 2709)
6  #  #
7  # 2003-01-31 Dobrica Pavlinusic <dpavlin@rot13.org>  # 2003-01-31 Dobrica Pavlinusic <dpavlin@rot13.org>
8  #  #
# Line 24  my $user = "root"; Line 24  my $user = "root";
24  my $passwd = "";  my $passwd = "";
25    
26  my $usage = 0;  my $usage = 0;
27    my $debug = 0;
28    
29  my $result = GetOptions(  my $result = GetOptions(
30          "database=s" => \$database,          "database=s" => \$database,
31          "host=s" => \$host,          "host=s" => \$host,
32          "user=s" => \$user,          "user=s" => \$user,
33          "password=s" => \$passwd,          "password=s" => \$passwd,
34            "debug!" => \$debug,
35          "help!" => \$usage,          "help!" => \$usage,
36          );          );
37    
# Line 43  my $dsn = "DBI:mysql:database=$database; Line 45  my $dsn = "DBI:mysql:database=$database;
45  my $dbh = DBI->connect($dsn, $user, $passwd, {'RaiseError' => 1});  my $dbh = DBI->connect($dsn, $user, $passwd, {'RaiseError' => 1});
46    
47  # UNIMARC leader format  # UNIMARC leader format
48  my $leader_fmt = qq{%05diam0 22%05d   45  };  #my $leader_fmt = qq{%05diam0 22%05d   45  };
49    # MARC leader format
50    my $leader_fmt = qq{%05dcas  22%05d a 4500};
51    
52    
53  my $sth = $dbh->prepare("SELECT marc FROM tblbib");  my $sth = $dbh->prepare("SELECT marc FROM tblbib");
54  $sth->execute();  $sth->execute();
55    
56    my $count = 0;
57    my $rec_nr = 0;
58    
59  while (my $row = $sth->fetchrow_hashref()) {  while (my $row = $sth->fetchrow_hashref()) {
60          my $marc = $row->{'marc'};          my $marc = $row->{'marc'};
61            $rec_nr++;
62    
63          my $real_len = length($marc);          my $real_len = length($marc);
64    
65            my $skip = 0;   # skip this record?
66    
67          # fix PhpMyLibrary MARC (why do I have to do this? It's MARC,          # fix PhpMyLibrary MARC (why do I have to do this? It's MARC,
68          # for gaddem sake!!!          # for gaddem sake!!!
69                    
# Line 83  while (my $row = $sth->fetchrow_hashref( Line 94  while (my $row = $sth->fetchrow_hashref(
94          #           (xxxxx)nam  22(.....)   450 <---          #           (xxxxx)nam  22(.....)   450 <---
95          $marc =~ m/^(.....)......(.....)polerioj/ || die "record: '$marc' unparsable!";          $marc =~ m/^(.....)......(.....)polerioj/ || die "record: '$marc' unparsable!";
96          my ($reclen,$base_addr) = ($1,$2);          my ($reclen,$base_addr) = ($1,$2);
         my $leader = sprintf($leader_fmt,length($marc)+1,$base_addr);  
97    
98          print $leader . substr($marc,24).chr(29);          my $directory = substr($marc,24,$base_addr-24);
99            my $fields = substr($marc,$base_addr-1);
100    
101            print STDERR "# $rec_nr fields: '$fields'\n" if ($debug);
102            print STDERR "# $rec_nr directory: [",length($directory),"]\n" if ($debug);
103    
104            # PhpMyLibrary MARC records don't have indicators, so we'll add them
105    
106            my $o = 0;      # offset
107            my $new_dictionary;
108            my $new_fields;
109    
110            while (!$skip && $directory =~ s/(\d{3})(\d{4})(\d{5})//) {
111                    my ($tag,$len,$addr) = ($1,$2,$3);
112    
113                    print STDERR "tag/len/addr: $tag $len $addr\n" if ($debug);
114    
115                    sub check_field($) {
116                            my $f = shift;
117                            my $del = substr($f,0,1);
118                    
119                            die "expected 0x1e, got '$del' (".ord($del)."): '$f'" if (ord($del) != 30);
120                    }
121    
122                    if (($addr+$len) > length($fields)) {
123                            print STDERR "WARNING: error in dictionary on record $rec_nr skipping...\n" if ($debug);
124                            $skip = 1;
125                            next;
126                    }
127    
128                    # take field
129                    my $f = substr($fields,$addr,$len);
130                    print STDERR "data $tag [$len] $addr: '$f'\n" if ($debug);
131    
132                    my $del = substr($fields,$addr+$len,1);
133    
134                    # check field delimiters...
135                    if ($del ne chr(30)) {
136                            print STDERR "WARNING: skipping record $rec_nr, can't find delimiters got: '$del'\n" if ($debug);
137                            $skip = 1;
138                            next;
139                    }
140    
141                    check_field($f);
142                            
143                    if ($tag =~ m/^00/) {
144                            # fields 001-008 doesn't have indicators
145                            $new_dictionary .= sprintf("%03d%04d%05d",$tag,$len,$addr);
146                            $new_fields.=$f;
147                    } else {
148                            $new_dictionary .= sprintf("%03d%04d%05d",$tag,($len+2),($addr+$o));
149                            $new_fields.=chr(30)."  ".substr($f,1);
150                            $o += 2;
151                    }
152            }
153    
154            if (! $skip) {
155                    my $new_leader = sprintf($leader_fmt,24+length($new_dictionary.$new_fields)+2,$base_addr);
156                    my $new_marc = $new_leader . $new_dictionary . $new_fields . chr(30);
157                    $new_marc .= chr(29);   # end of record
158    
159                    print STDERR "original and new marc: [$rec_nr]\n$marc\n$new_marc\n\n" if ($debug);
160                    print "$new_marc";
161                    $count++;
162            }
163    
164    #       last if ($count > 100);
165    
166  }  }
167  $sth->finish();  $sth->finish();
168  $dbh->disconnect();  $dbh->disconnect();
169    
170    print STDERR "$count records from database $database converted...\n";

Legend:
Removed from v.208  
changed lines
  Added in v.216

  ViewVC Help
Powered by ViewVC 1.1.26