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

(-)a/C4/Charset.pm (+54 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 419-424 sub nsb_clean { Link Here
419
    return($string) ;
420
    return($string) ;
420
}
421
}
421
422
423
=head2 SanitizeEntity
424
425
SanitizeEntity($marcrecord);
426
427
Sanitize Entity
428
A script for maintenance to clean any string with '&...', replacing it by '&'
429
430
=cut
431
432
sub SanitizeEntity {
433
    my $record = shift;
434
    my $string;
435
    my $noact;
436
    foreach my $field ($record->fields()) {
437
        if ($field->is_control_field()) {
438
            ($string,$noact) = entity_clean($field->data());
439
            $field->update($string);
440
        } else {
441
            my @subfields = $field->subfields();
442
            my @new_subfields;
443
            foreach my $subfield (@subfields) {
444
                ($string,$noact) = entity_clean($subfield->[1]);
445
                push @new_subfields, $subfield->[0] => $string;
446
            }
447
            if (scalar(@new_subfields) > 0) {
448
                my $new_field;
449
                eval {
450
                    $new_field = MARC::Field->new($field->tag(), $field->indicator(1), $field->indicator(2), @new_subfields);
451
                };
452
                if ($@) {
453
                    warn "error : $@";
454
                } else {
455
                    $field->replace_with($new_field);
456
                }
457
458
            }
459
        }
460
    }
461
462
    return $record,$noact;
463
}
464
465
sub entity_clean {
466
    my $string=shift;
467
    my $oldstring = $string;
468
    my $noact;
469
    $string=~s/(&)(amp;)+/$1/g;
470
    if ( $string eq $oldstring){
471
        $noact = 1;
472
        return $oldstring, $noact;
473
    }
474
    return $string;
475
}
422
476
423
=head1 INTERNAL FUNCTIONS
477
=head1 INTERNAL FUNCTIONS
424
478
(-)a/misc/maintenance/batchSanitizeEntity.pl (-1 / +180 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# small script that replaces '&etc.' by '&' in a record
4
5
# Copyright 2012 Biblibre SARL
6
#
7
# This file is part of Koha.
8
#
9
# Koha is free software; you can redistribute it and/or modify it under the
10
# terms of the GNU General Public License as published by the Free Software
11
# Foundation; either version 2 of the License, or (at your option) any later
12
# version.
13
#
14
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License along
19
# with Koha; if not, write to the Free Software Foundation, Inc.,
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22
use Modern::Perl;
23
use C4::Charset;
24
use C4::Context;
25
use DBI;
26
use C4::Biblio;
27
use Getopt::Long;
28
use Pod::Usage;
29
30
my ( $biblios,$run,$want_help,$batch,$cron,$reindex );
31
my $result = GetOptions(
32
    'where=s'  => \$biblios,
33
    'run'      => \$run,
34
    'help|h'   => \$want_help,
35
    'batch|b'  => \$batch,
36
    'cron|c'   => \$cron,
37
    'reindex|r'=> \$reindex,
38
);
39
40
# We check if required entries are given :
41
if ( not $result or $want_help or not $biblios or not $run ) {
42
    print_usage();
43
    exit 0;
44
}
45
46
# We initialise some tools :
47
my $count;
48
my $bibliocount;
49
my @bibliofile;
50
my @biblionum;
51
my @biblios;
52
my $record;
53
my $cleanrecord;
54
my $noact;
55
56
# We first detect if we have a file or biblos directly entered by command line
57
#or if we want to use findAmp() sub
58
if ( $biblios eq "search" ) {
59
    @biblios = findAmp();
60
}
61
else {
62
    if ( $biblios =~ m|/| ) {
63
        open( FILE, "$biblios" ) || die("Can't open $biblios");
64
        @bibliofile = <FILE>;
65
        close(FILE);
66
    }
67
    else {
68
        @biblionum = split ',', $biblios;
69
    }
70
71
    # We take the biblios
72
    @biblios = @bibliofile ? @bibliofile : @biblionum;
73
}
74
75
# We remove spaces
76
s/(^\s*|\s*$)//g for @biblios;
77
# Remove empty lines
78
@biblios = grep {!/^$/} @biblios;
79
80
# We valid the input
81
foreach my $biblio (@biblios) {
82
    if ( $biblio !~ /^\d+$/ ) {
83
        print
84
"------------- \"$biblio\" is not a biblionumber !!! -------------\n";
85
        print "------------- please verify \"$biblio\" !!! -------------\n";
86
        $count++;
87
    }
88
    exit(1) if $count;
89
}
90
91
$bibliocount = scalar @biblios;
92
93
print
94
"------------- $bibliocount Biblio(s) ready to be cleaned -------------\n";
95
if ( !$batch or !$cron ) {
96
    my $confirm = 0;
97
    print "------------- Proceed? [yes/no] :\n";
98
    while ( $confirm == 0 ) {
99
        my $prout = <STDIN>;
100
        if ($prout eq "yes\n") {
101
            $confirm = 1;
102
        }
103
        elsif ($prout eq "no\n") {
104
            print "------------- Ok, bye -------------\n";
105
            exit(0);
106
        }
107
        else {
108
            print "------------- Bad answer please types 'yes' or 'no' -------------\n";
109
        }
110
            }
111
}
112
113
my $num = -1;
114
foreach my $biblio (@biblios) {
115
    $num++;
116
    print "------------- N° $biblio selected -------------\n";
117
    $record      = GetMarcBiblio($biblio);
118
    ($cleanrecord,$noact) = SanitizeEntity($record);
119
    if ($noact==1){
120
        print "------------- Biblio n° $biblio nothing to clean -------------\n";
121
        splice(@biblios,$num,1);
122
    }else {
123
        my $frameworkcode = GetFrameworkCode($biblio);
124
        ModBiblio( $cleanrecord, $biblio, $frameworkcode );
125
        print "------------- Biblio n° $biblio cleaning done -------------\n";
126
        $count++;
127
    }
128
}
129
130
print "-------------------------------------------------------------\n";
131
if ($count){
132
    print "------------- $count Biblios cleaned -------------\n";
133
}else{
134
    print "------------- no Biblios cleaned -------------\n" if ($count);
135
}
136
137
if ( $cron or $reindex) {
138
    print "-------------------------------------------------------------\n";
139
    print
140
"--------- Now we are reindexing -b -v -where \"biblionumber=xxx\" ---------\n";
141
    print "-------------------------------------------------------------\n";
142
    $count = 0;
143
    my $kohapath = C4::Context->config('intranetdir');
144
    foreach my $biblio ( @biblios ) {
145
        system("$kohapath/misc/migration_tools/rebuild_zebra.pl
146
        -b -v -where \"biblionumber=$biblio\"");
147
        print "------------- Biblio n° $biblio re-indexing done -------------\n";
148
        $count ++;
149
}
150
    print "------------- $count Biblios re-indexed -------------\n";
151
}
152
153
sub print_usage {
154
    print <<_USAGE_;
155
$0: replaces '&amp;' by '&' in a record, you can either give some biblionumbers or a file with biblionumbers or ask for a search
156
157
Parameters:
158
    --where                 use this to give biblionumbers (separated by coma) in a string with ""
159
                            or an absolute path to a file containing biblionumbers (1 by row)
160
                            or the command 'search' that creates an array with biblionumbers containing "&amp;amp;..." in biblioitems.marxml
161
    --run                   run the command
162
    --batch or -b           run in batch mode
163
    --cron or -c            run in cron mode, it reindexes the changed records (you can redirect the output to a file)
164
    --help or -h            show this message.
165
    --reindex               reindexes the modified records
166
_USAGE_
167
}
168
169
sub findAmp {
170
    my @bibliosearch;
171
    my $dbh = C4::Context->dbh;
172
    my $strsth =
173
      qq{SELECT biblionumber FROM biblioitems WHERE marcxml LIKE "%&amp;amp;%"};
174
    my $sth = $dbh->prepare($strsth);
175
    $sth->execute();
176
    while ( my $bib = $sth-> fetchrow_array() ) {
177
        push @bibliosearch, $bib;
178
    }
179
    return @bibliosearch;
180
}

Return to bug 8218