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

(-)a/C4/Charset.pm (+48 lines)
Lines 42-47 BEGIN { Link Here
42
      StripNonXmlChars
42
      StripNonXmlChars
43
      nsb_clean
43
      nsb_clean
44
      fix_unimarc_titles
44
      fix_unimarc_titles
45
      SanitizeEntity
45
    );
46
    );
46
}
47
}
47
48
Lines 1232-1237 sub fix_unimarc_titles { Link Here
1232
    }
1233
    }
1233
}
1234
}
1234
1235
1236
=head2 SanitizeEntity
1237
1238
=over 4
1239
1240
SanitizeEntity($marcrecord);
1241
1242
=back
1243
1244
Sanitize Entity
1245
1246
=cut
1247
1248
sub SanitizeEntity {
1249
    my $record = shift;
1250
1251
    foreach my $field ($record->fields()) {
1252
        if ($field->is_control_field()) {
1253
            $field->update(entity_clean($field->data()));
1254
        } else {
1255
            my @subfields = $field->subfields();
1256
            my @new_subfields;
1257
            foreach my $subfield (@subfields) {
1258
                push @new_subfields, $subfield->[0] => entity_clean($subfield->[1]);
1259
            }
1260
            if (scalar(@new_subfields) > 0) {
1261
                my $new_field;
1262
                eval {
1263
                    $new_field = MARC::Field->new($field->tag(), $field->indicator(1), $field->indicator(2), @new_subfields);
1264
                };
1265
                if ($@) {
1266
                    warn "error : $@";
1267
                } else {
1268
                    $field->replace_with($new_field);
1269
                }
1270
1271
            }
1272
        }
1273
    }
1274
    return $record;
1275
}
1276
1277
sub entity_clean {
1278
    my $string=shift;
1279
    $string=~s/(&)(amp;)+/$1/g;
1280
    return $string;
1281
}
1282
1235
1;
1283
1;
1236
1284
1237
=head1 AUTHOR
1285
=head1 AUTHOR
(-)a/misc/maintenance/batchSanitizeEntity.pl (-1 / +165 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
# We initialise some tools :
45
my $count;
46
my $bibliocount;
47
my @bibliofile;
48
my @biblionum;
49
my @biblios;
50
my $record;
51
my $cleanrecord;
52
53
# We first detect if we have a file or biblos directly entered by command line
54
#or if we want to use findAmp() sub
55
if ( $biblios eq "search" ) {
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
if ( !$batch or !$cron ) {
93
    my $confirm = 0;
94
    print "=============== You are ok ? Types yes/no :\n";
95
    while ( $confirm == 0 ) {
96
        my $prout = <STDIN>;
97
        if ($prout eq "yes\n") {
98
            $confirm = 1;
99
        }
100
        elsif ($prout eq "no\n") {
101
            print "=============== Ok, bye ===============\n";
102
            exit(0);
103
        }
104
        else {
105
            print "======= Bad answer please types 'yes' or 'no' !!!!!!!!!!! ===============\n";
106
        }
107
            }
108
}
109
110
foreach my $biblio ( @biblios ) {
111
    print "=============== N° $biblio selected ===============\n";
112
    $record      = GetMarcBiblio($biblio);
113
    $cleanrecord = SanitizeEntity($record);
114
    my $frameworkcode = GetFrameworkCode($biblio);
115
    ModBiblio( $cleanrecord, $biblio, $frameworkcode );
116
    print "=============== Biblio n° $biblio cleaning done ===============\n";
117
    $count++;
118
}
119
120
print "==============================================================\n";
121
print "=============== $count Biblios cleaned ===============\n";
122
123
if ( $cron ) {
124
    print "==============================================================\n";
125
    print
126
"========= Now we are reindexing -b -v -where \"biblionumber=xxx\" =========\n";
127
    print "==============================================================\n";
128
    $count = 0;
129
    my $kohapath = C4::Context->config('intranetdir');
130
    foreach my $biblio ( @biblios ) {
131
        system("$kohapath/misc/migration_tools/rebuild_zebra.pl
132
        -b -v -where \"biblionumber=$biblio\"");
133
        print "========= Biblio n° $biblio re-indexing done =========\n";
134
        $count ++;
135
}
136
    print "=============== $count Biblios re-indexed ===============\n";
137
}
138
139
sub print_usage {
140
    print <<_USAGE_;
141
$0: replaces '&amp;' by '&' in a record, you can either give some biblionumbers or a file with biblionumbers or
142
143
Parameters:
144
    -where                  use this to give biblionumbers in a string with "" (separated by coma)
145
                            or an absolute path to a file containing biblionumbers (1 by row)
146
                            or the command 'search' that creates an array with biblionumbers with "&amp;amp;..."
147
    --run                   run the command
148
    --batch or -b           run in batch mode
149
    --cron or -c            run in cron mode, it reindexes the changed records (you can redirect the output to a file)
150
    --help or -h            show this message.
151
_USAGE_
152
}
153
154
sub findAmp {
155
    my @bibliosearch;
156
    my $dbh = C4::Context->dbh;
157
    my $strsth =
158
      qq{SELECT biblionumber FROM biblioitems WHERE marcxml LIKE "%amp;amp;%"};
159
    my $sth = $dbh->prepare($strsth);
160
    $sth->execute();
161
    while ( my $bib = $sth-> fetchrow_array() ) {
162
        push @bibliosearch, $bib;
163
    }
164
    return @bibliosearch;
165
}

Return to bug 8218