View | Details | Raw Unified | Return to bug 8218
Collapse All | Expand All

(-)a/C4/Charset.pm (-7 / +43 lines)
Lines 33-45 BEGIN { Link Here
33
    require Exporter;
33
    require Exporter;
34
    @ISA    = qw(Exporter);
34
    @ISA    = qw(Exporter);
35
    @EXPORT = qw(
35
    @EXPORT = qw(
36
        NormalizeString
36
      NormalizeString
37
        IsStringUTF8ish
37
      IsStringUTF8ish
38
        MarcToUTF8Record
38
      MarcToUTF8Record
39
        SetUTF8Flag
39
      SetUTF8Flag
40
        SetMarcUnicodeFlag
40
      SetMarcUnicodeFlag
41
        StripNonXmlChars
41
      StripNonXmlChars
42
        nsb_clean
42
      nsb_clean
43
      SanitizeEntity
43
    );
44
    );
44
}
45
}
45
46
Lines 1158-1163 sub char_decode5426 { Link Here
1158
  return $result;
1159
  return $result;
1159
}
1160
}
1160
1161
1162
sub SanitizeEntity {
1163
    my $record = shift;
1164
1165
    foreach my $field ($record->fields()) {
1166
        if ($field->is_control_field()) {
1167
            $field->update(entity_clean($field->data()));
1168
        } else {
1169
            my @subfields = $field->subfields();
1170
            my @new_subfields;
1171
            foreach my $subfield (@subfields) {
1172
                push @new_subfields, $subfield->[0] => entity_clean($subfield->[1]);
1173
            }
1174
            if (scalar(@new_subfields) > 0) {
1175
                my $new_field;
1176
                eval {
1177
                    $new_field = MARC::Field->new($field->tag(), $field->indicator(1), $field->indicator(2), @new_subfields);
1178
                };
1179
                if ($@) {
1180
                    warn "error : $@";
1181
                } else {
1182
                    $field->replace_with($new_field);
1183
                }
1184
1185
            }
1186
        }
1187
    }
1188
    return $record;
1189
}
1190
1191
sub entity_clean {
1192
    my $string=shift;
1193
    $string=~s/(&)(amp;)+/$1/g;
1194
    return $string;
1195
}
1196
1161
1;
1197
1;
1162
1198
1163
1199
(-)a/misc/maintenance/batchSanitizeEntity.pl (-1 / +128 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
# small script that replaces '&etc.' by '&' in a record
3
4
use warnings;
5
use C4::Charset;
6
use C4::Context;
7
use DBI;
8
use C4::Biblio;
9
use Getopt::Long;
10
use Pod::Usage;
11
12
my ($biblios,$run,$want_help);
13
my $result = GetOptions(
14
    'where=s'      => \$biblios,
15
    '--run'        => \$run,
16
    'help|h'       => \$want_help,
17
    'batch|b'      => \$batch,
18
);
19
20
# We check if required entries are given :
21
if ( not $result or $want_help or not $biblios or not $run) {
22
    print_usage();
23
    exit 0;
24
}
25
26
# We initialise some tools :
27
my $count;
28
my $bibliocount;
29
my @bibliofile;
30
my @biblionum;
31
my @biblios;
32
my $record;
33
my $cleanrecord;
34
35
# We first detect if we have a file or biblos directly entered by command line or if we want to use findAmp() sub :
36
if ($biblios eq "search"){
37
    @biblios = findAmp();
38
}
39
else {
40
    if ($biblios =~ /\//) {
41
        open (FILE, "$biblios") || die ("Can't open $biblios");
42
        @bibliofile = <FILE>;
43
        close (FILE);
44
    }
45
    else {
46
        @biblionum = split ',', $biblios;
47
    }
48
    # We take the biblios
49
    @biblios = @bibliofile?@bibliofile:@biblionum;
50
}
51
52
# We remove spaces
53
my @biblio;
54
foreach my $bib(@biblios) {
55
    $bib =~ s/(^\s*|\s*$)//g;
56
    next if $bib eq "";
57
    push @biblio, $bib;
58
}
59
@biblios = @biblio;
60
61
# We valid the input
62
foreach my $biblio (@biblios) {
63
    if ($biblio !~ /^\d+$/){
64
        print "=============== \"$biblio\" is not a biblionumber !!! ===============\n";
65
        print "=============== please verify \"$biblio\" !!! ===============\n";
66
        $count += 1;
67
    }
68
    exit(1) if $count;
69
}
70
71
$bibliocount = scalar @biblios;
72
my $confirm = 0;
73
74
print "=============== $bibliocount Biblio(s) ready to be cleaned ===============\n";
75
if (!$batch) {
76
    print "=============== You are ok ? Types yes/no :\n";
77
    while ($confirm == 0){
78
                my $prout = <STDIN>;
79
                if ($prout eq "yes\n") {
80
                        $confirm = 1;
81
                }
82
                elsif ($prout eq "no\n") {
83
                    print "=============== Ok, bye ===============\n";
84
                    exit(0);
85
                    }
86
                else {
87
                    print "======= Bad answer please types 'yes' or 'no' !!!!!!!!!!! ===============\n";
88
                }
89
            }
90
}
91
92
foreach my $biblio (@biblios){
93
    print "=============== N° $biblio selected ===============\n";
94
    $record = GetMarcBiblio($biblio);
95
    $cleanrecord = SanitizeEntity($record);
96
    my $frameworkcode = GetFrameworkCode($biblio);
97
    ModBiblio($cleanrecord, $biblio, $frameworkcode);
98
    print "=============== Biblio n° $biblio done ===============\n";
99
}
100
101
print "==============================================================\n";
102
print "=============== $bibliocount Biblios processed ===============\n";
103
104
sub print_usage {
105
    print <<_USAGE_;
106
$0: replaces '&amp;' by '&' in a record, you can either give some biblionumbers or a file with biblionumbers or
107
108
Parameters:
109
    -where                  use this to give biblionumbers in a string with "" (separated by coma)
110
                            or an absolute path to a file containing biblionumbers (1 by row)
111
                            or the command 'search' that creates an array with biblionumbers with "&amp;amp;..."
112
    --run                   run the command
113
    --batch                 run in batch mode
114
    --help or -h            show this message.
115
_USAGE_
116
}
117
118
sub findAmp {
119
    my @bibliosearch;
120
    my $dbh = C4::Context->dbh;
121
    my $strsth = qq{SELECT biblionumber FROM biblioitems WHERE marcxml LIKE "%amp;amp;%"};
122
    my $sth = $dbh->prepare($strsth);
123
    $sth->execute();
124
    while (my $bib = $sth-> fetchrow_array()){
125
        push @bibliosearch, $bib;
126
    }
127
    return @bibliosearch;
128
}

Return to bug 8218