-
Notifications
You must be signed in to change notification settings - Fork 257
/
Charset.pm
1274 lines (1102 loc) · 44.8 KB
/
Charset.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package C4::Charset;
# Copyright (C) 2008 LibLime
#
# This file is part of Koha.
#
# Koha 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 3 of the License, or
# (at your option) any later version.
#
# Koha 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 Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use MARC::Charset;
use Text::Iconv;
use Unicode::Normalize qw( NFC NFD );
use Encode;
use Koha::Logger;
our (@ISA, @EXPORT_OK);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
NormalizeString
IsStringUTF8ish
MarcToUTF8Record
SetUTF8Flag
SetMarcUnicodeFlag
StripNonXmlChars
nsb_clean
SanitizeRecord
);
}
=encoding UTF-8
=head1 NAME
C4::Charset - utilities for handling character set conversions.
=head1 SYNOPSIS
use C4::Charset;
=head1 DESCRIPTION
This module contains routines for dealing with character set
conversions, particularly for MARC records.
A variety of character encodings are in use by various MARC
standards, and even more character encodings are used by
non-standard MARC records. The various MARC formats generally
do not do a good job of advertising a given record's character
encoding, and even when a record does advertise its encoding,
e.g., via the Leader/09, experience has shown that one cannot
trust it.
Ultimately, all MARC records are stored in Koha in UTF-8 and
must be converted from whatever the source character encoding is.
The goal of this module is to ensure that these conversions
take place accurately. When a character conversion cannot take
place, or at least not accurately, the module was provide
enough information to allow user-facing code to inform the user
on how to deal with the situation.
=cut
=head1 FUNCTIONS
=head2 IsStringUTF8ish
my $is_utf8 = IsStringUTF8ish($str);
Determines if C<$str> is valid UTF-8. This can mean
one of two things:
=over
=item *
The Perl UTF-8 flag is set and the string contains valid UTF-8.
=item *
The Perl UTF-8 flag is B<not> set, but the octets contain
valid UTF-8.
=back
The function is named C<IsStringUTF8ish> instead of C<IsStringUTF8>
because in one could be presented with a MARC blob that is
not actually in UTF-8 but whose sequence of octets appears to be
valid UTF-8. The rest of the MARC character conversion functions
will assume that this situation occur does not very often.
=cut
sub IsStringUTF8ish {
my $str = shift;
return 1 if Encode::is_utf8($str);
return utf8::decode( $str );
}
=head2 SetUTF8Flag
my $marc_record = SetUTF8Flag($marc_record, $nfd);
This function sets the PERL UTF8 flag for data.
It is required when using new_from_usmarc
since MARC::File::USMARC does not handle PERL UTF8 setting.
When editing unicode marc records fields and subfields, you
would end up in double encoding without using this function.
If $nfd is set, string normalization will use NFD instead of NFC
FIXME
In my opinion, this function belongs to MARC::Record and not
to this package.
But since it handles charset, and MARC::Record, it finds its way in that package
=cut
sub SetUTF8Flag{
my ($record, $nfd)=@_;
return unless ($record && $record->fields());
foreach my $field ($record->fields()){
if ($field->tag()>=10){
my @subfields;
foreach my $subfield ($field->subfields()){
push @subfields,($$subfield[0],NormalizeString($$subfield[1],$nfd));
}
eval {
my $newfield=MARC::Field->new(
$field->tag(),
$field->indicator(1),
$field->indicator(2),
@subfields
);
$field->replace_with($newfield);
};
warn "ERROR occurred in SetUTF8Flag $@" if $@;
}
}
}
=head2 NormalizeString
my $normalized_string=NormalizeString($string,$nfd,$transform);
Given a string
nfd : If you want to set NFD and not NFC
transform : If you expect all the signs to be removed
Sets the PERL UTF8 Flag on your initial data if need be
and applies cleaning if required
Returns a utf8 NFC normalized string
Sample code :
my $string=NormalizeString ("l'ornithoptère");
#results into ornithoptère in NFC form and sets UTF8 Flag
=cut
sub NormalizeString{
my ($string,$nfd,$transform)=@_;
return $string unless defined($string); # force scalar context return.
$string = Encode::decode('UTF-8', $string) unless (Encode::is_utf8($string));
if ($nfd){
$string= NFD($string);
}
else {
$string=NFC($string);
}
if ($transform){
$string=~s/\<|\>|\^|\;|\.|\?|,|\-|\(|\)|\[|\]|\{|\}|\$|\%|\!|\*|\:|\\|\/|\&|\"|\'/ /g;
#removing one letter words "d'" "l'" was changed into "d " "l "
$string=~s/\b\S\b//g;
$string=~s/\s+$//g;
}
return $string;
}
=head2 MarcToUTF8Record
($marc_record, $converted_from, $errors_arrayref) = MarcToUTF8Record($marc_blob,
$marc_flavour, [, $source_encoding]);
Given a MARC blob or a C<MARC::Record>, the MARC flavour, and an
optional source encoding, return a C<MARC::Record> that is
converted to UTF-8.
The returned C<$marc_record> is guaranteed to be in valid UTF-8, but
is not guaranteed to have been converted correctly. Specifically,
if C<$converted_from> is 'failed', the MARC record returned failed
character conversion and had each of its non-ASCII octets changed
to the Unicode replacement character.
If the source encoding was not specified, this routine will
try to guess it; the character encoding used for a successful
conversion is returned in C<$converted_from>.
=cut
sub MarcToUTF8Record {
my $marc = shift;
my $marc_flavour = shift;
my $source_encoding = shift;
my $marc_record;
my $marc_blob_is_utf8 = 0;
if (ref($marc) eq 'MARC::Record') {
my $marc_blob = $marc->as_usmarc();
$marc_blob_is_utf8 = IsStringUTF8ish($marc_blob);
$marc_record = $marc;
} else {
# dealing with a MARC blob
# remove any ersatz whitespace from the beginning and
# end of the MARC blob -- these can creep into MARC
# files produced by several sources -- caller really
# should be doing this, however
$marc =~ s/^\s+//;
$marc =~ s/\s+$//;
$marc_blob_is_utf8 = IsStringUTF8ish($marc);
eval {
$marc_record = MARC::Record->new_from_usmarc($marc);
};
if ($@) {
# if we fail the first time, one likely problem
# is that we have a MARC21 record that says that it's
# UTF-8 (Leader/09 = 'a') but contains non-UTF-8 characters.
# We'll try parsing it again.
substr($marc, 9, 1) = ' ';
eval {
$marc_record = MARC::Record->new_from_usmarc($marc);
};
if ($@) {
# it's hopeless; return an empty MARC::Record
return MARC::Record->new(), 'failed', ['could not parse MARC blob'];
}
}
}
# If we do not know the source encoding, try some guesses
# as follows:
# 1. Record is UTF-8 already.
# 2. If MARC flavor is MARC21, then
# a. record is MARC-8
# b. record is ISO-8859-1
# 3. If MARC flavor is UNIMARC, then
if (not defined $source_encoding) {
if ($marc_blob_is_utf8) {
# note that for MARC21 we are not bothering to check
# if the Leader/09 is set to 'a' or not -- because
# of problems with various ILSs (including Koha in the
# past, alas), this just is not trustworthy.
SetMarcUnicodeFlag($marc_record, $marc_flavour);
return $marc_record, 'UTF-8', [];
} else {
if ($marc_flavour eq 'MARC21') {
return _default_marc21_charconv_to_utf8($marc_record, $marc_flavour);
} elsif ($marc_flavour =~/UNIMARC/) {
return _default_unimarc_charconv_to_utf8($marc_record, $marc_flavour);
} else {
return _default_marc21_charconv_to_utf8($marc_record, $marc_flavour);
}
}
} else {
# caller knows the character encoding
my $original_marc_record = $marc_record->clone();
my @errors;
if ($source_encoding =~ /utf-?8/i) {
if ($marc_blob_is_utf8) {
SetMarcUnicodeFlag($marc_record, $marc_flavour);
return $marc_record, 'UTF-8', [];
} else {
push @errors, 'specified UTF-8 => UTF-8 conversion, but record is not in UTF-8';
}
} elsif ($source_encoding =~ /marc-?8/i) {
@errors = _marc_marc8_to_utf8($marc_record, $marc_flavour);
} elsif ($source_encoding =~ /5426/) {
@errors = _marc_iso5426_to_utf8($marc_record, $marc_flavour);
} else {
# assume any other character encoding is for Text::Iconv
@errors = _marc_to_utf8_via_text_iconv($marc_record, $marc_flavour, $source_encoding);
}
if (@errors) {
_marc_to_utf8_replacement_char($original_marc_record, $marc_flavour);
return $original_marc_record, 'failed', \@errors;
} else {
return $marc_record, $source_encoding, [];
}
}
}
=head2 SetMarcUnicodeFlag
SetMarcUnicodeFlag($marc_record, $marc_flavour);
Set both the internal MARC::Record encoding flag
and the appropriate Leader/09 (MARC21) or
100/26-29 (UNIMARC) to indicate that the record
is in UTF-8. Note that this does B<not> do
any actual character conversion.
=cut
sub SetMarcUnicodeFlag {
my $marc_record = shift;
my $marc_flavour = shift; # || C4::Context->preference("marcflavour");
$marc_record->encoding('UTF-8');
if ($marc_flavour eq 'MARC21') {
my $leader = $marc_record->leader();
substr($leader, 9, 1) = 'a';
$marc_record->leader($leader);
} elsif ($marc_flavour =~/UNIMARC/) {
require C4::Context;
my $defaultlanguage = C4::Context->preference("UNIMARCField100Language");
$defaultlanguage = "fre" if (!$defaultlanguage || length($defaultlanguage) != 3);
my $string;
my ($subflength,$encodingposition)=($marc_flavour=~/AUTH/?(21,12):(36,25));
$string=$marc_record->subfield( 100, "a" );
if (defined $string && length($string)==$subflength) {
$string = substr $string, 0,$subflength if (length($string)>$subflength);
}
else {
$string = POSIX::strftime( "%Y%m%d", localtime );
$string =~ s/\-//g;
$string = sprintf( "%-*s", $subflength, $string );
substr ( $string, ($encodingposition - 3), 3, $defaultlanguage);
}
substr( $string, $encodingposition, 3, "y50" );
if ( $marc_record->subfield( 100, "a" ) ) {
$marc_record->field('100')->update(a=>$string);
}
else {
$marc_record->insert_grouped_field(
MARC::Field->new( 100, '', '', "a" => $string ) );
}
Koha::Logger->get->debug("encodage: ", substr( $marc_record->subfield(100, 'a'), $encodingposition, 3 ));
} else {
warn "Unrecognized marcflavour: $marc_flavour";
}
}
=head2 StripNonXmlChars
my $new_str = StripNonXmlChars($old_str);
Given a string, return a copy with the
characters that are illegal in XML
removed.
This function exists to work around a problem
that can occur with badly-encoded MARC records.
Specifically, if a UTF-8 MARC record also
has excape (\x1b) characters, MARC::File::XML
will let the escape characters pass through
when as_xml() or as_xml_record() is called. The
problem is that the escape character is not
legal in well-formed XML documents, so when
MARC::File::XML attempts to parse such a record,
the XML parser will fail.
Stripping such characters will allow a
MARC::Record->new_from_xml()
to work, at the possible risk of some data loss.
=cut
sub StripNonXmlChars {
my $str = shift;
if (!defined($str) || $str eq ""){
return "";
}
$str =~ s/[^\x09\x0A\x0D\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
return $str;
}
=head2 nsb_clean
=over 4
nsb_clean($string);
=back
Removes Non Sorting Block characters
=cut
sub nsb_clean {
my $NSB = '\x88' ; # NSB : begin Non Sorting Block
my $NSE = '\x89' ; # NSE : Non Sorting Block end
my $NSB2 = '\x98' ; # NSB : begin Non Sorting Block
my $NSE2 = '\x9C' ; # NSE : Non Sorting Block end
my $C2 = '\xC2' ; # What is this char ? It is sometimes left by the regexp after removing NSB / NSE
# handles non sorting blocks
my ($string) = @_ ;
$_ = $string ;
s/($C2){0,1}($NSB|$NSB2)//g ;
s/($C2){0,1}($NSE|$NSE2)//g ;
$string = $_ ;
return($string) ;
}
=head2 SanitizeRecord
SanitizeRecord($marcrecord);
Sanitize a record
This routine is called in the maintenance script misc/maintenance/sanitize_records.pl.
It cleans any string with '&amp;...', replacing it by '&'
=cut
sub SanitizeRecord {
my ( $record, $biblionumber ) = @_;
my $string;
my $record_modified = 0;
my $frameworkcode = C4::Biblio::GetFrameworkCode($biblionumber);
my ( $url_field, $url_subfield ) =
C4::Biblio::GetMarcFromKohaField( 'biblioitems.url' );
foreach my $field ( $record->fields() ) {
if ( $field->is_control_field() ) {
my $value = $field->data();
my $sanitized_value = _clean_ampersand($value);
$record_modified = 1 if $sanitized_value ne $value;
$field->update($sanitized_value);
}
else {
my @subfields = $field->subfields();
my @new_subfields;
foreach my $subfield (@subfields) {
next
if $url_field eq $field->tag()
and $url_subfield eq $subfield->[0];
my $value = $subfield->[1];
my $sanitized_value = _clean_ampersand($value);
push @new_subfields, $subfield->[0] => $sanitized_value;
$record_modified = 1 if $sanitized_value ne $value;
}
if ( scalar(@new_subfields) > 0 ) {
my $new_field = eval {
MARC::Field->new(
$field->tag(), $field->indicator(1),
$field->indicator(2), @new_subfields
);
};
if ($@) {
warn "error : $@";
}
else {
$field->replace_with($new_field);
}
}
}
}
return $record, $record_modified;
}
sub _clean_ampersand {
my ($string) = @_;
$string =~ s/(&)(amp;)+/$1/g;
return $string;
}
=head1 INTERNAL FUNCTIONS
=head2 _default_marc21_charconv_to_utf8
my ($new_marc_record, $guessed_charset) = _default_marc21_charconv_to_utf8($marc_record);
Converts a C<MARC::Record> of unknown character set to UTF-8,
first by trying a MARC-8 to UTF-8 conversion, then ISO-8859-1
to UTF-8, then a default conversion that replaces each non-ASCII
character with the replacement character.
The C<$guessed_charset> return value contains the character set
that resulted in a conversion to valid UTF-8; note that
if the MARC-8 and ISO-8859-1 conversions failed, the value of
this is 'failed'.
=cut
sub _default_marc21_charconv_to_utf8 {
my $marc_record = shift;
my $marc_flavour = shift;
my $trial_marc8 = $marc_record->clone();
my @all_errors = ();
my @errors = _marc_marc8_to_utf8($trial_marc8, $marc_flavour);
unless (@errors) {
return $trial_marc8, 'MARC-8', [];
}
push @all_errors, @errors;
my $trial_8859_1 = $marc_record->clone();
@errors = _marc_to_utf8_via_text_iconv($trial_8859_1, $marc_flavour, 'iso-8859-1');
unless (@errors) {
return $trial_8859_1, 'iso-8859-1', []; # note -- we could return \@all_errors
# instead if we wanted to report details
# of the failed attempt at MARC-8 => UTF-8
}
push @all_errors, @errors;
my $default_converted = $marc_record->clone();
_marc_to_utf8_replacement_char($default_converted, $marc_flavour);
return $default_converted, 'failed', \@all_errors;
}
=head2 _default_unimarc_charconv_to_utf8
my ($new_marc_record, $guessed_charset) = _default_unimarc_charconv_to_utf8($marc_record);
Converts a C<MARC::Record> of unknown character set to UTF-8,
first by trying a ISO-5426 to UTF-8 conversion, then ISO-8859-1
to UTF-8, then a default conversion that replaces each non-ASCII
character with the replacement character.
The C<$guessed_charset> return value contains the character set
that resulted in a conversion to valid UTF-8; note that
if the MARC-8 and ISO-8859-1 conversions failed, the value of
this is 'failed'.
=cut
sub _default_unimarc_charconv_to_utf8 {
my $marc_record = shift;
my $marc_flavour = shift;
my $trial_marc8 = $marc_record->clone();
my @all_errors = ();
my @errors = _marc_iso5426_to_utf8($trial_marc8, $marc_flavour);
unless (@errors) {
return $trial_marc8, 'iso-5426';
}
push @all_errors, @errors;
my $trial_8859_1 = $marc_record->clone();
@errors = _marc_to_utf8_via_text_iconv($trial_8859_1, $marc_flavour, 'iso-8859-1');
unless (@errors) {
return $trial_8859_1, 'iso-8859-1';
}
push @all_errors, @errors;
my $default_converted = $marc_record->clone();
_marc_to_utf8_replacement_char($default_converted, $marc_flavour);
return $default_converted, 'failed', \@all_errors;
}
=head2 _marc_marc8_to_utf8
my @errors = _marc_marc8_to_utf8($marc_record, $marc_flavour, $source_encoding);
Convert a C<MARC::Record> to UTF-8 in-place from MARC-8.
If the conversion fails for some reason, an
appropriate messages will be placed in the returned
C<@errors> array.
=cut
sub _marc_marc8_to_utf8 {
my $marc_record = shift;
my $marc_flavour = shift;
my $prev_ignore = MARC::Charset->ignore_errors();
MARC::Charset->ignore_errors(1);
# trap warnings raised by MARC::Charset
my @errors = ();
local $SIG{__WARN__} = sub {
my $msg = $_[0];
if ($msg =~ /MARC.Charset/) {
# FIXME - purpose of this regexp is to strip out the
# line reference to MARC/Charset.pm, but as it
# exists probably won't work quite on Windows --
# some sort of minimal-bunch back-tracking RE
# would be helpful here
$msg =~ s/at [\/].*?.MARC.Charset\.pm line \d+\.\n$//;
push @errors, $msg;
} else {
# if warning doesn't come from MARC::Charset, just
# pass it on
warn $msg;
}
};
foreach my $field ($marc_record->fields()) {
if ($field->is_control_field()) {
; # do nothing -- control fields should not contain non-ASCII characters
} else {
my @converted_subfields;
foreach my $subfield ($field->subfields()) {
my $utf8sf = MARC::Charset::marc8_to_utf8($subfield->[1]);
unless (IsStringUTF8ish($utf8sf)) {
# Because of a bug in MARC::Charset 0.98, if the string
# has (a) one or more diacritics that (b) are only in character positions
# 128 to 255 inclusive, the resulting converted string is not in
# UTF-8, but the legacy 8-bit encoding (e.g., ISO-8859-1). If that
# occurs, upgrade the string in place. Moral of the story seems to be
# that pack("U", ...) is better than chr(...) if you need to guarantee
# that the resulting string is UTF-8.
$utf8sf = Encode::encode('UTF-8', $utf8sf);
}
push @converted_subfields, $subfield->[0], $utf8sf;
}
$field->replace_with(MARC::Field->new(
$field->tag(), $field->indicator(1), $field->indicator(2),
@converted_subfields)
);
}
}
MARC::Charset->ignore_errors($prev_ignore);
SetMarcUnicodeFlag($marc_record, $marc_flavour);
return @errors;
}
=head2 _marc_iso5426_to_utf8
my @errors = _marc_iso5426_to_utf8($marc_record, $marc_flavour, $source_encoding);
Convert a C<MARC::Record> to UTF-8 in-place from ISO-5426.
If the conversion fails for some reason, an
appropriate messages will be placed in the returned
C<@errors> array.
FIXME - is ISO-5426 equivalent enough to MARC-8
that C<MARC::Charset> can be used instead?
=cut
sub _marc_iso5426_to_utf8 {
my $marc_record = shift;
my $marc_flavour = shift;
my @errors = ();
foreach my $field ($marc_record->fields()) {
if ($field->is_control_field()) {
; # do nothing -- control fields should not contain non-ASCII characters
} else {
my @converted_subfields;
foreach my $subfield ($field->subfields()) {
my $utf8sf = char_decode5426($subfield->[1]);
push @converted_subfields, $subfield->[0], $utf8sf;
}
$field->replace_with(MARC::Field->new(
$field->tag(), $field->indicator(1), $field->indicator(2),
@converted_subfields)
);
}
}
SetMarcUnicodeFlag($marc_record, $marc_flavour);
return @errors;
}
=head2 _marc_to_utf8_via_text_iconv
my @errors = _marc_to_utf8_via_text_iconv($marc_record, $marc_flavour, $source_encoding);
Convert a C<MARC::Record> to UTF-8 in-place using the
C<Text::Iconv> CPAN module. Any source encoding accepted
by the user's iconv installation should work. If
the source encoding is not recognized on the user's
server or the conversion fails for some reason,
appropriate messages will be placed in the returned
C<@errors> array.
=cut
sub _marc_to_utf8_via_text_iconv {
my $marc_record = shift;
my $marc_flavour = shift;
my $source_encoding = shift;
my @errors = ();
my $decoder;
eval { $decoder = Text::Iconv->new($source_encoding, 'utf8'); };
if ($@) {
push @errors, "Could not initialze $source_encoding => utf8 converter: $@";
return @errors;
}
my $prev_raise_error = Text::Iconv->raise_error();
Text::Iconv->raise_error(1);
foreach my $field ($marc_record->fields()) {
if ($field->is_control_field()) {
; # do nothing -- control fields should not contain non-ASCII characters
} else {
my @converted_subfields;
foreach my $subfield ($field->subfields()) {
my $converted_value;
my $conversion_ok = 1;
eval { $converted_value = $decoder->convert($subfield->[1]); };
if ($@) {
$conversion_ok = 0;
push @errors, $@;
} elsif (not defined $converted_value) {
$conversion_ok = 0;
push @errors, "Text::Iconv conversion failed - retval is " . $decoder->retval();
}
if ($conversion_ok) {
push @converted_subfields, $subfield->[0], $converted_value;
} else {
$converted_value = $subfield->[1];
$converted_value =~ s/[\200-\377]/\xef\xbf\xbd/g;
push @converted_subfields, $subfield->[0], $converted_value;
}
}
$field->replace_with(MARC::Field->new(
$field->tag(), $field->indicator(1), $field->indicator(2),
@converted_subfields)
);
}
}
SetMarcUnicodeFlag($marc_record, $marc_flavour);
Text::Iconv->raise_error($prev_raise_error);
return @errors;
}
=head2 _marc_to_utf8_replacement_char
_marc_to_utf8_replacement_char($marc_record, $marc_flavour);
Convert a C<MARC::Record> to UTF-8 in-place, adopting the
unsatisfactory method of replacing all non-ASCII (e.g.,
where the eight bit is set) octet with the Unicode
replacement character. This is meant as a last-ditch
method, and would be best used as part of a UI that
lets a cataloguer pick various character conversions
until they find the right one.
=cut
sub _marc_to_utf8_replacement_char {
my $marc_record = shift;
my $marc_flavour = shift;
foreach my $field ($marc_record->fields()) {
if ($field->is_control_field()) {
; # do nothing -- control fields should not contain non-ASCII characters
} else {
my @converted_subfields;
foreach my $subfield ($field->subfields()) {
my $value = $subfield->[1];
$value =~ s/[\200-\377]/\xef\xbf\xbd/g;
push @converted_subfields, $subfield->[0], $value;
}
$field->replace_with(MARC::Field->new(
$field->tag(), $field->indicator(1), $field->indicator(2),
@converted_subfields)
);
}
}
SetMarcUnicodeFlag($marc_record, $marc_flavour);
}
=head2 char_decode5426
my $utf8string = char_decode5426($iso_5426_string);
Converts a string from ISO-5426 to UTF-8.
=cut
my %chars;
####
## 0xb
$chars{0xb0}=0x0101;#3/0ayn[ain]
$chars{0xb1}=0x0623;#3/1alif/hamzah[alefwithhamzaabove]
#$chars{0xb2}=0x00e0;#'à';
$chars{0xb2}=0x00e0;#3/2leftlowsinglequotationmark
#$chars{0xb3}=0x00e7;#'ç';
$chars{0xb3}=0x00e7;#3/2leftlowsinglequotationmark
# $chars{0xb4}='è';
$chars{0xb4}=0x00e8;
# $chars{0xb5}='é';
$chars{0xb5}=0x00e9;
$chars{0xb6}=0x2021; # double dagger
$chars{0xb7}=0x00b7; # middle dot
$chars{0xb8}=0x2033; # double prime
$chars{0xb9}=0x2019; # right single quotation mark
$chars{0xba}=0x201d; # right double quotation mark
$chars{0xbb}=0x00bb; # right-pointing double angle quotation mark
$chars{0xbc}=0x266f; # music sharp sign
$chars{0xbd}=0x02b9; # modifier letter prime
$chars{0xbe}=0x02ba; # modifier letter double prime
$chars{0xbf}=0x00bf; # inverted question mark
####
## 0xe
$chars{0xe1}=0x00c6; # latin capital letter ae
$chars{0xe2}=0x0110; # latin capital letter d with stroke
$chars{0xe6}=0x0132; # latin capital ligature ij
$chars{0xe8}=0x0141; # latin capital letter l with stroke
$chars{0xe9}=0x00d8; # latin capital letter o with stroke
$chars{0xea}=0x0152; # latin capital ligature oe
$chars{0xec}=0x00de; # latin capital letter thorn
####
## 0xf
$chars{0xf1}=0x00e6; # latin small letter ae
$chars{0xf2}=0x0111; # latin small letter d with stroke
$chars{0xf3}=0x00f0; # latin small letter eth
$chars{0xf5}=0x0131; # latin small letter dotless i
$chars{0xf6}=0x0133; # latin small ligature ij
$chars{0xf8}=0x0142; # latin small letter l with stroke
$chars{0xf9}=0x00f8; # latin small letter o with stroke
$chars{0xfa}=0x0153; # latin small ligature oe
$chars{0xfb}=0x00df; # latin small letter sharp s
$chars{0xfc}=0x00fe; # latin small letter thorn
####
## Others
$chars{0x97}=0x003c;#3/2leftlowsinglequotationmark
$chars{0x98}=0x003e;#3/2leftlowsinglequotationmark
#$chars{0x81d1}=0x00b0; # FIXME useless
####
## combined characters iso5426
$chars{0xc041}=0x1ea2; # capital a with hook above
$chars{0xc045}=0x1eba; # capital e with hook above
$chars{0xc049}=0x1ec8; # capital i with hook above
$chars{0xc04f}=0x1ece; # capital o with hook above
$chars{0xc055}=0x1ee6; # capital u with hook above
$chars{0xc059}=0x1ef6; # capital y with hook above
$chars{0xc061}=0x1ea3; # small a with hook above
$chars{0xc065}=0x1ebb; # small e with hook above
$chars{0xc069}=0x1ec9; # small i with hook above
$chars{0xc06f}=0x1ecf; # small o with hook above
$chars{0xc075}=0x1ee7; # small u with hook above
$chars{0xc079}=0x1ef7; # small y with hook above
# 4/1 grave accent
$chars{0xc141}=0x00c0; # capital a with grave accent
$chars{0xc145}=0x00c8; # capital e with grave accent
$chars{0xc149}=0x00cc; # capital i with grave accent
$chars{0xc14f}=0x00d2; # capital o with grave accent
$chars{0xc155}=0x00d9; # capital u with grave accent
$chars{0xc157}=0x1e80; # capital w with grave
$chars{0xc159}=0x1ef2; # capital y with grave
$chars{0xc161}=0x00e0; # small a with grave accent
$chars{0xc165}=0x00e8; # small e with grave accent
$chars{0xc169}=0x00ec; # small i with grave accent
$chars{0xc16f}=0x00f2; # small o with grave accent
$chars{0xc175}=0x00f9; # small u with grave accent
$chars{0xc177}=0x1e81; # small w with grave
$chars{0xc179}=0x1ef3; # small y with grave
# 4/2 acute accent
$chars{0xc241}=0x00c1; # capital a with acute accent
$chars{0xc243}=0x0106; # capital c with acute accent
$chars{0xc245}=0x00c9; # capital e with acute accent
$chars{0xc247}=0x01f4; # capital g with acute
$chars{0xc249}=0x00cd; # capital i with acute accent
$chars{0xc24b}=0x1e30; # capital k with acute
$chars{0xc24c}=0x0139; # capital l with acute accent
$chars{0xc24d}=0x1e3e; # capital m with acute
$chars{0xc24e}=0x0143; # capital n with acute accent
$chars{0xc24f}=0x00d3; # capital o with acute accent
$chars{0xc250}=0x1e54; # capital p with acute
$chars{0xc252}=0x0154; # capital r with acute accent
$chars{0xc253}=0x015a; # capital s with acute accent
$chars{0xc255}=0x00da; # capital u with acute accent
$chars{0xc257}=0x1e82; # capital w with acute
$chars{0xc259}=0x00dd; # capital y with acute accent
$chars{0xc25a}=0x0179; # capital z with acute accent
$chars{0xc261}=0x00e1; # small a with acute accent
$chars{0xc263}=0x0107; # small c with acute accent
$chars{0xc265}=0x00e9; # small e with acute accent
$chars{0xc267}=0x01f5; # small g with acute
$chars{0xc269}=0x00ed; # small i with acute accent
$chars{0xc26b}=0x1e31; # small k with acute
$chars{0xc26c}=0x013a; # small l with acute accent
$chars{0xc26d}=0x1e3f; # small m with acute
$chars{0xc26e}=0x0144; # small n with acute accent
$chars{0xc26f}=0x00f3; # small o with acute accent
$chars{0xc270}=0x1e55; # small p with acute
$chars{0xc272}=0x0155; # small r with acute accent
$chars{0xc273}=0x015b; # small s with acute accent
$chars{0xc275}=0x00fa; # small u with acute accent
$chars{0xc277}=0x1e83; # small w with acute
$chars{0xc279}=0x00fd; # small y with acute accent
$chars{0xc27a}=0x017a; # small z with acute accent
$chars{0xc2e1}=0x01fc; # capital ae with acute
$chars{0xc2f1}=0x01fd; # small ae with acute
# 4/3 circumflex accent
$chars{0xc341}=0x00c2; # capital a with circumflex accent
$chars{0xc343}=0x0108; # capital c with circumflex
$chars{0xc345}=0x00ca; # capital e with circumflex accent
$chars{0xc347}=0x011c; # capital g with circumflex
$chars{0xc348}=0x0124; # capital h with circumflex
$chars{0xc349}=0x00ce; # capital i with circumflex accent
$chars{0xc34a}=0x0134; # capital j with circumflex
$chars{0xc34f}=0x00d4; # capital o with circumflex accent
$chars{0xc353}=0x015c; # capital s with circumflex
$chars{0xc355}=0x00db; # capital u with circumflex
$chars{0xc357}=0x0174; # capital w with circumflex
$chars{0xc359}=0x0176; # capital y with circumflex
$chars{0xc35a}=0x1e90; # capital z with circumflex
$chars{0xc361}=0x00e2; # small a with circumflex accent
$chars{0xc363}=0x0109; # small c with circumflex
$chars{0xc365}=0x00ea; # small e with circumflex accent
$chars{0xc367}=0x011d; # small g with circumflex
$chars{0xc368}=0x0125; # small h with circumflex
$chars{0xc369}=0x00ee; # small i with circumflex accent
$chars{0xc36a}=0x0135; # small j with circumflex
$chars{0xc36e}=0x00f1; # small n with tilde
$chars{0xc36f}=0x00f4; # small o with circumflex accent
$chars{0xc373}=0x015d; # small s with circumflex
$chars{0xc375}=0x00fb; # small u with circumflex
$chars{0xc377}=0x0175; # small w with circumflex
$chars{0xc379}=0x0177; # small y with circumflex
$chars{0xc37a}=0x1e91; # small z with circumflex
# 4/4 tilde
$chars{0xc441}=0x00c3; # capital a with tilde
$chars{0xc445}=0x1ebc; # capital e with tilde
$chars{0xc449}=0x0128; # capital i with tilde
$chars{0xc44e}=0x00d1; # capital n with tilde
$chars{0xc44f}=0x00d5; # capital o with tilde
$chars{0xc455}=0x0168; # capital u with tilde
$chars{0xc456}=0x1e7c; # capital v with tilde
$chars{0xc459}=0x1ef8; # capital y with tilde
$chars{0xc461}=0x00e3; # small a with tilde
$chars{0xc465}=0x1ebd; # small e with tilde
$chars{0xc469}=0x0129; # small i with tilde
$chars{0xc46e}=0x00f1; # small n with tilde
$chars{0xc46f}=0x00f5; # small o with tilde
$chars{0xc475}=0x0169; # small u with tilde
$chars{0xc476}=0x1e7d; # small v with tilde
$chars{0xc479}=0x1ef9; # small y with tilde
# 4/5 macron
$chars{0xc541}=0x0100; # capital a with macron
$chars{0xc545}=0x0112; # capital e with macron
$chars{0xc547}=0x1e20; # capital g with macron
$chars{0xc549}=0x012a; # capital i with macron
$chars{0xc54f}=0x014c; # capital o with macron
$chars{0xc555}=0x016a; # capital u with macron
$chars{0xc561}=0x0101; # small a with macron
$chars{0xc565}=0x0113; # small e with macron
$chars{0xc567}=0x1e21; # small g with macron
$chars{0xc569}=0x012b; # small i with macron
$chars{0xc56f}=0x014d; # small o with macron
$chars{0xc575}=0x016b; # small u with macron
$chars{0xc572}=0x0159; # small r with macron
$chars{0xc5e1}=0x01e2; # capital ae with macron
$chars{0xc5f1}=0x01e3; # small ae with macron
# 4/6 breve
$chars{0xc641}=0x0102; # capital a with breve
$chars{0xc645}=0x0114; # capital e with breve
$chars{0xc647}=0x011e; # capital g with breve
$chars{0xc649}=0x012c; # capital i with breve
$chars{0xc64f}=0x014e; # capital o with breve
$chars{0xc655}=0x016c; # capital u with breve
$chars{0xc661}=0x0103; # small a with breve
$chars{0xc665}=0x0115; # small e with breve
$chars{0xc667}=0x011f; # small g with breve
$chars{0xc669}=0x012d; # small i with breve
$chars{0xc66f}=0x014f; # small o with breve
$chars{0xc675}=0x016d; # small u with breve
# 4/7 dot above
$chars{0xc7b0}=0x01e1; # Ain with dot above