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

(-)a/C4/Charset.pm (+48 lines)
Lines 40-45 BEGIN { Link Here
40
        SetMarcUnicodeFlag
40
        SetMarcUnicodeFlag
41
        StripNonXmlChars
41
        StripNonXmlChars
42
        nsb_clean
42
        nsb_clean
43
	SanitizeEntity
43
    );
44
    );
44
}
45
}
45
46
Lines 1161-1166 sub char_decode5426 { Link Here
1161
  return $result;
1162
  return $result;
1162
}
1163
}
1163
1164
1165
=head2 SanitizeEntity
1166
1167
=over 4
1168
1169
SanitizeEntity($marcrecord);
1170
1171
=back
1172
1173
Sanitize Entity
1174
1175
=cut
1176
1177
sub SanitizeEntity {
1178
    my $record = shift;
1179
1180
    foreach my $field ($record->fields()) {
1181
        if ($field->is_control_field()) {
1182
            $field->update(entity_clean($field->data()));
1183
        } else {
1184
            my @subfields = $field->subfields();
1185
            my @new_subfields;
1186
            foreach my $subfield (@subfields) {
1187
                push @new_subfields, $subfield->[0] => entity_clean($subfield->[1]);
1188
            }
1189
            if (scalar(@new_subfields) > 0) {
1190
                my $new_field;
1191
                eval {
1192
                    $new_field = MARC::Field->new($field->tag(), $field->indicator(1), $field->indicator(2), @new_subfields);
1193
                };
1194
                if ($@) {
1195
                    warn "error : $@";
1196
                } else {
1197
                    $field->replace_with($new_field);
1198
                }
1199
1200
            }
1201
        }
1202
    }
1203
    return $record;
1204
}
1205
1206
sub entity_clean {
1207
    my $string=shift;
1208
    $string=~s/(&)(amp;)+/$1/g;
1209
    return $string;
1210
}
1211
1164
1;
1212
1;
1165
1213
1166
1214
(-)a/misc/maintenance/batchSanitizeEntity.pl (-1 / +173 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
# small script that replaces '&etc.' by '&' in a record
3
4
# Copyright 2012 Biblibre SARL
5
#
6
# This file is part of Koha.
7
#
8
# Koha is free software; you can redistribute it and/or modify it under the
9
# terms of the GNU General Public License as published by the Free Software
10
# Foundation; either version 2 of the License, or (at your option) any later
11
# version.
12
#
13
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License along
18
# with Koha; if not, write to the Free Software Foundation, Inc.,
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
use Modern::Perl;
22
use C4::Charset;
23
use C4::Context;
24
use DBI;
25
use C4::Biblio;
26
use Getopt::Long;
27
use Pod::Usage;
28
29
my ( $biblios,$run,$want_help,$batch,$cron );
30
my $result = GetOptions(
31
    'where=s' => \$biblios,
32
    '--run'   => \$run,
33
    'help|h'  => \$want_help,
34
    'batch|b' => \$batch,
35
    'cron|c'  => \$cron,
36
);
37
38
# We check if required entries are given :
39
if ( not $result or $want_help or not $biblios or not $run ) {
40
    print_usage();
41
    exit 0;
42
}
43
44
my $count;
45
my $bibliocount;
46
my @bibliofile;
47
my @biblionum;
48
my @biblios;
49
my $record;
50
my $cleanrecord;
51
52
# We first detect if we have a file or biblos directly entered by command line
53
#or if we want to use findAmp() sub
54
if ( $biblios eq "search" ) {
55
    print "=============== Searching, please wait ...\n";
56
    @biblios = findAmp();
57
}
58
else {
59
    if ( $biblios =~ m|/| ) {
60
        open( FILE, "$biblios" ) || die("Can't open $biblios");
61
        @bibliofile = <FILE>;
62
        close(FILE);
63
    }
64
    else {
65
        @biblionum = split ',', $biblios;
66
    }
67
    
68
    # We take the biblios
69
    @biblios = @bibliofile ? @bibliofile : @biblionum;
70
}
71
72
# We remove spaces
73
s/(^\s*|\s*$)//g for @biblios;
74
# Remove empty lines
75
@biblios = grep {!/^$/} @biblios;
76
77
# We valid the input
78
foreach my $biblio (@biblios) {
79
    if ( $biblio !~ /^\d+$/ ) {
80
        print
81
"=============== \"$biblio\" is not a biblionumber !!! ===============\n";
82
        print "=============== please verify \"$biblio\" !!! ===============\n";
83
        $count++;
84
    }
85
    exit(1) if $count;
86
}
87
88
$bibliocount = scalar @biblios;
89
90
print
91
"=============== $bibliocount Biblio(s) ready to be cleaned ===============\n";
92
93
if ($bibliocount == 0) {
94
    print "=============== nothing to clean\n";
95
    exit(0);
96
}
97
98
if ( !$batch and !$cron ) {
99
    my $confirm = 0;
100
    print "=============== You are ok ? Types yes/no :\n";
101
    while ( $confirm == 0 ) {
102
        my $prout = <STDIN>;
103
        if ($prout eq "yes\n") {
104
            $confirm = 1;
105
        }
106
        elsif ($prout eq "no\n") {
107
            print "=============== Ok, bye ===============\n";
108
            exit(0);
109
        }
110
        else {
111
            print "======= Bad answer please types 'yes' or 'no' ! ===============\n";
112
        }
113
            }
114
}
115
116
foreach my $biblio ( @biblios ) {
117
    print "=============== N° $biblio selected ===============\n";
118
    $record      = GetMarcBiblio($biblio);
119
    $cleanrecord = SanitizeEntity($record);
120
    my $frameworkcode = GetFrameworkCode($biblio);
121
    ModBiblio( $cleanrecord, $biblio, $frameworkcode );
122
    print "=============== Biblio n° $biblio cleaning done ===============\n";
123
    $count++;
124
}
125
126
print "==============================================================\n";
127
print "=============== $count Biblios cleaned ===============\n";
128
129
if ( $cron ) {
130
    print "==============================================================\n";
131
    print
132
"========= Now we are reindexing -b -v -where \"biblionumber=xxx\" =========\n";
133
    print "==============================================================\n";
134
    $count = 0;
135
    my $kohapath = C4::Context->config('intranetdir');
136
    foreach my $biblio ( @biblios ) {
137
        system("$kohapath/misc/migration_tools/rebuild_zebra.pl -b -v -where \"biblionumber=$biblio\"");
138
        print "========= Biblio n° $biblio re-indexing done =========\n";
139
        $count ++;
140
}
141
    print "=============== $count Biblios re-indexed ===============\n";
142
}
143
144
sub print_usage {
145
    print <<_USAGE_;
146
$0: replaces '&amp;' by '&' in a record, you can give some biblionumbers
147
or a file with biblionumbers
148
or ask to search in the DataBase, may be long.
149
WARNING : if there is a subfield with an url with '&amp' it will become '&'
150
151
Parameters:
152
    --where                 use this to give biblionumbers in a string with "" (separated by coma)
153
                            or an absolute path to a file containing biblionumbers (1 by row)
154
                            or the command 'search' that creates an array with biblionumbers with "&amp;amp;..."
155
    --run                   run the command
156
    --batch or -b           run in batch mode
157
    --cron or -c            run in cron mode, it reindexes the changed records (you can redirect the output to a file)
158
    --help or -h            show this message.
159
_USAGE_
160
}
161
162
sub findAmp {
163
    my @bibliosearch;
164
    my $dbh = C4::Context->dbh;
165
    my $strsth =
166
      qq{SELECT biblionumber FROM biblioitems WHERE marcxml LIKE "%amp;amp;%"};
167
    my $sth = $dbh->prepare($strsth);
168
    $sth->execute();
169
    while ( my $bib = $sth-> fetchrow_array() ) {
170
        push @bibliosearch, $bib;
171
    }
172
    return @bibliosearch;
173
}

Return to bug 8218